Skip to content

Commit

Permalink
Do not return non-canonical receipts. (#4138)
Browse files Browse the repository at this point in the history
Move code for logIndexStart from eth_getTransactionReceipt to BlockchainBridge.GetReceiptAndEffectiveGasPrice
  • Loading branch information
LukaszRozmej authored Jun 9, 2022
1 parent 6a404da commit dbab75e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void GetReceiptAndEffectiveGasPrice_returns_correct_results(bool isCanoni
_receiptStorage.FindBlockHash(txHash).Returns(blockHash);
_receiptStorage.Get(block).Returns(new[] {receipt});

(TxReceipt Receipt, UInt256? EffectiveGasPrice) result = isCanonical || isRemoved ? (receipt, effectiveGasPrice) : (null, null);
(TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) result = isCanonical ? (receipt, effectiveGasPrice, 0) : (null, null, 0);
_blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash).Should().BeEquivalentTo(result);
}
}
Expand Down
25 changes: 10 additions & 15 deletions src/Nethermind/Nethermind.Facade/BlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,25 @@ public Block? HeadBlock

public bool IsMining { get; }

public (TxReceipt Receipt, UInt256? EffectiveGasPrice) GetReceiptAndEffectiveGasPrice(Keccak txHash)
public (TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(Keccak txHash)
{
Keccak blockHash = _receiptFinder.FindBlockHash(txHash);
if (blockHash != null)
{
Block? block = _processingEnv.BlockTree.FindBlock(blockHash, BlockTreeLookupOptions.RequireCanonical);
bool isNotCanonical = block is null;
if (isNotCanonical)
if (block is not null)
{
block = _processingEnv.BlockTree.FindBlock(blockHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded);
TxReceipt[] txReceipts = _receiptFinder.Get(block);
TxReceipt txReceipt = txReceipts.ForTransaction(txHash);
int logIndexStart = txReceipts.GetBlockLogFirstIndex(txReceipt.Index);
Transaction tx = block.Transactions[txReceipt.Index];
bool is1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled;
UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas);
return (txReceipt, effectiveGasPrice, logIndexStart);
}
TxReceipt txReceipt = _receiptFinder.Get(block).ForTransaction(txHash);
if (isNotCanonical && !txReceipt.Removed)
{
return (null, null);
}
Transaction tx = block?.Transactions[txReceipt.Index];
bool is1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled;
UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas);

return (txReceipt, effectiveGasPrice);
}

return (null, null);
return (null, null, 0);
}

public (TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash)
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IBlockchainBridge : ILogFinder
void RecoverTxSenders(Block block);
void RecoverTxSender(Transaction tx);
TxReceipt GetReceipt(Keccak txHash);
(TxReceipt Receipt, UInt256? EffectiveGasPrice) GetReceiptAndEffectiveGasPrice(Keccak txHash);
(TxReceipt Receipt, UInt256? EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(Keccak txHash);
(TxReceipt Receipt, Transaction Transaction, UInt256? baseFee) GetTransaction(Keccak txHash);
BlockchainBridge.CallOutput Call(BlockHeader header, Transaction tx, CancellationToken cancellationToken);
BlockchainBridge.CallOutput EstimateGas(BlockHeader header, Transaction tx, CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public async Task Eth_get_transaction_receipt()
.WithLogs(entries).TestObject;
TxReceipt[] receiptsTab = {receipt};

blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any<Keccak>()).Returns((receipt, UInt256.One));
blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any<Keccak>()).Returns((receipt, UInt256.One, 0));
blockFinder.FindBlock(Arg.Any<BlockParameter>()).Returns(block);
receiptFinder.Get(Arg.Any<Block>()).Returns(receiptsTab);
receiptFinder.Get(Arg.Any<Keccak>()).Returns(receiptsTab);
Expand Down Expand Up @@ -656,7 +656,7 @@ public async Task Eth_get_transaction_receipt_when_block_has_few_receipts()
Logs = logEntries
};

blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any<Keccak>()).Returns((receipt2, UInt256.One));
blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any<Keccak>()).Returns((receipt2, UInt256.One, 2));

TxReceipt[] receipts = {receipt1, receipt2};

Expand Down Expand Up @@ -712,7 +712,7 @@ public async Task Eth_getTransactionReceipt_return_info_about_mined_tx()
blockFinder.FindBlock(Arg.Any<BlockParameter>()).Returns(block);
receiptFinder.Get(Arg.Any<Block>()).Returns(receiptsTab);
receiptFinder.Get(Arg.Any<Keccak>()).Returns(receiptsTab);
blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any<Keccak>()).Returns((receipt, UInt256.One));
blockchainBridge.GetReceiptAndEffectiveGasPrice(Arg.Any<Keccak>()).Returns((receipt, UInt256.One, 0));

ctx._test = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).WithBlockFinder(blockFinder).WithReceiptFinder(receiptFinder).WithBlockchainBridge(blockchainBridge).Build();
string serialized = ctx._test.TestEthRpc("eth_getTransactionReceipt", tx.Hash.ToString());
Expand Down
13 changes: 4 additions & 9 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,19 +498,14 @@ public ResultWrapper<TransactionForRpc> eth_getTransactionByBlockNumberAndIndex(

public Task<ResultWrapper<ReceiptForRpc>> eth_getTransactionReceipt(Keccak txHash)
{
(TxReceipt Receipt, UInt256? EffectiveGasPrice) result = _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash);
if (result.Receipt == null)
(TxReceipt receipt, UInt256? effectiveGasPrice, int logIndexStart) = _blockchainBridge.GetReceiptAndEffectiveGasPrice(txHash);
if (receipt == null)
{
return Task.FromResult(ResultWrapper<ReceiptForRpc>.Success(null));
}

Keccak blockHash = result.Receipt.BlockHash;
TxReceipt[] receipts = _receiptFinder.Get(blockHash!);
int logIndexStart = receipts.GetBlockLogFirstIndex(result.Receipt.Index);
ReceiptForRpc receiptModel = new(txHash, result.Receipt, result.EffectiveGasPrice, logIndexStart);


if (_logger.IsTrace) _logger.Trace($"eth_getTransactionReceipt request {txHash}, result: {txHash}");
return Task.FromResult(ResultWrapper<ReceiptForRpc>.Success(receiptModel));
return Task.FromResult(ResultWrapper<ReceiptForRpc>.Success(new(txHash, receipt, effectiveGasPrice, logIndexStart)));
}

public ResultWrapper<BlockForRpc> eth_getUncleByBlockHashAndIndex(Keccak blockHash, UInt256 positionIndex)
Expand Down

0 comments on commit dbab75e

Please sign in to comment.