From 7a3f26451ae412baee6e94cc53ed1f5630449e9f Mon Sep 17 00:00:00 2001 From: rian Date: Fri, 26 Apr 2024 13:10:46 +0300 Subject: [PATCH] fix marshalling error in TransactionReceiptWithBlockInfo --- rpc/mock_test.go | 9 +- ...60c39d3a668fe12f117ecedb9749466d8352b.json | 3 - rpc/transaction_test.go | 25 +-- rpc/types_transaction_receipt.go | 156 ++++++++++-------- 4 files changed, 105 insertions(+), 88 deletions(-) diff --git a/rpc/mock_test.go b/rpc/mock_test.go index ca802ee7..0a2cf5fb 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -337,7 +337,6 @@ func mock_starknet_getTransactionReceipt(result interface{}, method string, args if !ok || r == nil { return errWrongType } - fmt.Printf("%T, %d", result, len(args)) if len(args) != 1 { return errWrongArgs } @@ -350,18 +349,18 @@ func mock_starknet_getTransactionReceipt(result interface{}, method string, args } if arg0Felt.Equal(testTxnHash) { - var txnRec struct { - Result UnknownTransactionReceipt `json:"result"` - } + var txnRec TransactionReceiptWithBlockInfo read, err := os.ReadFile("tests/receipt/0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b.json") if err != nil { return err } + err = json.Unmarshal(read, &txnRec) if err != nil { return err } - txnReceipt, err := json.Marshal(txnRec.Result.TransactionReceipt) + + txnReceipt, err := json.Marshal(txnRec) if err != nil { return err } diff --git a/rpc/tests/receipt/0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b.json b/rpc/tests/receipt/0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b.json index 9c97b0bf..2807b737 100644 --- a/rpc/tests/receipt/0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b.json +++ b/rpc/tests/receipt/0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b.json @@ -1,6 +1,4 @@ { - "jsonrpc": "2.0", - "result": { "actual_fee": { "amount": "0x16409a78a10b00", "unit": "FRI" @@ -45,5 +43,4 @@ "messages_sent": [], "transaction_hash": "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b", "type": "INVOKE" - } } \ No newline at end of file diff --git a/rpc/transaction_test.go b/rpc/transaction_test.go index 526883f1..47dceb72 100644 --- a/rpc/transaction_test.go +++ b/rpc/transaction_test.go @@ -285,9 +285,9 @@ func TestTransactionReceipt(t *testing.T) { { TxnHash: utils.TestHexToFelt(t, "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b"), ExpectedResp: TransactionReceiptWithBlockInfo{ - TransactionReceipt: receiptTxn52767_16, - BlockNumber: 52767, - BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), + UnknownTransactionReceipt: UnknownTransactionReceipt{receiptTxn52767_16}, + BlockNumber: 52767, + BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), }, }, }, @@ -295,9 +295,9 @@ func TestTransactionReceipt(t *testing.T) { { TxnHash: utils.TestHexToFelt(t, "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b"), ExpectedResp: TransactionReceiptWithBlockInfo{ - TransactionReceipt: receiptTxn52767_16, - BlockNumber: 52767, - BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), + UnknownTransactionReceipt: UnknownTransactionReceipt{receiptTxn52767_16}, + BlockNumber: 52767, + BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), }, }, }, @@ -306,9 +306,9 @@ func TestTransactionReceipt(t *testing.T) { { TxnHash: utils.TestHexToFelt(t, "0x49728601e0bb2f48ce506b0cbd9c0e2a9e50d95858aa41463f46386dca489fd"), ExpectedResp: TransactionReceiptWithBlockInfo{ - TransactionReceipt: receiptTxnIntegration, - BlockNumber: 319132, - BlockHash: utils.TestHexToFelt(t, "0x50e864db6b81ce69fbeb70e6a7284ee2febbb9a2e707415de7adab83525e9cd"), + UnknownTransactionReceipt: UnknownTransactionReceipt{receiptTxnIntegration}, + BlockNumber: 319132, + BlockHash: utils.TestHexToFelt(t, "0x50e864db6b81ce69fbeb70e6a7284ee2febbb9a2e707415de7adab83525e9cd"), }, }, }}[testEnv] @@ -317,8 +317,13 @@ func TestTransactionReceipt(t *testing.T) { spy := NewSpy(testConfig.provider.c) testConfig.provider.c = spy txReceiptWithBlockInfo, err := testConfig.provider.TransactionReceipt(context.Background(), test.TxnHash) + // To inspect the data + // qwe, _ := json.MarshalIndent(txReceiptWithBlockInfo, "", "") + // fmt.Println(string(qwe)) + // wer, _ := json.MarshalIndent(test.ExpectedResp, "", "") + // fmt.Println(string(wer)) require.Nil(t, err) - require.Equal(t, test.ExpectedResp, txReceiptWithBlockInfo) + require.Equal(t, test.ExpectedResp, *txReceiptWithBlockInfo) } } diff --git a/rpc/types_transaction_receipt.go b/rpc/types_transaction_receipt.go index 9e06f0b0..d4dcd336 100644 --- a/rpc/types_transaction_receipt.go +++ b/rpc/types_transaction_receipt.go @@ -37,26 +37,10 @@ type CommonTransactionReceipt struct { ExecutionResources ExecutionResources `json:"execution_resources"` } -// Hash returns the transaction hash associated with the CommonTransactionReceipt. -// -// Parameters: -// -// none -// -// Returns: -// - *felt.Felt: the transaction hash func (tr CommonTransactionReceipt) Hash() *felt.Felt { return tr.TransactionHash } -// GetExecutionStatus returns the execution status of the CommonTransactionReceipt. -// -// Parameters: -// -// none -// -// Returns: -// - TxnExecutionStatus: the execution status func (tr CommonTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { return tr.ExecutionStatus } @@ -130,26 +114,10 @@ func (tt TransactionType) MarshalJSON() ([]byte, error) { // InvokeTransactionReceipt Invoke Transaction Receipt type InvokeTransactionReceipt CommonTransactionReceipt -// Hash returns the hash of the invoke transaction receipt. -// -// Parameters: -// -// none -// -// Returns: -// - *felt.Felt: the transaction hash func (tr InvokeTransactionReceipt) Hash() *felt.Felt { return tr.TransactionHash } -// GetExecutionStatus returns the execution status of the InvokeTransactionReceipt. -// -// Parameters: -// -// none -// -// Returns: -// - TxnExecutionStatus: the execution status func (tr InvokeTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { return tr.ExecutionStatus } @@ -366,8 +334,11 @@ func (tr *UnknownTransactionReceipt) UnmarshalJSON(data []byte) error { if err != nil { return err } - - t, err := unmarshalTransactionReceipt(dec) + bytes, err := json.Marshal(dec) + if err != nil { + return err + } + t, err := unmarshalTransactionReceipt(bytes) if err != nil { return err } @@ -382,43 +353,47 @@ func (tr *UnknownTransactionReceipt) UnmarshalJSON(data []byte) error { // Returns: // - TransactionReceipt: a TransactionReceipt // - error: an error if the unmarshaling fails -func unmarshalTransactionReceipt(t interface{}) (TransactionReceipt, error) { - switch casted := t.(type) { - case map[string]interface{}: - // NOTE(tvanas): Pathfinder 0.3.3 does not return - // transaction receipt types. We handle this by - // naively marshalling into an invoke type. Once it - // is supported, this condition can be removed. - typ, ok := casted["type"] - if !ok { - return nil, fmt.Errorf("unknown transaction type: %v", t) - } - - switch TransactionType(typ.(string)) { - case TransactionType_Invoke: - var txn InvokeTransactionReceipt - err := remarshal(casted, &txn) - return txn, err - case TransactionType_L1Handler: - var txn L1HandlerTransactionReceipt - err := remarshal(casted, &txn) - return txn, err - case TransactionType_Declare: - var txn DeclareTransactionReceipt - err := remarshal(casted, &txn) - return txn, err - case TransactionType_Deploy: - var txn DeployTransactionReceipt - err := remarshal(casted, &txn) - return txn, err - case TransactionType_DeployAccount: - var txn DeployAccountTransactionReceipt - err := remarshal(casted, &txn) - return txn, err - } +func unmarshalTransactionReceipt(data []byte) (TransactionReceipt, error) { + + var dec map[string]interface{} + if err := json.Unmarshal(data, &dec); err != nil { + return nil, err } - return nil, fmt.Errorf("unknown transaction type: %v", t) + typ, ok := dec["type"] + if !ok { + return nil, fmt.Errorf("unknown transaction type: %v", typ) + } + + jsonData, err := json.Marshal(dec) + if err != nil { + return nil, err + } + + switch TransactionType(typ.(string)) { + case TransactionType_Invoke: + var txn InvokeTransactionReceipt + err := json.Unmarshal(jsonData, &txn) + return txn, err + case TransactionType_L1Handler: + var txn L1HandlerTransactionReceipt + err := json.Unmarshal(jsonData, &txn) + return txn, err + case TransactionType_Declare: + var txn DeclareTransactionReceipt + err := json.Unmarshal(jsonData, &txn) + return txn, err + case TransactionType_Deploy: + var txn DeployTransactionReceipt + err := json.Unmarshal(jsonData, &txn) + return txn, err + case TransactionType_DeployAccount: + var txn DeployAccountTransactionReceipt + err := json.Unmarshal(jsonData, &txn) + return txn, err + } + + return nil, fmt.Errorf("unknown transaction type: %v", typ) } // The finality status of the transaction, including the case the txn is still in the mempool or failed validation during the block construction phase @@ -437,7 +412,48 @@ type TxnStatusResp struct { } type TransactionReceiptWithBlockInfo struct { - TransactionReceipt + UnknownTransactionReceipt BlockHash *felt.Felt `json:"block_hash,omitempty"` BlockNumber uint `json:"block_number,omitempty"` } + +func (t *TransactionReceiptWithBlockInfo) UnmarshalJSON(data []byte) error { + var uTxnRec UnknownTransactionReceipt + err := uTxnRec.UnmarshalJSON(data) + if err != nil { + return err + } + t.UnknownTransactionReceipt = uTxnRec + + aux := &struct { + BlockHash string `json:"block_hash,omitempty"` + BlockNumber uint `json:"block_number,omitempty"` + }{} + + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + blockHash, err := new(felt.Felt).SetString(aux.BlockHash) + if err != nil { + return err + } + + t.BlockHash = blockHash + t.BlockNumber = aux.BlockNumber + + return nil +} + +func (t *TransactionReceiptWithBlockInfo) MarshalJSON() ([]byte, error) { + aux := &struct { + TransactionReceipt + BlockHash string `json:"block_hash,omitempty"` + BlockNumber uint `json:"block_number,omitempty"` + }{ + TransactionReceipt: t.UnknownTransactionReceipt.TransactionReceipt, + BlockHash: t.BlockHash.String(), + BlockNumber: t.BlockNumber, + } + + return json.Marshal(aux) +}