github.com/phillinzzz/newBsc@v1.1.6/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/phillinzzz/newBsc/common" 26 "github.com/phillinzzz/newBsc/consensus/ethash" 27 "github.com/phillinzzz/newBsc/core" 28 "github.com/phillinzzz/newBsc/core/rawdb" 29 "github.com/phillinzzz/newBsc/core/types" 30 "github.com/phillinzzz/newBsc/core/vm" 31 "github.com/phillinzzz/newBsc/crypto" 32 "github.com/phillinzzz/newBsc/params" 33 "github.com/phillinzzz/newBsc/rpc" 34 ) 35 36 type testBackend struct { 37 chain *core.BlockChain 38 } 39 40 func (b *testBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { 41 if number == rpc.LatestBlockNumber { 42 return b.chain.CurrentBlock().Header(), nil 43 } 44 return b.chain.GetHeaderByNumber(uint64(number)), nil 45 } 46 47 func (b *testBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { 48 if number == rpc.LatestBlockNumber { 49 return b.chain.CurrentBlock(), nil 50 } 51 return b.chain.GetBlockByNumber(uint64(number)), nil 52 } 53 54 func (b *testBackend) ChainConfig() *params.ChainConfig { 55 return b.chain.Config() 56 } 57 58 func newTestBackend(t *testing.T) *testBackend { 59 var ( 60 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 61 addr = crypto.PubkeyToAddress(key.PublicKey) 62 gspec = &core.Genesis{ 63 Config: params.TestChainConfig, 64 Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(math.MaxInt64)}}, 65 } 66 signer = types.LatestSigner(gspec.Config) 67 ) 68 engine := ethash.NewFaker() 69 db := rawdb.NewMemoryDatabase() 70 genesis, _ := gspec.Commit(db) 71 72 // Generate testing blocks 73 blocks, _ := core.GenerateChain(params.TestChainConfig, genesis, engine, db, 32, func(i int, b *core.BlockGen) { 74 b.SetCoinbase(common.Address{1}) 75 tx, err := types.SignTx(types.NewTransaction(b.TxNonce(addr), common.HexToAddress("deadbeef"), big.NewInt(100), 21000, big.NewInt(int64(i+1)*params.GWei), nil), signer, key) 76 if err != nil { 77 t.Fatalf("failed to create tx: %v", err) 78 } 79 b.AddTx(tx) 80 }) 81 // Construct testing chain 82 diskdb := rawdb.NewMemoryDatabase() 83 gspec.Commit(diskdb) 84 chain, err := core.NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil, nil) 85 if err != nil { 86 t.Fatalf("Failed to create local chain, %v", err) 87 } 88 chain.InsertChain(blocks) 89 return &testBackend{chain: chain} 90 } 91 92 func (b *testBackend) CurrentHeader() *types.Header { 93 return b.chain.CurrentHeader() 94 } 95 96 func (b *testBackend) GetBlockByNumber(number uint64) *types.Block { 97 return b.chain.GetBlockByNumber(number) 98 } 99 100 func TestSuggestPrice(t *testing.T) { 101 config := Config{ 102 Blocks: 3, 103 Percentile: 60, 104 Default: big.NewInt(params.GWei), 105 } 106 backend := newTestBackend(t) 107 oracle := NewOracle(backend, config) 108 109 // The gas price sampled is: 32G, 31G, 30G, 29G, 28G, 27G 110 got, err := oracle.SuggestPrice(context.Background()) 111 if err != nil { 112 t.Fatalf("Failed to retrieve recommended gas price: %v", err) 113 } 114 expect := big.NewInt(params.GWei * int64(30)) 115 if got.Cmp(expect) != 0 { 116 t.Fatalf("Gas price mismatch, want %d, got %d", expect, got) 117 } 118 }