Skip to content

Commit

Permalink
fix: query deposit tx with correct nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed Jan 24, 2024
1 parent 1e020bd commit 569aca5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1026,7 +1027,16 @@ private List<TransactionWithMetadata> formatTransactions(
final int count = txs.size();
final List<TransactionWithMetadata> 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<TransactionReceipt> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public class TransactionWithMetadata {
private final Optional<Wei> baseFee;
private final Optional<Hash> blockHash;
private final Optional<Integer> transactionIndex;
private final Optional<Long> nonce;

public TransactionWithMetadata(final Transaction transaction) {
this.transaction = transaction;
this.blockNumber = Optional.empty();
this.baseFee = Optional.empty();
this.blockHash = Optional.empty();
this.transactionIndex = Optional.empty();
this.nonce = Optional.empty();
}

public TransactionWithMetadata(
Expand All @@ -44,11 +46,22 @@ public TransactionWithMetadata(
final Optional<Wei> 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<Wei> baseFee,
final Hash blockHash,
final int transactionIndex,
final Optional<Long> 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() {
Expand All @@ -70,4 +83,8 @@ public Optional<Hash> getBlockHash() {
public Optional<Integer> getTransactionIndex() {
return transactionIndex;
}

public long getNonce() {
return nonce.orElse(transaction.getNonce());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void remove(final Bytes prefix, final Bytes key) {
}

private Bytes rlpEncode(final List<TransactionReceipt> receipts) {
return RLP.encode(o -> o.writeList(receipts, TransactionReceipt::writeToWithRevertReason));
return RLP.encode(o -> o.writeList(receipts, TransactionReceipt::writeToForStorage));
}

private void removeVariables() {
Expand Down

0 comments on commit 569aca5

Please sign in to comment.