Skip to content

Commit 21fce63

Browse files
committed
systemcontracts: apply baseFee from policy contract for burning
Close #163
1 parent 3128d31 commit 21fce63

File tree

23 files changed

+125
-40
lines changed

23 files changed

+125
-40
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 8 additions & 0 deletions
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/eip1559"
2728
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
2829
"github.com/ethereum/go-ethereum/core"
2930
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -156,6 +157,13 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
156157
GasLimit: pre.Env.GasLimit,
157158
GetHash: getHash,
158159
}
160+
env := &pre.Env
161+
env.BaseFee = eip1559.CalcBaseFee(chainConfig, &types.Header{
162+
Number: new(big.Int).SetUint64(env.Number - 1),
163+
BaseFee: env.ParentBaseFee,
164+
GasUsed: env.ParentGasUsed,
165+
GasLimit: env.ParentGasLimit,
166+
}, statedb)
159167
// If currentBaseFee is defined, add it to the vmContext.
160168
if pre.Env.BaseFee != nil {
161169
vmContext.BaseFee = new(big.Int).Set(pre.Env.BaseFee)

cmd/evm/internal/t8ntool/transition.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ import (
2626

2727
"github.com/ethereum/go-ethereum/common"
2828
"github.com/ethereum/go-ethereum/common/hexutil"
29-
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
3029
"github.com/ethereum/go-ethereum/core"
3130
"github.com/ethereum/go-ethereum/core/state"
32-
"github.com/ethereum/go-ethereum/core/types"
3331
"github.com/ethereum/go-ethereum/core/vm"
3432
"github.com/ethereum/go-ethereum/eth/tracers"
3533
"github.com/ethereum/go-ethereum/eth/tracers/logger"
@@ -206,12 +204,6 @@ func applyLondonChecks(env *stEnv, chainConfig *params.ChainConfig) error {
206204
if env.ParentBaseFee == nil || env.Number == 0 {
207205
return NewError(ErrorConfig, errors.New("EIP-1559 config but missing 'currentBaseFee' in env section"))
208206
}
209-
env.BaseFee = eip1559.CalcBaseFee(chainConfig, &types.Header{
210-
Number: new(big.Int).SetUint64(env.Number - 1),
211-
BaseFee: env.ParentBaseFee,
212-
GasUsed: env.ParentGasUsed,
213-
GasLimit: env.ParentGasLimit,
214-
})
215207
return nil
216208
}
217209

consensus/beacon/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (beacon *Beacon) verifyHeader(chain consensus.ChainHeaderReader, header, pa
259259
return consensus.ErrInvalidNumber
260260
}
261261
// Verify the header's EIP-1559 attributes.
262-
if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
262+
if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header, nil); err != nil {
263263
return err
264264
}
265265
// Verify existence / non-existence of withdrawalsHash.

consensus/clique/clique.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
357357
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
358358
return err
359359
}
360-
} else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
360+
} else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header, nil); err != nil {
361361
// Verify the header's EIP-1559 attributes.
362362
return err
363363
}

consensus/dbft/chainreader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ChainHeaderReader interface {
1818
GetBlockByNumber(uint64) *types.Block
1919
VerifyBlock(block *types.Block) (*state.StateDB, types.Receipts, error)
2020
ProcessState(block *types.Block) (*state.StateDB, types.Receipts, []*types.Log, uint64, error)
21+
StateAt(root common.Hash) (*state.StateDB, error)
2122
}
2223

2324
// ChainHeaderWriter is a Blockchain API abstraction needed for proper blockQueue

consensus/dbft/dbft.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,12 @@ func (c *DBFT) verifyCascadingFields(chain consensus.ChainHeaderReader, header *
903903
if header.GasUsed > header.GasLimit {
904904
return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
905905
}
906+
907+
state, err := c.chain.StateAt(parent.Root)
908+
if err != nil {
909+
return fmt.Errorf("failed to get state: err %w, root %s", err, parent.Root)
910+
}
911+
906912
if !chain.Config().IsLondon(header.Number) {
907913
// Verify BaseFee not present before EIP-1559 fork.
908914
if header.BaseFee != nil {
@@ -911,7 +917,7 @@ func (c *DBFT) verifyCascadingFields(chain consensus.ChainHeaderReader, header *
911917
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
912918
return err
913919
}
914-
} else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
920+
} else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header, state); err != nil {
915921
// Verify the header's EIP-1559 attributes.
916922
return err
917923
}

consensus/ethash/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, pa
256256
if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil {
257257
return err
258258
}
259-
} else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header); err != nil {
259+
} else if err := eip1559.VerifyEIP1559Header(chain.Config(), parent, header, nil); err != nil {
260260
// Verify the header's EIP-1559 attributes.
261261
return err
262262
}

consensus/misc/eip1559/eip1559.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ import (
2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/common/math"
2626
"github.com/ethereum/go-ethereum/consensus/misc"
27+
"github.com/ethereum/go-ethereum/core/state"
28+
"github.com/ethereum/go-ethereum/core/systemcontracts"
2729
"github.com/ethereum/go-ethereum/core/types"
2830
"github.com/ethereum/go-ethereum/params"
2931
)
3032

3133
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
3234
// - gas limit check
3335
// - basefee check
34-
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
36+
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header, state *state.StateDB) error {
3537
// Verify that the gas limit remains within allowed bounds
3638
parentGasLimit := parent.GasLimit
3739
if !config.IsLondon(parent.Number) {
@@ -45,7 +47,7 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
4547
return errors.New("header is missing baseFee")
4648
}
4749
// Verify the baseFee is correct based on the parent header.
48-
expectedBaseFee := CalcBaseFee(config, parent)
50+
expectedBaseFee := CalcBaseFee(config, parent, state)
4951
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
5052
return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
5153
header.BaseFee, expectedBaseFee, parent.BaseFee, parent.GasUsed)
@@ -54,12 +56,20 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
5456
}
5557

5658
// CalcBaseFee calculates the basefee of the header.
57-
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
59+
func CalcBaseFee(config *params.ChainConfig, parent *types.Header, state *state.StateDB) *big.Int {
5860
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
5961
if !config.IsLondon(parent.Number) {
6062
return new(big.Int).SetUint64(params.InitialBaseFee)
6163
}
6264

65+
// Read baseFee from policy if baseFee is set in policy contract
66+
if state != nil {
67+
baseFee := state.GetState(systemcontracts.PolicyProxyHash, systemcontracts.GetBaseFeeStateHash()).Big()
68+
if baseFee.Cmp(common.Big0) > 0 {
69+
return baseFee
70+
}
71+
}
72+
6373
parentGasTarget := parent.GasLimit / config.ElasticityMultiplier()
6474
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
6575
if parent.GasUsed == parentGasTarget {

consensus/misc/eip1559/eip1559_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestBlockGasLimits(t *testing.T) {
9595
BaseFee: initial,
9696
Number: big.NewInt(tc.pNum + 1),
9797
}
98-
err := VerifyEIP1559Header(config(), parent, header)
98+
err := VerifyEIP1559Header(config(), parent, header, nil)
9999
if tc.ok && err != nil {
100100
t.Errorf("test %d: Expected valid header: %s", i, err)
101101
}
@@ -124,7 +124,7 @@ func TestCalcBaseFee(t *testing.T) {
124124
GasUsed: test.parentGasUsed,
125125
BaseFee: big.NewInt(test.parentBaseFee),
126126
}
127-
if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
127+
if have, want := CalcBaseFee(config(), parent, nil), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
128128
t.Errorf("test %d: have %d want %d, ", i, have, want)
129129
}
130130
}

core/chain_makers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (b *BlockGen) AddUncle(h *types.Header) {
230230
// The gas limit and price should be derived from the parent
231231
h.GasLimit = parent.GasLimit
232232
if b.cm.config.IsLondon(h.Number) {
233-
h.BaseFee = eip1559.CalcBaseFee(b.cm.config, parent)
233+
h.BaseFee = eip1559.CalcBaseFee(b.cm.config, parent, nil)
234234
if !b.cm.config.IsLondon(parent.Number) {
235235
parentGasLimit := parent.GasLimit * b.cm.config.ElasticityMultiplier()
236236
h.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit)
@@ -430,7 +430,7 @@ func (cm *chainMaker) makeHeader(parent *types.Block, state *state.StateDB, engi
430430
}
431431

432432
if cm.config.IsLondon(header.Number) {
433-
header.BaseFee = eip1559.CalcBaseFee(cm.config, parent.Header())
433+
header.BaseFee = eip1559.CalcBaseFee(cm.config, parent.Header(), nil)
434434
if !cm.config.IsLondon(parent.Number()) {
435435
parentGasLimit := parent.GasLimit() * cm.config.ElasticityMultiplier()
436436
header.GasLimit = CalcGasLimit(parentGasLimit, parentGasLimit)

0 commit comments

Comments
 (0)