Skip to content

Commit

Permalink
Fixes missing tx_hash field from TestTransactionByBlockIdAndIndex return
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Aug 19, 2024
1 parent 55c86dc commit 146df3b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 98 deletions.
4 changes: 2 additions & 2 deletions mocks/mock_rpc_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
80 changes: 4 additions & 76 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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.
Expand Down
27 changes: 9 additions & 18 deletions rpc/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand All @@ -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)
}
}

Expand Down

0 comments on commit 146df3b

Please sign in to comment.