Skip to content
Merged
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
@@ -0,0 +1,84 @@
/*
* 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 class SyncTransactionReceipt {

private final Bytes rlpBytes;
private Bytes transactionTypeCode;
private Bytes statusOrStateRoot;
private Bytes cumulativeGasUsed;
private Bytes bloomFilter;
private List<List<Bytes>> logs;

public SyncTransactionReceipt(final Bytes rlpBytes) {
this.rlpBytes = rlpBytes;
}

public SyncTransactionReceipt(
final Bytes rlpBytes,
final Bytes transactionTypeCode,
final Bytes statusOrStateRoot,
final Bytes cumulativeGasUsed,
final Bytes bloomFilter,
final List<List<Bytes>> logs) {
this.rlpBytes = rlpBytes;
this.transactionTypeCode = transactionTypeCode;
this.statusOrStateRoot = statusOrStateRoot;
this.cumulativeGasUsed = cumulativeGasUsed;
this.bloomFilter = bloomFilter;
this.logs = logs;
}

public Bytes getRlpBytes() {
return rlpBytes;
}

public Bytes getTransactionTypeCode() {
return transactionTypeCode;
}

public Bytes getStatusOrStateRoot() {
return statusOrStateRoot;
}

public Bytes getCumulativeGasUsed() {
return cumulativeGasUsed;
}

public Bytes getBloomFilter() {
return bloomFilter;
}

public List<List<Bytes>> getLogs() {
return logs;
}

/**
* Clears all variables except rlpBytes to allow garbage collection immediately instead of after
* the receipt has been fully processed
*/
public void clearSubVariables() {
transactionTypeCode = null;
statusOrStateRoot = null;
cumulativeGasUsed = null;
bloomFilter = null;
logs = null;
}
}
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 cumulativeGasUsed,
final Bytes bloomFilter) {
Bytes transactionTypeCode = Bytes.of(TransactionType.FRONTIER.getSerializedType());
List<List<Bytes>> logs = parseLogs(input);
return new SyncTransactionReceipt(
rawRlp,
transactionTypeCode,
statusOrStateRoot,
cumulativeGasUsed,
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.hash(Bytes.random(32));
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.getRlpBytes());
Assertions.assertEquals(
Bytes.of(TransactionType.FRONTIER.getSerializedType()),
syncTransactionReceipt.getTransactionTypeCode());
Assertions.assertEquals(stateRoot, syncTransactionReceipt.getStatusOrStateRoot());
Assertions.assertEquals(
Bytes.of((byte) cumulativeGasUsed), syncTransactionReceipt.getCumulativeGasUsed());
Assertions.assertEquals(bloomFilter, syncTransactionReceipt.getBloomFilter());
}

@Test
public void testDecodeEth69Receipt() {
final Hash stateRoot = Hash.hash(Bytes.random(32));
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.getRlpBytes());
Assertions.assertEquals(
Bytes.of(TransactionType.FRONTIER.getEthSerializedType()),
syncTransactionReceipt.getTransactionTypeCode());
Assertions.assertEquals(stateRoot, syncTransactionReceipt.getStatusOrStateRoot());
Assertions.assertEquals(
Bytes.of((byte) cumulativeGasUsed), syncTransactionReceipt.getCumulativeGasUsed());
Assertions.assertEquals(1, syncTransactionReceipt.getLogs().size());
Assertions.assertEquals(3, syncTransactionReceipt.getLogs().getFirst().size());
String expectedBloomFilterHex =
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000010800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000020";
Assertions.assertEquals(
expectedBloomFilterHex, syncTransactionReceipt.getBloomFilter().toHexString());
}

@Test
public void testDecodeTypedReceipt() {
final TransactionType transactionType = TransactionType.EIP1559;
final Hash stateRoot = Hash.hash(Bytes.random(32));
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.getRlpBytes());
Assertions.assertEquals(
Bytes.of(transactionType.getSerializedType()),
syncTransactionReceipt.getTransactionTypeCode());
Assertions.assertEquals(stateRoot, syncTransactionReceipt.getStatusOrStateRoot());
Assertions.assertEquals(
Bytes.of((byte) cumulativeGasUsed), syncTransactionReceipt.getCumulativeGasUsed());
Assertions.assertEquals(bloomFilter, syncTransactionReceipt.getBloomFilter());
}
}
Loading