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

Keep RLP to prevent unnecessary rlp encoding: TransactionReceipt #7651

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class TransactionReceipt implements org.hyperledger.besu.plugin.data.Tran
private final int status;
private final TransactionReceiptType transactionReceiptType;
private final Optional<Bytes> revertReason;
private final Bytes rlp;

/**
* Creates an instance of a state root-encoded transaction receipt.
Expand All @@ -78,7 +79,8 @@ public TransactionReceipt(
cumulativeGasUsed,
logs,
LogsBloomFilter.builder().insertLogs(logs).build(),
revertReason);
revertReason,
null);
}

private TransactionReceipt(
Expand All @@ -87,15 +89,17 @@ private TransactionReceipt(
final long cumulativeGasUsed,
final List<Log> logs,
final LogsBloomFilter bloomFilter,
final Optional<Bytes> revertReason) {
final Optional<Bytes> revertReason,
final Bytes rlp) {
this(
transactionType,
stateRoot,
NONEXISTENT,
cumulativeGasUsed,
logs,
bloomFilter,
revertReason);
revertReason,
rlp);
}

/**
Expand All @@ -118,7 +122,8 @@ public TransactionReceipt(
cumulativeGasUsed,
logs,
LogsBloomFilter.builder().insertLogs(logs).build(),
revertReason);
revertReason,
null);
}

public TransactionReceipt(
Expand All @@ -128,7 +133,18 @@ public TransactionReceipt(
final List<Log> logs,
final LogsBloomFilter bloomFilter,
final Optional<Bytes> revertReason) {
this(transactionType, null, status, cumulativeGasUsed, logs, bloomFilter, revertReason);
this(transactionType, null, status, cumulativeGasUsed, logs, bloomFilter, revertReason, null);
}

public TransactionReceipt(
final TransactionType transactionType,
final int status,
final long cumulativeGasUsed,
final List<Log> logs,
final LogsBloomFilter bloomFilter,
final Optional<Bytes> revertReason,
final Bytes rlp) {
this(transactionType, null, status, cumulativeGasUsed, logs, bloomFilter, revertReason, rlp);
}

public TransactionReceipt(
Expand All @@ -153,7 +169,8 @@ private TransactionReceipt(
final long cumulativeGasUsed,
final List<Log> logs,
final LogsBloomFilter bloomFilter,
final Optional<Bytes> revertReason) {
final Optional<Bytes> revertReason,
final Bytes rlp) {
this.transactionType = transactionType;
this.stateRoot = stateRoot;
this.cumulativeGasUsed = cumulativeGasUsed;
Expand All @@ -163,6 +180,7 @@ private TransactionReceipt(
this.transactionReceiptType =
stateRoot == null ? TransactionReceiptType.STATUS : TransactionReceiptType.ROOT;
this.revertReason = revertReason;
this.rlp = rlp;
}

/**
Expand Down Expand Up @@ -190,6 +208,11 @@ void writeTo(final RLPOutput rlpOutput, final boolean withRevertReason, final bo

public void writeToForReceiptTrie(
final RLPOutput rlpOutput, final boolean withRevertReason, final boolean compacted) {
if (rlp != null) {
rlpOutput.writeBytes(rlp);
return;
}

if (!transactionType.equals(TransactionType.FRONTIER)) {
rlpOutput.writeIntScalar(transactionType.getSerializedType());
}
Expand Down Expand Up @@ -233,6 +256,19 @@ public static TransactionReceipt readFrom(final RLPInput input) {
*/
public static TransactionReceipt readFrom(
final RLPInput rlpInput, final boolean revertReasonAllowed) {
return readFrom(rlpInput, revertReasonAllowed, false);
}

/**
* Creates a transaction receipt for the given RLP
*
* @param rlpInput the RLP-encoded transaction receipt
* @param revertReasonAllowed whether the rlp input is allowed to have a revert reason
* @param keepRlp whether to store the rlp with the receipt
* @return the transaction receipt
*/
public static TransactionReceipt readFrom(
final RLPInput rlpInput, final boolean revertReasonAllowed, final boolean keepRlp) {
RLPInput input = rlpInput;
TransactionType transactionType = TransactionType.FRONTIER;
if (!rlpInput.nextIsList()) {
Expand Down Expand Up @@ -271,18 +307,23 @@ public static TransactionReceipt readFrom(
revertReason = Optional.of(input.readBytes());
}

Bytes raw = null;
if (keepRlp) {
raw = input.raw();
}

// Status code-encoded transaction receipts have a single
// byte for success (0x01) or failure (0x80).
if (firstElement.raw().size() == 1) {
final int status = firstElement.readIntScalar();
input.leaveList();
return new TransactionReceipt(
transactionType, status, cumulativeGas, logs, bloomFilter, revertReason);
transactionType, status, cumulativeGas, logs, bloomFilter, revertReason, raw);
} else {
final Hash stateRoot = Hash.wrap(firstElement.readBytes32());
input.leaveList();
return new TransactionReceipt(
transactionType, stateRoot, cumulativeGas, logs, bloomFilter, revertReason);
transactionType, stateRoot, cumulativeGas, logs, bloomFilter, revertReason, raw);
}
}

Expand Down Expand Up @@ -353,6 +394,10 @@ public Optional<Bytes> getRevertReason() {
return revertReason;
}

public Optional<Bytes> getRlp() {
return Optional.ofNullable(rlp);
}

@Override
public boolean equals(final Object obj) {
if (obj == this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,21 @@ public static Hash receiptsRoot(final List<TransactionReceipt> receipts) {

IntStream.range(0, receipts.size())
.forEach(
i ->
trie.put(
indexKey(i),
RLP.encode(
rlpOutput ->
receipts.get(i).writeToForReceiptTrie(rlpOutput, false, false))));
i -> {
final TransactionReceipt receipt = receipts.get(i);
if (receipt.getRlp().isPresent()) {
System.out.println(receipt.getRlp().get());
}
System.out.println(
RLP.encode(rlpOutput -> receipt.writeToForReceiptTrie(rlpOutput, false, false)));
System.out.println();
trie.put(
indexKey(i),
receipt.getRlp().isPresent()
? receipt.getRlp().get()
: RLP.encode(
rlpOutput -> receipt.writeToForReceiptTrie(rlpOutput, false, false)));
});

return Hash.wrap(trie.getRootHash());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected Optional<Map<BlockHeader, List<TransactionReceipt>>> processResponse(
}

final ReceiptsMessage receiptsMessage = ReceiptsMessage.readFrom(message);
final List<List<TransactionReceipt>> receiptsByBlock = receiptsMessage.receipts();
final List<List<TransactionReceipt>> receiptsByBlock = receiptsMessage.receipts(true);
if (receiptsByBlock.isEmpty()) {
return Optional.empty();
} else if (receiptsByBlock.size() > blockHeaders.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,18 @@ public int getCode() {
}

public List<List<TransactionReceipt>> receipts() {
return receipts(false);
}

public List<List<TransactionReceipt>> receipts(final boolean keepRlp) {
final RLPInput input = new BytesValueRLPInput(data, false);
input.enterList();
final List<List<TransactionReceipt>> receipts = new ArrayList<>();
while (input.nextIsList()) {
final int setSize = input.enterList();
final List<TransactionReceipt> receiptSet = new ArrayList<>(setSize);
for (int i = 0; i < setSize; i++) {
receiptSet.add(TransactionReceipt.readFrom(input, false));
receiptSet.add(TransactionReceipt.readFrom(input.readAsRlp(), false, keepRlp));
}
input.leaveList();
receipts.add(receiptSet);
Expand Down
Loading