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