Skip to content

Commit

Permalink
Gas multiplier getter (#492)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context
Allow multiplier to be read to make it easier to create new gas meters
with the same multiplier

## Testing performed to validate your change
unit tests
  • Loading branch information
codchen authored Apr 25, 2024
1 parent 0031878 commit bc0008f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type GasMeter interface {
IsPastLimit() bool
IsOutOfGas() bool
String() string
Multiplier() (numerator uint64, denominator uint64)
}

type basicGasMeter struct {
Expand Down Expand Up @@ -164,6 +165,10 @@ func (g *basicGasMeter) String() string {
return fmt.Sprintf("BasicGasMeter:\n limit: %d\n consumed: %d", g.limit, g.consumed)
}

func (g *basicGasMeter) Multiplier() (numerator uint64, denominator uint64) {
return 1, 1
}

type multiplierGasMeter struct {
basicGasMeter
multiplierNumerator uint64
Expand Down Expand Up @@ -194,6 +199,10 @@ func (g *multiplierGasMeter) RefundGas(amount Gas, descriptor string) {
g.basicGasMeter.RefundGas(g.adjustGas(amount), descriptor)
}

func (g *multiplierGasMeter) Multiplier() (numerator uint64, denominator uint64) {
return g.multiplierNumerator, g.multiplierDenominator
}

type infiniteGasMeter struct {
consumed Gas
lock *sync.Mutex
Expand Down Expand Up @@ -272,6 +281,10 @@ func (g *infiniteGasMeter) String() string {
return fmt.Sprintf("InfiniteGasMeter:\n consumed: %d", g.consumed)
}

func (g *infiniteGasMeter) Multiplier() (numerator uint64, denominator uint64) {
return 1, 1
}

type infiniteMultiplierGasMeter struct {
infiniteGasMeter
multiplierNumerator uint64
Expand Down Expand Up @@ -301,6 +314,10 @@ func (g *infiniteMultiplierGasMeter) RefundGas(amount Gas, descriptor string) {
g.infiniteGasMeter.RefundGas(g.adjustGas(amount), descriptor)
}

func (g *infiniteMultiplierGasMeter) Multiplier() (numerator uint64, denominator uint64) {
return g.multiplierNumerator, g.multiplierDenominator
}

type noConsumptionInfiniteGasMeter struct {
infiniteGasMeter
}
Expand Down
14 changes: 14 additions & 0 deletions store/types/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func TestInfiniteGasMeter(t *testing.T) {
meter.ConsumeGas(Gas(math.MaxUint64/2), "consume half max uint64")
require.Panics(t, func() { meter.ConsumeGas(Gas(math.MaxUint64/2)+2, "panic") })
require.Panics(t, func() { meter.RefundGas(meter.GasConsumed()+1, "refund greater than consumed") })
n, d := meter.Multiplier()
require.Equal(t, uint64(1), n)
require.Equal(t, uint64(1), d)
}

func TestGasMeter(t *testing.T) {
Expand Down Expand Up @@ -68,6 +71,9 @@ func TestGasMeter(t *testing.T) {
meter2 := NewGasMeter(math.MaxUint64)
meter2.ConsumeGas(Gas(math.MaxUint64/2), "consume half max uint64")
require.Panics(t, func() { meter2.ConsumeGas(Gas(math.MaxUint64/2)+2, "panic") })
n, d := meter.Multiplier()
require.Equal(t, uint64(1), n)
require.Equal(t, uint64(1), d)
}
}

Expand Down Expand Up @@ -104,6 +110,10 @@ func TestMultiplierGasMeter(t *testing.T) {
require.True(t, meter.IsOutOfGas(), "At limit but got IsOutOfGas() false")
}
}

n, d := meter.Multiplier()
require.Equal(t, tc.multiplierNumerator, n)
require.Equal(t, tc.multiplierDenominator, d)
}
}

Expand Down Expand Up @@ -135,6 +145,10 @@ func TestInfiniteMultiplierGasMeter(t *testing.T) {
require.False(t, meter.IsPastLimit(), "Not exceeded limit but got IsPastLimit() true")
require.False(t, meter.IsOutOfGas(), "Not yet at limit but got IsOutOfGas() true")
}

n, d := meter.Multiplier()
require.Equal(t, tc.multiplierNumerator, n)
require.Equal(t, tc.multiplierDenominator, d)
}
}

Expand Down

0 comments on commit bc0008f

Please sign in to comment.