github.com/ethereum/go-ethereum@v1.16.1/consensus/misc/eip4844/eip4844_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 eip4844 18 19 import ( 20 "fmt" 21 "math/big" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/core/types" 25 "github.com/ethereum/go-ethereum/params" 26 ) 27 28 func TestCalcExcessBlobGas(t *testing.T) { 29 var ( 30 config = params.MainnetChainConfig 31 targetBlobs = targetBlobsPerBlock(config, *config.CancunTime) 32 targetBlobGas = uint64(targetBlobs) * params.BlobTxBlobGasPerBlob 33 ) 34 var tests = []struct { 35 excess uint64 36 blobs int 37 want uint64 38 }{ 39 // The excess blob gas should not increase from zero if the used blob 40 // slots are below - or equal - to the target. 41 {0, 0, 0}, 42 {0, 1, 0}, 43 {0, targetBlobs, 0}, 44 45 // If the target blob gas is exceeded, the excessBlobGas should increase 46 // by however much it was overshot 47 {0, targetBlobs + 1, params.BlobTxBlobGasPerBlob}, 48 {1, targetBlobs + 1, params.BlobTxBlobGasPerBlob + 1}, 49 {1, targetBlobs + 2, 2*params.BlobTxBlobGasPerBlob + 1}, 50 51 // The excess blob gas should decrease by however much the target was 52 // under-shot, capped at zero. 53 {targetBlobGas, targetBlobs, targetBlobGas}, 54 {targetBlobGas, targetBlobs - 1, targetBlobGas - params.BlobTxBlobGasPerBlob}, 55 {targetBlobGas, targetBlobs - 2, targetBlobGas - (2 * params.BlobTxBlobGasPerBlob)}, 56 {params.BlobTxBlobGasPerBlob - 1, targetBlobs - 1, 0}, 57 } 58 for i, tt := range tests { 59 blobGasUsed := uint64(tt.blobs) * params.BlobTxBlobGasPerBlob 60 header := &types.Header{ 61 ExcessBlobGas: &tt.excess, 62 BlobGasUsed: &blobGasUsed, 63 } 64 result := CalcExcessBlobGas(config, header, *config.CancunTime) 65 if result != tt.want { 66 t.Errorf("test %d: excess blob gas mismatch: have %v, want %v", i, result, tt.want) 67 } 68 } 69 } 70 71 func TestCalcBlobFee(t *testing.T) { 72 zero := uint64(0) 73 74 tests := []struct { 75 excessBlobGas uint64 76 blobfee int64 77 }{ 78 {0, 1}, 79 {2314057, 1}, 80 {2314058, 2}, 81 {10 * 1024 * 1024, 23}, 82 } 83 for i, tt := range tests { 84 config := ¶ms.ChainConfig{LondonBlock: big.NewInt(0), CancunTime: &zero, BlobScheduleConfig: params.DefaultBlobSchedule} 85 header := &types.Header{ExcessBlobGas: &tt.excessBlobGas} 86 have := CalcBlobFee(config, header) 87 if have.Int64() != tt.blobfee { 88 t.Errorf("test %d: blobfee mismatch: have %v want %v", i, have, tt.blobfee) 89 } 90 } 91 } 92 93 func TestFakeExponential(t *testing.T) { 94 tests := []struct { 95 factor int64 96 numerator int64 97 denominator int64 98 want int64 99 }{ 100 // When numerator == 0 the return value should always equal the value of factor 101 {1, 0, 1, 1}, 102 {38493, 0, 1000, 38493}, 103 {0, 1234, 2345, 0}, // should be 0 104 {1, 2, 1, 6}, // approximate 7.389 105 {1, 4, 2, 6}, 106 {1, 3, 1, 16}, // approximate 20.09 107 {1, 6, 2, 18}, 108 {1, 4, 1, 49}, // approximate 54.60 109 {1, 8, 2, 50}, 110 {10, 8, 2, 542}, // approximate 540.598 111 {11, 8, 2, 596}, // approximate 600.58 112 {1, 5, 1, 136}, // approximate 148.4 113 {1, 5, 2, 11}, // approximate 12.18 114 {2, 5, 2, 23}, // approximate 24.36 115 {1, 50000000, 2225652, 5709098764}, 116 } 117 for i, tt := range tests { 118 f, n, d := big.NewInt(tt.factor), big.NewInt(tt.numerator), big.NewInt(tt.denominator) 119 original := fmt.Sprintf("%d %d %d", f, n, d) 120 have := fakeExponential(f, n, d) 121 if have.Int64() != tt.want { 122 t.Errorf("test %d: fake exponential mismatch: have %v want %v", i, have, tt.want) 123 } 124 later := fmt.Sprintf("%d %d %d", f, n, d) 125 if original != later { 126 t.Errorf("test %d: fake exponential modified arguments: have\n%v\nwant\n%v", i, later, original) 127 } 128 } 129 }