Skip to content

Commit e376d2f

Browse files
holimanfjl
andauthored
cmd/evm: add state transition tool for testing (#20958)
This PR implements the EVM state transition tool, which is intended to be the replacement for our retesteth client implementation. Documentation is present in the cmd/evm/README.md file. Co-authored-by: Felix Lange <fjl@twurst.com>
1 parent dd91c7c commit e376d2f

37 files changed

Lines changed: 1507 additions & 46 deletions

‎cmd/evm/README.md‎

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
## EVM state transition tool
2+
3+
The `evm t8n` tool is a stateless state transition utility. It is a utility
4+
which can
5+
6+
1. Take a prestate, including
7+
- Accounts,
8+
- Block context information,
9+
- Previous blockshashes (*optional)
10+
2. Apply a set of transactions,
11+
3. Apply a mining-reward (*optional),
12+
4. And generate a post-state, including
13+
- State root, transaction root, receipt root,
14+
- Information about rejected transactions,
15+
- Optionally: a full or partial post-state dump
16+
17+
## Specification
18+
19+
The idea is to specify the behaviour of this binary very _strict_, so that other
20+
node implementors can build replicas based on their own state-machines, and the
21+
state generators can swap between a `geth`-based implementation and a `parityvm`-based
22+
implementation.
23+
24+
### Command line params
25+
26+
Command line params that has to be supported are
27+
```
28+
29+
--trace Output full trace logs to files <txhash>.jsonl
30+
--trace.nomemory Disable full memory dump in traces
31+
--trace.nostack Disable stack output in traces
32+
--output.alloc alloc Determines where to put the alloc of the post-state.
33+
`stdout` - into the stdout output
34+
`stderr` - into the stderr output
35+
--output.result result Determines where to put the result (stateroot, txroot etc) of the post-state.
36+
`stdout` - into the stdout output
37+
`stderr` - into the stderr output
38+
--state.fork value Name of ruleset to use.
39+
--state.chainid value ChainID to use (default: 1)
40+
--state.reward value Mining reward. Set to -1 to disable (default: 0)
41+
42+
```
43+
44+
### Error codes and output
45+
46+
All logging should happen against the `stderr`.
47+
There are a few (not many) errors that can occur, those are defined below.
48+
49+
#### EVM-based errors (`2` to `9`)
50+
51+
- Other EVM error. Exit code `2`
52+
- Failed configuration: when a non-supported or invalid fork was specified. Exit code `3`.
53+
- Block history is not supplied, but needed for a `BLOCKHASH` operation. If `BLOCKHASH`
54+
is invoked targeting a block which history has not been provided for, the program will
55+
exit with code `4`.
56+
57+
#### IO errors (`10`-`20`)
58+
59+
- Invalid input json: the supplied data could not be marshalled.
60+
The program will exit with code `10`
61+
- IO problems: failure to load or save files, the program will exit with code `11`
62+
63+
## Examples
64+
### Basic usage
65+
66+
Invoking it with the provided example files
67+
```
68+
./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json
69+
```
70+
Two resulting files:
71+
72+
`alloc.json`:
73+
```json
74+
{
75+
"0x8a8eafb1cf62bfbeb1741769dae1a9dd47996192": {
76+
"balance": "0xfeed1a9d",
77+
"nonce": "0x1"
78+
},
79+
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
80+
"balance": "0x5ffd4878be161d74",
81+
"nonce": "0xac"
82+
},
83+
"0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
84+
"balance": "0xa410"
85+
}
86+
}
87+
```
88+
`result.json`:
89+
```json
90+
{
91+
"stateRoot": "0x84208a19bc2b46ada7445180c1db162be5b39b9abc8c0a54b05d32943eae4e13",
92+
"txRoot": "0xc4761fd7b87ff2364c7c60b6c5c8d02e522e815328aaea3f20e3b7b7ef52c42d",
93+
"receiptRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
94+
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
95+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
96+
"receipts": [
97+
{
98+
"root": "0x",
99+
"status": "0x1",
100+
"cumulativeGasUsed": "0x5208",
101+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
102+
"logs": null,
103+
"transactionHash": "0x0557bacce3375c98d806609b8d5043072f0b6a8bae45ae5a67a00d3a1a18d673",
104+
"contractAddress": "0x0000000000000000000000000000000000000000",
105+
"gasUsed": "0x5208",
106+
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
107+
"transactionIndex": "0x0"
108+
}
109+
],
110+
"rejected": [
111+
1
112+
]
113+
}
114+
```
115+
116+
We can make them spit out the data to e.g. `stdout` like this:
117+
```
118+
./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json --output.result=stdout --output.alloc=stdout
119+
```
120+
Output:
121+
```json
122+
{
123+
"alloc": {
124+
"0x8a8eafb1cf62bfbeb1741769dae1a9dd47996192": {
125+
"balance": "0xfeed1a9d",
126+
"nonce": "0x1"
127+
},
128+
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
129+
"balance": "0x5ffd4878be161d74",
130+
"nonce": "0xac"
131+
},
132+
"0xc94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
133+
"balance": "0xa410"
134+
}
135+
},
136+
"result": {
137+
"stateRoot": "0x84208a19bc2b46ada7445180c1db162be5b39b9abc8c0a54b05d32943eae4e13",
138+
"txRoot": "0xc4761fd7b87ff2364c7c60b6c5c8d02e522e815328aaea3f20e3b7b7ef52c42d",
139+
"receiptRoot": "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
140+
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
141+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
142+
"receipts": [
143+
{
144+
"root": "0x",
145+
"status": "0x1",
146+
"cumulativeGasUsed": "0x5208",
147+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
148+
"logs": null,
149+
"transactionHash": "0x0557bacce3375c98d806609b8d5043072f0b6a8bae45ae5a67a00d3a1a18d673",
150+
"contractAddress": "0x0000000000000000000000000000000000000000",
151+
"gasUsed": "0x5208",
152+
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
153+
"transactionIndex": "0x0"
154+
}
155+
],
156+
"rejected": [
157+
1
158+
]
159+
}
160+
}
161+
```
162+
163+
## About Ommers
164+
165+
Mining rewards and ommer rewards might need to be added. This is how those are applied:
166+
167+
- `block_reward` is the block mining reward for the miner (`0xaa`), of a block at height `N`.
168+
- For each ommer (mined by `0xbb`), with blocknumber `N-delta`
169+
- (where `delta` is the difference between the current block and the ommer)
170+
- The account `0xbb` (ommer miner) is awarded `(8-delta)/ 8 * block_reward`
171+
- The account `0xaa` (block miner) is awarded `block_reward / 32`
172+
173+
To make `state_t8n` apply these, the following inputs are required:
174+
175+
- `state.reward`
176+
- For ethash, it is `5000000000000000000` `wei`,
177+
- If this is not defined, mining rewards are not applied,
178+
- A value of `0` is valid, and causes accounts to be 'touched'.
179+
- For each ommer, the tool needs to be given an `address` and a `delta`. This
180+
is done via the `env`.
181+
182+
Note: the tool does not verify that e.g. the normal uncle rules apply,
183+
and allows e.g two uncles at the same height, or the uncle-distance. This means that
184+
the tool allows for negative uncle reward (distance > 8)
185+
186+
Example:
187+
`./testdata/5/env.json`:
188+
```json
189+
{
190+
"currentCoinbase": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
191+
"currentDifficulty": "0x20000",
192+
"currentGasLimit": "0x750a163df65e8a",
193+
"currentNumber": "1",
194+
"currentTimestamp": "1000",
195+
"ommers": [
196+
{"delta": 1, "address": "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" },
197+
{"delta": 2, "address": "0xcccccccccccccccccccccccccccccccccccccccc" }
198+
]
199+
}
200+
```
201+
When applying this, using a reward of `0x08`
202+
Output:
203+
```json
204+
{
205+
"alloc": {
206+
"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": {
207+
"balance": "0x88"
208+
},
209+
"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": {
210+
"balance": "0x70"
211+
},
212+
"0xcccccccccccccccccccccccccccccccccccccccc": {
213+
"balance": "0x60"
214+
}
215+
}
216+
}
217+
```
218+
### Future EIPS
219+
220+
It is also possible to experiment with future eips that are not yet defined in a hard fork.
221+
Example, putting EIP-1344 into Frontier:
222+
```
223+
./evm t8n --state.fork=Frontier+1344 --input.pre=./testdata/1/pre.json --input.txs=./testdata/1/txs.json --input.env=/testdata/1/env.json
224+
```
225+
226+
### Block history
227+
228+
The `BLOCKHASH` opcode requires blockhashes to be provided by the caller, inside the `env`.
229+
If a required blockhash is not provided, the exit code should be `4`:
230+
Example where blockhashes are provided:
231+
```
232+
./evm t8n --input.alloc=./testdata/3/alloc.json --input.txs=./testdata/3/txs.json --input.env=./testdata/3/env.json --trace
233+
```
234+
```
235+
cat trace-0.jsonl | grep BLOCKHASH -C2
236+
```
237+
```
238+
{"pc":0,"op":96,"gas":"0x5f58ef8","gasCost":"0x3","memory":"0x","memSize":0,"stack":[],"returnStack":[],"depth":1,"refund":0,"opName":"PUSH1","error":""}
239+
{"pc":2,"op":64,"gas":"0x5f58ef5","gasCost":"0x14","memory":"0x","memSize":0,"stack":["0x1"],"returnStack":[],"depth":1,"refund":0,"opName":"BLOCKHASH","error":""}
240+
{"pc":3,"op":0,"gas":"0x5f58ee1","gasCost":"0x0","memory":"0x","memSize":0,"stack":["0xdac58aa524e50956d0c0bae7f3f8bb9d35381365d07804dd5b48a5a297c06af4"],"returnStack":[],"depth":1,"refund":0,"opName":"STOP","error":""}
241+
{"output":"","gasUsed":"0x17","time":155861}
242+
```
243+
244+
In this example, the caller has not provided the required blockhash:
245+
```
246+
./evm t8n --input.alloc=./testdata/4/alloc.json --input.txs=./testdata/4/txs.json --input.env=./testdata/4/env.json --trace
247+
```
248+
```
249+
ERROR(4): getHash(3) invoked, blockhash for that block not provided
250+
```
251+
Error code: 4
252+
### Chaining
253+
254+
Another thing that can be done, is to chain invocations:
255+
```
256+
./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json --output.alloc=stdout | ./evm t8n --input.alloc=stdin --input.env=./testdata/1/env.json --input.txs=./testdata/1/txs.json
257+
INFO [06-29|11:52:04.934] rejected tx index=1 hash="0557ba…18d673" from=0x8A8eAFb1cf62BfBeb1741769DAE1a9dd47996192 error="nonce too low"
258+
INFO [06-29|11:52:04.936] rejected tx index=0 hash="0557ba…18d673" from=0x8A8eAFb1cf62BfBeb1741769DAE1a9dd47996192 error="nonce too low"
259+
INFO [06-29|11:52:04.936] rejected tx index=1 hash="0557ba…18d673" from=0x8A8eAFb1cf62BfBeb1741769DAE1a9dd47996192 error="nonce too low"
260+
261+
```
262+
What happened here, is that we first applied two identical transactions, so the second one was rejected.
263+
Then, taking the poststate alloc as the input for the next state, we tried again to include
264+
the same two transactions: this time, both failed due to too low nonce.
265+
266+
In order to meaningfully chain invocations, one would need to provide meaningful new `env`, otherwise the
267+
actual blocknumber (exposed to the EVM) would not increase.
268+

0 commit comments

Comments
 (0)