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 10 commits into
base: main
Choose a base branch
from
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,13 @@ 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 && !compacted) {
// at this point we can ignore withRevertReason, because we would only have the rlp if the
// receipt was received via p2p, which means the receipt does not contain the revert reason.
rlpOutput.writeRaw(rlp);
return;
}

if (!transactionType.equals(TransactionType.FRONTIER)) {
rlpOutput.writeIntScalar(transactionType.getSerializedType());
}
Expand Down Expand Up @@ -233,6 +258,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 All @@ -249,13 +287,14 @@ public static TransactionReceipt readFrom(

LogsBloomFilter bloomFilter = null;

final boolean hasLogs = !input.nextIsList() && input.nextSize() == LogsBloomFilter.BYTE_SIZE;
if (hasLogs) {
final boolean hasBloomFilter =
!input.nextIsList() && input.nextSize() == LogsBloomFilter.BYTE_SIZE;
if (hasBloomFilter) {
// The logs below will populate the bloom filter upon construction.
bloomFilter = LogsBloomFilter.readFrom(input);
}
// TODO consider validating that the logs and bloom filter match.
final boolean compacted = !hasLogs;
final boolean compacted = !hasBloomFilter;
final List<Log> logs = input.readList(logInput -> Log.readFrom(logInput, compacted));
if (compacted) {
bloomFilter = LogsBloomFilter.builder().insertLogs(logs).build();
Expand All @@ -271,18 +310,23 @@ public static TransactionReceipt readFrom(
revertReason = Optional.of(input.readBytes());
}

Bytes rlpBytes = null;
if (keepRlp) {
rlpBytes = rlpInput.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, rlpBytes);
} 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, rlpBytes);
}
}

Expand Down Expand Up @@ -353,6 +397,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,15 @@ 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);
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
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public int enterList() {
*
* @see #enterList()
* @param skipCount true if the element count is not required.
* @return -1 if skipCount==true, otherwise, the number of item of the entered list.
* @return -1 if skipCount==true, otherwise, the number of items of the entered list.
*/
public int enterList(final boolean skipCount) {
if (currentItem >= size) {
Expand Down
Loading