Skip to content

Commit f404a2d

Browse files
jsvisaholiman
andauthored
cmd/evm: set ExcessBlobGas from env (#27796)
Sets the `currentExcessBlobGas` from env, alternatively calculates it based on `parentExcessBlobGas` and `parentBlobGasUsed`. It then emits the `currentExcessBlobGas` and `currentBlobGasUsed` into the output, to be used as parent-values for a future iteration. Closes #27785 Closes #27783 --------- Signed-off-by: jsvisa <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 7c95ebd commit f404a2d

File tree

7 files changed

+219
-72
lines changed

7 files changed

+219
-72
lines changed

cmd/evm/internal/t8ntool/execution.go

+75-40
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/common/math"
2525
"github.com/ethereum/go-ethereum/consensus/ethash"
2626
"github.com/ethereum/go-ethereum/consensus/misc"
27+
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
2728
"github.com/ethereum/go-ethereum/core"
2829
"github.com/ethereum/go-ethereum/core/rawdb"
2930
"github.com/ethereum/go-ethereum/core/state"
@@ -46,17 +47,19 @@ type Prestate struct {
4647
// ExecutionResult contains the execution status after running a state test, any
4748
// error that might have occurred and a dump of the final state if requested.
4849
type ExecutionResult struct {
49-
StateRoot common.Hash `json:"stateRoot"`
50-
TxRoot common.Hash `json:"txRoot"`
51-
ReceiptRoot common.Hash `json:"receiptsRoot"`
52-
LogsHash common.Hash `json:"logsHash"`
53-
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
54-
Receipts types.Receipts `json:"receipts"`
55-
Rejected []*rejectedTx `json:"rejected,omitempty"`
56-
Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
57-
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
58-
BaseFee *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
59-
WithdrawalsRoot *common.Hash `json:"withdrawalsRoot,omitempty"`
50+
StateRoot common.Hash `json:"stateRoot"`
51+
TxRoot common.Hash `json:"txRoot"`
52+
ReceiptRoot common.Hash `json:"receiptsRoot"`
53+
LogsHash common.Hash `json:"logsHash"`
54+
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
55+
Receipts types.Receipts `json:"receipts"`
56+
Rejected []*rejectedTx `json:"rejected,omitempty"`
57+
Difficulty *math.HexOrDecimal256 `json:"currentDifficulty" gencodec:"required"`
58+
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
59+
BaseFee *math.HexOrDecimal256 `json:"currentBaseFee,omitempty"`
60+
WithdrawalsRoot *common.Hash `json:"withdrawalsRoot,omitempty"`
61+
CurrentExcessBlobGas *math.HexOrDecimal64 `json:"currentExcessBlobGas,omitempty"`
62+
CurrentBlobGasUsed *math.HexOrDecimal64 `json:"currentBlobGasUsed,omitempty"`
6063
}
6164

6265
type ommer struct {
@@ -66,37 +69,43 @@ type ommer struct {
6669

6770
//go:generate go run github.com/fjl/gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
6871
type stEnv struct {
69-
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
70-
Difficulty *big.Int `json:"currentDifficulty"`
71-
Random *big.Int `json:"currentRandom"`
72-
ParentDifficulty *big.Int `json:"parentDifficulty"`
73-
ParentBaseFee *big.Int `json:"parentBaseFee,omitempty"`
74-
ParentGasUsed uint64 `json:"parentGasUsed,omitempty"`
75-
ParentGasLimit uint64 `json:"parentGasLimit,omitempty"`
76-
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
77-
Number uint64 `json:"currentNumber" gencodec:"required"`
78-
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
79-
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
80-
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
81-
Ommers []ommer `json:"ommers,omitempty"`
82-
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
83-
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
84-
ParentUncleHash common.Hash `json:"parentUncleHash"`
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"`
8591
}
8692

8793
type stEnvMarshaling struct {
88-
Coinbase common.UnprefixedAddress
89-
Difficulty *math.HexOrDecimal256
90-
Random *math.HexOrDecimal256
91-
ParentDifficulty *math.HexOrDecimal256
92-
ParentBaseFee *math.HexOrDecimal256
93-
ParentGasUsed math.HexOrDecimal64
94-
ParentGasLimit math.HexOrDecimal64
95-
GasLimit math.HexOrDecimal64
96-
Number math.HexOrDecimal64
97-
Timestamp math.HexOrDecimal64
98-
ParentTimestamp math.HexOrDecimal64
99-
BaseFee *math.HexOrDecimal256
94+
Coinbase common.UnprefixedAddress
95+
Difficulty *math.HexOrDecimal256
96+
Random *math.HexOrDecimal256
97+
ParentDifficulty *math.HexOrDecimal256
98+
ParentBaseFee *math.HexOrDecimal256
99+
ParentGasUsed math.HexOrDecimal64
100+
ParentGasLimit math.HexOrDecimal64
101+
GasLimit math.HexOrDecimal64
102+
Number math.HexOrDecimal64
103+
Timestamp math.HexOrDecimal64
104+
ParentTimestamp math.HexOrDecimal64
105+
BaseFee *math.HexOrDecimal256
106+
ExcessBlobGas *math.HexOrDecimal64
107+
ParentExcessBlobGas *math.HexOrDecimal64
108+
ParentBlobGasUsed *math.HexOrDecimal64
100109
}
101110

102111
type rejectedTx struct {
@@ -153,15 +162,34 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
153162
rnd := common.BigToHash(pre.Env.Random)
154163
vmContext.Random = &rnd
155164
}
165+
// If excessBlobGas is defined, add it to the vmContext.
166+
if pre.Env.ExcessBlobGas != nil {
167+
vmContext.ExcessBlobGas = pre.Env.ExcessBlobGas
168+
} else {
169+
// If it is not explicitly defined, but we have the parent values, we try
170+
// to calculate it ourselves.
171+
parentExcessBlobGas := pre.Env.ParentExcessBlobGas
172+
parentBlobGasUsed := pre.Env.ParentBlobGasUsed
173+
if parentExcessBlobGas != nil && parentBlobGasUsed != nil {
174+
excessBlobGas := eip4844.CalcExcessBlobGas(*parentExcessBlobGas, *parentBlobGasUsed)
175+
vmContext.ExcessBlobGas = &excessBlobGas
176+
}
177+
}
156178
// If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's
157179
// done in StateProcessor.Process(block, ...), right before transactions are applied.
158180
if chainConfig.DAOForkSupport &&
159181
chainConfig.DAOForkBlock != nil &&
160182
chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 {
161183
misc.ApplyDAOHardFork(statedb)
162184
}
163-
185+
var blobGasUsed uint64
164186
for i, tx := range txs {
187+
if tx.Type() == types.BlobTxType && vmContext.ExcessBlobGas == nil {
188+
errMsg := "blob tx used but field env.ExcessBlobGas missing"
189+
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", errMsg)
190+
rejectedTxs = append(rejectedTxs, &rejectedTx{i, errMsg})
191+
continue
192+
}
165193
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee)
166194
if err != nil {
167195
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
@@ -191,6 +219,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
191219
gaspool.SetGas(prevGas)
192220
continue
193221
}
222+
if tx.Type() == types.BlobTxType {
223+
blobGasUsed += params.BlobTxBlobGasPerBlob
224+
}
194225
includedTxs = append(includedTxs, tx)
195226
if hashError != nil {
196227
return nil, nil, NewError(ErrorMissingBlockhash, hashError)
@@ -286,6 +317,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
286317
h := types.DeriveSha(types.Withdrawals(pre.Env.Withdrawals), trie.NewStackTrie(nil))
287318
execRs.WithdrawalsRoot = &h
288319
}
320+
if vmContext.ExcessBlobGas != nil {
321+
execRs.CurrentExcessBlobGas = (*math.HexOrDecimal64)(vmContext.ExcessBlobGas)
322+
execRs.CurrentBlobGasUsed = (*math.HexOrDecimal64)(&blobGasUsed)
323+
}
289324
// Re-create statedb instance with new root upon the updated database
290325
// for accessing latest states.
291326
statedb, err = state.New(root, statedb.Database(), nil)

cmd/evm/internal/t8ntool/gen_stenv.go

+50-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/evm/t8n_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ func TestT8n(t *testing.T) {
259259
output: t8nOutput{alloc: true, result: true},
260260
expOut: "exp.json",
261261
},
262+
{ // Cancun tests
263+
base: "./testdata/28",
264+
input: t8nInput{
265+
"alloc.json", "txs.rlp", "env.json", "Cancun", "",
266+
},
267+
output: t8nOutput{alloc: true, result: true},
268+
expOut: "exp.json",
269+
},
262270
} {
263271
args := []string{"t8n"}
264272
args = append(args, tc.output.get()...)

cmd/evm/testdata/28/alloc.json

+16
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+
"0xb94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
10+
"balance" : "0x016345785d8a0000",
11+
"code" : "0x60004960015500",
12+
"nonce" : "0x00",
13+
"storage" : {
14+
}
15+
}
16+
}

cmd/evm/testdata/28/env.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
"blockHashes" : {
20+
"0" : "0x3a9b485972e7353edd9152712492f0c58d89ef80623686b6bf947a4a6dce6cb6"
21+
}
22+
}

0 commit comments

Comments
 (0)