Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -0,0 +1,27 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.core;

import java.util.List;

import org.apache.tuweni.bytes.Bytes;

public record SyncTransactionReceipt(
Bytes rlpBytes,
Bytes transactionTypeCode,
Bytes statusOrStateRoot,
Bytes cumulativeGasUsed,
Bytes bloomFilter,
List<List<Bytes>> logs) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.core.encoding.receipt;

import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.ethereum.core.SyncTransactionReceipt;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.evm.log.LogsBloomFilter;

import java.util.ArrayList;
import java.util.List;

import org.apache.tuweni.bytes.Bytes;

public class SyncTransactionReceiptDecoder {

public SyncTransactionReceipt decode(final Bytes rawRlp) {
RLPInput rlpInput = RLP.input(rawRlp);
// The first byte indicates whether the receipt is typed (eth/68) or flat (eth/69).
SyncTransactionReceipt result;
if (!rlpInput.nextIsList()) {
result = decodeTypedReceipt(rawRlp, rlpInput);
} else {
result = decodeFlatReceipt(rawRlp, rlpInput);
}
return result;
}

private SyncTransactionReceipt decodeTypedReceipt(final Bytes rawRlp, final RLPInput rlpInput) {
RLPInput input = rlpInput;
Bytes transactionTypeCode = input.readBytes();
input = new BytesValueRLPInput(transactionTypeCode.slice(1), false);
transactionTypeCode = transactionTypeCode.slice(0, 1);

input.enterList();
Bytes statusOrStateRoot = input.readBytes();
Bytes cumulativeGasUsed = input.readBytes();
final boolean isCompacted = isNextNotBloomFilter(input);
Bytes bloomFilter = null;
if (!isCompacted) {
bloomFilter = input.readBytes();
}
List<List<Bytes>> logs = parseLogs(input);
// if the receipt is compacted, we need to build the bloom filter from the logs
if (isCompacted) {
bloomFilter = LogsBloomFilter.builder().insertRawLogs(logs).build();
}
input.leaveList();
return new SyncTransactionReceipt(
rawRlp, transactionTypeCode, statusOrStateRoot, cumulativeGasUsed, bloomFilter, logs);
}

private SyncTransactionReceipt decodeFlatReceipt(final Bytes rawRlp, final RLPInput rlpInput) {
rlpInput.enterList();
// Flat receipts can be either legacy or eth/69 receipts.
// To determine the type, we need to examine the logs' position, as the bloom filter cannot be
// used. This is because compacted legacy receipts also lack a bloom filter.
// The first element can be either the transaction type (eth/69) or stateRootOrStatus (eth/68)
final Bytes firstElement = rlpInput.readBytes();
// The second element can be either the state root or status (eth/68) or stateRootOrStatus
// (eth/69)
final Bytes secondElement = rlpInput.readBytes();
final boolean isCompacted = isNextNotBloomFilter(rlpInput);
Bytes bloomFilter = null;
if (!isCompacted) {
bloomFilter = rlpInput.readBytes();
}
boolean isEth69Receipt = isCompacted && !rlpInput.nextIsList();
SyncTransactionReceipt result;
if (isEth69Receipt) {
result = decodeEth69Receipt(rawRlp, rlpInput, firstElement, secondElement);
} else {
result = decodeLegacyReceipt(rawRlp, rlpInput, firstElement, secondElement, bloomFilter);
}
rlpInput.leaveList();
return result;
}

private SyncTransactionReceipt decodeEth69Receipt(
final Bytes rawRlp,
final RLPInput input,
final Bytes transactionByteRlp,
final Bytes statusOrStateRoot) {
Bytes transactionTypeCode =
transactionByteRlp.isEmpty()
? Bytes.of(TransactionType.FRONTIER.getSerializedType())
: transactionByteRlp;
Bytes cumulativeGasUsed = input.readBytes();
List<List<Bytes>> logs = parseLogs(input);
Bytes bloomFilter = LogsBloomFilter.builder().insertRawLogs(logs).build();
Copy link
Contributor

Choose a reason for hiding this comment

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

do we actually need the bloomFilter later when we received an eth69 receipt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I wrote this class to mirror the functionality of the TransactionReceiptDecoder, which does give eth69 receipts a bloom filter in the same manner. This may be removed if we don't need bloom filters for syncing.

return new SyncTransactionReceipt(
rawRlp, transactionTypeCode, statusOrStateRoot, cumulativeGasUsed, bloomFilter, logs);
}

private SyncTransactionReceipt decodeLegacyReceipt(
final Bytes rawRlp,
final RLPInput input,
final Bytes statusOrStateRoot,
final Bytes cumulativeGas,
final Bytes bloomFilter) {
Bytes transactionTypeCode = Bytes.of(TransactionType.FRONTIER.getSerializedType());
List<List<Bytes>> logs = parseLogs(input);
return new SyncTransactionReceipt(
rawRlp,
transactionTypeCode,
statusOrStateRoot,
cumulativeGas,
bloomFilter == null ? LogsBloomFilter.builder().insertRawLogs(logs).build() : bloomFilter,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think when we are syncing we should not have to deal with the case that we have a legacy receipt without bloom filter. I think that it only us removing the bloom filter when we are storing receipts in a compacted form in the db.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I'll have to double check the PoC branch, but removing the bloom filter stuff should simplify this a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, we do need the bloom filters to calculate the receipts root

logs);
}

private List<List<Bytes>> parseLogs(final RLPInput input) {
return input.readList(
logInput -> {
logInput.enterList();

final Bytes logger = logInput.readBytes();

final List<Bytes> topics = logInput.readList(RLPInput::readBytes32);
final Bytes data = logInput.readBytes();

logInput.leaveList();
List<Bytes> result = new ArrayList<>(topics.size() + 2);
result.add(logger);
result.addAll(topics);
result.add(data);
return result;
});
}

private boolean isNextNotBloomFilter(final RLPInput input) {
return input.nextIsList() || input.nextSize() != LogsBloomFilter.BYTE_SIZE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.core.encoding.receipt;

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.ethereum.core.SyncTransactionReceipt;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.evm.log.Log;
import org.hyperledger.besu.evm.log.LogTopic;
import org.hyperledger.besu.evm.log.LogsBloomFilter;

import java.util.List;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SyncTransactionReceiptDecoderTest {

private SyncTransactionReceiptDecoder syncTransactionReceiptDecoder;

@BeforeEach
public void beforeTest() {
syncTransactionReceiptDecoder = new SyncTransactionReceiptDecoder();
}

@Test
public void testDecodeLegacyReceipt() {
final Hash stateRoot = Hash.fromHexStringLenient("01");
Copy link
Contributor

Choose a reason for hiding this comment

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

01 should be the status then, not the state root.
A legacy receipt can contain the state root, so maybe we should add a test with a state root as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case it's just using 01 as some test data. As it's used in the Hash class, it's always going to be 32 bytes long and the content doesn't actually matter, as long as it remains the same in the result

Copy link
Contributor Author

@Matilda-Clerke Matilda-Clerke Jan 8, 2026

Choose a reason for hiding this comment

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

I've changed these to a random 32 byte value instead, to make it clearer that this isn't a status

There's also no reason to write a separate test for receipt with a status instead of stateRoot as the code doesn't do anything different either way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm, state root should only be possible in legacy receipts. I will have another look at the code :-)

final long cumulativeGasUsed = 2;
final List<Log> logs =
List.of(
new Log(
Address.fromHexString("03"),
Bytes.fromHexStringLenient("04"),
List.of(LogTopic.fromHexString("05"))));
final LogsBloomFilter bloomFilter = LogsBloomFilter.fromHexString("0x" + "deadbeef".repeat(64));
final Optional<Bytes> revertReason = Optional.of(Bytes.fromHexString("06"));
TransactionReceipt transactionReceipt =
new TransactionReceipt(
TransactionType.FRONTIER,
stateRoot,
cumulativeGasUsed,
logs,
bloomFilter,
revertReason);

Bytes encodedReceipt =
RLP.encode(
(rlpOut) ->
TransactionReceiptEncoder.writeTo(
transactionReceipt, rlpOut, TransactionReceiptEncodingConfiguration.DEFAULT));

SyncTransactionReceipt syncTransactionReceipt =
syncTransactionReceiptDecoder.decode(encodedReceipt);

Assertions.assertEquals(encodedReceipt, syncTransactionReceipt.rlpBytes());
Assertions.assertEquals(
Bytes.of(TransactionType.FRONTIER.getSerializedType()),
syncTransactionReceipt.transactionTypeCode());
Assertions.assertEquals(stateRoot, syncTransactionReceipt.statusOrStateRoot());
Assertions.assertEquals(
Bytes.of((byte) cumulativeGasUsed), syncTransactionReceipt.cumulativeGasUsed());
Assertions.assertEquals(bloomFilter, syncTransactionReceipt.bloomFilter());
}

@Test
public void testDecodeEth69Receipt() {
final Hash stateRoot = Hash.fromHexStringLenient("01");
final long cumulativeGasUsed = 2;
final List<Log> logs =
List.of(
new Log(
Address.fromHexString("03"),
Bytes.fromHexStringLenient("04"),
List.of(LogTopic.fromHexString("05"))));
TransactionReceipt transactionReceipt =
new TransactionReceipt(stateRoot, cumulativeGasUsed, logs, Optional.empty());

Bytes encodedReceipt =
RLP.encode(
(rlpOut) ->
TransactionReceiptEncoder.writeTo(
transactionReceipt,
rlpOut,
TransactionReceiptEncodingConfiguration.ETH69_RECEIPT_CONFIGURATION));

SyncTransactionReceipt syncTransactionReceipt =
syncTransactionReceiptDecoder.decode(encodedReceipt);

Assertions.assertEquals(encodedReceipt, syncTransactionReceipt.rlpBytes());
Assertions.assertEquals(
Bytes.of(TransactionType.FRONTIER.getEthSerializedType()),
syncTransactionReceipt.transactionTypeCode());
Assertions.assertEquals(stateRoot, syncTransactionReceipt.statusOrStateRoot());
Assertions.assertEquals(
Bytes.of((byte) cumulativeGasUsed), syncTransactionReceipt.cumulativeGasUsed());
Assertions.assertEquals(1, syncTransactionReceipt.logs().size());
Assertions.assertEquals(3, syncTransactionReceipt.logs().getFirst().size());
String expectedBloomFilterHex =
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000010800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000020";
Assertions.assertEquals(
expectedBloomFilterHex, syncTransactionReceipt.bloomFilter().toHexString());
}

@Test
public void testDecodeTypedReceipt() {
final TransactionType transactionType = TransactionType.EIP1559;
final Hash stateRoot = Hash.fromHexStringLenient("01");
final long cumulativeGasUsed = 2;
final List<Log> logs =
List.of(
new Log(
Address.fromHexString("03"),
Bytes.fromHexStringLenient("04"),
List.of(LogTopic.fromHexString("05"))));
final LogsBloomFilter bloomFilter = LogsBloomFilter.fromHexString("0x" + "deadbeef".repeat(64));
final Optional<Bytes> revertReason = Optional.of(Bytes.fromHexString("06"));
TransactionReceipt transactionReceipt =
new TransactionReceipt(
transactionType, stateRoot, cumulativeGasUsed, logs, bloomFilter, revertReason);

Bytes encodedReceipt =
RLP.encode(
(rlpOut) ->
TransactionReceiptEncoder.writeTo(
transactionReceipt, rlpOut, TransactionReceiptEncodingConfiguration.DEFAULT));

SyncTransactionReceipt syncTransactionReceipt =
syncTransactionReceiptDecoder.decode(encodedReceipt);

Assertions.assertEquals(encodedReceipt, syncTransactionReceipt.rlpBytes());
Assertions.assertEquals(
Bytes.of(transactionType.getSerializedType()),
syncTransactionReceipt.transactionTypeCode());
Assertions.assertEquals(stateRoot, syncTransactionReceipt.statusOrStateRoot());
Assertions.assertEquals(
Bytes.of((byte) cumulativeGasUsed), syncTransactionReceipt.cumulativeGasUsed());
Assertions.assertEquals(bloomFilter, syncTransactionReceipt.bloomFilter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hyperledger.besu.ethereum.rlp.RLPInput;

import java.util.Collection;
import java.util.List;

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.DelegatingBytes;
Expand Down Expand Up @@ -184,6 +185,21 @@ public Builder insertLog(final Log log) {
return this;
}

/**
* Insert raw log.
*
* @param loggerAddress the address of the logger
* @param logTopics the log topics
* @return the builder
*/
public Builder insertRawLog(final Bytes loggerAddress, final List<Bytes> logTopics) {
insertBytes(loggerAddress);
for (Bytes logTopic : logTopics) {
insertBytes(logTopic);
}
return this;
}

/**
* Insert logs.
*
Expand All @@ -195,6 +211,20 @@ public Builder insertLogs(final Collection<Log> logs) {
return this;
}

/**
* Insert raw logs.
*
* @param logs the logs with each log separated into components, ordered like [logger
* address][topics...][data]
* @return the builder
*/
public Builder insertRawLogs(final Collection<List<Bytes>> logs) {
logs.forEach(
(bytesList) ->
insertRawLog(bytesList.getFirst(), bytesList.subList(1, bytesList.size() - 1)));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we could just call insertBytes for each log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought so too initially, but it seems the data portion of the log is actually not included in the bloom filter. This is why you can see I'm using a subList on the second parameter.

The insertRawLog method also makes it clear and explicit exactly which Bytes is the address and which Bytes are log topics, which imo is preferable to just inserting them into the filter.

return this;
}

/**
* Insert bytes.
*
Expand Down
Loading