Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rename gas price calculation function #3302

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions vms/components/fee/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func (g Gas) SubPerSecond(gasPerSecond Gas, seconds uint64) Gas {
return Gas(totalGas)
}

// MulExp returns an approximation of g * e^(excess / excessConversionConstant)
// CalculateGasPrice returns the gas price given the minimum gas price, the
// excess gas, and the excess conversion constant.
//
// It is defined as an approximation of:
//
// minPrice * e^(excess / excessConversionConstant)
//
// This implements the EIP-4844 fake exponential formula:
//
Expand All @@ -77,7 +82,8 @@ func (g Gas) SubPerSecond(gasPerSecond Gas, seconds uint64) Gas {
// This function does not perform any memory allocations.
//
//nolint:dupword // The python is copied from the EIP-4844 specification
func (g GasPrice) MulExp(
func CalculateGasPrice(
minPrice GasPrice,
excess Gas,
excessConversionConstant Gas,
) GasPrice {
Expand All @@ -95,7 +101,7 @@ func (g GasPrice) MulExp(
denominator.SetUint64(uint64(excessConversionConstant)) // range is [0, MaxUint64]

i.SetOne()
numeratorAccum.SetUint64(uint64(g)) // range is [0, MaxUint64]
numeratorAccum.SetUint64(uint64(minPrice)) // range is [0, MaxUint64]
numeratorAccum.Mul(&numeratorAccum, &denominator) // range is [0, MaxUint128]

maxOutput.Mul(&denominator, maxUint64) // range is [0, MaxUint128]
Expand Down
14 changes: 7 additions & 7 deletions vms/components/fee/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

var gasPriceMulExpTests = []struct {
var calculateGasPriceTests = []struct {
minPrice GasPrice
excess Gas
excessConversionConstant Gas
Expand Down Expand Up @@ -172,20 +172,20 @@ func Test_Gas_SubPerSecond(t *testing.T) {
}
}

func Test_GasPrice_MulExp(t *testing.T) {
for _, test := range gasPriceMulExpTests {
func Test_CalculateGasPrice(t *testing.T) {
for _, test := range calculateGasPriceTests {
t.Run(fmt.Sprintf("%d*e^(%d/%d)=%d", test.minPrice, test.excess, test.excessConversionConstant, test.expected), func(t *testing.T) {
actual := test.minPrice.MulExp(test.excess, test.excessConversionConstant)
actual := CalculateGasPrice(test.minPrice, test.excess, test.excessConversionConstant)
require.Equal(t, test.expected, actual)
})
}
}

func Benchmark_GasPrice_MulExp(b *testing.B) {
for _, test := range gasPriceMulExpTests {
func Benchmark_CalculateGasPrice(b *testing.B) {
for _, test := range calculateGasPriceTests {
b.Run(fmt.Sprintf("%d*e^(%d/%d)=%d", test.minPrice, test.excess, test.excessConversionConstant, test.expected), func(b *testing.B) {
for i := 0; i < b.N; i++ {
test.minPrice.MulExp(test.excess, test.excessConversionConstant)
CalculateGasPrice(test.minPrice, test.excess, test.excessConversionConstant)
}
})
}
Expand Down
Loading