Skip to content

Commit 3b794e9

Browse files
committed
cmd/evm: enable eip-4788 in evm t8n
1 parent d513b40 commit 3b794e9

File tree

13 files changed

+243
-79
lines changed

13 files changed

+243
-79
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,26 @@ type ommer struct {
6969

7070
//go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
7171
type stEnv struct {
72-
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
73-
Difficulty *big.Int `json:"currentDifficulty"`
74-
Random *big.Int `json:"currentRandom"`
75-
ParentDifficulty *big.Int `json:"parentDifficulty"`
76-
ParentBaseFee *big.Int `json:"parentBaseFee,omitempty"`
77-
ParentGasUsed uint64 `json:"parentGasUsed,omitempty"`
78-
ParentGasLimit uint64 `json:"parentGasLimit,omitempty"`
79-
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
80-
Number uint64 `json:"currentNumber" gencodec:"required"`
81-
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
82-
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
83-
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
84-
Ommers []ommer `json:"ommers,omitempty"`
85-
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
86-
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
87-
ParentUncleHash common.Hash `json:"parentUncleHash"`
88-
ExcessBlobGas *uint64 `json:"excessBlobGas,omitempty"`
89-
ParentExcessBlobGas *uint64 `json:"parentExcessBlobGas,omitempty"`
90-
ParentBlobGasUsed *uint64 `json:"parentBlobGasUsed,omitempty"`
72+
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
73+
Difficulty *big.Int `json:"currentDifficulty"`
74+
Random *big.Int `json:"currentRandom"`
75+
ParentDifficulty *big.Int `json:"parentDifficulty"`
76+
ParentBaseFee *big.Int `json:"parentBaseFee,omitempty"`
77+
ParentGasUsed uint64 `json:"parentGasUsed,omitempty"`
78+
ParentGasLimit uint64 `json:"parentGasLimit,omitempty"`
79+
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
80+
Number uint64 `json:"currentNumber" gencodec:"required"`
81+
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
82+
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
83+
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
84+
Ommers []ommer `json:"ommers,omitempty"`
85+
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
86+
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
87+
ParentUncleHash common.Hash `json:"parentUncleHash"`
88+
ExcessBlobGas *uint64 `json:"excessBlobGas,omitempty"`
89+
ParentExcessBlobGas *uint64 `json:"parentExcessBlobGas,omitempty"`
90+
ParentBlobGasUsed *uint64 `json:"parentBlobGasUsed,omitempty"`
91+
ParentBeaconBlockRoot *common.Hash `json:"parentBeaconBlockRoot"`
9192
}
9293

9394
type stEnvMarshaling struct {
@@ -182,6 +183,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
182183
chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
183184
misc.ApplyDAOHardFork(statedb)
184185
}
186+
if beaconRoot := pre.Env.ParentBeaconBlockRoot; beaconRoot != nil {
187+
evm := vm.NewEVM(vmContext, vm.TxContext{}, statedb, chainConfig, vmConfig)
188+
core.ProcessBeaconBlockRoot(*beaconRoot, evm, statedb)
189+
}
185190
var blobGasUsed uint64
186191
for i, tx := range txs {
187192
if tx.Type() == types.BlobTxType && vmContext.ExcessBlobGas == nil {

cmd/evm/internal/t8ntool/gen_stenv.go

Lines changed: 44 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/evm/internal/t8ntool/transition.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ func Transition(ctx *cli.Context) error {
292292
prestate.Env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp,
293293
env.ParentTimestamp, env.ParentDifficulty, env.ParentUncleHash)
294294
}
295+
if chainConfig.IsCancun(big.NewInt(int64(prestate.Env.Number)), prestate.Env.Timestamp) {
296+
// We require EIP-4788 beacon root to be set in the env
297+
if env.ParentBeaconBlockRoot == nil {
298+
return NewError(ErrorConfig, errors.New("post-cancun env requires parentBeaconBlockRoot to be set"))
299+
}
300+
} else {
301+
env.ParentBeaconBlockRoot = nil // un-set it if it has been set too early
302+
}
295303
// Run the test and aggregate the result
296304
s, result, err := prestate.Apply(vmConfig, chainConfig, txs, ctx.Int64(RewardFlag.Name), getTracer)
297305
if err != nil {

cmd/evm/t8n_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ func TestT8n(t *testing.T) {
267267
output: t8nOutput{alloc: true, result: true},
268268
expOut: "exp.json",
269269
},
270+
{ // More cancun tests
271+
base: "./testdata/29",
272+
input: t8nInput{
273+
"alloc.json", "txs.json", "env.json", "Cancun", "",
274+
},
275+
output: t8nOutput{alloc: true, result: true},
276+
expOut: "exp.json",
277+
},
270278
} {
271279
args := []string{"t8n"}
272280
args = append(args, tc.output.get()...)

cmd/evm/testdata/28/env.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
"parentBlobGasUsed" : "0x00",
1919
"blockHashes" : {
2020
"0" : "0x3a9b485972e7353edd9152712492f0c58d89ef80623686b6bf947a4a6dce6cb6"
21-
}
21+
},
22+
"parentBeaconBlockRoot": "0x0000beac00beac00beac00beac00beac00beac00beac00beac00beac00beac00"
2223
}

cmd/evm/testdata/29/alloc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
3+
"balance" : "0x016345785d8a0000",
4+
"code" : "0x",
5+
"nonce" : "0x00",
6+
"storage" : {
7+
}
8+
},
9+
"0xBeAC00541d49391ED88ABF392bfC1F4dEa8c4143" : {
10+
"balance" : "0x1",
11+
"code" : "0x3373fffffffffffffffffffffffffffffffffffffffe14604457602036146024575f5ffd5b620180005f350680545f35146037575f5ffd5b6201800001545f5260205ff35b6201800042064281555f359062018000015500",
12+
"nonce" : "0x00",
13+
"storage" : {
14+
}
15+
}
16+
}

cmd/evm/testdata/29/env.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"currentCoinbase" : "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
3+
"currentNumber" : "0x01",
4+
"currentTimestamp" : "0x079e",
5+
"currentGasLimit" : "0x7fffffffffffffff",
6+
"previousHash" : "0x3a9b485972e7353edd9152712492f0c58d89ef80623686b6bf947a4a6dce6cb6",
7+
"currentBlobGasUsed" : "0x00",
8+
"parentTimestamp" : "0x03b6",
9+
"parentDifficulty" : "0x00",
10+
"parentUncleHash" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
11+
"currentRandom" : "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
12+
"withdrawals" : [
13+
],
14+
"parentBaseFee" : "0x0a",
15+
"parentGasUsed" : "0x00",
16+
"parentGasLimit" : "0x7fffffffffffffff",
17+
"parentExcessBlobGas" : "0x00",
18+
"parentBlobGasUsed" : "0x00",
19+
"parentBeaconBlockRoot": "0x0000beac00beac00beac00beac00beac00beac00beac00beac00beac00beac00"
20+
}

cmd/evm/testdata/29/exp.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"alloc": {
3+
"0xbeac00541d49391ed88abf392bfc1f4dea8c4143": {
4+
"code": "0x3373fffffffffffffffffffffffffffffffffffffffe14604457602036146024575f5ffd5b620180005f350680545f35146037575f5ffd5b6201800001545f5260205ff35b6201800042064281555f359062018000015500",
5+
"storage": {
6+
"0x000000000000000000000000000000000000000000000000000000000000079e": "0x000000000000000000000000000000000000000000000000000000000000079e",
7+
"0x000000000000000000000000000000000000000000000000000000000001879e": "0x0000beac00beac00beac00beac00beac00beac00beac00beac00beac00beac00"
8+
},
9+
"balance": "0x1"
10+
},
11+
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
12+
"balance": "0x16345785d871db8",
13+
"nonce": "0x1"
14+
}
15+
},
16+
"result": {
17+
"stateRoot": "0xecde4500714cc55d9e3f13e154f6db0b21b2aad708380c39d64d5295b32af8a7",
18+
"txRoot": "0x248074fabe112f7d93917f292b64932394f835bb98da91f21501574d58ec92ab",
19+
"receiptsRoot": "0xf78dfb743fbd92ade140711c8bbc542b5e307f0ab7984eff35d751969fe57efa",
20+
"logsHash": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
21+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
22+
"receipts": [
23+
{
24+
"type": "0x2",
25+
"root": "0x",
26+
"status": "0x1",
27+
"cumulativeGasUsed": "0x5208",
28+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
29+
"logs": null,
30+
"transactionHash": "0x84f70aba406a55628a0620f26d260f90aeb6ccc55fed6ec2ac13dd4f727032ed",
31+
"contractAddress": "0x0000000000000000000000000000000000000000",
32+
"gasUsed": "0x5208",
33+
"effectiveGasPrice": null,
34+
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
35+
"transactionIndex": "0x0"
36+
}
37+
],
38+
"currentDifficulty": null,
39+
"gasUsed": "0x5208",
40+
"currentBaseFee": "0x9",
41+
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
42+
"currentExcessBlobGas": "0x0",
43+
"currentBlobGasUsed": "0x0"
44+
}
45+
}

cmd/evm/testdata/29/readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## EIP 4788
2+
3+
This test contains testcases for EIP-4788. The 4788-contract is
4+
located at address ``, and this test executes a simple transaction. It also
5+
implicitly invokes the system tx, which sets calls the contract and sets the
6+
storage values
7+
```
8+
$ dir=./testdata/29/ && go run . t8n --state.fork=Cancun --input.alloc=$dir/alloc.json --input.txs=$dir/txs.json --input.env=$dir/env.json --output.alloc=stdout
9+
INFO [08-15|20:07:56.335] Trie dumping started root=ecde45..2af8a7
10+
INFO [08-15|20:07:56.335] Trie dumping complete accounts=2 elapsed="225.848µs"
11+
INFO [08-15|20:07:56.335] Wrote file file=result.json
12+
{
13+
"alloc": {
14+
"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
15+
"balance": "0x16345785d871db8",
16+
"nonce": "0x1"
17+
},
18+
"0xbeac00541d49391ed88abf392bfc1f4dea8c4143": {
19+
"code": "0x3373fffffffffffffffffffffffffffffffffffffffe14604457602036146024575f5ffd5b620180005f350680545f35146037575f5ffd5b6201800001545f5260205ff35b6201800042064281555f359062018000015500",
20+
"storage": {
21+
"0x000000000000000000000000000000000000000000000000000000000000079e": "0x000000000000000000000000000000000000000000000000000000000000079e",
22+
"0x000000000000000000000000000000000000000000000000000000000001879e": "0x0000beac00beac00beac00beac00beac00beac00beac00beac00beac00beac00"
23+
},
24+
"balance": "0x
25+
}
26+
}
27+
}
28+
29+
```

0 commit comments

Comments
 (0)