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 3932e736d5a..a874a76a063 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 @@ -106,7 +106,7 @@ public class Transaction // Caches the hash used to uniquely identify the transaction. protected volatile Hash hash; // Caches the size in bytes of the encoded transaction. - protected volatile int size = -1; + protected volatile Optional networkSize = Optional.empty(); private final TransactionType transactionType; private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance(); @@ -686,20 +686,26 @@ public Hash getHash() { * * @return the size in bytes of the encoded transaction. */ - public int getSize() { - if (size == -1) { + public Optional getNetworkSize() { + if (networkSize.isEmpty()) { memoizeHashAndSize(); } - return size; + return networkSize; } private void memoizeHashAndSize() { final Bytes bytes = TransactionEncoder.encodeOpaqueBytes(this); hash = Hash.hash(bytes); - final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); - TransactionEncoder.encodeForWire(transactionType, bytes, rlpOutput); - size = rlpOutput.encodedSize(); + if (transactionType.supportsBlob()) { + if (getBlobsWithCommitments().isPresent()) { + networkSize = Optional.of(TransactionEncoder.encodeOpaqueBytesForNetwork(this).size()); + } + } else { + final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); + TransactionEncoder.encodeForWire(transactionType, bytes, rlpOutput); + networkSize = Optional.of(rlpOutput.encodedSize()); + } } /** 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 a3729713551..92039bb5570 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 @@ -95,6 +95,7 @@ void shouldCalculateCorrectTransactionSize(final String rlp_tx, final String ign // Decode bytes into a transaction final Transaction transaction = TransactionDecoder.decodeForWire(RLP.input(bytes)); // Bytes size should be equal to transaction size - assertThat(transaction.getSize()).isEqualTo(bytes.size()); + assertThat(transaction.getNetworkSize().isPresent()).isTrue(); + assertThat(transaction.getNetworkSize().get()).isEqualTo(bytes.size()); } } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionSSZEncodingTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionSSZEncodingTest.java index 9c6617cbdcf..05ec2089105 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionSSZEncodingTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionSSZEncodingTest.java @@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat; import org.hyperledger.besu.datatypes.Address; +import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.ethereum.core.Transaction; import java.io.IOException; @@ -103,7 +104,7 @@ private static String generateName(final Input input) { } private static String generateNameWithoutblobs(final InputWithoutBlobs input) { - return " chainId: " + input.getChainId(); + return " hash: " + input.getHash(); } @ParameterizedTest(name = "[{index}] {0}") @@ -131,6 +132,7 @@ void shouldDecodeSSZTransactionsWithoutBlobs( assertThat(transaction).isNotNull(); assertThat(transaction.getPayload()).isNotNull(); + assertThat(transaction.getHash()).isEqualTo(Hash.fromHexString(input.getHash())); final Bytes encodedBytes = TransactionEncoder.encodeOpaqueBytes(transaction); assertThat(encodedBytes).isNotNull(); assertThat(encodedBytes.toHexString()).isEqualTo(rawTransaction); @@ -263,14 +265,14 @@ public void setData(final String data) { } public static class InputWithoutBlobs { - String chainId; + String hash; - public String getChainId() { - return chainId; + public String getHash() { + return hash; } - public void setChainId(final String chainId) { - this.chainId = chainId; + public void setHash(final String hash) { + this.hash = hash; } } } diff --git a/ethereum/core/src/test/resources/org/hyperledger/besu/ethereum/core/encoding/blob_transactions_without_blobs_test_vectors.json b/ethereum/core/src/test/resources/org/hyperledger/besu/ethereum/core/encoding/blob_transactions_without_blobs_test_vectors.json index 622a8266019..695bceb2396 100644 --- a/ethereum/core/src/test/resources/org/hyperledger/besu/ethereum/core/encoding/blob_transactions_without_blobs_test_vectors.json +++ b/ethereum/core/src/test/resources/org/hyperledger/besu/ethereum/core/encoding/blob_transactions_without_blobs_test_vectors.json @@ -1,7 +1,7 @@ [ { "Input": { - "chainId": "4844001004" + "hash": "0x31b315056d41d2e05b3a9d6b8beb4dadcffd286a62ca37dd7de0c89debe2633e" }, "RawEncodedTransaction": "0x054500000000038c34b89390ade8350e994a5611eaab7c55b2544e70904558e649bb4f7a75f2f954b7adda21f61163b09ff39db79262ce15820b79b1fa8b039dbf2e97f4ad7dec96b920010000000000000000000000000000000000000000000000000000000000000000000000009435770000000000000000000000000000000000000000000000000000000000e876481700000000000000000000000000000000000000000000000000000040420f0000000000c00000000000000000000000000000000000000000000000000000000000000000000000d5000000d5000000005ed0b200000000000000000000000000000000000000000000000000000000d5000000010000000000000000000000000000000000000000010c30956be2a7db889757f855d3d42d4a3660a90f0c86aaef23586ac0f050b501c97b77e1fc5f69618cfbf94e0d7486ebdfab1ce1173e59c908ac5b61a12fc2" } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionAnnouncement.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionAnnouncement.java index 1533a6bdbe4..13ae527b650 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionAnnouncement.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionAnnouncement.java @@ -40,7 +40,7 @@ public TransactionAnnouncement(final Transaction transaction) { this( checkNotNull(transaction, "Transaction cannot be null").getHash(), transaction.getType(), - (long) transaction.getSize()); + (long) transaction.getNetworkSize().orElseThrow()); } public TransactionAnnouncement(final Hash hash, final TransactionType type, final Long size) { diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageProcessorTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageProcessorTest.java index 36c1a451d84..e89a7702239 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageProcessorTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageProcessorTest.java @@ -414,7 +414,8 @@ public void shouldEncodeAndDecodeTransactionAnnouncement_Eth68() { final TransactionAnnouncement announcement = announcementList.get(list.indexOf(transaction)); assertThat(announcement.getHash()).isEqualTo(transaction.getHash()); assertThat(announcement.getType()).hasValue(transaction.getType()); - assertThat(announcement.getSize()).hasValue((long) transaction.getSize()); + assertThat(announcement.getSize()) + .hasValue((long) transaction.getNetworkSize().orElseThrow()); } }