Skip to content

Commit

Permalink
fix marshalling error in TransactionReceiptWithBlockInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rian authored and AryanGodara committed May 6, 2024
1 parent 22aa10c commit 7a3f264
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 88 deletions.
9 changes: 4 additions & 5 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"jsonrpc": "2.0",
"result": {
"actual_fee": {
"amount": "0x16409a78a10b00",
"unit": "FRI"
Expand Down Expand Up @@ -45,5 +43,4 @@
"messages_sent": [],
"transaction_hash": "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b",
"type": "INVOKE"
}
}
25 changes: 15 additions & 10 deletions rpc/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,19 @@ 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"),
},
},
},
"testnet": {
{
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"),
},
},
},
Expand All @@ -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]
Expand All @@ -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)
}
}

Expand Down
156 changes: 86 additions & 70 deletions rpc/types_transaction_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit 7a3f264

Please sign in to comment.