From 76ccc7234332c04a382e67e70a4dc44fcbd360fa Mon Sep 17 00:00:00 2001 From: Mister-EA Date: Fri, 7 Apr 2023 12:21:30 +0200 Subject: [PATCH] core, params, consensus: enable london hard fork features with basefee=0 --- consensus/misc/eip1559.go | 67 ++++++++++++++++++---------------- consensus/misc/eip1559_test.go | 4 +- consensus/parlia/parlia.go | 60 +++++++++++++++++++++--------- core/state_processor_test.go | 4 +- core/state_transition.go | 11 +++--- core/types/block_test.go | 5 ++- params/protocol_params.go | 6 +-- 7 files changed, 93 insertions(+), 64 deletions(-) diff --git a/consensus/misc/eip1559.go b/consensus/misc/eip1559.go index 8fca0fdc70..52a199f4f9 100644 --- a/consensus/misc/eip1559.go +++ b/consensus/misc/eip1559.go @@ -20,8 +20,6 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" ) @@ -42,6 +40,10 @@ func VerifyEip1559Header(config *params.ChainConfig, parent, header *types.Heade if header.BaseFee == nil { return fmt.Errorf("header is missing baseFee") } + + if header.BaseFee.Cmp(big.NewInt(params.InitialBaseFee)) != 0 { + return fmt.Errorf("header has a non-zero baseFee of %v", header.BaseFee) + } // Verify the baseFee is correct based on the parent header. expectedBaseFee := CalcBaseFee(config, parent) if header.BaseFee.Cmp(expectedBaseFee) != 0 { @@ -57,37 +59,38 @@ func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int { if !config.IsLondon(parent.Number) { return new(big.Int).SetUint64(params.InitialBaseFee) } + return new(big.Int).SetUint64(params.InitialBaseFee) - var ( - parentGasTarget = parent.GasLimit / params.ElasticityMultiplier - parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget) - baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator) - ) - // If the parent gasUsed is the same as the target, the baseFee remains unchanged. - if parent.GasUsed == parentGasTarget { - return new(big.Int).Set(parent.BaseFee) - } - if parent.GasUsed > parentGasTarget { - // If the parent block used more gas than its target, the baseFee should increase. - gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget) - x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) - y := x.Div(x, parentGasTargetBig) - baseFeeDelta := math.BigMax( - x.Div(y, baseFeeChangeDenominator), - common.Big1, - ) + // var ( + // parentGasTarget = parent.GasLimit / params.ElasticityMultiplier + // parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget) + // baseFeeChangeDenominator = new(big.Int).SetUint64(params.BaseFeeChangeDenominator) + // ) + // // If the parent gasUsed is the same as the target, the baseFee remains unchanged. + // if parent.GasUsed == parentGasTarget { + // return new(big.Int).Set(parent.BaseFee) + // } + // if parent.GasUsed > parentGasTarget { + // // If the parent block used more gas than its target, the baseFee should increase. + // gasUsedDelta := new(big.Int).SetUint64(parent.GasUsed - parentGasTarget) + // x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) + // y := x.Div(x, parentGasTargetBig) + // baseFeeDelta := math.BigMax( + // x.Div(y, baseFeeChangeDenominator), + // common.Big1, + // ) - return x.Add(parent.BaseFee, baseFeeDelta) - } else { - // Otherwise if the parent block used less gas than its target, the baseFee should decrease. - gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed) - x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) - y := x.Div(x, parentGasTargetBig) - baseFeeDelta := x.Div(y, baseFeeChangeDenominator) + // return x.Add(parent.BaseFee, baseFeeDelta) + // } else { + // // Otherwise if the parent block used less gas than its target, the baseFee should decrease. + // gasUsedDelta := new(big.Int).SetUint64(parentGasTarget - parent.GasUsed) + // x := new(big.Int).Mul(parent.BaseFee, gasUsedDelta) + // y := x.Div(x, parentGasTargetBig) + // baseFeeDelta := x.Div(y, baseFeeChangeDenominator) - return math.BigMax( - x.Sub(parent.BaseFee, baseFeeDelta), - common.Big0, - ) - } + // return math.BigMax( + // x.Sub(parent.BaseFee, baseFeeDelta), + // common.Big0, + // ) + // } } diff --git a/consensus/misc/eip1559_test.go b/consensus/misc/eip1559_test.go index aaa09da9b9..da532e1957 100644 --- a/consensus/misc/eip1559_test.go +++ b/consensus/misc/eip1559_test.go @@ -115,8 +115,8 @@ func TestCalcBaseFee(t *testing.T) { expectedBaseFee int64 }{ {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target - {params.InitialBaseFee, 20000000, 9000000, 987500000}, // usage below target - {params.InitialBaseFee, 20000000, 11000000, 1012500000}, // usage above target + {params.InitialBaseFee, 20000000, 9000000, params.InitialBaseFee}, // usage below target + {params.InitialBaseFee, 20000000, 11000000, params.InitialBaseFee}, // usage above target } for i, test := range tests { parent := &types.Header{ diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go index aaa2f32cb1..2c53bd83f8 100644 --- a/consensus/parlia/parlia.go +++ b/consensus/parlia/parlia.go @@ -390,6 +390,22 @@ func getVoteAttestationFromHeader(header *types.Header, chainConfig *params.Chai return &attestation, nil } +func (p *Parlia) getParent(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) (*types.Header, error) { + var parent *types.Header + number := header.Number.Uint64() + if len(parents) > 0 { + parent = parents[len(parents)-1] + } else { + parent = chain.GetHeader(header.ParentHash, number-1) + } + + if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { + return nil, consensus.ErrUnknownAncestor + } else { + return parent, nil + } +} + // verifyVoteAttestation checks whether the vote attestation in the header is valid. func (p *Parlia) verifyVoteAttestation(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header) error { attestation, err := getVoteAttestationFromHeader(header, p.chainConfig, p.config) @@ -407,15 +423,9 @@ func (p *Parlia) verifyVoteAttestation(chain consensus.ChainHeaderReader, header } // Get parent block - number := header.Number.Uint64() - var parent *types.Header - if len(parents) > 0 { - parent = parents[len(parents)-1] - } else { - parent = chain.GetHeader(header.ParentHash, number-1) - } - if parent == nil || parent.Hash() != header.ParentHash { - return consensus.ErrUnknownAncestor + parent, err := p.getParent(chain, header, parents) + if err != nil { + return err } // The target block should be direct parent. @@ -537,6 +547,26 @@ func (p *Parlia) verifyHeader(chain consensus.ChainHeaderReader, header *types.H if err := misc.VerifyForkHashes(chain.Config(), header, false); err != nil { return err } + + parent, err := p.getParent(chain, header, parents) + if err != nil { + return err + } + + // Verify the block's gas usage and (if applicable) verify the base fee. + if !chain.Config().IsLondon(header.Number) { + // Verify BaseFee not present before EIP-1559 fork. + if header.BaseFee != nil { + return fmt.Errorf("invalid baseFee before fork: have %d, expected 'nil'", header.BaseFee) + } + if err := misc.VerifyGaslimit(parent.GasLimit, header.GasLimit); err != nil { + return err + } + } else if err := misc.VerifyEip1559Header(chain.Config(), parent, header); err != nil { + // Verify the header's EIP-1559 attributes. + return err + } + // All basic checks passed, verify cascading fields return p.verifyCascadingFields(chain, header, parents) } @@ -552,15 +582,9 @@ func (p *Parlia) verifyCascadingFields(chain consensus.ChainHeaderReader, header return nil } - var parent *types.Header - if len(parents) > 0 { - parent = parents[len(parents)-1] - } else { - parent = chain.GetHeader(header.ParentHash, number-1) - } - - if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { - return consensus.ErrUnknownAncestor + parent, err := p.getParent(chain, header, parents) + if err != nil { + return err } snap, err := p.snapshot(chain, number-1, header.ParentHash, parents) diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 964c08947c..d1dd278308 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -160,11 +160,11 @@ func TestStateProcessorErrors(t *testing.T) { }, want: "could not apply tx 0 [0xbd49d8dadfd47fb846986695f7d4da3f7b2c48c8da82dbc211a26eb124883de9]: gas limit reached", }, - { // ErrFeeCapTooLow + { // ErrFeeCapTooLow does not apply because default BaseFee is 0 txs: []*types.Transaction{ mkDynamicTx(0, common.Address{}, params.TxGas, big.NewInt(0), big.NewInt(0)), }, - want: "could not apply tx 0 [0xc4ab868fef0c82ae0387b742aee87907f2d0fc528fc6ea0a021459fb0fc4a4a8]: max fee per gas less than block base fee: address 0x71562b71999873DB5b286dF957af199Ec94617F7, maxFeePerGas: 0 baseFee: 875000000", + want: "invalid gas used (remote: 0 local: 21000)", }, { // ErrTipVeryHigh txs: []*types.Transaction{ diff --git a/core/state_transition.go b/core/state_transition.go index 8083a4ea61..f4f38b6e29 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -353,14 +353,15 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { st.refundGas(params.RefundQuotientEIP3529) } + effectiveTip := st.gasPrice + if rules.IsLondon { + effectiveTip = cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee)) + } + // consensus engine is parlia if st.evm.ChainConfig().Parlia != nil { - st.state.AddBalance(consensus.SystemAddress, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.gasPrice)) + st.state.AddBalance(consensus.SystemAddress, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip)) } else { - effectiveTip := st.gasPrice - if rules.IsLondon { - effectiveTip = cmath.BigMin(st.gasTipCap, new(big.Int).Sub(st.gasFeeCap, st.evm.Context.BaseFee)) - } st.state.AddBalance(st.evm.Context.Coinbase, new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), effectiveTip)) } diff --git a/core/types/block_test.go b/core/types/block_test.go index aa1db2f4fa..01ad95312a 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -69,7 +69,7 @@ func TestBlockEncoding(t *testing.T) { } func TestEIP1559BlockEncoding(t *testing.T) { - blockEnc := common.FromHex("f9030bf901fea083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4843b9aca00f90106f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1b8a302f8a0018080843b9aca008301e24194095e7baea6a6c7c4c2dfeb977efac326af552d878080f838f7940000000000000000000000000000000000000001e1a0000000000000000000000000000000000000000000000000000000000000000080a0fe38ca4e44a30002ac54af7cf922a6ac2ba11b7d22f548e8ecb3f51f41cb31b0a06de6a5cbae13c0c856e33acf021b51819636cfc009d39eafb9f606d546e305a8c0") + blockEnc := common.FromHex("f90303f901faa083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c480f90102f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1b89f02f89c018080808301e24194095e7baea6a6c7c4c2dfeb977efac326af552d878080f838f7940000000000000000000000000000000000000001e1a0000000000000000000000000000000000000000000000000000000000000000080a0fe38ca4e44a30002ac54af7cf922a6ac2ba11b7d22f548e8ecb3f51f41cb31b0a06de6a5cbae13c0c856e33acf021b51819636cfc009d39eafb9f606d546e305a8c0") var block Block if err := rlp.DecodeBytes(blockEnc, &block); err != nil { t.Fatal("decode error: ", err) @@ -87,7 +87,7 @@ func TestEIP1559BlockEncoding(t *testing.T) { check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1")) check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498")) check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017")) - check("Hash", block.Hash(), common.HexToHash("c7252048cd273fe0dac09650027d07f0e3da4ee0675ebbb26627cea92729c372")) + check("Hash", block.Hash(), common.HexToHash("0xae9d971d16de73f69f940f1bd4ce5961158781176e73abea92a2b8781403885e")) check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4)) check("Time", block.Time(), uint64(1426516743)) check("Size", block.Size(), common.StorageSize(len(blockEnc))) @@ -122,6 +122,7 @@ func TestEIP1559BlockEncoding(t *testing.T) { check("len(Transactions)", len(block.Transactions()), 2) check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash()) + check("Transactions[1].inner", block.Transactions()[1].inner, tx2.inner) check("Transactions[1].Hash", block.Transactions()[1].Hash(), tx2.Hash()) check("Transactions[1].Type", block.Transactions()[1].Type(), tx2.Type()) ourBlockEnc, err := rlp.EncodeToBytes(&block) diff --git a/params/protocol_params.go b/params/protocol_params.go index 653377cc41..9ac168a7db 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -121,9 +121,9 @@ const ( // Introduced in Tangerine Whistle (Eip 150) CreateBySelfdestructGas uint64 = 25000 - BaseFeeChangeDenominator = 8 // Bounds the amount the base fee can change between blocks. - ElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have. - InitialBaseFee = 1000000000 // Initial base fee for EIP-1559 blocks. + BaseFeeChangeDenominator = 8 // Bounds the amount the base fee can change between blocks. + ElasticityMultiplier = 2 // Bounds the maximum gas limit an EIP-1559 block may have. + InitialBaseFee = 0 // Initial base fee for EIP-1559 blocks. MaxCodeSize = 24576 // Maximum bytecode to permit for a contract