github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/consensus/misc/eip1559_test.go (about)

     1  // Copyright 2021 The adkgo Authors
     2  // This file is part of the adkgo library (adapted for adkgo from go--ethereum v1.10.8).
     3  //
     4  // the adkgo 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 adkgo 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 adkgo library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package misc
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/aidoskuneen/adk-node/common"
    24  	"github.com/aidoskuneen/adk-node/core/types"
    25  	"github.com/aidoskuneen/adk-node/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  		EIP150Hash:          original.EIP150Hash,
    38  		EIP155Block:         original.EIP155Block,
    39  		EIP158Block:         original.EIP158Block,
    40  		ByzantiumBlock:      original.ByzantiumBlock,
    41  		ConstantinopleBlock: original.ConstantinopleBlock,
    42  		PetersburgBlock:     original.PetersburgBlock,
    43  		IstanbulBlock:       original.IstanbulBlock,
    44  		MuirGlacierBlock:    original.MuirGlacierBlock,
    45  		BerlinBlock:         original.BerlinBlock,
    46  		LondonBlock:         original.LondonBlock,
    47  		CatalystBlock:       original.CatalystBlock,
    48  		Ethash:              original.Ethash,
    49  		Clique:              original.Clique,
    50  	}
    51  }
    52  
    53  func config() *params.ChainConfig {
    54  	config := copyConfig(params.TestChainConfig)
    55  	config.LondonBlock = big.NewInt(5)
    56  	return config
    57  }
    58  
    59  // TestBlockGasLimits tests the gasLimit checks for blocks both across
    60  // the EIP-1559 boundary and post-1559 blocks
    61  func TestBlockGasLimits(t *testing.T) {
    62  	initial := new(big.Int).SetUint64(params.InitialBaseFee)
    63  
    64  	for i, tc := range []struct {
    65  		pGasLimit uint64
    66  		pNum      int64
    67  		gasLimit  uint64
    68  		ok        bool
    69  	}{
    70  		// Transitions from non-london to london
    71  		{10000000, 4, 20000000, true},  // No change
    72  		{10000000, 4, 20019530, true},  // Upper limit
    73  		{10000000, 4, 20019531, false}, // Upper +1
    74  		{10000000, 4, 19980470, true},  // Lower limit
    75  		{10000000, 4, 19980469, false}, // Lower limit -1
    76  		// London to London
    77  		{20000000, 5, 20000000, true},
    78  		{20000000, 5, 20019530, true},  // Upper limit
    79  		{20000000, 5, 20019531, false}, // Upper limit +1
    80  		{20000000, 5, 19980470, true},  // Lower limit
    81  		{20000000, 5, 19980469, false}, // Lower limit -1
    82  		{40000000, 5, 40039061, true},  // Upper limit
    83  		{40000000, 5, 40039062, false}, // Upper limit +1
    84  		{40000000, 5, 39960939, true},  // lower limit
    85  		{40000000, 5, 39960938, false}, // Lower limit -1
    86  	} {
    87  		parent := &types.Header{
    88  			GasUsed:  tc.pGasLimit / 2,
    89  			GasLimit: tc.pGasLimit,
    90  			BaseFee:  initial,
    91  			Number:   big.NewInt(tc.pNum),
    92  		}
    93  		header := &types.Header{
    94  			GasUsed:  tc.gasLimit / 2,
    95  			GasLimit: tc.gasLimit,
    96  			BaseFee:  initial,
    97  			Number:   big.NewInt(tc.pNum + 1),
    98  		}
    99  		err := VerifyEip1559Header(config(), parent, header)
   100  		if tc.ok && err != nil {
   101  			t.Errorf("test %d: Expected valid header: %s", i, err)
   102  		}
   103  		if !tc.ok && err == nil {
   104  			t.Errorf("test %d: Expected invalid header", i)
   105  		}
   106  	}
   107  }
   108  
   109  // TestCalcBaseFee assumes all blocks are 1559-blocks
   110  func TestCalcBaseFee(t *testing.T) {
   111  	tests := []struct {
   112  		parentBaseFee   int64
   113  		parentGasLimit  uint64
   114  		parentGasUsed   uint64
   115  		expectedBaseFee int64
   116  	}{
   117  		{params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target
   118  		{params.InitialBaseFee, 20000000, 9000000, 987500000},              // usage below target
   119  		{params.InitialBaseFee, 20000000, 11000000, 1012500000},            // usage above target
   120  	}
   121  	for i, test := range tests {
   122  		parent := &types.Header{
   123  			Number:   common.Big32,
   124  			GasLimit: test.parentGasLimit,
   125  			GasUsed:  test.parentGasUsed,
   126  			BaseFee:  big.NewInt(test.parentBaseFee),
   127  		}
   128  		if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 {
   129  			t.Errorf("test %d: have %d  want %d, ", i, have, want)
   130  		}
   131  	}
   132  }