Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ResourceBounds encoding #2398

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Resource uint32
const (
ResourceL1Gas Resource = iota + 1
ResourceL2Gas
ResourceL1DataGas
)

func (r Resource) MarshalText() ([]byte, error) {
Expand All @@ -172,6 +173,8 @@ func (r Resource) MarshalText() ([]byte, error) {
return []byte("l1_gas"), nil
case ResourceL2Gas:
return []byte("l2_gas"), nil
case ResourceL1DataGas:
return []byte("l1_data_gas"), nil
default:
return nil, fmt.Errorf("unknown Resource %v", r)
}
Expand All @@ -183,6 +186,8 @@ func (r *Resource) UnmarshalJSON(data []byte) error {
*r = ResourceL1Gas
case `"l2_gas"`:
*r = ResourceL2Gas
case `"l1_data_gas"`:
*r = ResourceL1DataGas
default:
return fmt.Errorf("unknown Resource: %q", string(data))
}
Expand Down
51 changes: 51 additions & 0 deletions rpc/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1365,3 +1365,54 @@ func TestTransactionStatus(t *testing.T) {
})
}
}

func TestResourceMarshalText(t *testing.T) {
tests := []struct {
resource rpc.Resource
text string
hasError bool
}{
{rpc.ResourceL1Gas, "l1_gas", false},
{rpc.ResourceL2Gas, "l2_gas", false},
{rpc.ResourceL1DataGas, "l1_data_gas", false},
{rpc.Resource(999), "error", true},
}

for _, test := range tests {
t.Run(test.text, func(t *testing.T) {
b, err := test.resource.MarshalText()
if test.hasError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, test.text, string(b))
})
}
}

func TestResourceUnmarshalText(t *testing.T) {
tests := []struct {
resource rpc.Resource
text string
hasError bool
}{
{rpc.ResourceL1Gas, `"l1_gas"`, false},
{rpc.ResourceL2Gas, `"l2_gas"`, false},
{rpc.ResourceL1DataGas, `"l1_data_gas"`, false},
{rpc.Resource(999), "error", true},
}

for _, test := range tests {
t.Run(test.text, func(t *testing.T) {
var r rpc.Resource
err := r.UnmarshalText([]byte(test.text))
if test.hasError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, test.resource, r)
})
}
}
2 changes: 2 additions & 0 deletions starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func (r Resource) MarshalText() ([]byte, error) {
return []byte("L1_GAS"), nil
case ResourceL2Gas:
return []byte("L2_GAS"), nil
case ResourceL1DataGas:
return []byte("L1_DATA_GAS"), nil
default:
return nil, errors.New("unknown resource")
}
Expand Down
51 changes: 51 additions & 0 deletions starknet/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,54 @@ func TestUnmarshalFinalityStatus(t *testing.T) {

require.ErrorContains(t, json.Unmarshal([]byte(`"ABC"`), fs), "unknown FinalityStatus")
}

func TestResourceMarshalText(t *testing.T) {
tests := []struct {
resource starknet.Resource
text string
hasError bool
}{
{starknet.ResourceL1Gas, "L1_GAS", false},
{starknet.ResourceL2Gas, "L2_GAS", false},
{starknet.ResourceL1DataGas, "L1_DATA_GAS", false},
{starknet.Resource(999), "error", true},
}

for _, test := range tests {
t.Run(test.text, func(t *testing.T) {
b, err := test.resource.MarshalText()
if test.hasError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, test.text, string(b))
})
}
}

func TestResourceUnmarshalText(t *testing.T) {
tests := []struct {
resource starknet.Resource
text string
hasError bool
}{
{starknet.ResourceL1Gas, "L1_GAS", false},
{starknet.ResourceL2Gas, "L2_GAS", false},
{starknet.ResourceL1DataGas, "L1_DATA_GAS", false},
{starknet.Resource(999), "error", true},
}

for _, test := range tests {
t.Run(test.text, func(t *testing.T) {
var r starknet.Resource
err := r.UnmarshalText([]byte(test.text))
if test.hasError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, test.resource, r)
})
}
}
Loading