github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/core/txpool/blobpool/priority_test.go (about) 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 blobpool 18 19 import ( 20 "testing" 21 22 "github.com/holiman/uint256" 23 ) 24 25 // Tests that the priority fees are calculated correctly as the log2 of the fee 26 // jumps needed to go from the base fee to the tx's fee cap. 27 func TestPriorityCalculation(t *testing.T) { 28 tests := []struct { 29 basefee uint64 30 txfee uint64 31 result int 32 }{ 33 {basefee: 7, txfee: 10, result: 2}, // 3.02 jumps, 4 ceil, 2 log2 34 {basefee: 17_200_000_000, txfee: 17_200_000_000, result: 0}, // 0 jumps, special case 0 log2 35 {basefee: 9_853_941_692, txfee: 11_085_092_510, result: 0}, // 0.99 jumps, 1 ceil, 0 log2 36 {basefee: 11_544_106_391, txfee: 10_356_781_100, result: 0}, // -0.92 jumps, -1 floor, 0 log2 37 {basefee: 17_200_000_000, txfee: 7, result: -7}, // -183.57 jumps, -184 floor, -7 log2 38 {basefee: 7, txfee: 17_200_000_000, result: 7}, // 183.57 jumps, 184 ceil, 7 log2 39 } 40 for i, tt := range tests { 41 var ( 42 baseJumps = dynamicFeeJumps(uint256.NewInt(tt.basefee)) 43 feeJumps = dynamicFeeJumps(uint256.NewInt(tt.txfee)) 44 ) 45 if prio := evictionPriority1D(baseJumps, feeJumps); prio != tt.result { 46 t.Errorf("test %d priority mismatch: have %d, want %d", i, prio, tt.result) 47 } 48 } 49 } 50 51 // Benchmarks how many dynamic fee jump values can be done. 52 func BenchmarkDynamicFeeJumpCalculation(b *testing.B) { 53 fees := make([]*uint256.Int, b.N) 54 for i := 0; i < b.N; i++ { 55 fees[i] = uint256.NewInt(rand.Uint64()) 56 } 57 b.ResetTimer() 58 b.ReportAllocs() 59 for i := 0; i < b.N; i++ { 60 dynamicFeeJumps(fees[i]) 61 } 62 } 63 64 // Benchmarks how many priority recalculations can be done. 65 func BenchmarkPriorityCalculation(b *testing.B) { 66 // The basefee and blob fee is constant for all transactions across a block, 67 // so we can assume their absolute jump counts can be pre-computed. 68 basefee := uint256.NewInt(17_200_000_000) // 17.2 Gwei is the 22.03.2023 zero-emission basefee, random number 69 blobfee := uint256.NewInt(123_456_789_000) // Completely random, no idea what this will be 70 71 basefeeJumps := dynamicFeeJumps(basefee) 72 blobfeeJumps := dynamicFeeJumps(blobfee) 73 74 // The transaction's fee cap and blob fee cap are constant across the life 75 // of the transaction, so we can pre-calculate and cache them. 76 txBasefeeJumps := make([]float64, b.N) 77 txBlobfeeJumps := make([]float64, b.N) 78 for i := 0; i < b.N; i++ { 79 txBasefeeJumps[i] = dynamicFeeJumps(uint256.NewInt(rand.Uint64())) 80 txBlobfeeJumps[i] = dynamicFeeJumps(uint256.NewInt(rand.Uint64())) 81 } 82 b.ResetTimer() 83 b.ReportAllocs() 84 for i := 0; i < b.N; i++ { 85 evictionPriority(basefeeJumps, txBasefeeJumps[i], blobfeeJumps, txBlobfeeJumps[i]) 86 } 87 }