gasprice_test.go 3.8 KB

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