From 569aca58ecc442aa7e2ff640958bde39bd131443 Mon Sep 17 00:00:00 2001 From: thinkAfCod Date: Wed, 24 Jan 2024 23:24:46 +0800 Subject: [PATCH] fix: query deposit tx with correct nonce --- .../results/TransactionCompleteResult.java | 2 +- .../ethereum/api/query/BlockchainQueries.java | 12 ++++- .../api/query/TransactionWithMetadata.java | 17 +++++++ .../ethereum/core/TransactionReceipt.java | 44 +++++++++++++++++++ ...ueStoragePrefixedKeyBlockchainStorage.java | 2 +- 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/TransactionCompleteResult.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/TransactionCompleteResult.java index fb46e5a65c5..6f03901e3b4 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/TransactionCompleteResult.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/TransactionCompleteResult.java @@ -121,7 +121,7 @@ public TransactionCompleteResult(final TransactionWithMetadata tx) { this.hash = transaction.getHash().toString(); this.mint = transaction.getMint().map(Wei::toShortHexString).orElse(null); this.input = transaction.getPayload().toString(); - this.nonce = Quantity.create(transaction.getNonce()); + this.nonce = Quantity.create(tx.getNonce()); this.to = transaction.getTo().map(Bytes::toHexString).orElse(null); this.transactionIndex = Quantity.create(tx.getTransactionIndex().get()); this.type = diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java index 04bfdd9727a..bb51d8ab0f7 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/BlockchainQueries.java @@ -20,6 +20,7 @@ import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Hash; +import org.hyperledger.besu.datatypes.TransactionType; import org.hyperledger.besu.datatypes.Wei; import org.hyperledger.besu.ethereum.api.ApiConfiguration; import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration; @@ -1026,7 +1027,16 @@ private List formatTransactions( final int count = txs.size(); final List result = new ArrayList<>(count); for (int i = 0; i < count; i++) { - result.add(new TransactionWithMetadata(txs.get(i), blockNumber, baseFee, blockHash, i)); + if (TransactionType.OPTIMISM_DEPOSIT.equals(txs.get(i).getType())) { + final List txReceipts = + blockchain.getTxReceipts(blockHash).orElseThrow(); + final TransactionReceipt receipt = txReceipts.get(i); + result.add( + new TransactionWithMetadata( + txs.get(i), blockNumber, baseFee, blockHash, i, receipt.getDepositNonce())); + } else { + result.add(new TransactionWithMetadata(txs.get(i), blockNumber, baseFee, blockHash, i)); + } } return result; } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionWithMetadata.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionWithMetadata.java index 637b0f8f5ce..f8b81eb44de 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionWithMetadata.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionWithMetadata.java @@ -29,6 +29,7 @@ public class TransactionWithMetadata { private final Optional baseFee; private final Optional blockHash; private final Optional transactionIndex; + private final Optional nonce; public TransactionWithMetadata(final Transaction transaction) { this.transaction = transaction; @@ -36,6 +37,7 @@ public TransactionWithMetadata(final Transaction transaction) { this.baseFee = Optional.empty(); this.blockHash = Optional.empty(); this.transactionIndex = Optional.empty(); + this.nonce = Optional.empty(); } public TransactionWithMetadata( @@ -44,11 +46,22 @@ public TransactionWithMetadata( final Optional baseFee, final Hash blockHash, final int transactionIndex) { + this(transaction, blockNumber, baseFee, blockHash, transactionIndex, Optional.empty()); + } + + public TransactionWithMetadata( + final Transaction transaction, + final long blockNumber, + final Optional baseFee, + final Hash blockHash, + final int transactionIndex, + final Optional nonce) { this.transaction = transaction; this.blockNumber = Optional.of(blockNumber); this.baseFee = baseFee; this.blockHash = Optional.of(blockHash); this.transactionIndex = Optional.of(transactionIndex); + this.nonce = nonce; } public Transaction getTransaction() { @@ -70,4 +83,8 @@ public Optional getBlockHash() { public Optional getTransactionIndex() { return transactionIndex; } + + public long getNonce() { + return nonce.orElse(transaction.getNonce()); + } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java index 14d10bf7111..b0553752e2d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/TransactionReceipt.java @@ -208,6 +208,50 @@ private void writeTo(final RLPOutput rlpOutput, final boolean withRevertReason) } } + /** + * Write an RLP representation for block chain storage. + * @param rlpOutput The RLP output to write to + */ + public void writeToForStorage(final RLPOutput rlpOutput) { + if (transactionType.equals(TransactionType.FRONTIER)) { + writeToForChainStorage(rlpOutput, true); + } else { + rlpOutput.writeBytes(RLP.encode(out -> writeToForChainStorage(out, true))); + } + } + + /** + * Write an RLP representation for block chain storage. + * Will Always include deposit nonce and deposit receipt version. + * @param rlpOutput The RLP output to write to + */ + public void writeToForChainStorage(final RLPOutput rlpOutput, final boolean withRevertReason) { + if (!transactionType.equals(TransactionType.FRONTIER)) { + rlpOutput.writeIntScalar(transactionType.getSerializedType()); + } + + rlpOutput.startList(); + + // Determine whether it's a state root-encoded transaction receipt + // or is a status code-encoded transaction receipt. + if (stateRoot != null) { + rlpOutput.writeBytes(stateRoot); + } else { + rlpOutput.writeLongScalar(status); + } + rlpOutput.writeLongScalar(cumulativeGasUsed); + rlpOutput.writeBytes(bloomFilter); + rlpOutput.writeList(logs, Log::writeTo); + + depositNonce.ifPresentOrElse(rlpOutput::writeLongScalar, rlpOutput::writeNull); + depositReceiptVersion.ifPresentOrElse(rlpOutput::writeLongScalar, rlpOutput::writeNull); + + if (withRevertReason && revertReason.isPresent()) { + rlpOutput.writeBytes(revertReason.get()); + } + rlpOutput.endList(); + } + public void writeToForReceiptTrie(final RLPOutput rlpOutput, final boolean withRevertReason) { if (!transactionType.equals(TransactionType.FRONTIER)) { rlpOutput.writeIntScalar(transactionType.getSerializedType()); diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java index 9134ca91c01..73eadeaa638 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/storage/keyvalue/KeyValueStoragePrefixedKeyBlockchainStorage.java @@ -365,7 +365,7 @@ private void remove(final Bytes prefix, final Bytes key) { } private Bytes rlpEncode(final List receipts) { - return RLP.encode(o -> o.writeList(receipts, TransactionReceipt::writeToWithRevertReason)); + return RLP.encode(o -> o.writeList(receipts, TransactionReceipt::writeToForStorage)); } private void removeVariables() {