Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Base fee change denominator as genesis parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jelacamarko committed Sep 27, 2023
1 parent 226dd88 commit e83a108
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
11 changes: 4 additions & 7 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
)

const (
// defaultBaseFeeChangeDenom is the value to bound the amount the base fee can change between blocks.
defaultBaseFeeChangeDenom = 8

// blockGasTargetDivisor is the bound divisor of the gas limit, used in update calculations
blockGasTargetDivisor uint64 = 1024

Expand Down Expand Up @@ -1381,22 +1378,22 @@ func (b *Blockchain) CalculateBaseFee(parent *types.Header) uint64 {
// If the parent block used more gas than its target, the baseFee should increase.
if parent.GasUsed > parentGasTarget {
gasUsedDelta := parent.GasUsed - parentGasTarget
baseFeeDelta := calcBaseFeeDelta(gasUsedDelta, parentGasTarget, parent.BaseFee)
baseFeeDelta := b.calcBaseFeeDelta(gasUsedDelta, parentGasTarget, parent.BaseFee)

return parent.BaseFee + common.Max(baseFeeDelta, 1)
}

// Otherwise, if the parent block used less gas than its target, the baseFee should decrease.
gasUsedDelta := parentGasTarget - parent.GasUsed
baseFeeDelta := calcBaseFeeDelta(gasUsedDelta, parentGasTarget, parent.BaseFee)
baseFeeDelta := b.calcBaseFeeDelta(gasUsedDelta, parentGasTarget, parent.BaseFee)

return common.Max(parent.BaseFee-baseFeeDelta, 0)
}

func calcBaseFeeDelta(gasUsedDelta, parentGasTarget, baseFee uint64) uint64 {
func (b *Blockchain) calcBaseFeeDelta(gasUsedDelta, parentGasTarget, baseFee uint64) uint64 {
y := baseFee * gasUsedDelta / parentGasTarget

return y / defaultBaseFeeChangeDenom
return y / b.config.Params.BaseFeeChangeDenom
}

func (b *Blockchain) writeBatchAndUpdate(
Expand Down
1 change: 1 addition & 0 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ func TestBlockchain_CalculateBaseFee(t *testing.T) {
Forks: &chain.Forks{
chain.London: chain.NewFork(5),
},
BaseFeeChangeDenom: chain.BaseFeeChangeDenom,
},
Genesis: &chain.Genesis{
BaseFeeEM: test.elasticityMultiplier,
Expand Down
3 changes: 3 additions & 0 deletions chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const (

// GenesisDifficulty is the default difficulty of the Genesis block.
GenesisDifficulty uint64 = 131072

// BaseFeeChangeDenom is the value to bound the amount the base fee can change between blocks
BaseFeeChangeDenom = uint64(8)
)

var (
Expand Down
3 changes: 3 additions & 0 deletions chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ type Params struct {
BurnContract map[uint64]types.Address `json:"burnContract"`
// Destination address to initialize default burn contract with
BurnContractDestinationAddress types.Address `json:"burnContractDestinationAddress,omitempty"`

// BaseFeeChangeDenom is the value to bound the amount the base fee can change between blocks
BaseFeeChangeDenom uint64 `json:"baseFeeChangeDenom,omitempty"`
}

type AddressListConfig struct {
Expand Down
15 changes: 8 additions & 7 deletions command/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

const (
DefaultGenesisFileName = "genesis.json"
DefaultChainName = "polygon-edge"
DefaultChainID = 100
DefaultConsensus = server.PolyBFTConsensus
DefaultGenesisGasUsed = 458752 // 0x70000
DefaultGenesisGasLimit = 5242880 // 0x500000
DefaultGenesisBaseFeeEM = chain.GenesisBaseFeeEM
DefaultGenesisFileName = "genesis.json"
DefaultChainName = "polygon-edge"
DefaultChainID = 100
DefaultConsensus = server.PolyBFTConsensus
DefaultGenesisGasUsed = 458752 // 0x70000
DefaultGenesisGasLimit = 5242880 // 0x500000
DefaultGenesisBaseFeeEM = chain.GenesisBaseFeeEM
DefaultGenesisBaseFeeChangeDenom = chain.BaseFeeChangeDenom
)

var (
Expand Down
7 changes: 7 additions & 0 deletions command/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ func setFlags(cmd *cobra.Command) {
defaultBlockTrackerPollInterval,
"interval (number of seconds) at which block tracker polls for latest block at rootchain",
)

cmd.Flags().Uint64Var(
&params.baseFeeChangeDenom,
baseFeeChangeDenomFlag,
command.DefaultGenesisBaseFeeChangeDenom,
"default base fee change denominator the value to bound the amount the base fee can change between blocks.",
)
}

// Access Control Lists
Expand Down
15 changes: 12 additions & 3 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
rewardTokenCodeFlag = "reward-token-code"
rewardWalletFlag = "reward-wallet"
blockTrackerPollIntervalFlag = "block-tracker-poll-interval"
baseFeeChangeDenomFlag = "base-fee-change-denom"

defaultNativeTokenName = "Polygon"
defaultNativeTokenSymbol = "MATIC"
Expand All @@ -65,6 +66,7 @@ var (
errRewardWalletAmountZero = errors.New("reward wallet amount can not be zero or negative")
errReserveAccMustBePremined = errors.New("it is mandatory to premine reserve account (0x0 address)")
errBlockTrackerPollInterval = errors.New("block tracker poll interval must be greater than 0")
errBaseFeeChangeDenomZero = errors.New("base fee change denominator must be greater than 0")
)

type genesisParams struct {
Expand Down Expand Up @@ -134,6 +136,8 @@ type genesisParams struct {
rewardWallet string

blockTrackerPollInterval time.Duration

baseFeeChangeDenom uint64
}

func (p *genesisParams) validateFlags() error {
Expand All @@ -153,6 +157,10 @@ func (p *genesisParams) validateFlags() error {
return err
}

if p.baseFeeChangeDenom == 0 {
return errBaseFeeChangeDenomZero
}

if p.isPolyBFTConsensus() {
if err := p.extractNativeTokenMetadata(); err != nil {
return err
Expand Down Expand Up @@ -401,9 +409,10 @@ func (p *genesisParams) initGenesisConfig() error {
GasUsed: command.DefaultGenesisGasUsed,
},
Params: &chain.Params{
ChainID: int64(p.chainID),
Forks: enabledForks,
Engine: p.consensusEngineConfig,
ChainID: int64(p.chainID),
Forks: enabledForks,
Engine: p.consensusEngineConfig,
BaseFeeChangeDenom: p.baseFeeChangeDenom,
},
Bootnodes: p.bootnodes,
}
Expand Down
1 change: 1 addition & 0 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er
Engine: map[string]interface{}{
string(server.PolyBFTConsensus): polyBftConfig,
},
BaseFeeChangeDenom: p.baseFeeChangeDenom,
},
Bootnodes: p.bootnodes,
}
Expand Down

0 comments on commit e83a108

Please sign in to comment.