Blob tx network size and hash (#5015)

* adding test for nonblob blob transaction
* Adapt hash and network size computation for blob txs

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
Co-authored-by: Jiri Peinlich <jiri.peinlich@gmail.com>
Co-authored-by: Justin Florentine <justin+github@florentine.us>
Signed-off-by: Justin Florentine <justin+github@florentine.us>
Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com>
pull/5022/head
Fabio Di Fabio 2 years ago committed by GitHub
parent 6085b23373
commit fd427d4f90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/Transaction.java
  2. 3
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionRLPDecoderTest.java
  3. 14
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/encoding/TransactionSSZEncodingTest.java
  4. 2
      ethereum/core/src/test/resources/org/hyperledger/besu/ethereum/core/encoding/blob_transactions_without_blobs_test_vectors.json
  5. 2
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/encoding/TransactionAnnouncementEncoder.java
  6. 2
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionAnnouncement.java
  7. 3
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/NewPooledTransactionHashesMessageProcessorTest.java

@ -112,7 +112,7 @@ public class Transaction
// Caches the hash used to uniquely identify the transaction. // Caches the hash used to uniquely identify the transaction.
protected volatile Hash hash; protected volatile Hash hash;
// Caches the size in bytes of the encoded transaction. // Caches the size in bytes of the encoded transaction.
protected volatile int size = -1; protected volatile Optional<Integer> networkSize = Optional.empty();
private final TransactionType transactionType; private final TransactionType transactionType;
private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance(); private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
@ -688,20 +688,26 @@ public class Transaction
* *
* @return the size in bytes of the encoded transaction. * @return the size in bytes of the encoded transaction.
*/ */
public int getSize() { public Optional<Integer> getNetworkSize() {
if (size == -1) { if (networkSize.isEmpty()) {
memoizeHashAndSize(); memoizeHashAndSize();
} }
return size; return networkSize;
} }
private void memoizeHashAndSize() { private void memoizeHashAndSize() {
final Bytes bytes = TransactionEncoder.encodeOpaqueBytes(this); final Bytes bytes = TransactionEncoder.encodeOpaqueBytes(this);
hash = Hash.hash(bytes); hash = Hash.hash(bytes);
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); if (transactionType.supportsBlob()) {
TransactionEncoder.encodeForWire(transactionType, bytes, rlpOutput); if (getBlobsWithCommitments().isPresent()) {
size = rlpOutput.encodedSize(); networkSize = Optional.of(TransactionEncoder.encodeOpaqueBytesForNetwork(this).size());
}
} else {
final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput();
TransactionEncoder.encodeForWire(transactionType, bytes, rlpOutput);
networkSize = Optional.of(rlpOutput.encodedSize());
}
} }
/** /**

@ -116,6 +116,7 @@ class TransactionRLPDecoderTest {
// Decode bytes into a transaction // Decode bytes into a transaction
final Transaction transaction = TransactionDecoder.decodeForWire(RLP.input(bytes)); final Transaction transaction = TransactionDecoder.decodeForWire(RLP.input(bytes));
// Bytes size should be equal to transaction size // 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());
} }
} }

@ -20,6 +20,7 @@ package org.hyperledger.besu.ethereum.core.encoding;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.Transaction; import org.hyperledger.besu.ethereum.core.Transaction;
import java.io.IOException; import java.io.IOException;
@ -103,7 +104,7 @@ public class TransactionSSZEncodingTest {
} }
private static String generateNameWithoutblobs(final InputWithoutBlobs input) { private static String generateNameWithoutblobs(final InputWithoutBlobs input) {
return " chainId: " + input.getChainId(); return " hash: " + input.getHash();
} }
@ParameterizedTest(name = "[{index}] {0}") @ParameterizedTest(name = "[{index}] {0}")
@ -131,6 +132,7 @@ public class TransactionSSZEncodingTest {
assertThat(transaction).isNotNull(); assertThat(transaction).isNotNull();
assertThat(transaction.getPayload()).isNotNull(); assertThat(transaction.getPayload()).isNotNull();
assertThat(transaction.getHash()).isEqualTo(Hash.fromHexString(input.getHash()));
final Bytes encodedBytes = TransactionEncoder.encodeOpaqueBytes(transaction); final Bytes encodedBytes = TransactionEncoder.encodeOpaqueBytes(transaction);
assertThat(encodedBytes).isNotNull(); assertThat(encodedBytes).isNotNull();
assertThat(encodedBytes.toHexString()).isEqualTo(rawTransaction); assertThat(encodedBytes.toHexString()).isEqualTo(rawTransaction);
@ -263,14 +265,14 @@ public class TransactionSSZEncodingTest {
} }
public static class InputWithoutBlobs { public static class InputWithoutBlobs {
String chainId; String hash;
public String getChainId() { public String getHash() {
return chainId; return hash;
} }
public void setChainId(final String chainId) { public void setHash(final String hash) {
this.chainId = chainId; this.hash = hash;
} }
} }
} }

@ -1,7 +1,7 @@
[ [
{ {
"Input": { "Input": {
"chainId": "4844001004" "hash": "0x31b315056d41d2e05b3a9d6b8beb4dadcffd286a62ca37dd7de0c89debe2633e"
}, },
"RawEncodedTransaction": "0x054500000000038c34b89390ade8350e994a5611eaab7c55b2544e70904558e649bb4f7a75f2f954b7adda21f61163b09ff39db79262ce15820b79b1fa8b039dbf2e97f4ad7dec96b920010000000000000000000000000000000000000000000000000000000000000000000000009435770000000000000000000000000000000000000000000000000000000000e876481700000000000000000000000000000000000000000000000000000040420f0000000000c00000000000000000000000000000000000000000000000000000000000000000000000d5000000d5000000005ed0b200000000000000000000000000000000000000000000000000000000d5000000010000000000000000000000000000000000000000010c30956be2a7db889757f855d3d42d4a3660a90f0c86aaef23586ac0f050b501c97b77e1fc5f69618cfbf94e0d7486ebdfab1ce1173e59c908ac5b61a12fc2" "RawEncodedTransaction": "0x054500000000038c34b89390ade8350e994a5611eaab7c55b2544e70904558e649bb4f7a75f2f954b7adda21f61163b09ff39db79262ce15820b79b1fa8b039dbf2e97f4ad7dec96b920010000000000000000000000000000000000000000000000000000000000000000000000009435770000000000000000000000000000000000000000000000000000000000e876481700000000000000000000000000000000000000000000000000000040420f0000000000c00000000000000000000000000000000000000000000000000000000000000000000000d5000000d5000000005ed0b200000000000000000000000000000000000000000000000000000000d5000000010000000000000000000000000000000000000000010c30956be2a7db889757f855d3d42d4a3660a90f0c86aaef23586ac0f050b501c97b77e1fc5f69618cfbf94e0d7486ebdfab1ce1173e59c908ac5b61a12fc2"
} }

@ -81,7 +81,7 @@ public class TransactionAnnouncementEncoder {
transactions.forEach( transactions.forEach(
transaction -> { transaction -> {
types.add(transaction.getType()); types.add(transaction.getType());
sizes.add(transaction.getSize()); sizes.add(transaction.getNetworkSize().orElseThrow());
hashes.add(transaction.getHash()); hashes.add(transaction.getHash());
}); });

@ -40,7 +40,7 @@ public class TransactionAnnouncement {
this( this(
checkNotNull(transaction, "Transaction cannot be null").getHash(), checkNotNull(transaction, "Transaction cannot be null").getHash(),
transaction.getType(), transaction.getType(),
(long) transaction.getSize()); (long) transaction.getNetworkSize().orElseThrow());
} }
public TransactionAnnouncement(final Hash hash, final TransactionType type, final Long size) { public TransactionAnnouncement(final Hash hash, final TransactionType type, final Long size) {

@ -366,7 +366,8 @@ public class NewPooledTransactionHashesMessageProcessorTest {
final TransactionAnnouncement announcement = announcementList.get(list.indexOf(transaction)); final TransactionAnnouncement announcement = announcementList.get(list.indexOf(transaction));
assertThat(announcement.getHash()).isEqualTo(transaction.getHash()); assertThat(announcement.getHash()).isEqualTo(transaction.getHash());
assertThat(announcement.getType()).hasValue(transaction.getType()); assertThat(announcement.getType()).hasValue(transaction.getType());
assertThat(announcement.getSize()).hasValue((long) transaction.getSize()); assertThat(announcement.getSize())
.hasValue((long) transaction.getNetworkSize().orElseThrow());
} }
} }

Loading…
Cancel
Save