github.com/dim4egster/coreth@v0.10.2/eth/gasprice/feehistory_test.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2021 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package gasprice 28 29 import ( 30 "context" 31 "errors" 32 "math/big" 33 "testing" 34 35 "github.com/dim4egster/coreth/core" 36 "github.com/dim4egster/coreth/core/types" 37 38 "github.com/dim4egster/coreth/params" 39 "github.com/dim4egster/coreth/rpc" 40 "github.com/ethereum/go-ethereum/common" 41 ) 42 43 func TestFeeHistory(t *testing.T) { 44 var cases = []struct { 45 pending bool 46 maxCallBlock int 47 maxBlock int 48 count int 49 last rpc.BlockNumber 50 percent []float64 51 expFirst uint64 52 expCount int 53 expErr error 54 }{ 55 // Standard go-ethereum tests 56 {false, 0, 1000, 10, 30, nil, 21, 10, nil}, 57 {false, 0, 1000, 10, 30, []float64{0, 10}, 21, 10, nil}, 58 {false, 0, 1000, 10, 30, []float64{20, 10}, 0, 0, errInvalidPercentile}, 59 {false, 0, 1000, 1000000000, 30, nil, 0, 31, nil}, 60 {false, 0, 1000, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil}, 61 {false, 0, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead}, 62 {true, 0, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead}, 63 {false, 0, 2, 100, rpc.LatestBlockNumber, []float64{0, 10}, 31, 2, nil}, 64 {false, 0, 2, 100, 32, []float64{0, 10}, 31, 2, nil}, 65 {false, 0, 1000, 1, rpc.PendingBlockNumber, nil, 0, 0, nil}, 66 {false, 0, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil}, 67 {true, 0, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil}, 68 {true, 0, 1000, 2, rpc.PendingBlockNumber, []float64{0, 10}, 32, 1, nil}, 69 70 // Modified tests 71 {false, 0, 2, 100, rpc.LatestBlockNumber, nil, 31, 2, nil}, // apply block lookback limits even if only headers required 72 {false, 0, 10, 10, 30, nil, 23, 8, nil}, // limit lookback based on maxHistory from latest block 73 {false, 0, 33, 1000000000, 10, nil, 0, 11, nil}, // handle truncation edge case 74 {false, 0, 2, 10, 20, nil, 0, 0, errBeyondHistoricalLimit}, // query behind historical limit 75 {false, 10, 30, 100, rpc.LatestBlockNumber, nil, 23, 10, nil}, // ensure [MaxCallBlockHistory] is honored 76 } 77 for i, c := range cases { 78 config := Config{ 79 MaxCallBlockHistory: c.maxCallBlock, 80 MaxBlockHistory: c.maxBlock, 81 } 82 tip := big.NewInt(1 * params.GWei) 83 backend := newTestBackendFakerEngine(t, params.TestChainConfig, 32, common.Big0, func(i int, b *core.BlockGen) { 84 signer := types.LatestSigner(params.TestChainConfig) 85 86 b.SetCoinbase(common.Address{1}) 87 88 baseFee := b.BaseFee() 89 feeCap := new(big.Int).Add(baseFee, tip) 90 91 var tx *types.Transaction 92 txdata := &types.DynamicFeeTx{ 93 ChainID: params.TestChainConfig.ChainID, 94 Nonce: b.TxNonce(addr), 95 To: &common.Address{}, 96 Gas: params.TxGas, 97 GasFeeCap: feeCap, 98 GasTipCap: tip, 99 Data: []byte{}, 100 } 101 tx = types.NewTx(txdata) 102 tx, err := types.SignTx(tx, signer, key) 103 if err != nil { 104 t.Fatalf("failed to create tx: %v", err) 105 } 106 b.AddTx(tx) 107 }) 108 oracle := NewOracle(backend, config) 109 110 first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent) 111 112 expReward := c.expCount 113 if len(c.percent) == 0 { 114 expReward = 0 115 } 116 expBaseFee := c.expCount 117 118 if first.Uint64() != c.expFirst { 119 t.Fatalf("Test case %d: first block mismatch, want %d, got %d", i, c.expFirst, first) 120 } 121 if len(reward) != expReward { 122 t.Fatalf("Test case %d: reward array length mismatch, want %d, got %d", i, expReward, len(reward)) 123 } 124 if len(baseFee) != expBaseFee { 125 t.Fatalf("Test case %d: baseFee array length mismatch, want %d, got %d", i, expBaseFee, len(baseFee)) 126 } 127 if len(ratio) != c.expCount { 128 t.Fatalf("Test case %d: gasUsedRatio array length mismatch, want %d, got %d", i, c.expCount, len(ratio)) 129 } 130 if err != c.expErr && !errors.Is(err, c.expErr) { 131 t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err) 132 } 133 } 134 }