Skip to content
Open
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,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,147 @@
/*
* 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The first element can be either the transaction type (eth/69 or stateRootOrStatus (eth/68
// 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 cumulative gas (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();
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,
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,83 @@
/*
* 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 testDecode() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public void testDecode() {
public void testDecodeLegacyReceipt() {

Copy link
Contributor

Choose a reason for hiding this comment

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

is this testing legacy, or eth/69 ?

can you add test coverage for the other decode methods?

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.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());
}
}
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)));
return this;
}

/**
* Insert bytes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ void logsBloomFilter() {
Bytes.fromHexString(
"0x00000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000040000000000000000000000000000000000000000000000000000000"));
}

@Test
void testInsertRawLog() {
final Bytes address = Address.fromHexString("0x095e7baea6a6c7c4c2dfeb977efac326af552d87");
final List<Bytes> topics = new ArrayList<>();
topics.add(
Bytes.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000000"));

final LogsBloomFilter bloom = LogsBloomFilter.builder().insertRawLog(address, topics).build();

Assertions.assertThat(bloom)
.isEqualTo(
Bytes.fromHexString(
"0x00000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000040000000000000000000000000000000000000000000000000000000"));
}
}