8000 Verify with the block number config by kjeom · Pull Request #1530 · klaytn/klaytn · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 19, 2024. It is now read-only.

Verify with the block number config #1530

Merged
merged 10 commits into from
Jul 20, 2022
2 changes: 1 addition & 1 deletion blockchain/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
Time: time,
}
if chain.Config().IsMagmaForkEnabled(header.Number) {
header.BaseFee = misc.NextBlockBaseFee(parent.Header(), chain.Config())
header.BaseFee = misc.NextMagmaBlockBaseFee(parent.Header(), chain.Config().Governance.KIP71)
}
return header
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480, 10000 7 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {

// It need to update gas price of tx pool after magma hardfork
if pool.magma {
pool.gasPrice = misc.NextBlockBaseFee(newHead, pool.chainconfig)
pool.gasPrice = misc.NextMagmaBlockBaseFee(newHead, pool.chainconfig.Governance.KIP71)
}
}

Expand Down
37 changes: 30 additions & 7 deletions consensus/istanbul/backend/engine.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"math/big"
"time"

"github.com/klaytn/klaytn/reward"

lru "github.com/hashicorp/golang-lru"
"github.com/klaytn/klaytn/blockchain/state"
"github.com/klaytn/klaytn/blockchain/types"
Expand All @@ -41,8 +39,10 @@ import (
"github.com/klaytn/klaytn/consensus/istanbul/validator"
"github.com/klaytn/klaytn/consensus/misc"
"github.com/klaytn/klaytn/crypto/sha3"
"github.com/klaytn/klaytn/governance"
"github.com/klaytn/klaytn/networks/rpc"
"github.com/klaytn/klaytn/params"
"github.com/klaytn/klaytn/reward"
"github.com/klaytn/klaytn/rlp"
)

Expand Down Expand Up @@ -192,12 +192,35 @@ func (sb *backend) verifyHeader(chain consensus.ChainReader, header *types.Heade
}

// Header verify before/after magma fork
if !chain.Config().IsMagmaForkEnabled(header.Number) {
if header.BaseFee != nil {
return consensus.ErrInvalidBaseFee
if chain.Config().IsMagmaForkEnabled(header.Number) {
_, data, err := sb.governance.ReadGovernance(header.Number.Uint64())
if err != nil {
return err
}
} else if err := misc.VerifyMagmaHeader(chain.Config(), parents[len(parents)-1], header); err != nil {
return err

kip71 := params.GetDefaultKIP71Config()
gkmr := governance.GovernanceKeyMapReverse
if l := data[gkmr[params.LowerBoundBaseFee]]; l != nil {
kip71.LowerBoundBaseFee = l.(uint64)
}
if u := data[gkmr[params.UpperBoundBaseFee]]; u != nil {
kip71.UpperBoundBaseFee = u.(uint64)
}
if g := data[gkmr[params.GasTarget]]; g != nil {
kip71.GasTarget = g.(uint64)
}
if b := data[gkmr[params.BaseFeeDenominator]]; b != nil {
kip71.BaseFeeDenominator = b.(uint64)
}
if m := data[gkmr[params.MaxBlockGasUsedForBaseFee]]; m != nil {
kip71.MaxBlockGasUsedForBaseFee = m.(uint64)
}

if err := misc.VerifyMagmaHeader(parents[len(parents)-1], header, kip71); err != nil {
return err
}
} else if header.BaseFee != nil {
return consensus.ErrInvalidBaseFee
}

// Don't waste time checking blocks from the future
Expand Down
20 changes: 10 additions & 10 deletions consensus/misc/kip71.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/klaytn/klaytn/params"
)

func VerifyMagmaHeader(config *params.ChainConfig, parentHeader, header *types.Header) error {
func VerifyMagmaHeader(parentHeader, header *types.Header, kip71Config *params.KIP71Config) error {
if header.BaseFee == nil {
return fmt.Errorf("header is missing baseFee")
}
// Verify the baseFee is correct based on the parent header.
expectedBaseFee := NextBlockBaseFee(parentHeader, config)
expectedBaseFee := NextMagmaBlockBaseFee(parentHeader, kip71Config)
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
header.BaseFee, expectedBaseFee, parentHeader.BaseFee, parentHeader.GasUsed)
Expand All @@ -38,27 +38,27 @@ func makeEvenByUp(baseFee *big.Int) *big.Int {
return baseFee
}

func NextBlockBaseFee(parentHeader *types.Header, config *params.ChainConfig) *big.Int {
func NextMagmaBlockBaseFee(parentHeader *types.Header, kip71Config *params.KIP71Config) *big.Int {
// governance parameters
lowerBoundBaseFee := new(big.Int).SetUint64(config.Governance.KIP71.LowerBoundBaseFee)
upperBoundBaseFe 8000 e := new(big.Int).SetUint64(config.Governance.KIP71.UpperBoundBaseFee)
lowerBoundBaseFee := new(big.Int).SetUint64(kip71Config.LowerBoundBaseFee)
upperBoundBaseFee := new(big.Int).SetUint64(kip71Config.UpperBoundBaseFee)
makeEvenByUp(lowerBoundBaseFee)
makeEvenByDown(upperBoundBaseFee)

// If the parent is the magma disabled block or genesis, then return the lowerBoundBaseFee (default 25ston)
if !config.IsMagmaForkEnabled(parentHeader.Number) || parentHeader.Number.Cmp(new(big.Int).SetUint64(0)) == 0 {
if parentHeader.Number.Cmp(new(big.Int).SetUint64(0)) == 0 || parentHeader.BaseFee == nil {
return lowerBoundBaseFee
}

var baseFeeDenominator *big.Int
if config.Governance.KIP71.BaseFeeDenominator == 0 {
if kip71Config.BaseFeeDenominator == 0 {
// To avoid panic, set the fluctuation range small
baseFeeDenominator = new(big.Int).SetUint64(64)
} else {
baseFeeDenominator = new(big.Int).SetUint64(config.Governance.KIP71.BaseFeeDenominator)
baseFeeDenominator = new(big.Int).SetUint64(kip71Config.BaseFeeDenominator)
}
gasTarget := config.Governance.KIP71.GasTarget
upperGasLimit := config.Governance.KIP71.MaxBlockGasUsedForBaseFee
gasTarget := kip71Config.GasTarget
upperGasLimit := kip71Config.MaxBlockGasUsedForBaseFee

// check the case of upper/lowerBoundBaseFee is updated by governance mechanism
parentBaseFee := parentHeader.BaseFee
Expand Down
22 changes: 12 additions & 10 deletions consensus/misc/kip71_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestEvenBaseFee(t *testing.T) {
GasUsed: test.parentGasUsed,
BaseFee: new(big.Int).SetUint64(test.parentBaseFee),
}
even := NextBlockBaseFee(parent, testConfig)
even := NextMagmaBlockBaseFee(parent, testConfig.Governance.KIP71)
// even check
if even.Bit(0) != 0 {
t.Errorf("NextBlockBaseFee:%d is not a even number", even)
Expand Down Expand Up @@ -120,7 +120,10 @@ func TestNextBlockBaseFee(t *testing.T) {
GasUsed: test.parentGasUsed,
BaseFee: big.NewInt(test.parentBaseFee),
}
if have, want := NextBlockBaseFee(parent, getTestConfig(big.NewInt(3))), big.NewInt(test.nextBaseFee); have.Cmp(want) != 0 {
if have, want := NextMagmaBlockBaseFee(
parent,
getTestConfig(big.NewInt(3)).Governance.KIP71),
big.NewInt(test.nextBaseFee); have.Cmp(want) != 0 {
t.Errorf("test %d: have %d want %d, ", i, have, want)
}
}
Expand Down Expand Up @@ -152,7 +155,7 @@ func TestNextBlockBaseFeeWhenGovernanceUpdated(t *testing.T) {
GasUsed: test.parentGasUsed,
BaseFee: big.NewInt(test.parentBaseFee),
}
if have, want := NextBlockBaseFee(parent, config), big.NewInt(test.nextBaseFee); have.Cmp(want) != 0 {
if have, want := NextMagmaBlockBaseFee(parent, config.Governance.KIP71), big.NewInt(test.nextBaseFee); have.Cmp(want) != 0 {
t.Errorf("test %d: have %d want %d, ", i, have, want)
}
}
Expand Down Expand Up @@ -219,7 +222,7 @@ func blocksToReachExpectedBaseFee(t *testing.T, testCase BaseFeeTestCase) {
GasUsed: testCase.GasUsed,
BaseFee: parentBaseFee,
}
parentBaseFee = NextBlockBaseFee(parent, testConfig)
parentBaseFee = NextMagmaBlockBaseFee(parent, testConfig.Governance.KIP71)

if testCase.compMethod(parentBaseFee, testCase.expectedBaseFee) {
break
Expand All @@ -235,9 +238,8 @@ func TestInactieDynamicPolicyBeforeForkedBlock(t *testing.T) {
parent := &types.Header{
Number: common.Big3,
GasUsed: 84000000,
BaseFee: parentBaseFee,
}
nextBaseFee := NextBlockBaseFee(parent, getTestConfig(big.NewInt(5)))
nextBaseFee := NextMagmaBlockBaseFee(parent, getTestConfig(big.NewInt(5)).Governance.KIP71)
if parentBaseFee.Cmp(nextBaseFee) < 0 {
t.Errorf("before fork, dynamic base fee policy should be inactive, current base fee: %d next base fee: %d", parentBaseFee, nextBaseFee)
}
Expand All @@ -250,7 +252,7 @@ func TestActieDynamicPolicyAfterForkedBlock(t *testing.T) {
GasUsed: 84000000,
BaseFee: parentBaseFee,
}
nextBaseFee := NextBlockBaseFee(parent, getTestConfig(big.NewInt(2)))
nextBaseFee := NextMagmaBlockBaseFee(parent, getTestConfig(big.NewInt(2)).Governance.KIP71)
if parentBaseFee.Cmp(nextBaseFee) > 0 {
t.Errorf("after fork, dynamic base fee policy should be active, current base fee: %d next base fee: %d", parentBaseFee, nextBaseFee)
}
Expand All @@ -269,7 +271,7 @@ func BenchmarkNextBlockBaseFeeRandom(b *testing.B) {
} else {
parent.GasUsed = 40000000
}
_ = NextBlockBaseFee(parent, getTestConfig(big.NewInt(2)))
_ = NextMagmaBlockBaseFee(parent, getTestConfig(big.NewInt(2)).Governance.KIP71)
}
}

Expand All @@ -281,7 +283,7 @@ func BenchmarkNextBlockBaseFeeUpperBound(b *testing.B) {
BaseFee: parentBaseFee,
}
for i := 0; i < b.N; i++ {
_ = NextBlockBaseFee(parent, getTestConfig(big.NewInt(2)))
_ = NextMagmaBlockBaseFee(parent, getTestConfig(big.NewInt(2)).Governance.KIP71)
}
}

Expand All @@ -293,6 +295,6 @@ func BenchmarkNextBlockBaseFeeLowerBound(b *testing.B) {
BaseFee: parentBaseFee,
}
for i := 0; i < b.N; i++ {
_ = NextBlockBaseFee(parent, getTestConfig(big.NewInt(2)))
_ = NextMagmaBlockBaseFee(parent, getTestConfig(big.NewInt(2)).Governance.KIP71)
}
}
2 changes: 1 addition & 1 deletion node/cn/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
}
// TODO-Klaytn: If we implement baseFee feature like Ethereum does, we should calculate nextBaseFee from parent block header.
if chainconfig.IsMagmaForkEnabled(big.NewInt(int64(bf.blockNumber + 1))) {
bf.results.nextBaseFee = misc.NextBlockBaseFee(bf.header, chainconfig)
bf.results.nextBaseFee = misc.NextMagmaBlockBaseFee(bf.header, chainconfig.Governance.KIP71)
} else {
bf.results.nextBaseFee = new(big.Int).SetUint64(params.ZeroBaseFee)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/klay_test_blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (bcdata *BCData) prepareHeader() (*types.Header, error) {
Vote: common.Hex2Bytes("e194e733cb4d279da696f30d470f8c04decb54fcb0d28565706f6368853330303030"),
}
if bcdata.bc.Config().IsMagmaForkEnabled(num) {
header.BaseFee = misc.NextBlockBaseFee(parent.Header(), bcdata.bc.Config())
header.BaseFee = misc.NextMagmaBlockBaseFee(parent.Header(), bcdata.bc.Config().Governance.KIP71)
}

if err := bcdata.engine.Prepare(bcdata.bc, header); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion work/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func (self *worker) commitNewWork() {
if self.config.IsMagmaForkEnabled(nextBlockNum) {
// NOTE-klaytn NextBlockBaseFee needs the header of parent, self.chain.CurrentBlock
// So above code, TxPool().Pending(), is separated with this and can be refactored later.
nextBaseFee = misc.NextBlockBaseFee(parent.Header(), self.config)
nextBaseFee = misc.NextMagmaBlockBaseFee(parent.Header(), self.config.Governance.KIP71)
pending = types.FilterTransactionWithBaseFee(pending, nextBaseFee)
}
}
Expand Down
0