From 41134d047383edccaa8340795c7a58b6ab5137e2 Mon Sep 17 00:00:00 2001 From: Lucca Martins Date: Sun, 12 Jan 2025 04:07:25 -0300 Subject: [PATCH] state sync tests on rpc methods --- internal/ethapi/api.go | 10 ++- internal/ethapi/api_test.go | 67 +++++++++++++++++-- ...h_getTransactionReceipt-state-sync-tx.json | 31 +++++---- 3 files changed, 86 insertions(+), 22 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index a469a8c0e0..c02b66f256 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -2074,7 +2074,10 @@ func (api *TransactionAPI) GetTransactionReceipt(ctx context.Context, hash commo } if !found { - tx, blockHash, blockNumber, index = rawdb.ReadBorTransaction(api.b.ChainDb(), hash) + tx, blockHash, blockNumber, index, err = api.b.GetBorBlockTransaction(ctx, hash) + if err != nil { + return nil, err + } borTx = true } @@ -2086,7 +2089,10 @@ func (api *TransactionAPI) GetTransactionReceipt(ctx context.Context, hash commo if borTx { // Fetch bor block receipt - receipt = rawdb.ReadBorReceipt(api.b.ChainDb(), blockHash, blockNumber, api.b.ChainConfig()) + receipt, err = api.b.GetBorBlockReceipt(ctx, blockHash) + if err != nil { + return nil, err + } } else { receipts, err := api.b.GetReceipts(ctx, blockHash) if err != nil { diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index a6d620b330..619bd9f072 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -587,7 +587,11 @@ func (b testBackend) SendTx(ctx context.Context, signedTx *types.Transaction) er } func (b testBackend) GetTransaction(ctx context.Context, txHash common.Hash) (bool, *types.Transaction, common.Hash, uint64, uint64, error) { tx, blockHash, blockNumber, index := rawdb.ReadTransaction(b.db, txHash) - return true, tx, blockHash, blockNumber, index, nil + found := true + if tx == nil { + found = false + } + 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") } @@ -625,7 +629,8 @@ func (b testBackend) ServiceFilter(ctx context.Context, session *bloombits.Match // GetBorBlockTransaction returns bor block tx func (b testBackend) GetBorBlockTransaction(ctx context.Context, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { - panic("implement me") + tx, blockHash, blockNumber, index := rawdb.ReadBorTransaction(b.ChainDb(), hash) + return tx, blockHash, blockNumber, index, nil } func (b testBackend) GetBorBlockTransactionWithBlockHash(ctx context.Context, txHash common.Hash, blockHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { @@ -1935,7 +1940,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha }, } signer = types.LatestSignerForChainID(params.TestChainConfig.ChainID) - txHashes = make([]common.Hash, genBlocks) + txHashes = make([]common.Hash, genBlocks+1) ) // Set the terminal total difficulty in the config @@ -1997,9 +2002,33 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha txHashes[i] = tx.Hash() } }) + + 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() @@ -2052,6 +2081,11 @@ func TestRPCGetTransactionReceipt(t *testing.T) { txHash: txHashes[5], file: "blob-tx", }, + // 8. state sync tx found + { + txHash: txHashes[6], + file: "state-sync-tx", + }, } for i, tt := range testSuite { @@ -2103,7 +2137,7 @@ func TestRPCGetTransactionReceiptsByBlock(t *testing.T) { contract: {Balance: big.NewInt(params.Ether), Code: common.FromHex("0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063a9059cbb14610030575b600080fd5b61004a6004803603810190610045919061016a565b610060565b60405161005791906101c5565b60405180910390f35b60008273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516100bf91906101ef565b60405180910390a36001905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610101826100d6565b9050919050565b610111816100f6565b811461011c57600080fd5b50565b60008135905061012e81610108565b92915050565b6000819050919050565b61014781610134565b811461015257600080fd5b50565b6000813590506101648161013e565b92915050565b60008060408385031215610181576101806100d1565b5b600061018f8582860161011f565b92505060206101a085828601610155565b9150509250929050565b60008115159050919050565b6101bf816101aa565b82525050565b60006020820190506101da60008301846101b6565b92915050565b6101e981610134565b82525050565b600060208201905061020460008301846101e0565b9291505056fea2646970667358221220b469033f4b77b9565ee84e0a2f04d496b18160d26034d54f9487e57788fd36d564736f6c63430008120033")}, }, } - genTxs = 5 + genTxs = 6 genBlocks = 1 signer = types.LatestSignerForChainID(params.TestChainConfig.ChainID) txHashes = make([]common.Hash, 0, genTxs) @@ -2149,6 +2183,11 @@ func TestRPCGetTransactionReceiptsByBlock(t *testing.T) { } }) + txHashes = append(txHashes, mockStateSyncTxOnCurrentBlock(t, backend)) + + // map sprint 0 to block 1 + backend.ChainConfig().Bor.Sprint["0"] = 1 + api := NewBlockChainAPI(backend) blockHashes := make([]common.Hash, genBlocks+1) ctx := context.Background() @@ -2315,6 +2354,26 @@ func TestRPCGetTransactionReceiptsByBlock(t *testing.T) { "type": "0x1" }`, }, + // 5. state sync tx + { + txHash: txHashes[5], + want: `{ + "blockHash": "0x1728b788dfe51e507d25f14f01414b5a17f807953c13833811d2afae1982b53b", + "blockNumber": "0x1", + "contractAddress": null, + "cumulativeGasUsed": "0x0", + "effectiveGasPrice": "0x0", + "from": "0x0000000000000000000000000000000000000000", + "gasUsed": "0x0", + "logs": [], + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "status": "0x1", + "to": "0x0000000000000000000000000000000000000000", + "transactionHash": "0xba46f68d5c3729ac3fb672fec579fc2cad543bc9edf5b2d47d7c6636ac2fbec9", + "transactionIndex": "0x5", + "type": "0x0" + }`, + }, } receipts, err := api.GetTransactionReceiptsByBlock(context.Background(), blockNrOrHash) diff --git a/internal/ethapi/testdata/eth_getTransactionReceipt-state-sync-tx.json b/internal/ethapi/testdata/eth_getTransactionReceipt-state-sync-tx.json index 2fb229ce2f..5096fa818f 100644 --- a/internal/ethapi/testdata/eth_getTransactionReceipt-state-sync-tx.json +++ b/internal/ethapi/testdata/eth_getTransactionReceipt-state-sync-tx.json @@ -1,17 +1,16 @@ { - "blockHash": "0x4d780246cde52e535f40603d47af8fa1aea807dd3065e1acd97127bea0922b3e", - "blockNumber": "0x6", - "contractAddress": null, - "cumulativeGasUsed": "0xe01c", - "effectiveGasPrice": "0x1ecb3fb4", - "from": "0x703c4b2bd70c169f5717101caee543299fc946c7", - "gasUsed": "0xe01c", - "logs": [], - "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "status": "0x1", - "to": null, - "transactionHash": "0xb5a1148819cfdfff9bfe70035524fec940eb735d89b76960b97751d01ae2a9f2", - "transactionIndex": "0x0", - "type": "0x1" - } - \ No newline at end of file + "blockHash": "0x4446b1498499cab37cfec330097fadf285c703444d431756940528d32fe0e97b", + "blockNumber": "0x6", + "contractAddress": null, + "cumulativeGasUsed": "0x0", + "effectiveGasPrice": "0x0", + "from": "0x0000000000000000000000000000000000000000", + "gasUsed": "0x0", + "logs": [], + "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "status": "0x1", + "to": "0x0000000000000000000000000000000000000000", + "transactionHash": "0xfff9ab54386f3f5c5817e27cf1c8249968c13578c027389c021172d0568ebe12", + "transactionIndex": "0x1", + "type": "0x0" +} \ No newline at end of file