Skip to content
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
50 changes: 49 additions & 1 deletion arbitrum/multigas/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,52 @@ const (
)

// MultiGas tracks gas for each resource separately.
type MultiGas [NumResourceKind]uint64
type MultiGas struct {
gas [NumResourceKind]uint64
}

func ZeroGas() *MultiGas {
return &MultiGas{}
}

func NewMultiGas(kind ResourceKind, amount uint64) *MultiGas {
mg := ZeroGas()
mg.gas[kind] = amount
return mg
}

func ComputationGas(amount uint64) *MultiGas {
return NewMultiGas(ResourceKindComputation, amount)
}

func HistoryGrowthGas(amount uint64) *MultiGas {
return NewMultiGas(ResourceKindHistoryGrowth, amount)
}

func StorageAccessGas(amount uint64) *MultiGas {
return NewMultiGas(ResourceKindStorageAccess, amount)
}

func StorageGrowthGas(amount uint64) *MultiGas {
return NewMultiGas(ResourceKindStorageGrowth, amount)
}

func (z *MultiGas) Get(kind ResourceKind) uint64 {
return z.gas[kind]
}

// Add sets z to the sum x+y and returns z.
func (z *MultiGas) Add(x *MultiGas, y *MultiGas) *MultiGas {
for i := ResourceKindUnknown; i < NumResourceKind; i++ {
z.gas[i] = x.gas[i] + y.gas[i]
}
return z
}

// Sub sets z to the difference x-y and returns z.
func (z *MultiGas) Sub(x *MultiGas, y *MultiGas) *MultiGas {
for i := ResourceKindUnknown; i < NumResourceKind; i++ {
z.gas[i] = x.gas[i] - y.gas[i]
}
return z
}
25 changes: 25 additions & 0 deletions arbitrum/multigas/resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package multigas

import "testing"

func TestMultiGas(t *testing.T) {
gas := new(MultiGas).Add(ComputationGas(10), HistoryGrowthGas(20))
gas = gas.Add(gas, StorageAccessGas(30))
gas = gas.Add(gas, StorageGrowthGas(40))
if got, want := gas.Get(ResourceKindComputation), uint64(10); got != want {
t.Errorf("unexpected computation gas: got %v, want %v", got, want)
}
if got, want := gas.Get(ResourceKindHistoryGrowth), uint64(20); got != want {
t.Errorf("unexpected history growth gas: got %v, want %v", got, want)
}
if got, want := gas.Get(ResourceKindStorageAccess), uint64(30); got != want {
t.Errorf("unexpected storage access gas: got %v, want %v", got, want)
}
if got, want := gas.Get(ResourceKindStorageGrowth), uint64(40); got != want {
t.Errorf("unexpected storage growth gas: got %v, want %v", got, want)
}
gas = gas.Sub(gas, StorageGrowthGas(40))
if got, want := gas.Get(ResourceKindStorageGrowth), uint64(0); got != want {
t.Errorf("unexpected storage growth gas: got %v, want %v", got, want)
}
}
Loading