Decouple encoding from transaction

Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
pull/7871/head
Gabriel-Trintinalia 3 weeks ago
parent 054b66c355
commit 2bcf0ac808
  1. 4
      acceptance-tests/test-plugins/src/main/java/org/hyperledger/besu/tests/acceptance/plugins/privacy/TestSigningPrivateMarkerTransactionFactory.java
  2. 4
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/internal/pojoadapter/TransactionAdapter.java
  3. 4
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/DebugGetRawTransaction.java
  4. 3
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Block.java
  5. 3
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/BlockBody.java
  6. 9
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java
  7. 4
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/privacy/markertransaction/SigningPrivateMarkerTransactionFactory.java
  8. 7
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/TransactionBuilderTest.java
  9. 4
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/TransactionIntegrationTest.java
  10. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/util/RawBlockIteratorTest.java
  11. 4
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/LimitedTransactionsMessages.java
  12. 3
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/messages/TransactionsMessage.java
  13. 8
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPool.java
  14. 18
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/layered/LayeredPendingTransactions.java
  15. 6
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsSorter.java
  16. 3
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EthServerTest.java
  17. 23
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/PendingTransactionEstimatedMemorySizeTest.java
  18. 3
      ethereum/evmtool/src/main/java/org/hyperledger/besu/evmtool/T8nExecutor.java
  19. 3
      ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/core/TransactionTest.java

@ -72,9 +72,7 @@ public class TestSigningPrivateMarkerTransactionFactory implements PrivateMarker
LOG.info("Signing PMT from {}", sender);
final BytesValueRLPOutput out = new BytesValueRLPOutput();
transaction.writeTo(out);
return out.encoded();
return transaction.encoded();
}
@Override

@ -527,9 +527,7 @@ public class TransactionAdapter extends AdapterBase {
* @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());
}
/**

@ -61,8 +61,6 @@ public class DebugGetRawTransaction implements JsonRpcMethod {
}
private String toRawString(final Transaction transaction) {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
transaction.writeTo(out);
return out.encoded().toHexString();
return transaction.encoded().toHexString();
}
}

@ -16,6 +16,7 @@ package org.hyperledger.besu.ethereum.core;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
@ -60,7 +61,7 @@ public class Block {
out.startList();
header.writeTo(out);
out.writeList(body.getTransactions(), Transaction::writeTo);
out.writeList(body.getTransactions(), TransactionEncoder::writeTo);
out.writeList(body.getOmmers(), BlockHeader::writeTo);
body.getWithdrawals().ifPresent(withdrawals -> out.writeList(withdrawals, Withdrawal::writeTo));

@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.core;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
@ -96,7 +97,7 @@ public class BlockBody implements org.hyperledger.besu.plugin.data.BlockBody {
}
public void writeTo(final RLPOutput output) {
output.writeList(getTransactions(), Transaction::writeTo);
output.writeList(getTransactions(), TransactionEncoder::writeTo);
output.writeList(getOmmers(), BlockHeader::writeTo);
withdrawals.ifPresent(withdrawals -> output.writeList(withdrawals, Withdrawal::writeTo));
}

@ -469,15 +469,6 @@ public class Transaction
return hashNoSignature;
}
/**
* Writes the transaction to RLP
*
* @param out the output to write the transaction to
*/
public void writeTo(final RLPOutput out) {
TransactionEncoder.writeTo(this, out);
}
@Override
public Bytes encoded() {
final BytesValueRLPOutput rplOutput = new BytesValueRLPOutput();

@ -40,8 +40,6 @@ public class SigningPrivateMarkerTransactionFactory {
.payload(unsignedPrivateMarkerTransaction.getPayload())
.signAndBuild(signingKey);
final BytesValueRLPOutput out = new BytesValueRLPOutput();
transaction.writeTo(out);
return out.encoded();
return transaction.encoded();
}
}

@ -25,6 +25,7 @@ import org.hyperledger.besu.crypto.SignatureAlgorithmFactory;
import org.hyperledger.besu.datatypes.AccessListEntry;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import java.math.BigInteger;
@ -88,10 +89,6 @@ class TransactionBuilderTest {
final Transaction copy = builder.copiedFrom(transaction).build();
assertThat(copy).isEqualTo(transaction).isNotSameAs(transaction);
assertThat(copy.getHash()).isEqualTo(transaction.getHash());
BytesValueRLPOutput sourceRLP = new BytesValueRLPOutput();
transaction.writeTo(sourceRLP);
BytesValueRLPOutput copyRLP = new BytesValueRLPOutput();
copy.writeTo(copyRLP);
assertEquals(sourceRLP.encoded(), copyRLP.encoded());
assertEquals(transaction.encoded(), copy.encoded());
}
}

@ -51,9 +51,7 @@ public class TransactionIntegrationTest {
final Bytes encoded = Bytes.fromHexString(encodedString);
final RLPInput input = RLP.input(encoded);
final Transaction transaction = TransactionDecoder.readFrom(input);
final BytesValueRLPOutput output = new BytesValueRLPOutput();
transaction.writeTo(output);
assertThat(output.encoded().toString()).isEqualTo(encodedString);
assertThat(transaction.encoded().toString()).isEqualTo(encodedString);
}
@Test

@ -21,6 +21,7 @@ import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
@ -99,7 +100,7 @@ public class RawBlockIteratorTest {
final BytesValueRLPOutput out = new BytesValueRLPOutput();
out.startList();
block.getHeader().writeTo(out);
out.writeList(block.getBody().getTransactions(), Transaction::writeTo);
out.writeList(block.getBody().getTransactions(), TransactionEncoder::writeTo);
out.writeList(block.getBody().getOmmers(), BlockHeader::writeTo);
out.endList();
return out.encoded().toArray();

@ -42,9 +42,7 @@ public final class LimitedTransactionsMessages {
int messageSize = 0;
message.startList();
for (final Transaction transaction : transactions) {
final BytesValueRLPOutput encodedTransaction = new BytesValueRLPOutput();
transaction.writeTo(encodedTransaction);
final Bytes encodedBytes = encodedTransaction.encoded();
final Bytes encodedBytes = transaction.encoded();
if (messageSize != 0 // always at least one message
&& messageSize + encodedBytes.size() > LIMIT) {
break;

@ -16,6 +16,7 @@ package org.hyperledger.besu.ethereum.eth.messages;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.AbstractMessageData;
import org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
@ -43,7 +44,7 @@ public class TransactionsMessage extends AbstractMessageData {
final BytesValueRLPOutput tmp = new BytesValueRLPOutput();
tmp.startList();
for (final Transaction transaction : transactions) {
transaction.writeTo(tmp);
TransactionEncoder.writeTo(transaction, tmp);
}
tmp.endList();
return new TransactionsMessage(tmp.encoded());

@ -800,12 +800,8 @@ public class TransactionPool implements BlockAddedObserver {
allTxs.parallelStream()
.takeWhile(unused -> !isCancelled.get())
.map(
ptx -> {
final BytesValueRLPOutput rlp = new BytesValueRLPOutput();
ptx.getTransaction().writeTo(rlp);
return (ptx.isReceivedFromLocalSource() ? "l" : "r")
+ rlp.encoded().toBase64String();
})
ptx -> (ptx.isReceivedFromLocalSource() ? "l" : "r")
+ ptx.getTransaction().encoded().toBase64String())
.mapToInt(
line -> {
synchronized (bw) {

@ -235,11 +235,7 @@ public class LayeredPendingTransactions implements PendingTransactions {
.addArgument(pendingTransaction.getTransaction().getType())
.addArgument(pendingTransaction::getHash)
.addArgument(
() -> {
final BytesValueRLPOutput rlp = new BytesValueRLPOutput();
pendingTransaction.getTransaction().writeTo(rlp);
return rlp.encoded().toHexString();
})
() -> pendingTransaction.getTransaction().encoded().toHexString())
.log();
}
@ -256,11 +252,7 @@ public class LayeredPendingTransactions implements PendingTransactions {
.addArgument(pendingTransaction.getTransaction().getType())
.addArgument(pendingTransaction::getHash)
.addArgument(
() -> {
final BytesValueRLPOutput rlp = new BytesValueRLPOutput();
pendingTransaction.getTransaction().writeTo(rlp);
return rlp.encoded().toHexString();
})
() -> pendingTransaction.getTransaction().encoded().toHexString())
.log();
LOG.atInfo()
.addMarker(INVALID_TX_REMOVED)
@ -269,11 +261,7 @@ public class LayeredPendingTransactions implements PendingTransactions {
.addKeyValue("reason", result)
.addKeyValue(
"txrlp",
() -> {
final BytesValueRLPOutput rlp = new BytesValueRLPOutput();
pendingTransaction.getTransaction().writeTo(rlp);
return rlp.encoded().toHexString();
})
() -> pendingTransaction.getTransaction().encoded().toHexString())
.log();
}

@ -278,11 +278,7 @@ public abstract class AbstractPendingTransactionsSorter implements PendingTransa
.addKeyValue("reason", result)
.addKeyValue(
"txrlp",
() -> {
final BytesValueRLPOutput rlp = new BytesValueRLPOutput();
pendingTransaction.getTransaction().writeTo(rlp);
return rlp.encoded().toHexString();
})
() -> pendingTransaction.getTransaction().encoded().toHexString())
.log();
}

@ -28,6 +28,7 @@ import org.hyperledger.besu.ethereum.core.BlockDataGenerator;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.messages.BlockBodiesMessage;
import org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage;
@ -387,7 +388,7 @@ public class EthServerTest {
}
private int calculateRlpEncodedSize(final Transaction tx) {
return RLP.encode(tx::writeTo).size();
return RLP.encode(output -> TransactionEncoder.writeTo(tx, output)).size();
}
private int calculateRlpEncodedSize(final Bytes data) {

@ -27,6 +27,7 @@ import org.hyperledger.besu.ethereum.core.TransactionTestFixture;
import org.hyperledger.besu.ethereum.core.encoding.PooledTransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.PooledTransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.eth.transactions.layered.BaseTransactionPoolTest;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
@ -73,7 +74,7 @@ public class PendingTransactionEstimatedMemorySizeTest extends BaseTransactionPo
Transaction txTo =
preparedTx.to(Optional.of(Address.extract(Bytes32.random()))).createTransaction(KEYS1);
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
txTo.writeTo(rlpOut);
TransactionEncoder.writeTo(txTo, rlpOut);
txTo = TransactionDecoder.readFrom(new BytesValueRLPInput(rlpOut.encoded(), false)).detachedCopy();
System.out.println(txTo.getSender());
@ -117,11 +118,9 @@ public class PendingTransactionEstimatedMemorySizeTest extends BaseTransactionPo
TransactionTestFixture preparedTx =
prepareTransaction(TransactionType.ACCESS_LIST, 10, Wei.of(500), Wei.ZERO, 10, 0);
Transaction txPayload = preparedTx.createTransaction(KEYS1);
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
txPayload.writeTo(rlpOut);
txPayload =
TransactionDecoder.readFrom(new BytesValueRLPInput(rlpOut.encoded(), false)).detachedCopy();
TransactionDecoder.readFrom(new BytesValueRLPInput(txPayload.encoded(), false)).detachedCopy();
System.out.println(txPayload.getSender());
System.out.println(txPayload.getHash());
System.out.println(txPayload.getSize());
@ -270,11 +269,9 @@ public class PendingTransactionEstimatedMemorySizeTest extends BaseTransactionPo
TransactionTestFixture preparedTx =
prepareTransaction(TransactionType.ACCESS_LIST, 10, Wei.of(500), Wei.ZERO, 10, 0);
Transaction txPayload = preparedTx.createTransaction(KEYS1);
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
txPayload.writeTo(rlpOut);
txPayload =
TransactionDecoder.readFrom(new BytesValueRLPInput(rlpOut.encoded(), false)).detachedCopy();
TransactionDecoder.readFrom(new BytesValueRLPInput(txPayload.encoded(), false)).detachedCopy();
System.out.println(txPayload.getSender());
System.out.println(txPayload.getHash());
System.out.println(txPayload.getSize());
@ -302,11 +299,9 @@ public class PendingTransactionEstimatedMemorySizeTest extends BaseTransactionPo
TransactionTestFixture preparedTx =
prepareTransaction(TransactionType.ACCESS_LIST, 0, Wei.of(500), Wei.ZERO, 0, 0);
Transaction txAccessList = preparedTx.accessList(ales).createTransaction(KEYS1);
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
txAccessList.writeTo(rlpOut);
txAccessList =
TransactionDecoder.readFrom(new BytesValueRLPInput(rlpOut.encoded(), false)).detachedCopy();
TransactionDecoder.readFrom(new BytesValueRLPInput(txAccessList.encoded(), false)).detachedCopy();
System.out.println(txAccessList.getSender());
System.out.println(txAccessList.getHash());
System.out.println(txAccessList.getSize());
@ -344,11 +339,9 @@ public class PendingTransactionEstimatedMemorySizeTest extends BaseTransactionPo
public void baseEIP1559AndEIP4844TransactionMemorySize() {
System.setProperty("jol.magicFieldOffset", "true");
Transaction txEip1559 = createEIP1559Transaction(1, KEYS1, 10);
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
txEip1559.writeTo(rlpOut);
txEip1559 =
TransactionDecoder.readFrom(new BytesValueRLPInput(rlpOut.encoded(), false)).detachedCopy();
TransactionDecoder.readFrom(new BytesValueRLPInput(txEip1559.encoded(), false)).detachedCopy();
System.out.println(txEip1559.getSender());
System.out.println(txEip1559.getHash());
System.out.println(txEip1559.getSize());
@ -388,11 +381,9 @@ public class PendingTransactionEstimatedMemorySizeTest extends BaseTransactionPo
public void baseFrontierAndAccessListTransactionMemorySize() {
System.setProperty("jol.magicFieldOffset", "true");
Transaction txFrontier = createTransaction(TransactionType.FRONTIER, 1, Wei.of(500), 0, KEYS1);
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
txFrontier.writeTo(rlpOut);
txFrontier =
TransactionDecoder.readFrom(new BytesValueRLPInput(rlpOut.encoded(), false)).detachedCopy();
TransactionDecoder.readFrom(new BytesValueRLPInput(txFrontier.encoded(), false)).detachedCopy();
System.out.println(txFrontier.getSender());
System.out.println(txFrontier.getHash());
System.out.println(txFrontier.getSize());

@ -38,6 +38,7 @@ import org.hyperledger.besu.ethereum.core.Request;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.core.TransactionReceipt;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.mainnet.BodyValidation;
import org.hyperledger.besu.ethereum.mainnet.MainnetTransactionProcessor;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
@ -616,7 +617,7 @@ public class T8nExecutor {
});
BytesValueRLPOutput rlpOut = new BytesValueRLPOutput();
rlpOut.writeList(transactions, Transaction::writeTo);
rlpOut.writeList(transactions, TransactionEncoder::writeTo);
TextNode bodyBytes = TextNode.valueOf(rlpOut.encoded().toHexString());
return new T8nResult(allocObject, bodyBytes, resultObject);
}

@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidator;
import org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams;
import org.hyperledger.besu.ethereum.mainnet.ValidationResult;
@ -184,7 +185,7 @@ public class TransactionTest {
}
// Test rlp encoding
final Bytes actualRlp = RLP.encode(transaction::writeTo);
final Bytes actualRlp = RLP.encode(output -> TransactionEncoder.writeTo(transaction, output));
assertThat(expected.isSucceeds())
.withFailMessage("Transaction " + name + "/" + milestone + " was supposed to be invalid")
.isTrue();

Loading…
Cancel
Save