diff --git a/arbitrum/multigas/resources.go b/arbitrum/multigas/resources.go index c26ff1e53e..3fcfecf99f 100644 --- a/arbitrum/multigas/resources.go +++ b/arbitrum/multigas/resources.go @@ -2,6 +2,7 @@ package multigas import ( "encoding/json" + "fmt" "io" "math/bits" @@ -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 { diff --git a/arbitrum/multigas/resources_test.go b/arbitrum/multigas/resources_test.go index 5299622949..b62e32dca2 100644 --- a/arbitrum/multigas/resources_test.go +++ b/arbitrum/multigas/resources_test.go @@ -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 {