Skip to content

Commit

Permalink
Creates RPCData Marshal func
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Nov 6, 2024
1 parent 640dea3 commit e578814
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 53 deletions.
2 changes: 1 addition & 1 deletion account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions rpc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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:
return &SyncStatus{SyncStatus: &res}, nil
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"})
}

}
6 changes: 3 additions & 3 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ 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
if _, exists := (rawClass)["contract_class_version"]; exists {
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
}
Expand Down
70 changes: 41 additions & 29 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/utils"
)

const (
Expand All @@ -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}
Expand All @@ -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 {
Expand All @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rpc/spy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions rpc/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,40 @@ 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"] {
case string(TransactionType_Invoke):
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"})

}

Expand Down
2 changes: 1 addition & 1 deletion rpc/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion rpc/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion rpc/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
},
{
Expand Down

0 comments on commit e578814

Please sign in to comment.