Skip to content

Commit

Permalink
GetTransactionByHash tests and rawdb call replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca30 committed Jan 12, 2025
1 parent 41134d0 commit 828fc00
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 38 deletions.
12 changes: 9 additions & 3 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,10 @@ func (api *BlockChainAPI) GetTransactionReceiptsByBlock(ctx context.Context, blo

var txHash common.Hash

borReceipt := rawdb.ReadBorReceipt(api.b.ChainDb(), block.Hash(), block.NumberU64(), api.b.ChainConfig())
borReceipt, err := api.b.GetBorBlockReceipt(ctx, block.Hash())
if err != nil {
return nil, err
}
if borReceipt != nil {
receipts = append(receipts, borReceipt)

Expand Down Expand Up @@ -1115,7 +1118,10 @@ func (api *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rp
result[i] = marshalReceipt(receipt, block.Hash(), block.NumberU64(), signer, txs[i], i, false)
}

stateSyncReceipt := rawdb.ReadBorReceipt(api.b.ChainDb(), block.Hash(), block.NumberU64(), api.b.ChainConfig())
stateSyncReceipt, err := api.b.GetBorBlockReceipt(ctx, block.Hash())
if err != nil {
return nil, err
}
if stateSyncReceipt != nil {
tx, _, _, _ := rawdb.ReadBorTransaction(api.b.ChainDb(), stateSyncReceipt.TxHash)
result = append(result, marshalReceipt(stateSyncReceipt, block.Hash(), block.NumberU64(), signer, tx, len(result), true))
Expand Down Expand Up @@ -1901,7 +1907,7 @@ func (api *TransactionAPI) getAllBlockTransactions(ctx context.Context, block *t

stateSyncPresent := false

borReceipt := rawdb.ReadBorReceipt(api.b.ChainDb(), block.Hash(), block.NumberU64(), api.b.ChainConfig())
borReceipt, _ := api.b.GetBorBlockReceipt(ctx, block.Hash())
if borReceipt != nil {
txHash := types.GetDerivedBorTxHash(types.BorReceiptKey(block.Number().Uint64(), block.Hash()))
if txHash != (common.Hash{}) {
Expand Down
97 changes: 62 additions & 35 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,10 @@ func (b testBackend) GetTransaction(ctx context.Context, txHash common.Hash) (bo
}
return found, tx, blockHash, blockNumber, index, nil
}
func (b testBackend) GetPoolTransactions() (types.Transactions, error) { panic("implement me") }
func (b testBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction { panic("implement me") }
func (b testBackend) GetPoolTransactions() (types.Transactions, error) { panic("implement me") }
func (b testBackend) GetPoolTransaction(txHash common.Hash) *types.Transaction {
return nil
}
func (b testBackend) GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) {
return 0, nil
}
Expand Down Expand Up @@ -1907,7 +1909,10 @@ func TestRPCGetBlockOrHeader(t *testing.T) {
}
}

func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Hash) {
func setupBackendWithTxs(t *testing.T, genBlocks int) (*testBackend, []common.Hash, []struct {
txHash common.Hash
file string
}) {
config := *params.TestChainConfig

config.ShanghaiBlock = big.NewInt(0)
Expand Down Expand Up @@ -2005,38 +2010,6 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha

txHashes[genBlocks] = mockStateSyncTxOnCurrentBlock(t, backend)

return backend, txHashes
}

func mockStateSyncTxOnCurrentBlock(t *testing.T, backend *testBackend) common.Hash {
// State Sync Tx Setup
var stateSyncLogs []*types.Log
block, err := backend.BlockByHash(context.Background(), backend.CurrentBlock().Hash())
if err != nil {
t.Errorf("failed to get current block: %v", err)
}

types.DeriveFieldsForBorLogs(stateSyncLogs, block.Hash(), block.NumberU64(), 0, 0)

// Write bor receipt
rawdb.WriteBorReceipt(backend.ChainDb(), block.Hash(), block.NumberU64(), &types.ReceiptForStorage{
Status: types.ReceiptStatusSuccessful, // make receipt status successful
Logs: stateSyncLogs,
})

// Write bor tx reverse lookup
rawdb.WriteBorTxLookupEntry(backend.ChainDb(), block.Hash(), block.NumberU64())
return types.GetDerivedBorTxHash(types.BorReceiptKey(block.NumberU64(), block.Hash()))
}

func TestRPCGetTransactionReceipt(t *testing.T) {
t.Parallel()

var (
backend, txHashes = setupReceiptBackend(t, 6)
api = NewTransactionAPI(backend, new(AddrLocker))
)

var testSuite = []struct {
txHash common.Hash
file string
Expand Down Expand Up @@ -2088,6 +2061,38 @@ func TestRPCGetTransactionReceipt(t *testing.T) {
},
}

return backend, txHashes, testSuite
}

func mockStateSyncTxOnCurrentBlock(t *testing.T, backend *testBackend) common.Hash {
// State Sync Tx Setup
var stateSyncLogs []*types.Log
block, err := backend.BlockByHash(context.Background(), backend.CurrentBlock().Hash())
if err != nil {
t.Errorf("failed to get current block: %v", err)
}

types.DeriveFieldsForBorLogs(stateSyncLogs, block.Hash(), block.NumberU64(), 0, 0)

// Write bor receipt
rawdb.WriteBorReceipt(backend.ChainDb(), block.Hash(), block.NumberU64(), &types.ReceiptForStorage{
Status: types.ReceiptStatusSuccessful, // make receipt status successful
Logs: stateSyncLogs,
})

// Write bor tx reverse lookup
rawdb.WriteBorTxLookupEntry(backend.ChainDb(), block.Hash(), block.NumberU64())
return types.GetDerivedBorTxHash(types.BorReceiptKey(block.NumberU64(), block.Hash()))
}

func TestRPCGetTransactionReceipt(t *testing.T) {
t.Parallel()

var (
backend, _, testSuite = setupBackendWithTxs(t, 6)
api = NewTransactionAPI(backend, new(AddrLocker))
)

for i, tt := range testSuite {
var (
result interface{}
Expand Down Expand Up @@ -2391,3 +2396,25 @@ func TestRPCGetTransactionReceiptsByBlock(t *testing.T) {
require.JSONEqf(t, want, have, "test %d: json not match, want: %s, have: %s", i, want, have)
}
}

func TestRPCGetTransactionByHash(t *testing.T) {
t.Parallel()

var (
backend, _, testSuite = setupBackendWithTxs(t, 6)
api = NewTransactionAPI(backend, new(AddrLocker))
)

for i, tt := range testSuite {
var (
result interface{}
err error
)
result, err = api.GetTransactionByHash(context.Background(), tt.txHash)
if err != nil {
t.Errorf("test %d: want no error, have %v", i, err)
continue
}
testRPCResponseWithFile(t, i, result, "eth_getTransactionByHash", tt.file)
}
}
26 changes: 26 additions & 0 deletions internal/ethapi/testdata/eth_getTransactionByHash-blob-tx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"blockHash": "0x4446b1498499cab37cfec330097fadf285c703444d431756940528d32fe0e97b",
"blockNumber": "0x6",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0x5208",
"gasPrice": "0x1b09d63b",
"maxFeePerGas": "0x1b09d82e",
"maxPriorityFeePerGas": "0x1",
"maxFeePerBlobGas": "0x1",
"hash": "0xb51ee3d2a89ba5d5623c73133c8d7a6ba9fb41194c17f4302c21b30994a1180f",
"input": "0x",
"nonce": "0x5",
"to": "0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e",
"transactionIndex": "0x0",
"value": "0x0",
"type": "0x3",
"accessList": [],
"chainId": "0x1",
"blobVersionedHashes": [
"0x0100000000000000000000000000000000000000000000000000000000000000"
],
"v": "0x1",
"r": "0x38bb9fd000a5c38eab56c1b8ae96556fd58b417d2c8e175d22aadacbdea85d69",
"s": "0x2d50cc6ec19ce8d0736554e740ed7d451ac5e72488c186f87402d28bf301f0a3",
"yParity": "0x1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"blockHash": "0xc594f1b119b071d288cd11371da2de73d67986a717974a85cac4ce634eba3015",
"blockNumber": "0x2",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0xcf6c",
"gasPrice": "0x2db16291",
"hash": "0x340e58cda5086495010b571fe25067fecc9954dc4ee3cedece00691fa3f5904a",
"input": "0x60806040",
"nonce": "0x1",
"to": null,
"transactionIndex": "0x0",
"value": "0x0",
"type": "0x0",
"chainId": "0x1",
"v": "0x26",
"r": "0xfa210b09f8a13ea096b3a005a5feb0270121ab259555898bf03826e993ec14f4",
"s": "0x7173d180059852006668647a793207d488723b89acdd6c484273f16beb9aaa3"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"blockHash": "0x4d780246cde52e535f40603d47af8fa1aea807dd3065e1acd97127bea0922b3e",
"blockNumber": "0x5",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0xe2f4",
"gasPrice": "0x1ecb3fb4",
"hash": "0xb5a1148819cfdfff9bfe70035524fec940eb735d89b76960b97751d01ae2a9f2",
"input": "0x60806040",
"nonce": "0x4",
"to": null,
"transactionIndex": "0x0",
"value": "0x0",
"type": "0x1",
"accessList": [
{
"address": "0x0000000000000000000000000000000000031ec7",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000000"
]
}
],
"chainId": "0x1",
"v": "0x0",
"r": "0xb4f6dc1e9c3c5904b7b3969e84dd3eefe68cf1b1951039ccf1848f471660671c",
"s": "0x77137b10f51515bcf3268f969a8bee4cb6d0d9521f402b058927071a9cc7e3d8",
"yParity": "0x0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"blockHash": "0x15158c4fb0661a38967dbf81b1592c54999b44a4580153b3469b29724903fc95",
"blockNumber": "0x4",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0xea60",
"gasPrice": "0x2325c42f",
"maxFeePerGas": "0x2325c42f",
"maxPriorityFeePerGas": "0x1f4",
"hash": "0xdcde2574628c9d7dff22b9afa19f235959a924ceec65a9df903a517ae91f5c84",
"input": "0xa9059cbb0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e",
"nonce": "0x3",
"to": "0x0000000000000000000000000000000000031ec7",
"transactionIndex": "0x0",
"value": "0x1",
"type": "0x2",
"accessList": [],
"chainId": "0x1",
"v": "0x1",
"r": "0x41fa5d541e9081c6f636e6cb6c5dc0c4369c7d65df9b17f8ac47834f1eba4a6a",
"s": "0x6f811df916d0ec44e108740d6d307fd203273c26f9f52a785cb5fc551c5802f9",
"yParity": "0x1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"blockHash": "0xfddc403b2acdace77774a35cbad7b68d50ce258fafaac399e21533a3b77ad94a",
"blockNumber": "0x1",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0x5208",
"gasPrice": "0x342770c0",
"hash": "0x644a31c354391520d00e95b9affbbb010fc79ac268144ab8e28207f4cf51097e",
"input": "0x",
"nonce": "0x0",
"to": "0x0d3ab14bbad3d99f4203bd7a11acb94882050e7e",
"transactionIndex": "0x0",
"value": "0x3e8",
"type": "0x0",
"v": "0x1b",
"r": "0xcd190747077598c3b9c5f21ea06f8487d6ab1e23358fbe0e0e0c4e64651b68f3",
"s": "0x467ebcb2186b332969991e2e25244959dcdb3feb7652ff6b81ce96e646d86abd"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"blockHash": "0x4446b1498499cab37cfec330097fadf285c703444d431756940528d32fe0e97b",
"blockNumber": "0x6",
"from": "0x0000000000000000000000000000000000000000",
"gas": "0x0",
"gasPrice": "0x0",
"hash": "0xfff9ab54386f3f5c5817e27cf1c8249968c13578c027389c021172d0568ebe12",
"input": "0x",
"nonce": "0x0",
"to": "0x0000000000000000000000000000000000000000",
"transactionIndex": "0x1",
"value": "0x0",
"type": "0x0",
"v": "0x0",
"r": "0x0",
"s": "0x0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
18 changes: 18 additions & 0 deletions internal/ethapi/testdata/eth_getTransactionByHash-with-logs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"blockHash": "0x12db9de067ca71d47e360ab4eb9bf7bc80824f5edd39c7576e253f37a1a22782",
"blockNumber": "0x3",
"from": "0x703c4b2bd70c169f5717101caee543299fc946c7",
"gas": "0xea60",
"gasPrice": "0x281c2585",
"hash": "0xeaf3921cbf03ba45bad4e6ab807b196ce3b2a0b5bacc355b6272fa96b11b4287",
"input": "0xa9059cbb0000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000d",
"nonce": "0x2",
"to": "0x0000000000000000000000000000000000031ec7",
"transactionIndex": "0x0",
"value": "0x0",
"type": "0x0",
"chainId": "0x1",
"v": "0x25",
"r": "0xabecc8622990867d3889d6abb094e9247b5af59978c23d95b7020da6efa2c834",
"s": "0x31267ff2ffba8d2ff70d908feb98584872c3b3df47454cc3d04f342eb6c68b77"
}

0 comments on commit 828fc00

Please sign in to comment.