Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix eth_getTransactionByBlockNumberAndIndex #1877

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const (
getL2BlockByNumberSQL = "SELECT header, uncles, received_at FROM state.l2block b WHERE b.block_num = $1"
getL2BlockHeaderByNumberSQL = "SELECT header FROM state.l2block b WHERE b.block_num = $1"
getTransactionByHashSQL = "SELECT transaction.encoded FROM state.transaction WHERE hash = $1"
getTransactionByL2BlockHashAndIndexSQL = "SELECT t.encoded FROM state.transaction t INNER JOIN state.l2block b ON t.l2_block_num = b.batch_num WHERE b.block_hash = $1 AND 0 = $2"
getTransactionByL2BlockNumberAndIndexSQL = "SELECT t.encoded FROM state.transaction t WHERE t.l2_block_num = $1 AND 0 = $2"
getL2BlockTransactionCountByHashSQL = "SELECT COUNT(*) FROM state.transaction t INNER JOIN state.l2block b ON b.block_num = t.l2_block_num WHERE b.block_hash = $1"
getL2BlockTransactionCountByNumberSQL = "SELECT COUNT(*) FROM state.transaction t WHERE t.l2_block_num = $1"
Expand Down Expand Up @@ -1245,7 +1244,16 @@ func (p *PostgresStorage) GetTransactionReceipt(ctx context.Context, transaction
func (p *PostgresStorage) GetTransactionByL2BlockHashAndIndex(ctx context.Context, blockHash common.Hash, index uint64, dbTx pgx.Tx) (*types.Transaction, error) {
var encoded string
q := p.getExecQuerier(dbTx)
err := q.QueryRow(ctx, getTransactionByL2BlockHashAndIndexSQL, blockHash.String(), index).Scan(&encoded)
const query = `
SELECT t.encoded
FROM state.transaction t
INNER JOIN state.l2block b
ON t.l2_block_num = b.block_num
INNER JOIN state.receipt r
ON r.tx_hash = t.hash
WHERE b.block_hash = $1
AND r.tx_index = $2`
err := q.QueryRow(ctx, query, blockHash.String(), index).Scan(&encoded)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
} else if err != nil {
Expand Down
46 changes: 34 additions & 12 deletions test/e2e/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,25 +513,47 @@ func Test_Transactions(t *testing.T) {
defer Teardown()
for _, network := range networks {
log.Infof("Network %s", network.Name)
client, err := ethclient.Dial(network.URL)
ethClient, err := ethclient.Dial(network.URL)
require.NoError(t, err)
auth, err := operations.GetAuth(network.PrivateKey, network.ChainID)
require.NoError(t, err)

// Test Case: Successful transfer
tx, err := createTX(client, auth, toAddress, big.NewInt(100000))
tx, err := createTX(ethClient, auth, toAddress, big.NewInt(100000))
require.NoError(t, err)
err = operations.WaitTxToBeMined(ctx, client, tx, operations.DefaultTimeoutTxToBeMined)
err = operations.WaitTxToBeMined(ctx, ethClient, tx, operations.DefaultTimeoutTxToBeMined)
require.NoError(t, err)

// Test Case: get transaction by block number and index
receipt, err := ethClient.TransactionReceipt(ctx, tx.Hash())
require.NoError(t, err)
require.NotNil(t, receipt)
res, err := client.JSONRPCCall(network.URL, "eth_getTransactionByBlockNumberAndIndex", hex.EncodeBig(receipt.BlockNumber), hex.EncodeUint64(uint64(receipt.TransactionIndex)))
require.NoError(t, err)
require.Nil(t, res.Error)
require.NotNil(t, res.Result)
var txByBlockNumberAndIndex *types.Transaction
err = json.Unmarshal(res.Result, &txByBlockNumberAndIndex)
require.NoError(t, err)

require.Equal(t, tx.Hash().String(), txByBlockNumberAndIndex.Hash.String())

// Test Case: get transaction by block hash and index
receipt, err = ethClient.TransactionReceipt(ctx, tx.Hash())
require.NoError(t, err)
require.NotNil(t, receipt)
txByBlockHashAndIndex, err := ethClient.TransactionInBlock(ctx, receipt.BlockHash, receipt.TransactionIndex)
require.NoError(t, err)
require.Equal(t, tx.Hash().String(), txByBlockHashAndIndex.Hash().String())

// Setup for test cases
nonce, err := client.NonceAt(context.Background(), auth.From, nil)
nonce, err := ethClient.NonceAt(context.Background(), auth.From, nil)
require.NoError(t, err)

gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{From: auth.From, To: &toAddress, Value: big.NewInt(10000)})
gasLimit, err := ethClient.EstimateGas(context.Background(), ethereum.CallMsg{From: auth.From, To: &toAddress, Value: big.NewInt(10000)})
require.NoError(t, err)

gasPrice, err := client.SuggestGasPrice(context.Background())
gasPrice, err := ethClient.SuggestGasPrice(context.Background())
require.NoError(t, err)

// Test Case: TX with invalid nonce
Expand All @@ -542,7 +564,7 @@ func Test_Transactions(t *testing.T) {
require.NoError(t, err)

log.Infof("Sending Tx %v Nonce (invalid) %v", signedTx.Hash(), signedTx.Nonce())
err = client.SendTransaction(context.Background(), signedTx)
err = ethClient.SendTransaction(context.Background(), signedTx)
require.ErrorContains(t, err, "nonce too low")

// End Test Case
Expand All @@ -556,16 +578,16 @@ func Test_Transactions(t *testing.T) {
GasPrice: gasPrice,
Data: nil,
})
err = client.SendTransaction(context.Background(), invalidTx)
err = ethClient.SendTransaction(context.Background(), invalidTx)
require.Error(t, err)
// End Test Case

// Test Case: TX with amount being higher than balance

balance, err := client.BalanceAt(context.Background(), auth.From, nil)
balance, err := ethClient.BalanceAt(context.Background(), auth.From, nil)
require.NoError(t, err)

nonce, err = client.NonceAt(context.Background(), auth.From, nil)
nonce, err = ethClient.NonceAt(context.Background(), auth.From, nil)
require.NoError(t, err)

log.Infof("Balance: %d", balance)
Expand All @@ -575,14 +597,14 @@ func Test_Transactions(t *testing.T) {
require.NoError(t, err)

log.Infof("Sending Tx %v Nonce %v", signedTx.Hash(), signedTx.Nonce())
err = client.SendTransaction(context.Background(), signedTx)
err = ethClient.SendTransaction(context.Background(), signedTx)
require.ErrorContains(t, err, pool.ErrInsufficientFunds.Error())

// no contract code at given address test

// deploy contract with not enough gas for storage, just execution
address := common.HexToAddress("0xDEADBEEF596a836C9063a7EE35dA94DDA3b57B62")
instance, err := Double.NewDouble(address, client)
instance, err := Double.NewDouble(address, ethClient)
require.NoError(t, err)

callOpts := &bind.CallOpts{Pending: false}
Expand Down