|
| 1 | +// Copyright 2023 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 misc |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/big" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/params" |
| 25 | +) |
| 26 | + |
| 27 | +func TestCalcBlobFee(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + excessDataGas int64 |
| 30 | + blobfee int64 |
| 31 | + }{ |
| 32 | + {0, 1}, |
| 33 | + {1542706, 1}, |
| 34 | + {1542707, 2}, |
| 35 | + {10 * 1024 * 1024, 111}, |
| 36 | + } |
| 37 | + have := CalcBlobFee(nil) |
| 38 | + if have.Int64() != params.BlobTxMinDataGasprice { |
| 39 | + t.Errorf("nil test: blobfee mismatch: have %v, want %v", have, params.BlobTxMinDataGasprice) |
| 40 | + } |
| 41 | + for i, tt := range tests { |
| 42 | + have := CalcBlobFee(big.NewInt(tt.excessDataGas)) |
| 43 | + if have.Int64() != tt.blobfee { |
| 44 | + t.Errorf("test %d: blobfee mismatch: have %v want %v", i, have, tt.blobfee) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestFakeExponential(t *testing.T) { |
| 50 | + tests := []struct { |
| 51 | + factor int64 |
| 52 | + numerator int64 |
| 53 | + denominator int64 |
| 54 | + want int64 |
| 55 | + }{ |
| 56 | + // When numerator == 0 the return value should always equal the value of factor |
| 57 | + {1, 0, 1, 1}, |
| 58 | + {38493, 0, 1000, 38493}, |
| 59 | + {0, 1234, 2345, 0}, // should be 0 |
| 60 | + {1, 2, 1, 6}, // approximate 7.389 |
| 61 | + {1, 4, 2, 6}, |
| 62 | + {1, 3, 1, 16}, // approximate 20.09 |
| 63 | + {1, 6, 2, 18}, |
| 64 | + {1, 4, 1, 49}, // approximate 54.60 |
| 65 | + {1, 8, 2, 50}, |
| 66 | + {10, 8, 2, 542}, // approximate 540.598 |
| 67 | + {11, 8, 2, 596}, // approximate 600.58 |
| 68 | + {1, 5, 1, 136}, // approximate 148.4 |
| 69 | + {1, 5, 2, 11}, // approximate 12.18 |
| 70 | + {2, 5, 2, 23}, // approximate 24.36 |
| 71 | + {1, 50000000, 2225652, 5709098764}, |
| 72 | + } |
| 73 | + for i, tt := range tests { |
| 74 | + f, n, d := big.NewInt(tt.factor), big.NewInt(tt.numerator), big.NewInt(tt.denominator) |
| 75 | + original := fmt.Sprintf("%d %d %d", f, n, d) |
| 76 | + have := fakeExponential(f, n, d) |
| 77 | + if have.Int64() != tt.want { |
| 78 | + t.Errorf("test %d: fake exponential mismatch: have %v want %v", i, have, tt.want) |
| 79 | + } |
| 80 | + later := fmt.Sprintf("%d %d %d", f, n, d) |
| 81 | + if original != later { |
| 82 | + t.Errorf("test %d: fake exponential modified arguments: have\n%v\nwant\n%v", i, later, original) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments