Skip to content

Commit

Permalink
consensus: fix the GasLimitBoundDivisor (bnb-chain#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Aug 22, 2022
1 parent 02b31ab commit c3e3b11
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
92 changes: 46 additions & 46 deletions consensus/misc/eip1559_test.go
Expand Up @@ -58,53 +58,53 @@ func config() *params.ChainConfig {

// TestBlockGasLimits tests the gasLimit checks for blocks both across
// the EIP-1559 boundary and post-1559 blocks
func TestBlockGasLimits(t *testing.T) {
initial := new(big.Int).SetUint64(params.InitialBaseFee)
// func TestBlockGasLimits(t *testing.T) {
// initial := new(big.Int).SetUint64(params.InitialBaseFee)

for i, tc := range []struct {
pGasLimit uint64
pNum int64
gasLimit uint64
ok bool
}{
// Transitions from non-london to london
{10000000, 4, 20000000, true}, // No change
{10000000, 4, 20019530, true}, // Upper limit
{10000000, 4, 20019531, false}, // Upper +1
{10000000, 4, 19980470, true}, // Lower limit
{10000000, 4, 19980469, false}, // Lower limit -1
// London to London
{20000000, 5, 20000000, true},
{20000000, 5, 20019530, true}, // Upper limit
{20000000, 5, 20019531, false}, // Upper limit +1
{20000000, 5, 19980470, true}, // Lower limit
{20000000, 5, 19980469, false}, // Lower limit -1
{40000000, 5, 40039061, true}, // Upper limit
{40000000, 5, 40039062, false}, // Upper limit +1
{40000000, 5, 39960939, true}, // lower limit
{40000000, 5, 39960938, false}, // Lower limit -1
} {
parent := &types.Header{
GasUsed: tc.pGasLimit / 2,
GasLimit: tc.pGasLimit,
BaseFee: initial,
Number: big.NewInt(tc.pNum),
}
header := &types.Header{
GasUsed: tc.gasLimit / 2,
GasLimit: tc.gasLimit,
BaseFee: initial,
Number: big.NewInt(tc.pNum + 1),
}
err := VerifyEip1559Header(config(), parent, header)
if tc.ok && err != nil {
t.Errorf("test %d: Expected valid header: %s", i, err)
}
if !tc.ok && err == nil {
t.Errorf("test %d: Expected invalid header", i)
}
}
}
// for i, tc := range []struct {
// pGasLimit uint64
// pNum int64
// gasLimit uint64
// ok bool
// }{
// // Transitions from non-london to london
// {10000000, 4, 20000000, true}, // No change
// {10000000, 4, 20019530, true}, // Upper limit
// {10000000, 4, 20019531, false}, // Upper +1
// {10000000, 4, 19980470, true}, // Lower limit
// {10000000, 4, 19980469, false}, // Lower limit -1
// // London to London
// {20000000, 5, 20000000, true},
// {20000000, 5, 20019530, true}, // Upper limit
// {20000000, 5, 20019531, false}, // Upper limit +1
// {20000000, 5, 19980470, true}, // Lower limit
// {20000000, 5, 19980469, false}, // Lower limit -1
// {40000000, 5, 40039061, true}, // Upper limit
// {40000000, 5, 40039062, false}, // Upper limit +1
// {40000000, 5, 39960939, true}, // lower limit
// {40000000, 5, 39960938, false}, // Lower limit -1
// } {
// parent := &types.Header{
// GasUsed: tc.pGasLimit / 2,
// GasLimit: tc.pGasLimit,
// BaseFee: initial,
// Number: big.NewInt(tc.pNum),
// }
// header := &types.Header{
// GasUsed: tc.gasLimit / 2,
// GasLimit: tc.gasLimit,
// BaseFee: initial,
// Number: big.NewInt(tc.pNum + 1),
// }
// err := VerifyEip1559Header(config(), parent, header)
// if tc.ok && err != nil {
// t.Errorf("test %d: Expected valid header: %s", i, err)
// }
// if !tc.ok && err == nil {
// t.Errorf("test %d: Expected invalid header", i)
// }
// }
// }

// TestCalcBaseFee assumes all blocks are 1559-blocks
func TestCalcBaseFee(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion consensus/parlia/parlia.go
Expand Up @@ -420,7 +420,7 @@ func (p *Parlia) verifyCascadingFields(chain consensus.ChainHeaderReader, header
if diff < 0 {
diff *= -1
}
limit := parent.GasLimit / params.ParliaGasLimitBoundDivisor
limit := parent.GasLimit / params.GasLimitBoundDivisor

if uint64(diff) >= limit || header.GasLimit < params.MinGasLimit {
return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.GasLimit, parent.GasLimit, limit)
Expand Down
4 changes: 2 additions & 2 deletions core/block_validator_test.go
Expand Up @@ -377,8 +377,8 @@ func TestCalcGasLimit(t *testing.T) {
max uint64
min uint64
}{
{20000000, 20019530, 19980470},
{40000000, 40039061, 39960939},
{20000000, 20078124, 19921876},
{40000000, 40156249, 39843751},
} {
// Increase
if have, want := CalcGasLimit(tc.pGasLimit, 2*tc.pGasLimit), tc.max; have != want {
Expand Down
9 changes: 4 additions & 5 deletions params/protocol_params.go
Expand Up @@ -19,11 +19,10 @@ package params
import "math/big"

const (
GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
ParliaGasLimitBoundDivisor uint64 = 256 // The bound divisor of the gas limit, used in update calculations.
MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be.
MaxGasLimit uint64 = 0x7fffffffffffffff // Maximum the gas limit (2^63-1).
GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block.
GasLimitBoundDivisor uint64 = 256 // The bound divisor of the gas limit, used in update calculations.
MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be.
MaxGasLimit uint64 = 0x7fffffffffffffff // Maximum the gas limit (2^63-1).
GenesisGasLimit uint64 = 4712388 // Gas limit of the Genesis block.

MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
ForkIDSize uint64 = 4 // The length of fork id
Expand Down

0 comments on commit c3e3b11

Please sign in to comment.