Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base fee change denominator as genesis parameter #1934

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -249,6 +249,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,
"represents 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 @@ -42,6 +42,7 @@ const (
rewardWalletFlag = "reward-wallet"
blockTrackerPollIntervalFlag = "block-tracker-poll-interval"
proxyContractsAdminFlag = "proxy-contracts-admin"
baseFeeChangeDenomFlag = "base-fee-change-denom"

defaultNativeTokenName = "Polygon"
defaultNativeTokenSymbol = "MATIC"
Expand All @@ -67,6 +68,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 @@ -138,6 +140,8 @@ type genesisParams struct {
blockTrackerPollInterval time.Duration

proxyContractsAdmin string

baseFeeChangeDenom uint64
}

func (p *genesisParams) validateFlags() error {
Expand All @@ -157,6 +161,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 @@ -409,9 +417,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 @@ -175,6 +175,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
Loading