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