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 log index in transaction receipt #6206

Merged
merged 8 commits into from
Nov 29, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Bug fixes
- Fix Docker image name clash between Besu and evmtool [#6194](https://github.com/hyperledger/besu/pull/6194)
- Fix `logIndex` in `eth_getTransactionReceipt` JSON RPC method [#6206](https://github.com/hyperledger/besu/pull/6206)

## 23.10.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ protected TransactionReceiptResult(final TransactionReceiptWithMetadata receiptW
receiptWithMetadata.getBlockNumber(),
txn.getHash(),
receiptWithMetadata.getBlockHash(),
receiptWithMetadata.getTransactionIndex());
receiptWithMetadata.getTransactionIndex(),
receiptWithMetadata.getLogIndexOffset());
this.logsBloom = receipt.getBloomFilter().toString();
this.to = txn.getTo().map(Bytes::toHexString).orElse(null);
this.transactionHash = txn.getHash().toString();
Expand Down Expand Up @@ -192,14 +193,15 @@ private List<TransactionReceiptLogResult> logReceipts(
final long blockNumber,
final Hash transactionHash,
final Hash blockHash,
final int transactionIndex) {
final int transactionIndex,
final int logIndexOffset) {
final List<TransactionReceiptLogResult> logResults = new ArrayList<>(logs.size());

for (int i = 0; i < logs.size(); i++) {
final Log log = logs.get(i);
logResults.add(
new TransactionReceiptLogResult(
log, blockNumber, transactionHash, blockHash, transactionIndex, i));
log, blockNumber, transactionHash, blockHash, transactionIndex, i + logIndexOffset));
}

return logResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ public Optional<BlockHeader> safeBlockHeader() {
*/
public Optional<UInt256> storageAt(
final Address address, final UInt256 storageIndex, final long blockNumber) {
final Hash blockHash =
getBlockHeaderByNumber(blockNumber).map(BlockHeader::getHash).orElse(Hash.EMPTY);
final Hash blockHash = getBlockHashByNumber(blockNumber).orElse(Hash.EMPTY);

return storageAt(address, storageIndex, blockHash);
}
Expand All @@ -211,8 +210,7 @@ public Optional<UInt256> storageAt(
* @return The balance of the account in Wei.
*/
public Optional<Wei> accountBalance(final Address address, final long blockNumber) {
final Hash blockHash =
getBlockHeaderByNumber(blockNumber).map(BlockHeader::getHash).orElse(Hash.EMPTY);
final Hash blockHash = getBlockHashByNumber(blockNumber).orElse(Hash.EMPTY);

return accountBalance(address, blockHash);
}
Expand All @@ -236,8 +234,7 @@ public Optional<Wei> accountBalance(final Address address, final Hash blockHash)
* @return The code associated with this address.
*/
public Optional<Bytes> getCode(final Address address, final long blockNumber) {
final Hash blockHash =
getBlockHeaderByNumber(blockNumber).map(BlockHeader::getHash).orElse(Hash.EMPTY);
final Hash blockHash = getBlockHashByNumber(blockNumber).orElse(Hash.EMPTY);

return getCode(address, blockHash);
}
Expand All @@ -263,13 +260,7 @@ public Optional<Integer> getTransactionCount(final long blockNumber) {
if (outsideBlockchainRange(blockNumber)) {
return Optional.empty();
}
return Optional.of(
blockchain
.getBlockHashByNumber(blockNumber)
.flatMap(this::blockByHashWithTxHashes)
.map(BlockWithMetadata::getTransactions)
.map(List::size)
.orElse(-1));
return blockchain.getBlockHashByNumber(blockNumber).map(this::getTransactionCount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks better now!

}

/**
Expand Down Expand Up @@ -624,22 +615,25 @@ public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionH
// getTransactionLocation should not return if the TX or block doesn't exist, so throwing
// on a missing optional is appropriate.
final TransactionLocation location = maybeLocation.get();
final Block block = blockchain.getBlockByHash(location.getBlockHash()).orElseThrow();
final Transaction transaction =
block.getBody().getTransactions().get(location.getTransactionIndex());

final Hash blockhash = location.getBlockHash();
final int transactionIndex = location.getTransactionIndex();

final Block block = blockchain.getBlockByHash(blockhash).orElseThrow();
final Transaction transaction = block.getBody().getTransactions().get(transactionIndex);

final BlockHeader header = block.getHeader();
final List<TransactionReceipt> transactionReceipts =
blockchain.getTxReceipts(blockhash).orElseThrow();
final TransactionReceipt transactionReceipt =
transactionReceipts.get(location.getTransactionIndex());
final TransactionReceipt transactionReceipt = transactionReceipts.get(transactionIndex);

long gasUsed = transactionReceipt.getCumulativeGasUsed();
if (location.getTransactionIndex() > 0) {
gasUsed =
gasUsed
- transactionReceipts.get(location.getTransactionIndex() - 1).getCumulativeGasUsed();
int logIndexOffset = 0;
if (transactionIndex > 0) {
gasUsed -= transactionReceipts.get(transactionIndex - 1).getCumulativeGasUsed();
logIndexOffset =
IntStream.range(0, transactionIndex)
.map(i -> transactionReceipts.get(i).getLogsList().size())
.sum();
}

Optional<Long> maybeBlobGasUsed =
Expand All @@ -653,13 +647,14 @@ public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionH
transactionReceipt,
transaction,
transactionHash,
location.getTransactionIndex(),
transactionIndex,
gasUsed,
header.getBaseFee(),
blockhash,
header.getNumber(),
maybeBlobGasUsed,
maybeBlobGasPrice));
maybeBlobGasPrice,
logIndexOffset));
}

/**
Expand Down Expand Up @@ -1075,7 +1070,7 @@ private int logIndexOffset(
break;
}

logIndexOffset += receipts.get(i).getLogs().size();
logIndexOffset += receipts.get(i).getLogsList().size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice opt

}

return logIndexOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TransactionReceiptWithMetadata {
private final Transaction transaction;
private final Optional<Long> blobGasUsed;
private final Optional<Wei> blobGasPrice;
private final int logIndexOffset;

private TransactionReceiptWithMetadata(
final TransactionReceipt receipt,
Expand All @@ -43,7 +44,8 @@ private TransactionReceiptWithMetadata(
final Hash blockHash,
final long blockNumber,
final Optional<Long> blobGasUsed,
final Optional<Wei> blobGasPrice) {
final Optional<Wei> blobGasPrice,
final int logIndexOffset) {
this.receipt = receipt;
this.transactionHash = transactionHash;
this.transactionIndex = transactionIndex;
Expand All @@ -54,6 +56,7 @@ private TransactionReceiptWithMetadata(
this.transaction = transaction;
this.blobGasUsed = blobGasUsed;
this.blobGasPrice = blobGasPrice;
this.logIndexOffset = logIndexOffset;
}

public static TransactionReceiptWithMetadata create(
Expand All @@ -66,7 +69,8 @@ public static TransactionReceiptWithMetadata create(
final Hash blockHash,
final long blockNumber,
final Optional<Long> blobGasUsed,
final Optional<Wei> blobGasPrice) {
final Optional<Wei> blobGasPrice,
final int logIndexOffset) {
return new TransactionReceiptWithMetadata(
receipt,
transaction,
Expand All @@ -77,7 +81,8 @@ public static TransactionReceiptWithMetadata create(
blockHash,
blockNumber,
blobGasUsed,
blobGasPrice);
blobGasPrice,
logIndexOffset);
}

public TransactionReceipt getReceipt() {
Expand Down Expand Up @@ -121,4 +126,8 @@ public Optional<Long> getBlobGasUsed() {
public Optional<Wei> getBlobGasPrice() {
return blobGasPrice;
}

public int getLogIndexOffset() {
return logIndexOffset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void emptyBlobs() throws Exception {
fakedHash,
1,
Optional.empty(),
Optional.empty());
Optional.empty(),
0);
when(query.transactionReceiptByTransactionHash(any(), any()))
.thenReturn(Optional.of(transactionReceiptWithMetadata));

Expand Down Expand Up @@ -109,7 +110,8 @@ void hasZeroBlobs() throws Exception {
fakedHash,
1,
Optional.of(0L),
Optional.of(Wei.ZERO));
Optional.of(Wei.ZERO),
0);
when(query.transactionReceiptByTransactionHash(any(), any()))
.thenReturn(Optional.of(transactionReceiptWithMetadata));

Expand Down Expand Up @@ -143,7 +145,8 @@ void hasOneBlob() throws Exception {
fakedHash,
1,
Optional.of(blobGasUsed),
Optional.of(blobGasPrice));
Optional.of(blobGasPrice),
0);
when(query.transactionReceiptByTransactionHash(any(), any()))
.thenReturn(Optional.of(transactionReceiptWithMetadata));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public class EthGetTransactionReceiptTest {
blockHash,
4,
Optional.empty(),
Optional.empty());
Optional.empty(),
0);
private final TransactionReceiptWithMetadata rootReceiptWithMetaData =
TransactionReceiptWithMetadata.create(
rootReceipt,
Expand All @@ -115,7 +116,8 @@ public class EthGetTransactionReceiptTest {
blockHash,
4,
Optional.empty(),
Optional.empty());
Optional.empty(),
0);

private final ProtocolSpec rootTransactionTypeSpec =
new ProtocolSpec(
Expand Down Expand Up @@ -243,7 +245,8 @@ public void shouldWorkFor1559Txs() {
blockHash,
4,
Optional.empty(),
Optional.empty());
Optional.empty(),
0);
when(blockchainQueries.transactionReceiptByTransactionHash(receiptHash, protocolSchedule))
.thenReturn(Optional.of(transactionReceiptWithMetadata));
when(protocolSchedule.getByBlockHeader(blockHeader(1))).thenReturn(rootTransactionTypeSpec);
Expand Down
Loading