github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/eth/gasprice/gasprice_test.go (about) 1 // Copyright 2020 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 gasprice 18 19 import ( 20 "context" 21 "math" 22 "math/big" 23 "testing" 24 25 "github.com/ethw3/go-ethereuma/common" 26 "github.com/ethw3/go-ethereuma/consensus/ethash" 27 "github.com/ethw3/go-ethereuma/core" 28 "github.com/ethw3/go-ethereuma/core/rawdb" 29 "github.com/ethw3/go-ethereuma/core/types" 30 "github.com/ethw3/go-ethereuma/core/vm" 31 "github.com/ethw3/go-ethereuma/crypto" 32 "github.com/ethw3/go-ethereuma/event" 33 "github.com/ethw3/go-ethereuma/params" 34 "github.com/ethw3/go-ethereuma/rpc" 35 ) 36 37 const testHead = 32 38 39 type testBackend struct { 40 chain *core.BlockChain 41 pending bool // pending block available 42 } 43 44 func (b *testBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 45 if number > testHead { 46 return nil, nil 47 } 48 if number == rpc.EarliestBlockNumber { 49 number = 0 50 } 51 if number == rpc.FinalizedBlockNumber { 52 return b.chain.CurrentFinalizedBlock().Header(), nil 53 } 54 if number == rpc.SafeBlockNumber { 55 return b.chain.CurrentSafeBlock().Header(), nil 56 } 57 if number == rpc.LatestBlockNumber { 58 number = testHead 59 } 60 if number == rpc.PendingBlockNumber { 61 if b.pending { 62 number = testHead + 1 63 } else { 64 return nil, nil 65 } 66 } 67 return b.chain.GetHeaderByNumber(uint64(number)), nil 68 } 69 70 func (b *testBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 71 if number > testHead { 72 return nil, nil 73 } 74 if number == rpc.EarliestBlockNumber { 75 number = 0 76 } 77 if number == rpc.FinalizedBlockNumber { 78 return b.chain.CurrentFinalizedBlock(), nil 79 } 80 if number == rpc.SafeBlockNumber { 81 return b.chain.CurrentSafeBlock(), nil 82 } 83 if number == rpc.LatestBlockNumber { 84 number = testHead 85 } 86 if number == rpc.PendingBlockNumber { 87 if b.pending { 88 number = testHead + 1 89 } else { 90 return nil, nil 91 } 92 } 93 return b.chain.GetBlockByNumber(uint64(number)), nil 94 } 95 96 func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 97 return b.chain.GetReceiptsByHash(hash), nil 98 } 99 100 func (b *testBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 101 if b.pending { 102 block := b.chain.GetBlockByNumber(testHead + 1) 103 return block, b.chain.GetReceiptsByHash(block.Hash()) 104 } 105 return nil, nil 106 } 107 108 func (b *testBackend) ChainConfig() *params.ChainConfig { 109 return b.chain.Config() 110 } 111 112 func (b *testBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { 113 return nil 114 } 115 116 func newTestBackend(t *testing.T, londonBlock *big.Int, pending bool) *testBackend { 117 var ( 118 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 119 addr = crypto.PubkeyToAddress(key.PublicKey) 120 config = *params.TestChainConfig // needs copy because it is modified below 121 gspec = &core.Genesis{ 122 Config: &config, 123 Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}}, 124 } 125 signer = types.LatestSigner(gspec.Config) 126 ) 127 config.LondonBlock = londonBlock 128 config.ArrowGlacierBlock = londonBlock 129 config.GrayGlacierBlock = londonBlock 130 config.TerminalTotalDifficulty = common.Big0 131 engine := ethash.NewFaker() 132 db := rawdb.NewMemoryDatabase() 133 genesis := gspec.MustCommit(db) 134 135 // Generate testing blocks 136 blocks, _ := core.GenerateChain(gspec.Config, genesis, engine, db, testHead+1, func(i int, b *core.BlockGen) { 137 b.SetCoinbase(common.Address{1}) 138 139 var txdata types.TxData 140 if londonBlock != nil && b.Number().Cmp(londonBlock) >= 0 { 141 txdata = &types.DynamicFeeTx{ 142 ChainID: gspec.Config.ChainID, 143 Nonce: b.TxNonce(addr), 144 To: &common.Address{}, 145 Gas: 30000, 146 GasFeeCap: big.NewInt(100 * params.GWei), 147 GasTipCap: big.NewInt(int64(i+1) * params.GWei), 148 Data: []byte{}, 149 } 150 } else { 151 txdata = &types.LegacyTx{ 152 Nonce: b.TxNonce(addr), 153 To: &common.Address{}, 154 Gas: 21000, 155 GasPrice: big.NewInt(int64(i+1) * params.GWei), 156 Value: big.NewInt(100), 157 Data: []byte{}, 158 } 159 } 160 b.AddTx(types.MustSignNewTx(key, signer, txdata)) 161 }) 162 // Construct testing chain 163 diskdb := rawdb.NewMemoryDatabase() 164 gspec.MustCommit(diskdb) 165 chain, err := core.NewBlockChain(diskdb, &core.CacheConfig{TrieCleanNoPrefetch: true}, gspec.Config, engine, vm.Config{}, nil, nil) 166 if err != nil { 167 t.Fatalf("Failed to create local chain, %v", err) 168 } 169 chain.InsertChain(blocks) 170 chain.SetFinalized(chain.GetBlockByNumber(25)) 171 chain.SetSafe(chain.GetBlockByNumber(25)) 172 return &testBackend{chain: chain, pending: pending} 173 } 174 175 func (b *testBackend) CurrentHeader() *types.Header { 176 return b.chain.CurrentHeader() 177 } 178 179 func (b *testBackend) GetBlockByNumber(number uint64) *types.Block { 180 return b.chain.GetBlockByNumber(number) 181 } 182 183 func TestSuggestTipCap(t *testing.T) { 184 config := Config{ 185 Blocks: 3, 186 Percentile: 60, 187 Default: big.NewInt(params.GWei), 188 } 189 var cases = []struct { 190 fork *big.Int // London fork number 191 expect *big.Int // Expected gasprice suggestion 192 }{ 193 {nil, big.NewInt(params.GWei * int64(30))}, 194 {big.NewInt(0), big.NewInt(params.GWei * int64(30))}, // Fork point in genesis 195 {big.NewInt(1), big.NewInt(params.GWei * int64(30))}, // Fork point in first block 196 {big.NewInt(32), big.NewInt(params.GWei * int64(30))}, // Fork point in last block 197 {big.NewInt(33), big.NewInt(params.GWei * int64(30))}, // Fork point in the future 198 } 199 for _, c := range cases { 200 backend := newTestBackend(t, c.fork, false) 201 oracle := NewOracle(backend, config) 202 203 // The gas price sampled is: 32G, 31G, 30G, 29G, 28G, 27G 204 got, err := oracle.SuggestTipCap(context.Background()) 205 if err != nil { 206 t.Fatalf("Failed to retrieve recommended gas price: %v", err) 207 } 208 if got.Cmp(c.expect) != 0 { 209 t.Fatalf("Gas price mismatch, want %d, got %d", c.expect, got) 210 } 211 } 212 }