Skip to content

Commit 7a81af4

Browse files
committed
move ap4 block gas cost check to header (#849)
1 parent 1daec97 commit 7a81af4

File tree

3 files changed

+58
-46
lines changed

3 files changed

+58
-46
lines changed

consensus/dummy/consensus.go

+32-40
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,14 @@ func verifyHeaderGasFields(config *params.ChainConfig, header *types.Header, par
154154
return fmt.Errorf("expected base fee (%d), found (%d)", expectedBaseFee, header.BaseFee)
155155
}
156156

157-
if !config.IsSubnetEVM(header.Time) {
158-
if header.BlockGasCost != nil {
159-
return fmt.Errorf("invalid blockGasCost before fork: have %d, want <nil>", header.BlockGasCost)
160-
}
161-
return nil
162-
}
163-
164157
// Enforce BlockGasCost constraints
165158
expectedBlockGasCost := customheader.BlockGasCost(
159+
config,
166160
feeConfig,
167161
parent,
168162
header.Time,
169163
)
170-
if !utils.BigEqualUint64(header.BlockGasCost, expectedBlockGasCost) {
164+
if !utils.BigEqual(header.BlockGasCost, expectedBlockGasCost) {
171165
return fmt.Errorf("invalid block gas cost: have %d, want %d", header.BlockGasCost, expectedBlockGasCost)
172166
}
173167
return nil
@@ -334,25 +328,24 @@ func (eng *DummyEngine) verifyBlockFee(
334328
func (eng *DummyEngine) Finalize(chain consensus.ChainHeaderReader, block *types.Block, parent *types.Header, state *state.StateDB, receipts []*types.Receipt) error {
335329
config := chain.Config()
336330
timestamp := block.Time()
331+
// we use the parent to determine the fee config
332+
// since the current block has not been finalized yet.
333+
feeConfig, _, err := chain.GetFeeConfigAt(parent)
334+
if err != nil {
335+
return err
336+
}
337+
// Verify the BlockGasCost set in the header matches the expected value.
338+
blockGasCost := block.BlockGasCost()
339+
expectedBlockGasCost := customheader.BlockGasCost(
340+
config,
341+
feeConfig,
342+
parent,
343+
timestamp,
344+
)
345+
if !utils.BigEqual(blockGasCost, expectedBlockGasCost) {
346+
return fmt.Errorf("invalid blockGasCost: have %d, want %d", blockGasCost, expectedBlockGasCost)
347+
}
337348
if config.IsSubnetEVM(timestamp) {
338-
// we use the parent to determine the fee config
339-
// since the current block has not been finalized yet.
340-
feeConfig, _, err := chain.GetFeeConfigAt(parent)
341-
if err != nil {
342-
return err
343-
}
344-
345-
// Verify the BlockGasCost set in the header matches the expected value.
346-
blockGasCost := block.BlockGasCost()
347-
expectedBlockGasCost := customheader.BlockGasCost(
348-
feeConfig,
349-
parent,
350-
timestamp,
351-
)
352-
if !utils.BigEqualUint64(blockGasCost, expectedBlockGasCost) {
353-
return fmt.Errorf("invalid blockGasCost: have %d, want %d", blockGasCost, expectedBlockGasCost)
354-
}
355-
356349
// Verify the block fee was paid.
357350
if err := eng.verifyBlockFee(
358351
block.BaseFee(),
@@ -371,21 +364,20 @@ func (eng *DummyEngine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, h
371364
uncles []*types.Header, receipts []*types.Receipt,
372365
) (*types.Block, error) {
373366
config := chain.Config()
367+
// we use the parent to determine the fee config
368+
// since the current block has not been finalized yet.
369+
feeConfig, _, err := chain.GetFeeConfigAt(parent)
370+
if err != nil {
371+
return nil, err
372+
}
373+
// Calculate the required block gas cost for this block.
374+
header.BlockGasCost = customheader.BlockGasCost(
375+
config,
376+
feeConfig,
377+
parent,
378+
header.Time,
379+
)
374380
if config.IsSubnetEVM(header.Time) {
375-
// we use the parent to determine the fee config
376-
// since the current block has not been finalized yet.
377-
feeConfig, _, err := chain.GetFeeConfigAt(parent)
378-
if err != nil {
379-
return nil, err
380-
}
381-
// Calculate the required block gas cost for this block.
382-
blockGasCost := customheader.BlockGasCost(
383-
feeConfig,
384-
parent,
385-
header.Time,
386-
)
387-
header.BlockGasCost = new(big.Int).SetUint64(blockGasCost)
388-
389381
// Verify that this block covers the block fee.
390382
if err := eng.verifyBlockFee(
391383
header.BaseFee,

plugin/evm/header/block_gas_cost.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ var (
2222

2323
// BlockGasCost calculates the required block gas cost based on the parent
2424
// header and the timestamp of the new block.
25+
// Prior to Subnet-EVM, the returned block gas cost will be nil.
2526
func BlockGasCost(
27+
config *params.ChainConfig,
2628
feeConfig commontype.FeeConfig,
2729
parent *types.Header,
2830
timestamp uint64,
29-
) uint64 {
31+
) *big.Int {
32+
if !config.IsSubnetEVM(timestamp) {
33+
return nil
34+
}
3035
step := feeConfig.BlockGasCostStep.Uint64()
3136
// Treat an invalid parent/current time combination as 0 elapsed time.
3237
//
@@ -36,12 +41,12 @@ func BlockGasCost(
3641
if parent.Time <= timestamp {
3742
timeElapsed = timestamp - parent.Time
3843
}
39-
return BlockGasCostWithStep(
44+
return new(big.Int).SetUint64(BlockGasCostWithStep(
4045
feeConfig,
4146
parent.BlockGasCost,
4247
step,
4348
timeElapsed,
44-
)
49+
))
4550
}
4651

4752
// BlockGasCostWithStep calculates the required block gas cost based on the

plugin/evm/header/block_gas_cost_test.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,50 @@ func BlockGasCostTest(t *testing.T, feeConfig commontype.FeeConfig) {
5757

5858
tests := []struct {
5959
name string
60+
upgrades params.NetworkUpgrades
6061
parentTime uint64
6162
parentCost *big.Int
6263
timestamp uint64
63-
expected uint64
64+
expected *big.Int
6465
}{
66+
{
67+
name: "before_subnet_Evm",
68+
parentTime: 10,
69+
upgrades: params.TestPreSubnetEVMChainConfig.NetworkUpgrades,
70+
parentCost: maxBlockGasCostBig,
71+
timestamp: 10 + targetBlockRate + 1,
72+
expected: nil,
73+
},
6574
{
6675
name: "normal",
76+
upgrades: params.TestChainConfig.NetworkUpgrades,
6777
parentTime: 10,
6878
parentCost: maxBlockGasCostBig,
6979
timestamp: 10 + targetBlockRate + 1,
70-
expected: maxBlockGasCost - blockGasCostStep,
80+
expected: new(big.Int).SetUint64(maxBlockGasCost - blockGasCostStep),
7181
},
7282
{
7383
name: "negative_time_elapsed",
84+
upgrades: params.TestChainConfig.NetworkUpgrades,
7485
parentTime: 10,
7586
parentCost: feeConfig.MinBlockGasCost,
7687
timestamp: 9,
77-
expected: minBlockGasCost + blockGasCostStep*targetBlockRate,
88+
expected: new(big.Int).SetUint64(minBlockGasCost + blockGasCostStep*targetBlockRate),
7889
},
7990
}
8091

8192
for _, test := range tests {
8293
t.Run(test.name, func(t *testing.T) {
94+
config := &params.ChainConfig{
95+
NetworkUpgrades: test.upgrades,
96+
}
8397
parent := &types.Header{
8498
Time: test.parentTime,
8599
BlockGasCost: test.parentCost,
86100
}
87101

88102
assert.Equal(t, test.expected, BlockGasCost(
103+
config,
89104
feeConfig,
90105
parent,
91106
test.timestamp,

0 commit comments

Comments
 (0)