mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-03 17:44:29 +00:00
36e70c545b
Logic to compute fees for data blobs as well as additional check that verifies if user was willing to pay the current `data_gas` price. Updated `FakeExponential` function to work with uint256.
94 lines
3.0 KiB
Go
94 lines
3.0 KiB
Go
// Copyright 2022 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package misc
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/holiman/uint256"
|
|
"github.com/ledgerwatch/erigon/params"
|
|
)
|
|
|
|
func TestFakeExponential(t *testing.T) {
|
|
var tests = []struct {
|
|
factor, num, denom int64
|
|
want int64
|
|
}{
|
|
// When num==0 the return value should always equal the value of factor
|
|
{1, 0, 1, 1},
|
|
{38493, 0, 1000, 38493},
|
|
{0, 1234, 2345, 0}, // should be 0
|
|
{1, 2, 1, 6}, // approximate 7.389
|
|
{1, 4, 2, 6},
|
|
{1, 3, 1, 16}, // approximate 20.09
|
|
{1, 6, 2, 18},
|
|
{1, 4, 1, 49}, // approximate 54.60
|
|
{1, 8, 2, 50},
|
|
{10, 8, 2, 542}, // approximate 540.598
|
|
{11, 8, 2, 596}, // approximate 600.58
|
|
{1, 5, 1, 136}, // approximate 148.4
|
|
{1, 5, 2, 11}, // approximate 12.18
|
|
{2, 5, 2, 23}, // approximate 24.36
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
factor := uint256.NewInt(uint64(tt.factor))
|
|
num := big.NewInt(tt.num)
|
|
denom := uint256.NewInt(uint64(tt.denom))
|
|
result, err := FakeExponential(factor, denom, num)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
//t.Logf("%v*e^(%v/%v): %v", factor, num, denom, result)
|
|
if tt.want != result.ToBig().Int64() {
|
|
t.Errorf("got %v want %v", result, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCalcExcessDataGas(t *testing.T) {
|
|
var tests = []struct {
|
|
parentExcessDataGas int64
|
|
newBlobs int
|
|
want int64
|
|
}{
|
|
{0, 0, 0},
|
|
{0, 1, 0},
|
|
{0, params.TargetDataGasPerBlock / params.DataGasPerBlob, 0},
|
|
{0, (params.TargetDataGasPerBlock / params.DataGasPerBlob) + 1, params.DataGasPerBlob},
|
|
{100000, (params.TargetDataGasPerBlock / params.DataGasPerBlob) + 1, params.DataGasPerBlob + 100000},
|
|
{params.TargetDataGasPerBlock, 1, params.DataGasPerBlob},
|
|
{params.TargetDataGasPerBlock, 0, 0},
|
|
{params.TargetDataGasPerBlock, (params.TargetDataGasPerBlock / params.DataGasPerBlob), params.TargetDataGasPerBlock},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
parentExcessDataGas := big.NewInt(tt.parentExcessDataGas)
|
|
result := CalcExcessDataGas(parentExcessDataGas, tt.newBlobs)
|
|
if tt.want != result.Int64() {
|
|
t.Errorf("got %v want %v", result, tt.want)
|
|
}
|
|
}
|
|
|
|
// Test nil value for parentExcessDataGas
|
|
result := CalcExcessDataGas(nil, (params.TargetDataGasPerBlock/params.DataGasPerBlob)+1)
|
|
if result.Int64() != params.DataGasPerBlob {
|
|
t.Errorf("got %v want %v", result, params.DataGasPerBlob)
|
|
}
|
|
}
|