github.com/ethereum/go-ethereum@v1.14.3/consensus/misc/eip1559/eip1559_test.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eip1559
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/params"
    26  )
    27  
    28  // copyConfig does a _shallow_ copy of a given config. Safe to set new values, but
    29  // do not use e.g. SetInt() on the numbers. For testing only
    30  func copyConfig(original *params.ChainConfig) *params.ChainConfig {
    31  	return &params.ChainConfig{
    32  		ChainID:                 original.ChainID,
    33  		HomesteadBlock:          original.HomesteadBlock,
    34  		DAOForkBlock:            original.DAOForkBlock,
    35  		DAOForkSupport:          original.DAOForkSupport,
    36  		EIP150Block:             original.EIP150Block,
    37  		EIP155Block:             original.EIP155Block,
    38  		EIP158Block:             original.EIP158Block,
    39  		ByzantiumBlock:          original.ByzantiumBlock,
    40  		ConstantinopleBlock:     original.ConstantinopleBlock,
    41  		PetersburgBlock:         original.PetersburgBlock,
    42  		IstanbulBlock:           original.IstanbulBlock,
    43  		MuirGlacierBlock:        original.MuirGlacierBlock,
    44  		BerlinBlock:             original.BerlinBlock,
    45  		LondonBlock:             original.LondonBlock,
    46  		TerminalTotalDifficulty: original.TerminalTotalDifficulty,
    47  		Ethash:                  original.Ethash,
    48  		Clique:                  original.Clique,
    49  	}
    50  }
    51  
    52  func config() *params.ChainConfig {
    53  	config := copyConfig(params.TestChainConfig)
    54  	config.LondonBlock = big.NewInt(5)
    55  	return config
    56  }
    57  
    58  // TestBlockGasLimits tests the gasLimit checks for blocks both across
    59  // the EIP-1559 boundary and post-1559 blocks
    60  func TestBlockGasLimits(t *testing.T) {
    61  	initial := new(big.Int).SetUint64(params.InitialBaseFee)
    62  
    63  	for i, tc := range []struct {
    64  		pGasLimit uint64
    65  		pNum      int64
    66  		gasLimit  uint64
    67  		ok        bool
    68  	}{
    69  		// Transitions from non-london to london
    70  		{10000000, 4, 20000000, true},  // No change
    71  		{10000000, 4, 20019530, true},  // Upper limit
    72  		{10000000, 4, 20019531, false}, // Upper +1
    73  		{10000000, 4, 19980470, true},  // Lower limit
    74  		{10000000, 4, 19980469, false}, // Lower limit -1
    75  		// London to London
    76  		{20000000, 5, 20000000, true},
    77  		{20000000, 5, 20019530, true},  // Upper limit
    78  		{20000000, 5, 20019531, false}, // Upper limit +1
    79  		{20000000, 5, 19980470, true},  // Lower limit
    80  		{20000000, 5, 19980469, false}, // Lower limit -1
    81  		{40000000, 5, 40039061, true},  // Upper limit
    82  		{40000000, 5, 40039062, false}, // Upper limit +1
    83  		{40000000, 5, 39960939, true},  // lower limit
    84  		{40000000, 5, 39960938, false}, // Lower limit -1
    85  	} {
    86  		parent := &types.Header{
    87  			GasUsed:  tc.pGasLimit / 2,
    88  			GasLimit: tc.pGasLimit,
    89  			BaseFee:  initial,
    90  			Number:   big.NewInt(tc.pNum),
    91  		}
    92  		header := &types.Header{
    93  			GasUsed:  tc.gasLimit / 2,
    94  			GasLimit: tc.gasLimit,
    95  			BaseFee:  initial,
    96  			Number:   big.NewInt(tc.pNum + 1),
    97  		}
    98  		err := VerifyEIP1559Header(config(), parent, header)
    99  		if tc.ok && err != nil {
   100  			t.Errorf("test %d: Expected valid header: %s", i, err)
   101  		}
   102  		if !tc.ok && err == nil {
   103  			t.Errorf("test %d: Expected invalid header", i)
   104  		}
   105  	}
   106  }
   107  
   108  // TestCalcBaseFee assumes all blocks are 1559-blocks
   109  func TestCalcBaseFee(t *testing.T) {
   110  	tests := []struct {
   111  		parentBaseFee   int64
   112  		parentGasLimit  uint64
   113  		parentGasUsed   uint64
   114  		expectedBaseFee int64
   115  	}{
   116  		{params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target
   117  		{params.InitialBaseFee, 20000000, 9000000, 987500000},              // usage below target
   118  		{params.InitialBaseFee, 20000000, 11000000, 1012500000},            // usage above target
   119  	}
   120  	for i, test := range tests {
   121  		parent := &types.Header{
   122  			Number:   common.Big32,
   123  			GasLimit: test.parentGasLimit,
   124  			GasUsed:  test.parentGasUsed,
   125  			BaseFee:  big.NewInt(test.parentBaseFee),
   126  		}
   127  		if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
   128  			t.Errorf("test %d: have %d  want %d, ", i, have, want)
   129  		}
   130  	}
   131  }