diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java index 284b6ea2eb5..a7cad4b8d85 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java @@ -680,18 +680,15 @@ public int getSize() { private void memoizeHashAndSize() { final Bytes bytes = TransactionEncoder.encodeOpaqueBytes(this, EncodingContext.BLOCK_BODY); hash = Hash.hash(bytes); - if (transactionType.supportsBlob()) { if (getBlobsWithCommitments().isPresent()) { - final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); - TransactionEncoder.encodeRLP(this, rlpOutput, EncodingContext.POOLED_TRANSACTION); - size = rlpOutput.encodedSize(); + final Bytes pooledBytes = + TransactionEncoder.encodeOpaqueBytes(this, EncodingContext.POOLED_TRANSACTION); + size = pooledBytes.size(); return; } } - final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); - TransactionEncoder.encodeRLP(transactionType, bytes, rlpOutput); - size = rlpOutput.encodedSize(); + size = bytes.size(); } /** diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/BlobPooledTransactionEncoder.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/BlobPooledTransactionEncoder.java index c2167103cda..b47a406102d 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/BlobPooledTransactionEncoder.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/encoding/BlobPooledTransactionEncoder.java @@ -22,19 +22,26 @@ import org.hyperledger.besu.ethereum.core.Transaction; import org.hyperledger.besu.ethereum.rlp.RLPOutput; +import java.security.InvalidParameterException; + import org.slf4j.Logger; public class BlobPooledTransactionEncoder { private static final Logger LOG = getLogger(BlobPooledTransactionEncoder.class); + static final String NO_BLOBS_ERROR = + "Transaction with no blobsWithCommitments cannot be encoded for Pooled Transaction"; public static void encode(final Transaction transaction, final RLPOutput out) { LOG.trace("Encoding transaction with blobs {}", transaction); + var blobsWithCommitments = transaction.getBlobsWithCommitments(); + if (blobsWithCommitments.isEmpty() || blobsWithCommitments.get().getBlobs().isEmpty()) { + throw new InvalidParameterException(NO_BLOBS_ERROR); + } out.startList(); - var blobsWithCommitments = transaction.getBlobsWithCommitments().orElseThrow(); BlobTransactionEncoder.encode(transaction, out); - out.writeList(blobsWithCommitments.getBlobs(), Blob::writeTo); - out.writeList(blobsWithCommitments.getKzgCommitments(), KZGCommitment::writeTo); - out.writeList(blobsWithCommitments.getKzgProofs(), KZGProof::writeTo); + out.writeList(blobsWithCommitments.get().getBlobs(), Blob::writeTo); + out.writeList(blobsWithCommitments.get().getKzgCommitments(), KZGCommitment::writeTo); + out.writeList(blobsWithCommitments.get().getKzgProofs(), KZGProof::writeTo); out.endList(); } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/BlobTransactionEncodingTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/BlobTransactionEncodingTest.java index b1bb4be6764..49461a75303 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/BlobTransactionEncodingTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/BlobTransactionEncodingTest.java @@ -63,7 +63,7 @@ public void blobTransactionEncodingDecodingForNetWorkTest( final BytesValueRLPOutput bytesValueRLPOutput = new BytesValueRLPOutput(); TransactionEncoder.encodeRLP( transaction, bytesValueRLPOutput, EncodingContext.POOLED_TRANSACTION); - assertThat(transaction.getSize()).isEqualTo(output.encodedSize()); + assertThat(transaction.getSize()).isEqualTo(bytes.size()); } @ParameterizedTest(name = "{index} {0}") @@ -81,7 +81,7 @@ public void blobTransactionEncodingDecodingTest(final TypedTransactionBytesArgum final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); TransactionEncoder.encodeRLP(transaction.getType(), bytes, rlpOutput); - assertThat(transaction.getSize()).isEqualTo(rlpOutput.encodedSize()); + assertThat(transaction.getSize()).isEqualTo(bytes.size()); } private static Arguments createArgumentFromFile(final String path) throws IOException { diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionRLPDecoderTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionRLPDecoderTest.java index 882a2329d8e..245561158b0 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionRLPDecoderTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionRLPDecoderTest.java @@ -94,9 +94,11 @@ void shouldCalculateCorrectTransactionSize(final String rlp_tx, final String ign // Create bytes from String final Bytes bytes = Bytes.fromHexString(rlp_tx); // Decode bytes into a transaction - final Transaction transaction = decodeRLP(RLP.input(Bytes.fromHexString(rlp_tx))); + final Transaction transaction = decodeRLP(RLP.input(bytes)); + Bytes transactionBytes = + TransactionEncoder.encodeOpaqueBytes(transaction, EncodingContext.POOLED_TRANSACTION); // Bytes size should be equal to transaction size - assertThat(transaction.getSize()).isEqualTo(bytes.size()); + assertThat(transaction.getSize()).isEqualTo(transactionBytes.size()); } @ParameterizedTest @@ -107,6 +109,6 @@ void shouldReturnCorrectEncodedBytes(final String txRlp) { } private Transaction decodeRLP(final RLPInput input) { - return TransactionDecoder.decodeRLP(input, EncodingContext.BLOCK_BODY); + return TransactionDecoder.decodeRLP(input, EncodingContext.POOLED_TRANSACTION); } }