github.com/theQRL/go-zond@v0.2.1/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/theQRL/go-zond/common" 24 "github.com/theQRL/go-zond/core/types" 25 "github.com/theQRL/go-zond/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 ¶ms.ChainConfig{ 32 ChainID: original.ChainID, 33 } 34 } 35 36 func config() *params.ChainConfig { 37 config := copyConfig(params.TestChainConfig) 38 return config 39 } 40 41 // TestBlockGasLimits tests the gasLimit checks for blocks both across 42 // the EIP-1559 boundary and post-1559 blocks 43 //func TestBlockGasLimits(t *testing.T) { 44 // initial := new(big.Int).SetUint64(params.InitialBaseFee) 45 // 46 // for i, tc := range []struct { 47 // pGasLimit uint64 48 // pNum int64 49 // gasLimit uint64 50 // ok bool 51 // }{ 52 // {20000000, 5, 20000000, true}, 53 // {20000000, 5, 20019530, true}, // Upper limit 54 // {20000000, 5, 20019531, false}, // Upper limit +1 55 // {20000000, 5, 19980470, true}, // Lower limit 56 // {20000000, 5, 19980469, false}, // Lower limit -1 57 // {40000000, 5, 40039061, true}, // Upper limit 58 // {40000000, 5, 40039062, false}, // Upper limit +1 59 // {40000000, 5, 39960939, true}, // lower limit 60 // {40000000, 5, 39960938, false}, // Lower limit -1 61 // } { 62 // parent := &types.Header{ 63 // GasUsed: tc.pGasLimit / 2, 64 // GasLimit: tc.pGasLimit, 65 // BaseFee: initial, 66 // Number: big.NewInt(tc.pNum), 67 // } 68 // header := &types.Header{ 69 // GasUsed: tc.gasLimit / 2, 70 // GasLimit: tc.gasLimit, 71 // BaseFee: initial, 72 // Number: big.NewInt(tc.pNum + 1), 73 // } 74 // err := VerifyEIP1559Header(config(), parent, header) 75 // if tc.ok && err != nil { 76 // t.Errorf("test %d: Expected valid header: %s", i, err) 77 // } 78 // if !tc.ok && err == nil { 79 // t.Errorf("test %d: Expected invalid header", i) 80 // } 81 // } 82 //} 83 84 func TestBlockGasLimits(t *testing.T) { 85 initial := new(big.Int).SetUint64(params.InitialBaseFee) 86 87 for i, tc := range []struct { 88 pGasLimit uint64 89 pNum int64 90 gasLimit uint64 91 ok bool 92 }{ 93 {20000000, 5, 20000000, true}, // within min and max gas limit 94 {20000000, 5, 5000, true}, // within min and max gas limit 95 {20000000, 5, 4999, false}, // within min and max gas limit 96 {20000000, 5, 20019530, false}, // Upper limit 97 {20000000, 5, 20019531, false}, // Beyond max gas limit 98 {20000000, 5, 19980470, true}, // within min and max gas limit 99 {20000000, 5, 19980469, true}, // within min and max gas limit 100 {40000000, 5, 40039061, false}, // Beyond max gas limit 101 {40000000, 5, 40039062, false}, // Beyond max gas limit 102 {40000000, 5, 39960939, false}, // Beyond max gas limit 103 {40000000, 5, 39960938, false}, // Beyond max gas limit 104 } { 105 parent := &types.Header{ 106 GasUsed: tc.pGasLimit / 2, 107 GasLimit: tc.pGasLimit, 108 BaseFee: initial, 109 Number: big.NewInt(tc.pNum), 110 } 111 header := &types.Header{ 112 GasUsed: tc.gasLimit / 2, 113 GasLimit: tc.gasLimit, 114 BaseFee: initial, 115 Number: big.NewInt(tc.pNum + 1), 116 } 117 err := VerifyEIP1559Header(config(), parent, header) 118 if tc.ok && err != nil { 119 t.Errorf("test %d: Expected valid header: %s", i, err) 120 } 121 if !tc.ok && err == nil { 122 t.Errorf("test %d: Expected invalid header", i) 123 } 124 } 125 } 126 127 // TestCalcBaseFee assumes all blocks are 1559-blocks 128 func TestCalcBaseFee(t *testing.T) { 129 tests := []struct { 130 parentBaseFee int64 131 parentGasLimit uint64 132 parentGasUsed uint64 133 expectedBaseFee int64 134 }{ 135 {params.InitialBaseFee, 20000000, 10000000, params.InitialBaseFee}, // usage == target 136 {params.InitialBaseFee, 20000000, 9000000, 987500000}, // usage below target 137 {params.InitialBaseFee, 20000000, 11000000, 1012500000}, // usage above target 138 } 139 for i, test := range tests { 140 parent := &types.Header{ 141 Number: common.Big32, 142 GasLimit: test.parentGasLimit, 143 GasUsed: test.parentGasUsed, 144 BaseFee: big.NewInt(test.parentBaseFee), 145 } 146 if have, want := CalcBaseFee(config(), parent), big.NewInt(test.expectedBaseFee); have.Cmp(want) != 0 { 147 t.Errorf("test %d: have %d want %d, ", i, have, want) 148 } 149 } 150 }