Fix state sync hash (#1207)

* fix: hashing of statesync txn

* add: state sync txn in getBlockReceipts, testcase to check hashing of state sync txn in GetTransactonReceiptsByBlock
This commit is contained in:
Anshal Shukla 2024-05-06 20:04:02 +05:30 committed by GitHub
parent b8ad00095a
commit 7c729f542b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View file

@ -1092,7 +1092,13 @@ func (s *BlockChainAPI) GetBlockReceipts(ctx context.Context, blockNrOrHash rpc.
result := make([]map[string]interface{}, len(receipts))
for i, receipt := range receipts {
result[i] = marshalReceipt(receipt, block.Hash(), block.NumberU64(), signer, txs[i], i)
result[i] = marshalReceipt(receipt, block.Hash(), block.NumberU64(), signer, txs[i], i, false)
}
stateSyncReceipt := rawdb.ReadBorReceipt(s.b.ChainDb(), block.Hash(), block.NumberU64(), s.b.ChainConfig())
if stateSyncReceipt != nil {
tx, _, _, _ := rawdb.ReadBorTransaction(s.b.ChainDb(), stateSyncReceipt.TxHash)
result = append(result, marshalReceipt(stateSyncReceipt, block.Hash(), block.NumberU64(), signer, tx, len(result), true))
}
return result, nil
@ -2225,17 +2231,24 @@ func (s *TransactionAPI) GetTransactionReceipt(ctx context.Context, hash common.
// Derive the sender.
signer := types.MakeSigner(s.b.ChainConfig(), header.Number, header.Time)
return marshalReceipt(receipt, blockHash, blockNumber, signer, tx, int(index)), nil
return marshalReceipt(receipt, blockHash, blockNumber, signer, tx, int(index), borTx), nil
}
// marshalReceipt marshals a transaction receipt into a JSON object.
func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber uint64, signer types.Signer, tx *types.Transaction, txIndex int) map[string]interface{} {
func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber uint64, signer types.Signer, tx *types.Transaction, txIndex int, borTx bool) map[string]interface{} {
from, _ := types.Sender(signer, tx)
txHash := common.Hash{}
if borTx {
txHash = types.GetDerivedBorTxHash(types.BorReceiptKey(blockNumber, blockHash))
} else {
txHash = tx.Hash()
}
fields := map[string]interface{}{
"blockHash": blockHash,
"blockNumber": hexutil.Uint64(blockNumber),
"transactionHash": tx.Hash(),
"transactionHash": txHash,
"transactionIndex": hexutil.Uint64(txIndex),
"from": from,
"to": tx.To(),

View file

@ -98,6 +98,15 @@ func testGetTransactionReceiptsByBlock(t *testing.T, publicBlockchainAPI *ethapi
assert.Equal(t, 2, len(receiptsOut))
assert.True(t, areDifferentHashes(receiptsOut))
// check 5: Tx hash for state sync txn
block, err := publicBlockchainAPI.GetBlockByNumber(context.Background(), rpc.BlockNumber(4), false)
assert.Nil(t, err)
blockHash := block["hash"].(common.Hash)
txHash := types.GetDerivedBorTxHash(types.BorReceiptKey(4, blockHash))
// Compare tx hash from GetTransactionReceiptsByBlock with hash computed above
txReceipts, err := publicBlockchainAPI.GetTransactionReceiptsByBlock(context.Background(), rpc.BlockNumberOrHashWithNumber(4))
assert.Nil(t, err)
assert.Equal(t, txHash, txReceipts[1]["transactionHash"].(common.Hash))
}
// Test for GetTransactionByBlockNumberAndIndex
@ -122,7 +131,7 @@ func testGetTransactionByBlockNumberAndIndex(t *testing.T, publicTransactionPool
tx = publicTransactionPoolAPI.GetTransactionByBlockNumberAndIndex(context.Background(), rpc.BlockNumber(4), 0)
assert.Equal(t, common.HexToAddress("0x1000"), *tx.To)
// check 5 : Normal Transaction
// check 5 : State Sync Transaction
tx = publicTransactionPoolAPI.GetTransactionByBlockNumberAndIndex(context.Background(), rpc.BlockNumber(4), 1)
assert.Equal(t, common.HexToAddress("0x0"), *tx.To)
}