File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package multigas
22
33import (
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.
3039type MultiGas struct {
Original file line number Diff line number Diff 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+
1331func TestZeroGas (t * testing.T ) {
1432 zero := ZeroGas ()
1533 if zero .SingleGas () != 0 {
You can’t perform that action at this time.
0 commit comments