Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions arbitrum/multigas/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multigas

import (
"encoding/json"
"fmt"
"io"
"math/bits"

Expand All @@ -25,6 +26,14 @@ const (
NumResourceKind
)

// CheckResourceKind checks whether the given id is a valid resource.
func CheckResourceKind(id uint8) (ResourceKind, error) {
if id <= uint8(ResourceKindUnknown) || id >= uint8(NumResourceKind) {
return ResourceKindUnknown, fmt.Errorf("invalid resource id: %v", id)
}
return ResourceKind(id), nil
}

// MultiGas tracks gas usage across multiple resource kinds, while also
// maintaining a single-dimensional total gas sum and refund amount.
type MultiGas struct {
Expand Down
18 changes: 18 additions & 0 deletions arbitrum/multigas/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)

func TestCheckResourceKind(t *testing.T) {
_, err := CheckResourceKind(0)
if err == nil {
t.Errorf("expected error, got nil")
}
_, err = CheckResourceKind(uint8(NumResourceKind))
if err == nil {
t.Errorf("expected error, got nil")
}
resource, err := CheckResourceKind(uint8(ResourceKindComputation))
if err != nil {
t.Errorf("unexpected error, got %v", err)
}
if resource != ResourceKindComputation {
t.Errorf("expected computation resource, got %v", resource)
}
}

func TestZeroGas(t *testing.T) {
zero := ZeroGas()
if zero.SingleGas() != 0 {
Expand Down
Loading