From 146df3b72daacca298e34a1fe60225fb0fc81596 Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Mon, 19 Aug 2024 10:46:43 -0300 Subject: [PATCH] Fixes missing tx_hash field from TestTransactionByBlockIdAndIndex return --- mocks/mock_rpc_provider.go | 4 +- rpc/mock_test.go | 2 +- rpc/provider.go | 2 +- rpc/transaction.go | 80 ++------------------------------------ rpc/transaction_test.go | 27 +++++-------- 5 files changed, 17 insertions(+), 98 deletions(-) diff --git a/mocks/mock_rpc_provider.go b/mocks/mock_rpc_provider.go index eedfd486..130e9c2b 100644 --- a/mocks/mock_rpc_provider.go +++ b/mocks/mock_rpc_provider.go @@ -432,10 +432,10 @@ func (mr *MockRpcProviderMockRecorder) TraceTransaction(ctx, transactionHash any } // TransactionByBlockIdAndIndex mocks base method. -func (m *MockRpcProvider) TransactionByBlockIdAndIndex(ctx context.Context, blockID rpc.BlockID, index uint64) (rpc.Transaction, error) { +func (m *MockRpcProvider) TransactionByBlockIdAndIndex(ctx context.Context, blockID rpc.BlockID, index uint64) (*rpc.BlockTransaction, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "TransactionByBlockIdAndIndex", ctx, blockID, index) - ret0, _ := ret[0].(rpc.Transaction) + ret0, _ := ret[0].(*rpc.BlockTransaction) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/rpc/mock_test.go b/rpc/mock_test.go index 58f7c3fc..8a37ee56 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -197,7 +197,7 @@ func mock_starknet_getTransactionByBlockIdAndIndex(result interface{}, method st return errWrongArgs } - var InvokeTxnV3example InvokeTxnV3 + var InvokeTxnV3example BlockInvokeTxnV3 read, err := os.ReadFile("tests/transactions/sepoliaTx_0x6a4a9c4f1a530f7d6dd7bba9b71f090a70d1e3bbde80998fde11a08aab8b282.json") if err != nil { return err diff --git a/rpc/provider.go b/rpc/provider.go index 3f1bbf30..537d3996 100644 --- a/rpc/provider.go +++ b/rpc/provider.go @@ -67,7 +67,7 @@ type RpcProvider interface { SpecVersion(ctx context.Context) (string, error) Syncing(ctx context.Context) (*SyncStatus, error) TraceBlockTransactions(ctx context.Context, blockID BlockID) ([]Trace, error) - TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (Transaction, error) + TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (*BlockTransaction, error) TransactionByHash(ctx context.Context, hash *felt.Felt) (*BlockTransaction, error) TransactionReceipt(ctx context.Context, transactionHash *felt.Felt) (*TransactionReceiptWithBlockInfo, error) TraceTransaction(ctx context.Context, transactionHash *felt.Felt) (TxnTrace, error) diff --git a/rpc/transaction.go b/rpc/transaction.go index 7cddd6e6..7d2743e4 100644 --- a/rpc/transaction.go +++ b/rpc/transaction.go @@ -2,79 +2,10 @@ package rpc import ( "context" - "encoding/json" - "fmt" "github.com/NethermindEth/juno/core/felt" ) -var ( - feltZero = new(felt.Felt).SetUint64(0) - feltOne = new(felt.Felt).SetUint64(1) - feltTwo = new(felt.Felt).SetUint64(2) - feltThree = new(felt.Felt).SetUint64(3) -) - -// adaptTransaction adapts a TXN to a Transaction and returns it, along with any error encountered. -// -// Parameters: -// - t: the TXN to be adapted to a Transaction -// Returns: -// - Transaction: a Transaction -// - error: an error if the adaptation failed. -func adaptTransaction(t TXN) (Transaction, error) { - txMarshalled, err := json.Marshal(t) - if err != nil { - return nil, Err(InternalError, err) - } - switch t.Type { - case TransactionType_Invoke: - switch { - case t.Version.Equal(feltZero): - var tx InvokeTxnV0 - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - case t.Version.Equal(feltOne): - var tx InvokeTxnV1 - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - case t.Version.Equal(feltThree): - var tx InvokeTxnV3 - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - } - case TransactionType_Declare: - switch { - case t.Version.Equal(feltZero): - var tx DeclareTxnV0 - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - case t.Version.Equal(feltOne): - var tx DeclareTxnV1 - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - case t.Version.Equal(feltTwo): - var tx DeclareTxnV2 - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - } - case TransactionType_Deploy: - var tx DeployTxn - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - case TransactionType_DeployAccount: - var tx DeployAccountTxn - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - case TransactionType_L1Handler: - var tx L1HandlerTxn - err := json.Unmarshal(txMarshalled, &tx) - return tx, err - } - return nil, Err(InternalError, fmt.Sprint("internal error with adaptTransaction() : unknown transaction type ", t.Type)) - -} - // TransactionByHash retrieves the details and status of a transaction by its hash. // // Parameters: @@ -84,7 +15,6 @@ func adaptTransaction(t TXN) (Transaction, error) { // - BlockTransaction: The retrieved Transaction // - error: An error if any func (provider *Provider) TransactionByHash(ctx context.Context, hash *felt.Felt) (*BlockTransaction, error) { - // todo: update to return a custom Transaction type, then use adapt function var tx BlockTransaction if err := do(ctx, provider.c, "starknet_getTransactionByHash", &tx, hash); err != nil { return nil, tryUnwrapToRPCErr(err, ErrHashNotFound) @@ -99,16 +29,14 @@ func (provider *Provider) TransactionByHash(ctx context.Context, hash *felt.Felt // - blockID: The ID of the block containing the transaction. // - index: The index of the transaction within the block. // Returns: -// - Transaction: The retrieved Transaction object +// - BlockTransaction: The retrieved Transaction object // - error: An error, if any -func (provider *Provider) TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (Transaction, error) { - var tx TXN +func (provider *Provider) TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (*BlockTransaction, error) { + var tx BlockTransaction if err := do(ctx, provider.c, "starknet_getTransactionByBlockIdAndIndex", &tx, blockID, index); err != nil { - return nil, tryUnwrapToRPCErr(err, ErrInvalidTxnIndex, ErrBlockNotFound) - } - return adaptTransaction(tx) + return &tx, nil } // TransactionReceipt fetches the transaction receipt for a given transaction hash. diff --git a/rpc/transaction_test.go b/rpc/transaction_test.go index 0695cc1d..a674738a 100644 --- a/rpc/transaction_test.go +++ b/rpc/transaction_test.go @@ -66,7 +66,7 @@ func TestTransactionByHash(t *testing.T) { txCasted, ok := (tx.IBlockTransaction).(BlockDeclareTxnV2) require.True(t, ok) - require.Equal(t, txCasted, test.ExpectedTxn.IBlockTransaction) + require.Equal(t, test.ExpectedTxn.IBlockTransaction, txCasted) } } @@ -90,10 +90,10 @@ func TestTransactionByBlockIdAndIndex(t *testing.T) { type testSetType struct { BlockID BlockID Index uint64 - ExpectedTxn Transaction + ExpectedTxn BlockTransaction } - var InvokeTxnV3example InvokeTxnV3 + var InvokeTxnV3example BlockTransaction read, err := os.ReadFile("tests/transactions/sepoliaTx_0x6a4a9c4f1a530f7d6dd7bba9b71f090a70d1e3bbde80998fde11a08aab8b282.json") require.NoError(t, err) err = json.Unmarshal(read, &InvokeTxnV3example) @@ -117,22 +117,13 @@ func TestTransactionByBlockIdAndIndex(t *testing.T) { "mainnet": {}, }[testEnv] for _, test := range testSet { - spy := NewSpy(testConfig.provider.c) - testConfig.provider.c = spy + tx, err := testConfig.provider.TransactionByBlockIdAndIndex(context.Background(), test.BlockID, test.Index) - if err != nil { - t.Fatal(err) - } - if tx == nil { - t.Fatal("transaction should exist") - } - txCasted, ok := (tx).(InvokeTxnV3) - if !ok { - t.Fatalf("transaction should be InvokeTxnV3, instead %T", tx) - } - - require.Equal(t, txCasted.Type, TransactionType_Invoke) - require.Equal(t, txCasted, test.ExpectedTxn) + require.NoError(t, err) + require.NotNil(t, tx) + txCasted, ok := (tx.IBlockTransaction).(BlockInvokeTxnV3) + require.True(t, ok) + require.Equal(t, test.ExpectedTxn.IBlockTransaction, txCasted) } }