-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[blockchain] verify EIP1559 header (#4353)
- Loading branch information
Showing
12 changed files
with
194 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2024 IoTeX Foundation | ||
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability | ||
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. | ||
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. | ||
|
||
package block | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/iotexproject/iotex-core/action" | ||
"github.com/iotexproject/iotex-core/action/protocol" | ||
"github.com/iotexproject/iotex-core/blockchain/genesis" | ||
) | ||
|
||
func VerifyEIP1559Header(g genesis.Blockchain, parent *protocol.TipInfo, header *Header) error { | ||
if header.BaseFee() == nil { | ||
return errors.New("header is missing baseFee") | ||
} | ||
// Verify the baseFee is correct based on the parent header. | ||
expectedBaseFee := CalcBaseFee(g, parent) | ||
if header.BaseFee().Cmp(expectedBaseFee) != 0 { | ||
return errors.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d", | ||
header.BaseFee(), expectedBaseFee, parent.BaseFee, parent.GasUsed) | ||
} | ||
return nil | ||
} | ||
|
||
// CalcBaseFee calculates the basefee of the header. | ||
func CalcBaseFee(g genesis.Blockchain, parent *protocol.TipInfo) *big.Int { | ||
// If the current block is the first EIP-1559 block, return the InitialBaseFee. | ||
if parent.Height < g.VanuatuBlockHeight { | ||
return new(big.Int).SetUint64(action.InitialBaseFee) | ||
} | ||
|
||
parentGasTarget := g.BlockGasLimitByHeight(parent.Height) / action.DefaultElasticityMultiplier | ||
// 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) | ||
} | ||
|
||
var ( | ||
num = new(big.Int) | ||
denom = new(big.Int) | ||
) | ||
if parent.GasUsed > parentGasTarget { | ||
// If the parent block used more gas than its target, the baseFee should increase. | ||
// max(1, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator) | ||
num.SetUint64(parent.GasUsed - parentGasTarget) | ||
num.Mul(num, parent.BaseFee) | ||
num.Div(num, denom.SetUint64(parentGasTarget)) | ||
num.Div(num, denom.SetUint64(action.DefaultBaseFeeChangeDenominator)) | ||
baseFeeDelta := math.BigMax(num, common.Big1) | ||
return num.Add(parent.BaseFee, baseFeeDelta) | ||
} else { | ||
// Otherwise if the parent block used less gas than its target, the baseFee should decrease. | ||
// max(0, parentBaseFee * gasUsedDelta / parentGasTarget / baseFeeChangeDenominator) | ||
num.SetUint64(parentGasTarget - parent.GasUsed) | ||
num.Mul(num, parent.BaseFee) | ||
num.Div(num, denom.SetUint64(parentGasTarget)) | ||
num.Div(num, denom.SetUint64(action.DefaultBaseFeeChangeDenominator)) | ||
baseFee := num.Sub(parent.BaseFee, num) | ||
return math.BigMax(baseFee, new(big.Int).SetUint64(action.InitialBaseFee)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2024 IoTeX Foundation | ||
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability | ||
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. | ||
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file. | ||
|
||
package block | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/iotexproject/iotex-core/action" | ||
"github.com/iotexproject/iotex-core/action/protocol" | ||
"github.com/iotexproject/iotex-core/blockchain/genesis" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCalcBaseFee(t *testing.T) { | ||
r := require.New(t) | ||
tests := []struct { | ||
parentBaseFee int64 | ||
parentGasUsed uint64 | ||
expectedBaseFee int64 | ||
}{ | ||
{action.InitialBaseFee, 25000000, action.InitialBaseFee}, // usage == target | ||
{2 * action.InitialBaseFee, 24000000, 1990000000000}, // usage below target | ||
{action.InitialBaseFee, 24000000, 1000000000000}, // usage below target but capped to default | ||
{action.InitialBaseFee, 26000000, 1005000000000}, // usage above target | ||
} | ||
g := genesis.Default.Blockchain | ||
for _, test := range tests { | ||
parent := &protocol.TipInfo{ | ||
Height: g.VanuatuBlockHeight, | ||
GasUsed: test.parentGasUsed, | ||
BaseFee: big.NewInt(test.parentBaseFee), | ||
} | ||
expect := big.NewInt(test.expectedBaseFee) | ||
r.Equal(expect, CalcBaseFee(g, parent)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.