From e5788143c30fa3151a806ecd93ed93a430edf247 Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Wed, 6 Nov 2024 15:05:13 -0300 Subject: [PATCH] Creates RPCData Marshal func --- account/account.go | 2 +- account/account_test.go | 6 ++-- rpc/block.go | 6 ++-- rpc/chain.go | 4 +-- rpc/contract.go | 6 ++-- rpc/errors.go | 70 ++++++++++++++++++++++++----------------- rpc/mock_test.go | 4 +-- rpc/spy_test.go | 2 +- rpc/trace.go | 12 +++---- rpc/trace_test.go | 2 +- rpc/version.go | 2 +- rpc/write_test.go | 2 +- 12 files changed, 65 insertions(+), 53 deletions(-) diff --git a/account/account.go b/account/account.go index 233f110a..26f575a4 100644 --- a/account/account.go +++ b/account/account.go @@ -496,7 +496,7 @@ func (account *Account) WaitForTransactionReceipt(ctx context.Context, transacti for { select { case <-ctx.Done(): - return nil, rpc.Err(rpc.InternalError, rpc.RPCData{Message: ctx.Err().Error()}) + return nil, rpc.Err(rpc.InternalError, &rpc.RPCData{Message: ctx.Err().Error()}) case <-t.C: receiptWithBlockInfo, err := account.TransactionReceipt(ctx, transactionHash) if err != nil { diff --git a/account/account_test.go b/account/account_test.go index defcbaef..db01893c 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -991,7 +991,7 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) { ShouldCallTransactionReceipt: true, Hash: new(felt.Felt).SetUint64(1), ExpectedReceipt: nil, - ExpectedErr: rpc.Err(rpc.InternalError, rpc.RPCData{Message: "UnExpectedErr"}), + ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: "UnExpectedErr"}), }, { Timeout: time.Duration(1000), @@ -1010,7 +1010,7 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) { Hash: new(felt.Felt).SetUint64(3), ShouldCallTransactionReceipt: false, ExpectedReceipt: nil, - ExpectedErr: rpc.Err(rpc.InternalError, rpc.RPCData{Message: context.DeadlineExceeded.Error()}), + ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: context.DeadlineExceeded.Error()}), }, }, }[testEnv] @@ -1076,7 +1076,7 @@ func TestWaitForTransactionReceipt(t *testing.T) { Timeout: 3, // Should poll 3 times Hash: new(felt.Felt).SetUint64(100), ExpectedReceipt: rpc.TransactionReceipt{}, - ExpectedErr: rpc.Err(rpc.InternalError, rpc.RPCData{Message: "Post \"http://0.0.0.0:5050/\": context deadline exceeded"}), + ExpectedErr: rpc.Err(rpc.InternalError, &rpc.RPCData{Message: "Post \"http://0.0.0.0:5050/\": context deadline exceeded"}), }, }, }[testEnv] diff --git a/rpc/block.go b/rpc/block.go index 67962054..14ce86a3 100644 --- a/rpc/block.go +++ b/rpc/block.go @@ -184,20 +184,20 @@ func (provider *Provider) BlockWithReceipts(ctx context.Context, blockID BlockID var m map[string]interface{} if err := json.Unmarshal(result, &m); err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } // PendingBlockWithReceipts doesn't contain a "status" field if _, ok := m["status"]; ok { var block BlockWithReceipts if err := json.Unmarshal(result, &block); err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return &block, nil } else { var pendingBlock PendingBlockWithReceipts if err := json.Unmarshal(result, &pendingBlock); err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return &pendingBlock, nil } diff --git a/rpc/chain.go b/rpc/chain.go index cda619c4..4f927e88 100644 --- a/rpc/chain.go +++ b/rpc/chain.go @@ -36,7 +36,7 @@ func (provider *Provider) Syncing(ctx context.Context) (*SyncStatus, error) { var result interface{} // Note: []interface{}{}...force an empty `params[]` in the jsonrpc request if err := provider.c.CallContext(ctx, &result, "starknet_syncing", []interface{}{}...); err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } switch res := result.(type) { case bool: @@ -44,7 +44,7 @@ func (provider *Provider) Syncing(ctx context.Context) (*SyncStatus, error) { case SyncStatus: return &res, nil default: - return nil, Err(InternalError, RPCData{Message: "internal error with starknet_syncing"}) + return nil, Err(InternalError, &RPCData{Message: "internal error with starknet_syncing"}) } } diff --git a/rpc/contract.go b/rpc/contract.go index 954fea11..5653b59b 100644 --- a/rpc/contract.go +++ b/rpc/contract.go @@ -56,7 +56,7 @@ func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contract func typecastClassOutput(rawClass map[string]any) (ClassOutput, error) { rawClassByte, err := json.Marshal(rawClass) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } // if contract_class_version exists, then it's a ContractClass type @@ -64,14 +64,14 @@ func typecastClassOutput(rawClass map[string]any) (ClassOutput, error) { var contractClass ContractClass err = json.Unmarshal(rawClassByte, &contractClass) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return &contractClass, nil } var depContractClass DeprecatedContractClass err = json.Unmarshal(rawClassByte, &depContractClass) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return &depContractClass, nil } diff --git a/rpc/errors.go b/rpc/errors.go index fd54eb0c..999e81ee 100644 --- a/rpc/errors.go +++ b/rpc/errors.go @@ -4,7 +4,6 @@ import ( "encoding/json" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/starknet.go/utils" ) const ( @@ -22,7 +21,7 @@ const ( // - data: any data associated with the error. // Returns // - *RPCError: a pointer to an RPCError object. -func Err(code int, data RPCData) *RPCError { +func Err(code int, data *RPCData) *RPCError { switch code { case InvalidJSON: return &RPCError{Code: InvalidJSON, Message: "Parse error", Data: data} @@ -49,13 +48,13 @@ func Err(code int, data RPCData) *RPCError { func tryUnwrapToRPCErr(err error, rpcErrors ...*RPCError) *RPCError { errBytes, errIn := json.Marshal(err) if errIn != nil { - return Err(InternalError, RPCData{Message: errIn.Error()}) + return Err(InternalError, &RPCData{Message: errIn.Error()}) } var nodeErr RPCError errIn = json.Unmarshal(errBytes, &nodeErr) if errIn != nil { - return Err(InternalError, RPCData{Message: errIn.Error()}) + return Err(InternalError, &RPCData{Message: errIn.Error()}) } for _, rpcErr := range rpcErrors { @@ -65,61 +64,74 @@ func tryUnwrapToRPCErr(err error, rpcErrors ...*RPCError) *RPCError { } if nodeErr.Code == 0 { - return Err(InternalError, RPCData{Message: err.Error()}) + return Err(InternalError, &RPCData{Message: err.Error()}) } return Err(nodeErr.Code, nodeErr.Data) } type RPCError struct { - Code int `json:"code"` - Message string `json:"message"` - Data RPCData `json:"data,omitempty"` + Code int `json:"code"` + Message string `json:"message"` + Data *RPCData `json:"data,omitempty"` } func (e RPCError) Error() string { + if e.Data == nil { + return e.Message + } return e.Message + e.Data.Message } type RPCData struct { - Message string - ContractErrorData ContractErrorData `json:",omitempty"` - TransactionExecutionErrorData TransactionExecutionErrorData `json:",omitempty"` + Message string `json:",omitempty"` + ContractErrorData *ContractErrorData `json:",omitempty"` + TransactionExecutionErrorData *TransactionExecutionErrorData `json:",omitempty"` } func (rpcData *RPCData) UnmarshalJSON(data []byte) error { var message string - if err := json.Unmarshal(data, &message); err == nil { rpcData.Message = message return nil } - var rpcMap map[string]json.RawMessage - if err := json.Unmarshal(data, &rpcMap); err != nil { - return err + var contractErrData ContractErrorData + if err := json.Unmarshal(data, &contractErrData); err == nil { + *rpcData = RPCData{ + Message: rpcData.Message + contractErrData.RevertError.Message, + ContractErrorData: &contractErrData, + } + return nil } - message, err := utils.GetTypedFieldFromJSONMap[string](rpcMap, "Message") - if err == nil { - rpcData.Message = message + var txExErrData TransactionExecutionErrorData + if err := json.Unmarshal(data, &txExErrData); err == nil { + *rpcData = RPCData{ + Message: rpcData.Message + txExErrData.ExecutionError.Message, + TransactionExecutionErrorData: &txExErrData, + } return nil } - contractErrData, err := utils.GetTypedFieldFromJSONMap[ContractErrorData](rpcMap, "ContractErrorData") - if err != nil { - return err + return nil +} + +func (rpcData RPCData) MarshalJSON() ([]byte, error) { + var temp any + + if rpcData.ContractErrorData != nil { + temp = *rpcData.ContractErrorData + return json.Marshal(temp) } - rpcData.Message += contractErrData.RevertError.Message - rpcData.ContractErrorData = contractErrData - txExErrData, err := utils.GetTypedFieldFromJSONMap[TransactionExecutionErrorData](rpcMap, "ContractErrorData") - if err != nil { - return err + if rpcData.TransactionExecutionErrorData != nil { + temp = *rpcData.TransactionExecutionErrorData + return json.Marshal(temp) } - rpcData.Message += txExErrData.ExecutionError.Message - rpcData.TransactionExecutionErrorData = txExErrData - return nil + temp = rpcData.Message + + return json.Marshal(temp) } var ( diff --git a/rpc/mock_test.go b/rpc/mock_test.go index da08620c..e00a5d1e 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -837,7 +837,7 @@ func mock_starknet_addInvokeTransaction(result interface{}, args ...interface{}) if invokeTx.SenderAddress != nil { if invokeTx.SenderAddress.Equal(new(felt.Felt).SetUint64(123)) { unexpErr := *ErrUnexpectedError - unexpErr.Data = RPCData{Message: "Something crazy happened"} + unexpErr.Data = &RPCData{Message: "Something crazy happened"} return &unexpErr } } @@ -1382,7 +1382,7 @@ func mock_starknet_traceTransaction(result interface{}, args ...interface{}) err return &RPCError{ Code: 10, Message: "No trace available for transaction", - Data: RPCData{Message: "REJECTED"}, + Data: &RPCData{Message: "REJECTED"}, } default: return ErrHashNotFound diff --git a/rpc/spy_test.go b/rpc/spy_test.go index c0fd3141..55ae6799 100644 --- a/rpc/spy_test.go +++ b/rpc/spy_test.go @@ -103,7 +103,7 @@ func (s *spy) Compare(o interface{}, debug bool) (string, error) { } b, err := json.Marshal(o) if err != nil { - return "", Err(InternalError, RPCData{Message: err.Error()}) + return "", Err(InternalError, &RPCData{Message: err.Error()}) } diff, _ := jsondiff.Compare(s.s, b, &jsondiff.Options{}) if debug { diff --git a/rpc/trace.go b/rpc/trace.go index bce41047..ad1d189e 100644 --- a/rpc/trace.go +++ b/rpc/trace.go @@ -24,7 +24,7 @@ func (provider *Provider) TraceTransaction(ctx context.Context, transactionHash rawTraceByte, err := json.Marshal(rawTxnTrace) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } switch rawTxnTrace["type"] { @@ -32,32 +32,32 @@ func (provider *Provider) TraceTransaction(ctx context.Context, transactionHash var trace InvokeTxnTrace err = json.Unmarshal(rawTraceByte, &trace) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return trace, nil case string(TransactionType_Declare): var trace DeclareTxnTrace err = json.Unmarshal(rawTraceByte, &trace) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return trace, nil case string(TransactionType_DeployAccount): var trace DeployAccountTxnTrace err = json.Unmarshal(rawTraceByte, &trace) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return trace, nil case string(TransactionType_L1Handler): var trace L1HandlerTxnTrace err = json.Unmarshal(rawTraceByte, &trace) if err != nil { - return nil, Err(InternalError, RPCData{Message: err.Error()}) + return nil, Err(InternalError, &RPCData{Message: err.Error()}) } return trace, nil } - return nil, Err(InternalError, RPCData{Message: "Unknown transaction type"}) + return nil, Err(InternalError, &RPCData{Message: "Unknown transaction type"}) } diff --git a/rpc/trace_test.go b/rpc/trace_test.go index 73db25de..6ab8ebd3 100644 --- a/rpc/trace_test.go +++ b/rpc/trace_test.go @@ -53,7 +53,7 @@ func TestTransactionTrace(t *testing.T) { ExpectedError: &RPCError{ Code: 10, Message: "No trace available for transaction", - Data: RPCData{Message: "REJECTED"}, + Data: &RPCData{Message: "REJECTED"}, }, }, }, diff --git a/rpc/version.go b/rpc/version.go index bdd837d4..c0aba2a0 100644 --- a/rpc/version.go +++ b/rpc/version.go @@ -9,7 +9,7 @@ func (provider *Provider) SpecVersion(ctx context.Context) (string, error) { var result string err := do(ctx, provider.c, "starknet_specVersion", &result) if err != nil { - return "", Err(InternalError, RPCData{Message: err.Error()}) + return "", Err(InternalError, &RPCData{Message: err.Error()}) } return result, nil } diff --git a/rpc/write_test.go b/rpc/write_test.go index 8c2db88b..1dcb3b3f 100644 --- a/rpc/write_test.go +++ b/rpc/write_test.go @@ -75,7 +75,7 @@ func TestAddInvokeTransaction(t *testing.T) { ExpectedError: &RPCError{ Code: ErrUnexpectedError.Code, Message: ErrUnexpectedError.Message, - Data: RPCData{Message: "Something crazy happened"}, + Data: &RPCData{Message: "Something crazy happened"}, }, }, {