Skip to content

Commit

Permalink
Merge pull request #98 from testinprod-io/pcw109550/canyon/eip1559-de…
Browse files Browse the repository at this point in the history
…nominator-adjust

[Canyon Hard Fork] Change EIP 1559 Denominator with Canyon
  • Loading branch information
pcw109550 authored Nov 6, 2023
2 parents 8b92f9f + 77b2209 commit 88a18c0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func newRPCBorTransaction(opaqueTx types.Transaction, txHash common.Hash, blockH
func newRPCPendingTransaction(tx types.Transaction, current *types.Header, config *chain.Config) *RPCTransaction {
var baseFee *big.Int
if current != nil {
baseFee = misc.CalcBaseFee(config, current)
baseFee = misc.CalcBaseFee(config, current, current.Time+1)
}
return newRPCTransaction(tx, common.Hash{}, 0, 0, baseFee, nil)
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func VerifyEip1559Header(config *chain.Config, parent, header *types.Header, ski
return fmt.Errorf("header is missing baseFee")
}
// Verify the baseFee is correct based on the parent header.
expectedBaseFee := CalcBaseFee(config, parent)
expectedBaseFee := CalcBaseFee(config, parent, header.Time)
if header.BaseFee.Cmp(expectedBaseFee) != 0 {
return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d",
expectedBaseFee, header.BaseFee, parent.BaseFee, parent.GasUsed)
Expand All @@ -58,7 +58,7 @@ func VerifyEip1559Header(config *chain.Config, parent, header *types.Header, ski
}

// CalcBaseFee calculates the basefee of the header.
func CalcBaseFee(config *chain.Config, parent *types.Header) *big.Int {
func CalcBaseFee(config *chain.Config, parent *types.Header, time uint64) *big.Int {
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
if !config.IsLondon(parent.Number.Uint64()) {
return new(big.Int).SetUint64(params.InitialBaseFee)
Expand All @@ -67,7 +67,7 @@ func CalcBaseFee(config *chain.Config, parent *types.Header) *big.Int {
var (
parentGasTarget = parent.GasLimit / config.ElasticityMultiplier(params.ElasticityMultiplier)
parentGasTargetBig = new(big.Int).SetUint64(parentGasTarget)
baseFeeChangeDenominator = new(big.Int).SetUint64(getBaseFeeChangeDenominator(config, parent.Number.Uint64()))
baseFeeChangeDenominator = new(big.Int).SetUint64(getBaseFeeChangeDenominator(config, parent.Number.Uint64(), time))
)
// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.GasUsed == parentGasTarget {
Expand Down Expand Up @@ -98,12 +98,12 @@ func CalcBaseFee(config *chain.Config, parent *types.Header) *big.Int {
}
}

func getBaseFeeChangeDenominator(config *chain.Config, number uint64) uint64 {
func getBaseFeeChangeDenominator(config *chain.Config, number, time uint64) uint64 {
// If we're running bor based chain post delhi hardfork, return the new value
if config.Bor != nil && config.Bor.IsDelhi(number) {
return params.BaseFeeChangeDenominatorPostDelhi
}

// Return the original once for other chains and pre-fork cases
return config.BaseFeeChangeDenominator(params.BaseFeeChangeDenominator)
return config.BaseFeeChangeDenominator(params.BaseFeeChangeDenominator, time)
}
47 changes: 46 additions & 1 deletion consensus/misc/eip1559_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func config() *chain.Config {
return config
}

func opConfig() *chain.Config {
config := copyConfig(params.TestChainConfig)
config.LondonBlock = big.NewInt(5)
config.CanyonTime = big.NewInt(10)
config.Optimism = &chain.OptimismConfig{
EIP1559Elasticity: 6,
EIP1559Denominator: 50,
EIP1559DenominatorPostCanyon: 250,
}
return config
}

// TestBlockGasLimits tests the gasLimit checks for blocks both across
// the EIP-1559 boundary and post-1559 blocks
func TestBlockGasLimits(t *testing.T) {
Expand Down Expand Up @@ -109,7 +121,40 @@ func TestCalcBaseFee(t *testing.T) {
GasUsed: test.parentGasUsed,
BaseFee: big.NewInt(test.parentBaseFee),
}
if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
if have, want := CalcBaseFee(config(), parent, 0), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
t.Errorf("test %d: have %d want %d, ", i, have, want)
}
}
}

// TestCalcBaseFeeOptimism assumes all blocks are 1559-blocks but tests the Canyon activation
func TestCalcBaseFeeOptimism(t *testing.T) {
tests := []struct {
parentBaseFee int64
parentGasLimit uint64
parentGasUsed uint64
expectedBaseFee int64
postCanyon bool
}{
{params.InitialBaseFee, 30_000_000, 5_000_000, params.InitialBaseFee, false}, // usage == target
{params.InitialBaseFee, 30_000_000, 4_000_000, 996000000, false}, // usage below target
{params.InitialBaseFee, 30_000_000, 10_000_000, 1020000000, false}, // usage above target
{params.InitialBaseFee, 30_000_000, 5_000_000, params.InitialBaseFee, true}, // usage == target
{params.InitialBaseFee, 30_000_000, 4_000_000, 999200000, true}, // usage below target
{params.InitialBaseFee, 30_000_000, 10_000_000, 1004000000, true}, // usage above target
}
for i, test := range tests {
parent := &types.Header{
Number: common.Big32,
GasLimit: test.parentGasLimit,
GasUsed: test.parentGasUsed,
BaseFee: big.NewInt(test.parentBaseFee),
Time: 6,
}
if test.postCanyon {
parent.Time = 8
}
if have, want := CalcBaseFee(opConfig(), parent, parent.Time+2), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
t.Errorf("test %d: have %d want %d, ", i, have, want)
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func MakeEmptyHeader(parent *types.Header, chainConfig *chain.Config, timestamp
parentGasLimit := parent.GasLimit
// Set baseFee and GasLimit if we are on an EIP-1559 chain
if chainConfig.IsLondon(header.Number.Uint64()) {
header.BaseFee = misc.CalcBaseFee(chainConfig, parent)
header.BaseFee = misc.CalcBaseFee(chainConfig, parent, timestamp)
if !chainConfig.IsLondon(parent.Number.Uint64()) {
parentGasLimit = parent.GasLimit * chainConfig.ElasticityMultiplier(params.ElasticityMultiplier)
}
Expand Down
2 changes: 1 addition & 1 deletion eth/gasprice/feehistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
bf.baseFee = new(big.Int)
}
if chainconfig.IsLondon(bf.blockNumber + 1) {
bf.nextBaseFee = misc.CalcBaseFee(chainconfig, bf.header)
bf.nextBaseFee = misc.CalcBaseFee(chainconfig, bf.header, bf.header.Time+1)
} else {
bf.nextBaseFee = new(big.Int)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.19

//fork with minor protobuf file changes and txpool support
replace github.com/ledgerwatch/erigon-lib v0.0.0-20230627104814-797724496a65 => github.com/testinprod-io/erigon-lib v0.0.0-20231102042317-293c0e76a349
replace github.com/ledgerwatch/erigon-lib v0.0.0-20230627104814-797724496a65 => github.com/testinprod-io/erigon-lib v0.0.0-20231102054516-b93d9d11c10f

//for local dev:
//replace github.com/ledgerwatch/erigon-lib v0.0.0-20230423044930-fc9dd74e6407 => ../erigon-lib
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/supranational/blst v0.3.10 h1:CMciDZ/h4pXDDXQASe8ZGTNKUiVNxVVA5hpci2Uuhuk=
github.com/supranational/blst v0.3.10/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/testinprod-io/erigon-lib v0.0.0-20231102042317-293c0e76a349 h1:ukPVzVJeJ/wt0mJd0HaxT/FCvIysNMPV6gwSTGXBr50=
github.com/testinprod-io/erigon-lib v0.0.0-20231102042317-293c0e76a349/go.mod h1:yq49cI1Z1S+aS0gj6ERZU03UIrseMmDRj4zWa1EA96Y=
github.com/testinprod-io/erigon-lib v0.0.0-20231102054516-b93d9d11c10f h1:6/GcZLIgvF+LsbFiDabkModKXjAZml+5nddMix5XNes=
github.com/testinprod-io/erigon-lib v0.0.0-20231102054516-b93d9d11c10f/go.mod h1:yq49cI1Z1S+aS0gj6ERZU03UIrseMmDRj4zWa1EA96Y=
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e h1:cR8/SYRgyQCt5cNCMniB/ZScMkhI9nk8U5C7SbISXjo=
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e/go.mod h1:Tu4lItkATkonrYuvtVjG0/rhy15qrNGNTjPdaphtZ/8=
github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg=
Expand Down
2 changes: 1 addition & 1 deletion turbo/stages/stageloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (h *Hook) AfterRun(tx kv.Tx, finishProgressBefore uint64) error {
}

if notifications != nil && notifications.Accumulator != nil && currentHeder != nil {
pendingBaseFee := misc.CalcBaseFee(h.chainConfig, currentHeder)
pendingBaseFee := misc.CalcBaseFee(h.chainConfig, currentHeder, 0)
if currentHeder.Number.Uint64() == 0 {
notifications.Accumulator.StartChange(0, currentHeder.Hash(), nil, false)
}
Expand Down

0 comments on commit 88a18c0

Please sign in to comment.