Skip to content

Commit 06eb2dc

Browse files
gligneulMishkaRogachev
authored andcommitted
Add helper to check resource kind
1 parent 11f9df8 commit 06eb2dc

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

arbitrum/multigas/resources.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package multigas
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io"
67
"math/bits"
78

@@ -25,6 +26,14 @@ const (
2526
NumResourceKind
2627
)
2728

29+
// CheckResourceKind checks whether the given id is a valid resource.
30+
func CheckResourceKind(id uint8) (ResourceKind, error) {
31+
if id <= uint8(ResourceKindUnknown) || id >= uint8(NumResourceKind) {
32+
return ResourceKindUnknown, fmt.Errorf("invalid resource id: %v", id)
33+
}
34+
return ResourceKind(id), nil
35+
}
36+
2837
// MultiGas tracks gas usage across multiple resource kinds, while also
2938
// maintaining a single-dimensional total gas sum and refund amount.
3039
type MultiGas struct {

arbitrum/multigas/resources_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ import (
1010
"github.com/ethereum/go-ethereum/rlp"
1111
)
1212

13+
func TestCheckResourceKind(t *testing.T) {
14+
_, err := CheckResourceKind(0)
15+
if err == nil {
16+
t.Errorf("expected error, got nil")
17+
}
18+
_, err = CheckResourceKind(uint8(NumResourceKind))
19+
if err == nil {
20+
t.Errorf("expected error, got nil")
21+
}
22+
resource, err := CheckResourceKind(uint8(ResourceKindComputation))
23+
if err != nil {
24+
t.Errorf("unexpected error, got %v", err)
25+
}
26+
if resource != ResourceKindComputation {
27+
t.Errorf("expected computation resource, got %v", resource)
28+
}
29+
}
30+
1331
func TestZeroGas(t *testing.T) {
1432
zero := ZeroGas()
1533
if zero.SingleGas() != 0 {

0 commit comments

Comments
 (0)