github.com/dominant-strategies/go-quai@v0.28.2/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/dominant-strategies/go-quai/common" 26 "github.com/dominant-strategies/go-quai/consensus/progpow" 27 "github.com/dominant-strategies/go-quai/core" 28 "github.com/dominant-strategies/go-quai/core/rawdb" 29 "github.com/dominant-strategies/go-quai/core/types" 30 "github.com/dominant-strategies/go-quai/core/vm" 31 "github.com/dominant-strategies/go-quai/crypto" 32 "github.com/dominant-strategies/go-quai/params" 33 "github.com/dominant-strategies/go-quai/rpc" 34 ) 35 36 const testHead = 32 37 38 type testBackend struct { 39 chain *core.BlockChain 40 pending bool // pending block available 41 } 42 43 func (b *testBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 44 if number > testHead { 45 return nil, nil 46 } 47 if number == rpc.LatestBlockNumber { 48 number = testHead 49 } 50 if number == rpc.PendingBlockNumber { 51 if b.pending { 52 number = testHead + 1 53 } else { 54 return nil, nil 55 } 56 } 57 return b.chain.GetHeaderByNumber(uint64(number)), nil 58 } 59 60 func (b *testBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 61 if number > testHead { 62 return nil, nil 63 } 64 if number == rpc.LatestBlockNumber { 65 number = testHead 66 } 67 if number == rpc.PendingBlockNumber { 68 if b.pending { 69 number = testHead + 1 70 } else { 71 return nil, nil 72 } 73 } 74 return b.chain.GetBlockByNumber(uint64(number)), nil 75 } 76 77 func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { 78 return b.chain.GetReceiptsByHash(hash), nil 79 } 80 81 func (b *testBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { 82 if b.pending { 83 block := b.chain.GetBlockByNumber(testHead + 1) 84 return block, b.chain.GetReceiptsByHash(block.Hash()) 85 } 86 return nil, nil 87 } 88 89 func (b *testBackend) ChainConfig() *params.ChainConfig { 90 return b.chain.Config() 91 } 92 93 func newTestBackend(t *testing.T, pending bool) *testBackend { 94 var ( 95 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 96 addr = crypto.PubkeyToAddress(key.PublicKey) 97 gspec = &core.Genesis{ 98 Config: params.TestChainConfig, 99 Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}}, 100 } 101 signer = types.LatestSigner(gspec.Config) 102 ) 103 104 signer = types.LatestSigner(gspec.Config) 105 106 engine := progpow.NewFaker() 107 db := rawdb.NewMemoryDatabase() 108 genesis, _ := gspec.Commit(db) 109 110 // Generate testing blocks 111 blocks, _ := core.GenerateChain(gspec.Config, genesis, engine, db, testHead+1, func(i int, b *core.BlockGen) { 112 b.SetCoinbase(common.Address{1}) 113 114 var tx *types.Transaction 115 116 txdata := &types.DynamicFeeTx{ 117 ChainID: gspec.Config.ChainID, 118 Nonce: b.TxNonce(addr), 119 To: &common.Address{}, 120 Gas: 30000, 121 GasFeeCap: big.NewInt(100 * params.GWei), 122 GasTipCap: big.NewInt(int64(i+1) * params.GWei), 123 Data: []byte{}, 124 } 125 tx = types.NewTx(txdata) 126 tx, err := types.SignTx(tx, signer, key) 127 if err != nil { 128 t.Fatalf("failed to create tx: %v", err) 129 } 130 b.AddTx(tx) 131 }) 132 // Construct testing chain 133 diskdb := rawdb.NewMemoryDatabase() 134 gspec.Commit(diskdb) 135 chain, err := core.NewBlockChain(diskdb, nil, gspec.Config, engine, vm.Config{}, nil, nil) 136 if err != nil { 137 t.Fatalf("Failed to create local chain, %v", err) 138 } 139 chain.InsertChain(blocks) 140 return &testBackend{chain: chain, pending: pending} 141 } 142 143 func (b *testBackend) CurrentHeader() *types.Header { 144 return b.chain.CurrentHeader() 145 } 146 147 func (b *testBackend) GetBlockByNumber(number uint64) *types.Block { 148 return b.chain.GetBlockByNumber(number) 149 } 150 151 func TestSuggestTipCap(t *testing.T) { 152 config := Config{ 153 Blocks: 3, 154 Percentile: 60, 155 Default: big.NewInt(params.GWei), 156 } 157 var cases = []struct { 158 fork *big.Int 159 expect *big.Int // Expected gasprice suggestion 160 }{ 161 {nil, big.NewInt(params.GWei * int64(30))}, 162 {big.NewInt(0), big.NewInt(params.GWei * int64(30))}, // Fork point in genesis 163 {big.NewInt(1), big.NewInt(params.GWei * int64(30))}, // Fork point in first block 164 {big.NewInt(32), big.NewInt(params.GWei * int64(30))}, // Fork point in last block 165 {big.NewInt(33), big.NewInt(params.GWei * int64(30))}, // Fork point in the future 166 } 167 for _, c := range cases { 168 backend := newTestBackend(t, c.fork, false) 169 oracle := NewOracle(backend, config) 170 171 // The gas price sampled is: 32G, 31G, 30G, 29G, 28G, 27G 172 got, err := oracle.SuggestTipCap(context.Background()) 173 if err != nil { 174 t.Fatalf("Failed to retrieve recommended gas price: %v", err) 175 } 176 if got.Cmp(c.expect) != 0 { 177 t.Fatalf("Gas price mismatch, want %d, got %d", c.expect, got) 178 } 179 } 180 }