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

Replace UnmarshalJSON() with UnmarshalText() for transaction statuses #2220

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 23 additions & 31 deletions starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
Rejected
)

func (es *ExecutionStatus) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"SUCCEEDED"`:
func (es *ExecutionStatus) UnmarshalText(data []byte) error {
switch str := string(data); str {
case "SUCCEEDED":
*es = Succeeded
case `"REVERTED"`:
case "REVERTED":
*es = Reverted
case `"REJECTED"`:
case "REJECTED":

Check warning on line 25 in starknet/transaction.go

View check run for this annotation

Codecov / codecov/patch

starknet/transaction.go#L25

Added line #L25 was not covered by tests
*es = Rejected
default:
return errors.New("unknown ExecutionStatus")
return fmt.Errorf("unknown ExecutionStatus %q", str)
}
return nil
}
Expand All @@ -39,18 +39,18 @@
Received
)

func (fs *FinalityStatus) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"ACCEPTED_ON_L2"`:
func (fs *FinalityStatus) UnmarshalText(data []byte) error {
switch str := string(data); str {
case "ACCEPTED_ON_L2":
*fs = AcceptedOnL2
case `"ACCEPTED_ON_L1"`:
case "ACCEPTED_ON_L1":
*fs = AcceptedOnL1
case `"NOT_RECEIVED"`:
case "NOT_RECEIVED":
*fs = NotReceived
case `"RECEIVED"`:
case "RECEIVED":
*fs = Received
default:
return errors.New("unknown FinalityStatus")
return fmt.Errorf("unknown FinalityStatus %q", str)
}
return nil
}
Expand Down Expand Up @@ -83,24 +83,24 @@
}
}

func (t TransactionType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t)), nil
func (t TransactionType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}

func (t *TransactionType) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"DECLARE"`:
func (t *TransactionType) UnmarshalText(data []byte) error {
switch str := string(data); str {
case "DECLARE":
*t = TxnDeclare
case `"DEPLOY"`:
case "DEPLOY":
*t = TxnDeploy
case `"DEPLOY_ACCOUNT"`:
case "DEPLOY_ACCOUNT":
*t = TxnDeployAccount
case `"INVOKE"`, `"INVOKE_FUNCTION"`:
case "INVOKE", "INVOKE_FUNCTION":
*t = TxnInvoke
case `"L1_HANDLER"`:
case "L1_HANDLER":
*t = TxnL1Handler
default:
return errors.New("unknown TransactionType")
return fmt.Errorf("unknown TransactionType %q", str)

Check warning on line 103 in starknet/transaction.go

View check run for this annotation

Codecov / codecov/patch

starknet/transaction.go#L103

Added line #L103 was not covered by tests
}
return nil
}
Expand Down Expand Up @@ -139,14 +139,6 @@
}
}

func (r Resource) MarshalJSON() ([]byte, error) {
result, err := r.MarshalText()
if err != nil {
return nil, err
}
return []byte(`"` + string(result) + `"`), nil
}

type DataAvailabilityMode uint32

const (
Expand Down
37 changes: 25 additions & 12 deletions starknet/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package starknet_test

import (
"encoding/json"
"testing"

"github.com/NethermindEth/juno/starknet"
Expand All @@ -10,22 +11,34 @@ import (

func TestUnmarshalExecutionStatus(t *testing.T) {
es := new(starknet.ExecutionStatus)
require.NoError(t, es.UnmarshalJSON([]byte(`"SUCCEEDED"`)))
assert.Equal(t, starknet.Succeeded, *es)

require.NoError(t, es.UnmarshalJSON([]byte(`"REVERTED"`)))
assert.Equal(t, starknet.Reverted, *es)

require.ErrorContains(t, es.UnmarshalJSON([]byte("ABC")), "unknown ExecutionStatus")
cases := map[string]starknet.ExecutionStatus{
"SUCCEEDED": starknet.Succeeded,
"REVERTED": starknet.Reverted,
kirugan marked this conversation as resolved.
Show resolved Hide resolved
}
for str, expected := range cases {
quotedStr := `"` + str + `"`
require.NoError(t, json.Unmarshal([]byte(quotedStr), es))
assert.Equal(t, expected, *es)
}

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

func TestUnmarshalFinalityStatus(t *testing.T) {
fs := new(starknet.FinalityStatus)
require.NoError(t, fs.UnmarshalJSON([]byte(`"ACCEPTED_ON_L1"`)))
assert.Equal(t, starknet.AcceptedOnL1, *fs)

require.NoError(t, fs.UnmarshalJSON([]byte(`"ACCEPTED_ON_L2"`)))
assert.Equal(t, starknet.AcceptedOnL2, *fs)

require.ErrorContains(t, fs.UnmarshalJSON([]byte("ABC")), "unknown FinalityStatus")
cases := map[string]starknet.FinalityStatus{
"ACCEPTED_ON_L2": starknet.AcceptedOnL2,
"ACCEPTED_ON_L1": starknet.AcceptedOnL1,
"NOT_RECEIVED": starknet.NotReceived,
"RECEIVED": starknet.Received,
}
for str, expected := range cases {
quotedStr := `"` + str + `"`
require.NoError(t, json.Unmarshal([]byte(quotedStr), fs))
assert.Equal(t, expected, *fs)
}

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