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