feehistory_test.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2021 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. "errors"
  20. "math/big"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. func TestFeeHistory(t *testing.T) {
  25. var cases = []struct {
  26. pending bool
  27. maxHeader, maxBlock int
  28. count int
  29. last rpc.BlockNumber
  30. percent []float64
  31. expFirst uint64
  32. expCount int
  33. expErr error
  34. }{
  35. {false, 1000, 1000, 10, 30, nil, 21, 10, nil},
  36. {false, 1000, 1000, 10, 30, []float64{0, 10}, 21, 10, nil},
  37. {false, 1000, 1000, 10, 30, []float64{20, 10}, 0, 0, errInvalidPercentile},
  38. {false, 1000, 1000, 1000000000, 30, nil, 0, 31, nil},
  39. {false, 1000, 1000, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil},
  40. {false, 1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
  41. {true, 1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
  42. {false, 20, 2, 100, rpc.LatestBlockNumber, nil, 13, 20, nil},
  43. {false, 20, 2, 100, rpc.LatestBlockNumber, []float64{0, 10}, 31, 2, nil},
  44. {false, 20, 2, 100, 32, []float64{0, 10}, 31, 2, nil},
  45. {false, 1000, 1000, 1, rpc.PendingBlockNumber, nil, 0, 0, nil},
  46. {false, 1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
  47. {true, 1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 2, nil},
  48. {true, 1000, 1000, 2, rpc.PendingBlockNumber, []float64{0, 10}, 32, 2, nil},
  49. {false, 1000, 1000, 2, rpc.FinalizedBlockNumber, []float64{0, 10}, 24, 2, nil},
  50. {false, 1000, 1000, 2, rpc.SafeBlockNumber, []float64{0, 10}, 24, 2, nil},
  51. }
  52. for i, c := range cases {
  53. config := Config{
  54. MaxHeaderHistory: c.maxHeader,
  55. MaxBlockHistory: c.maxBlock,
  56. }
  57. backend := newTestBackend(t, big.NewInt(16), c.pending)
  58. oracle := NewOracle(backend, config)
  59. first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
  60. expReward := c.expCount
  61. if len(c.percent) == 0 {
  62. expReward = 0
  63. }
  64. expBaseFee := c.expCount
  65. if expBaseFee != 0 {
  66. expBaseFee++
  67. }
  68. if first.Uint64() != c.expFirst {
  69. t.Fatalf("Test case %d: first block mismatch, want %d, got %d", i, c.expFirst, first)
  70. }
  71. if len(reward) != expReward {
  72. t.Fatalf("Test case %d: reward array length mismatch, want %d, got %d", i, expReward, len(reward))
  73. }
  74. if len(baseFee) != expBaseFee {
  75. t.Fatalf("Test case %d: baseFee array length mismatch, want %d, got %d", i, expBaseFee, len(baseFee))
  76. }
  77. if len(ratio) != c.expCount {
  78. t.Fatalf("Test case %d: gasUsedRatio array length mismatch, want %d, got %d", i, c.expCount, len(ratio))
  79. }
  80. if err != c.expErr && !errors.Is(err, c.expErr) {
  81. t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err)
  82. }
  83. }
  84. }