Commit 40cd38d
[revive] revm backend (#9285)
# EVM initial support for pallet-revive
Initial EVM support via the REVM crate to create a dual-VM system that
can execute both PolkaVM and EVM
- Added `AllowEVMBytecode: Get<bool>` to the config to enable/disable
EVM call and instantiation
- CodeInfo has been updated to add the type of bytecode (EVM / PVM).
`migration/v2.rs` takes care of migrating existing storages
- The CodeUploadDeposit is not held by a pallet account instead of being
held on the uploader, It's automatically refunded when the refcount
drops to 0 and the code is removed.
- The basic flow of uploading an EVM contract and running it should work
- instructions are copied and adapted from REVM they should be ignored
in this PR and reviewed in follow-up PR
(**reviewers** please ignore
`substrate/frame/revive/src/vm/evm/instructions/*` for now)
## Implementation Guidelines
### Basic Instruction Structure
A basic instruction looks like this:
```rust
pub fn coinbase<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
gas_legacy!(context.interpreter, revm_gas::BASE);
push!(context.interpreter, context.host.beneficiary().into_word().into());
}
```
### Required Changes for REVM Instructions
All instructions have been copied from `REVM` and updated with generic
types for pallet-revive. Two main changes are required:
#### 1. Gas Handling
Replace REVM gas calls with existing benchmarks where available:
```diff
- gas_legacy!(context.interpreter, revm_gas::BASE);
+ gas!(context.interpreter, RuntimeCosts::BlockAuthor);
```
#### 2. Context Access
Replace `context.host` calls with `context.extend` (set to `&mut Ext`):
```diff
- push!(context.interpreter, context.host.beneficiary().into_word().into());
+ let coinbase: Address = context.interpreter.extend.block_author().unwrap_or_default().0.into();
+ push!(context.interpreter, coinbase.into_word().into());
```
### Gas Benchmarking Notes
- For cases without existing benchmarks (e.g arithmetic, bitwise) , we
will keep `gas_legacy!`
- The u64 gas value are multiplied by a base cost benchmarked by
`evm_opcode`
- ### Important Rules
- All calls to `context.host` should be removed (initialized to default
values)
- All calls to `context.interpreter.gas` should be removed (except
`gas.memory` handled by `resize_memory!` macro)
- See `block_number` implementation as a reference example
The following instructions in src/vm/evm/instructions/** need to be
updated
### Basic Instructions
We probably don't need to touch these implementations here, they use the
gas_legacy! macro to charge a low gas value that will be scaled with our
gas_to_weight benchmark. The only thing needed here are tests that
exercise these instructions
<details>
#### Arithmetic Instructions
- [ ] **add**
- [ ] **mul**
- [ ] **sub**
- [ ] **div**
- [ ] **sdiv**
- [ ] **rem**
- [ ] **smod**
- [ ] **addmod**
- [ ] **mulmod**
- [ ] **exp**
- [ ] **signextend**
#### Bitwise Instructions
- [ ] **lt**
- [ ] **gt**
- [ ] **slt**
- [ ] **sgt**
- [ ] **eq**
- [ ] **iszero**
- [ ] **bitand**
- [ ] **bitor**
- [ ] **bitxor**
- [ ] **not**
- [ ] **byte**
- [ ] **shl**
- [ ] **shr**
- [ ] **sar**
- [ ] **clz**
#### Control Flow Instructions
- [ ] **jump**
- [ ] **jumpi**
- [ ] **jumpdest**
- [ ] **pc**
- [ ] **stop**
- [ ] **ret**
- [ ] **revert**
- [ ] **invalid**
### Memory Instructions
- [ ] **mload**
- [ ] **mstore**
- [ ] **mstore8**
- [ ] **msize**
- [ ] **mcopy**
#### Stack Instructions
- [ ] **pop**
- [ ] **push0**
- [ ] **push**
- [ ] **dup**
- [ ] **swap**
</details>
### Sys calls instructions
These instructions should be updated from using gas_legacy! to gas! with
the appropriate RuntimeCost, the returned value need to be pulled from
our `&mut Ext` ctx.interpreter.extend instead of the host or input
context value
<details>
#### Block Info Instructions
- [x] **block_number**
- [ ] **coinbase**
- [ ] **timestamp**
- [ ] **difficulty**
- [ ] **gaslimit**
- [ ] **chainid**
- [ ] **basefee**
- [ ] **blob_basefee**
#### Host Instructions
- [ ] **balance**
- [ ] **extcodesize**
- [ ] **extcodecopy**
- [ ] **extcodehash**
- [ ] **blockhash**
- [ ] **sload**
- [ ] **sstore**
- [ ] **tload**
- [ ] **tstore**
- [ ] **log**
- [ ] **selfdestruct**
- [ ] **selfbalance**
#### System Instructions
- [ ] **keccak256**
- [ ] **address**
- [ ] **caller**
- [ ] **callvalue**
- [ ] **calldataload**
- [ ] **calldatasize**
- [ ] **calldatacopy**
- [ ] **codesize**
- [ ] **codecopy**
- [ ] **returndatasize**
- [ ] **returndatacopy**
- [ ] **gas**
#### Transaction Info Instructions
- [ ] **origin**
- [ ] **gasprice**
- [ ] **blob_hash**
</details>
### Contract Instructions
These instructions should be updated,, that's where I expect the most
code change in the instruction implementation.
See how it's done in vm/pvm module, the final result should look pretty
similar to what we are doing there with the addition of custom gas_limit
calculation that works with our gas model.
see also example code here https://github.com/paritytech/revm_example
<details>
- [ ] **create**
- [ ] **create**
- [ ] **call**
- [ ] **call_code**
- [ ] **delegate_call**
- [ ] **static_call**
</details>
---------
Signed-off-by: Cyrill Leutwiler <[email protected]>
Signed-off-by: xermicus <[email protected]>
Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Alexander Theißen <[email protected]>
Co-authored-by: xermicus <[email protected]>
Co-authored-by: 0xRVE <[email protected]>
Co-authored-by: Robert van Eerdewijk <[email protected]>1 parent 3381910 commit 40cd38d
File tree
45 files changed
+5136
-886
lines changed- .github/workflows
- cumulus/parachains/runtimes
- assets/asset-hub-westend
- src
- tests
- testing/penpal/src
- prdoc
- substrate
- bin/node/runtime/src
- frame/revive
- fixtures
- contracts
- src
- evm
- exec
- migrations
- tests
- vm
- evm/instructions
- contract
- pvm
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
45 files changed
+5136
-886
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
370 | 370 | | |
371 | 371 | | |
372 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
373 | 384 | | |
374 | 385 | | |
375 | 386 | | |
| |||
406 | 417 | | |
407 | 418 | | |
408 | 419 | | |
| 420 | + | |
Lines changed: 8 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1182 | 1182 | | |
1183 | 1183 | | |
1184 | 1184 | | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
1185 | 1189 | | |
1186 | 1190 | | |
1187 | 1191 | | |
| |||
1199 | 1203 | | |
1200 | 1204 | | |
1201 | 1205 | | |
1202 | | - | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
1203 | 1210 | | |
1204 | 1211 | | |
1205 | 1212 | | |
| |||
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1664 | 1664 | | |
1665 | 1665 | | |
1666 | 1666 | | |
| 1667 | + | |
1667 | 1668 | | |
1668 | 1669 | | |
1669 | 1670 | | |
1670 | 1671 | | |
1671 | 1672 | | |
| 1673 | + | |
| 1674 | + | |
1672 | 1675 | | |
1673 | 1676 | | |
1674 | 1677 | | |
| |||
1729 | 1732 | | |
1730 | 1733 | | |
1731 | 1734 | | |
| 1735 | + | |
1732 | 1736 | | |
1733 | 1737 | | |
1734 | 1738 | | |
1735 | 1739 | | |
1736 | 1740 | | |
1737 | 1741 | | |
1738 | 1742 | | |
| 1743 | + | |
| 1744 | + | |
1739 | 1745 | | |
1740 | 1746 | | |
1741 | 1747 | | |
| |||
1772 | 1778 | | |
1773 | 1779 | | |
1774 | 1780 | | |
| 1781 | + | |
1775 | 1782 | | |
1776 | 1783 | | |
1777 | 1784 | | |
1778 | 1785 | | |
1779 | 1786 | | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
1780 | 1790 | | |
1781 | 1791 | | |
1782 | 1792 | | |
| |||
1822 | 1832 | | |
1823 | 1833 | | |
1824 | 1834 | | |
| 1835 | + | |
1825 | 1836 | | |
1826 | 1837 | | |
1827 | 1838 | | |
1828 | 1839 | | |
1829 | 1840 | | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
1830 | 1844 | | |
1831 | 1845 | | |
1832 | 1846 | | |
| |||
1875 | 1889 | | |
1876 | 1890 | | |
1877 | 1891 | | |
| 1892 | + | |
1878 | 1893 | | |
1879 | 1894 | | |
1880 | 1895 | | |
1881 | 1896 | | |
1882 | 1897 | | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
1883 | 1901 | | |
1884 | 1902 | | |
1885 | 1903 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
853 | 853 | | |
854 | 854 | | |
855 | 855 | | |
| 856 | + | |
856 | 857 | | |
857 | 858 | | |
858 | 859 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1490 | 1490 | | |
1491 | 1491 | | |
1492 | 1492 | | |
| 1493 | + | |
1493 | 1494 | | |
1494 | 1495 | | |
1495 | 1496 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
463 | 463 | | |
464 | 464 | | |
465 | 465 | | |
466 | | - | |
| 466 | + | |
467 | 467 | | |
468 | 468 | | |
469 | 469 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
0 commit comments