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

Refactor Transaction rlp encoding #7871

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.plugin.data.PrivateTransaction;
import org.hyperledger.besu.plugin.data.UnsignedPrivateMarkerTransaction;
import org.hyperledger.besu.plugin.services.privacy.PrivateMarkerTransactionFactory;
Expand Down Expand Up @@ -72,9 +71,7 @@ public Bytes create(

LOG.info("Signing PMT from {}", sender);

final BytesValueRLPOutput out = new BytesValueRLPOutput();
transaction.writeTo(out);
return out.encoded();
return transaction.encoded();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

import org.hyperledger.besu.cli.util.VersionProvider;
import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;

import java.io.BufferedReader;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -105,7 +104,7 @@ public void run() {

void dump(final Bytes tx) {
try {
var transaction = TransactionDecoder.decodeOpaqueBytes(tx, EncodingContext.BLOCK_BODY);
var transaction = RlpProvider.transaction().decodeOpaqueBytes(tx);

// https://github.com/hyperledger/besu/blob/5fe49c60b30fe2954c7967e8475c3b3e9afecf35/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java#L252
if (transaction.getChainId().isPresent() && !transaction.getChainId().get().equals(chainId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.hyperledger.besu.ethereum.core.LogWithMetadata;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.eth.EthProtocol;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
Expand Down Expand Up @@ -129,7 +130,7 @@ DataFetcher<Optional<Bytes32>> getSendRawTransactionDataFetcher() {
dataFetchingEnvironment.getGraphQlContext().get(GraphQLContextType.TRANSACTION_POOL);
final Bytes rawTran = dataFetchingEnvironment.getArgument("data");

final Transaction transaction = Transaction.readFrom(RLP.input(rawTran));
final Transaction transaction = RlpProvider.transaction().readFrom(RLP.input(rawTran));
final ValidationResult<TransactionInvalidReason> validationResult =
transactionPool.addTransactionViaApi(transaction);
if (validationResult.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,7 @@ public List<AccessListEntryAdapter> getAccessList() {
* @return an Optional containing a Bytes object representing the raw transaction data.
*/
public Optional<Bytes> getRaw() {
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput();
transactionWithMetadata.getTransaction().writeTo(rlpOutput);
return Optional.of(rlpOutput.encoded());
return Optional.of(transactionWithMetadata.getTransaction().encoded());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;

public class DebugGetRawTransaction implements JsonRpcMethod {

Expand Down Expand Up @@ -61,8 +60,6 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
}

private String toRawString(final Transaction transaction) {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
transaction.writeTo(out);
return out.encoded().toHexString();
return transaction.encoded().toHexString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
import org.hyperledger.besu.ethereum.core.Request;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.eth.manager.EthPeers;
import org.hyperledger.besu.ethereum.mainnet.BodyValidation;
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions;
Expand Down Expand Up @@ -221,7 +220,7 @@ public JsonRpcResponse syncResponse(final JsonRpcRequestContext requestContext)
transactions =
blockParam.getTransactions().stream()
.map(Bytes::fromHexString)
.map(in -> TransactionDecoder.decodeOpaqueBytes(in, EncodingContext.BLOCK_BODY))
.map(RlpProvider.transaction()::decodeOpaqueBytes)
.collect(Collectors.toList());
transactions.forEach(
transaction ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
import org.hyperledger.besu.ethereum.privacy.PrivateTransaction;
Expand Down Expand Up @@ -155,7 +156,7 @@ protected Transaction createPrivateMarkerTransaction(
final Bytes rlpBytes =
privateMarkerTransactionFactory.create(
unsignedPrivateMarkerTransaction, privateTransaction, privacyUserId);
return Transaction.readFrom(rlpBytes);
return RlpProvider.transaction().readFrom(rlpBytes);
}

protected abstract long getGasLimit(PrivateTransaction privateTransaction, String pmtPayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Request;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;

import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -96,9 +95,7 @@ public BlockResult transactionComplete(final Block block) {
public EngineGetPayloadResultV1 payloadTransactionCompleteV1(final Block block) {
final List<String> txs =
block.getBody().getTransactions().stream()
.map(
transaction ->
TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.BLOCK_BODY))
.map(RlpProvider.transaction()::encodeOpaqueBytes)
.map(Bytes::toHexString)
.collect(Collectors.toList());

Expand All @@ -109,9 +106,7 @@ public EngineGetPayloadResultV2 payloadTransactionCompleteV2(final PayloadWrappe
final var blockWithReceipts = payload.blockWithReceipts();
final List<String> txs =
blockWithReceipts.getBlock().getBody().getTransactions().stream()
.map(
transaction ->
TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.BLOCK_BODY))
.map(RlpProvider.transaction()::encodeOpaqueBytes)
.map(Bytes::toHexString)
.collect(Collectors.toList());

Expand All @@ -135,9 +130,7 @@ public EngineGetPayloadResultV3 payloadTransactionCompleteV3(final PayloadWrappe
final var blockWithReceipts = payload.blockWithReceipts();
final List<String> txs =
blockWithReceipts.getBlock().getBody().getTransactions().stream()
.map(
transaction ->
TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.BLOCK_BODY))
.map(RlpProvider.transaction()::encodeOpaqueBytes)
.map(Bytes::toHexString)
.collect(Collectors.toList());

Expand All @@ -155,9 +148,7 @@ public EngineGetPayloadResultV4 payloadTransactionCompleteV4(final PayloadWrappe
final var blockWithReceipts = payload.blockWithReceipts();
final List<String> txs =
blockWithReceipts.getBlock().getBody().getTransactions().stream()
.map(
transaction ->
TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.BLOCK_BODY))
.map(RlpProvider.transaction()::encodeOpaqueBytes)
.map(Bytes::toHexString)
.collect(Collectors.toList());
final Optional<List<String>> requestsWithoutRequestId =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.WithdrawalParameter;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -53,9 +52,7 @@ public static class PayloadBody {
public PayloadBody(final BlockBody blockBody) {
this.transactions =
blockBody.getTransactions().stream()
.map(
transaction ->
TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.BLOCK_BODY))
.map(RlpProvider.transaction()::encodeOpaqueBytes)
.map(Bytes::toHexString)
.collect(Collectors.toList());
this.withdrawals =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import org.hyperledger.besu.datatypes.VersionedHash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;

import java.util.List;

Expand Down Expand Up @@ -102,9 +101,7 @@ public TransactionPendingResult(final Transaction transaction) {
this.input = transaction.getPayload().toString();
this.nonce = Quantity.create(transaction.getNonce());
this.publicKey = transaction.getPublicKey().orElse(null);
this.raw =
TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.POOLED_TRANSACTION)
.toString();
this.raw = RlpProvider.pooledTransaction().encodeOpaqueBytes(transaction).toString();
this.to = transaction.getTo().map(Address::toHexString).orElse(null);
if (transactionType == TransactionType.FRONTIER) {
this.type = Quantity.create(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcRequestException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.rlp.RLPException;

import org.apache.tuweni.bytes.Bytes;
Expand All @@ -29,7 +28,7 @@ public static Transaction decodeRawTransaction(final String rawTransaction)
throws InvalidJsonRpcRequestException {
try {
Bytes txnBytes = Bytes.fromHexString(rawTransaction);
return TransactionDecoder.decodeOpaqueBytes(txnBytes, EncodingContext.POOLED_TRANSACTION);
return RlpProvider.pooledTransaction().decodeOpaqueBytes(txnBytes);
} catch (final IllegalArgumentException e) {
throw new InvalidJsonRpcRequestException(
"Invalid raw transaction hex", RpcErrorType.INVALID_TRANSACTION_PARAMS, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;

Expand Down Expand Up @@ -108,8 +109,7 @@ void shouldReturnNullResultWhenTransactionDoesNotExist() {
@Test
void shouldReturnPendingTransactionWhenTransactionExistsAndIsPending() {
final org.hyperledger.besu.ethereum.core.Transaction transaction =
org.hyperledger.besu.ethereum.core.Transaction.readFrom(
Bytes.fromHexString(VALID_TRANSACTION));
RlpProvider.transaction().readFrom(Bytes.fromHexString(VALID_TRANSACTION));

when(transactionPool.getTransactionByHash(transaction.getHash()))
.thenReturn(Optional.of(transaction));
Expand All @@ -131,8 +131,7 @@ void shouldReturnPendingTransactionWhenTransactionExistsAndIsPending() {
@Test
void shouldReturnCompleteTransactionWhenTransactionExistsInBlockchain() {
final org.hyperledger.besu.ethereum.core.Transaction transaction =
org.hyperledger.besu.ethereum.core.Transaction.readFrom(
Bytes.fromHexString(VALID_TRANSACTION));
RlpProvider.transaction().readFrom(Bytes.fromHexString(VALID_TRANSACTION));
final TransactionWithMetadata transactionWithMetadata =
new TransactionWithMetadata(transaction, 1, Optional.empty(), Hash.ZERO, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.core.Withdrawal;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.mainnet.BodyValidation;
import org.hyperledger.besu.ethereum.mainnet.CancunTargetingGasLimitCalculator;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
Expand Down Expand Up @@ -226,8 +225,7 @@ public void shouldValidateExcessBlobGasCorrectly() {
public void shouldRejectTransactionsWithFullBlobs() {

Bytes transactionWithBlobsBytes =
TransactionEncoder.encodeOpaqueBytes(
createTransactionWithBlobs(), EncodingContext.POOLED_TRANSACTION);
RlpProvider.pooledTransaction().encodeOpaqueBytes(createTransactionWithBlobs());

List<String> transactions = List.of(transactionWithBlobsBytes.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.EncodingContext;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;

import java.math.BigInteger;
Expand Down Expand Up @@ -58,7 +57,7 @@ public class DomainObjectDecodeUtilsTest {
@Test
public void testAccessListRLPSerDes() {
final BytesValueRLPOutput encoded = new BytesValueRLPOutput();
TransactionEncoder.encodeRLP(accessListTxn, encoded, EncodingContext.POOLED_TRANSACTION);
RlpProvider.pooledTransaction().writeTo(accessListTxn, encoded);
Transaction decoded =
DomainObjectDecodeUtils.decodeRawTransaction(encoded.encoded().toHexString());
Assertions.assertThat(decoded.getAccessList().isPresent()).isTrue();
Expand All @@ -67,8 +66,7 @@ public void testAccessListRLPSerDes() {

@Test
public void testAccessList2718OpaqueSerDes() {
final Bytes encoded =
TransactionEncoder.encodeOpaqueBytes(accessListTxn, EncodingContext.POOLED_TRANSACTION);
final Bytes encoded = RlpProvider.pooledTransaction().encodeOpaqueBytes(accessListTxn);
Transaction decoded = DomainObjectDecodeUtils.decodeRawTransaction(encoded.toString());
Assertions.assertThat(decoded.getAccessList().isPresent()).isTrue();
Assertions.assertThat(decoded.getAccessList().map(List::size).get()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.hyperledger.besu.ethereum.core.ExecutionContextTestFixture;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.debug.TraceFrame;
import org.hyperledger.besu.ethereum.debug.TraceOptions;
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
Expand Down Expand Up @@ -170,8 +171,8 @@ public void shouldTraceContractCreation() {
final DebugOperationTracer tracer =
new DebugOperationTracer(new TraceOptions(true, true, true), false);
final Transaction transaction =
Transaction.readFrom(
new BytesValueRLPInput(Bytes.fromHexString(CONTRACT_CREATION_TX), false));
RlpProvider.transaction()
.readFrom(new BytesValueRLPInput(Bytes.fromHexString(CONTRACT_CREATION_TX), false));
final BlockHeader genesisBlockHeader = genesisBlock.getHeader();
transactionProcessor.processTransaction(
worldStateArchive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.core;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void writeTo(final RLPOutput out) {
out.startList();

header.writeTo(out);
out.writeList(body.getTransactions(), Transaction::writeTo);
out.writeList(body.getTransactions(), RlpProvider.transaction()::writeTo);
out.writeList(body.getOmmers(), BlockHeader::writeTo);
body.getWithdrawals().ifPresent(withdrawals -> out.writeList(withdrawals, Withdrawal::writeTo));

Expand All @@ -69,7 +70,7 @@ public void writeTo(final RLPOutput out) {
public static Block readFrom(final RLPInput in, final BlockHeaderFunctions hashFunction) {
in.enterList();
final BlockHeader header = BlockHeader.readFrom(in, hashFunction);
final List<Transaction> transactions = in.readList(Transaction::readFrom);
final List<Transaction> transactions = in.readList(RlpProvider.transaction()::readFrom);
final List<BlockHeader> ommers = in.readList(rlp -> BlockHeader.readFrom(rlp, hashFunction));
final Optional<List<Withdrawal>> withdrawals =
in.isEndOfCurrentList() ? Optional.empty() : Optional.of(in.readList(Withdrawal::readFrom));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.core;

import org.hyperledger.besu.ethereum.core.encoding.registry.RlpProvider;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;

Expand Down Expand Up @@ -95,7 +96,7 @@ public void writeWrappedBodyTo(final RLPOutput output) {
}

public void writeTo(final RLPOutput output) {
output.writeList(getTransactions(), Transaction::writeTo);
output.writeList(getTransactions(), RlpProvider.transaction()::writeTo);
output.writeList(getOmmers(), BlockHeader::writeTo);
withdrawals.ifPresent(withdrawals -> output.writeList(withdrawals, Withdrawal::writeTo));
}
Expand Down Expand Up @@ -142,7 +143,7 @@ public static BlockBody readWrappedBodyFrom(
public static BlockBody readFrom(
final RLPInput input, final BlockHeaderFunctions blockHeaderFunctions) {
return new BlockBody(
input.readList(Transaction::readFrom),
input.readList(RlpProvider.transaction()::readFrom),
input.readList(rlp -> BlockHeader.readFrom(rlp, blockHeaderFunctions)),
input.isEndOfCurrentList()
? Optional.empty()
Expand Down
Loading
Loading