Skip to content

Commit

Permalink
Blob tx network size and hash (#5015)
Browse files Browse the repository at this point in the history
* 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>
(cherry picked from commit fd427d4)
(cherry picked from commit 28e8d18130cebe1e2835e299f6df2079f2351925)
  • Loading branch information
fab-10 authored and jflo committed Jun 16, 2023
1 parent a63e71f commit c5731b0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> networkSize = Optional.empty();
private final TransactionType transactionType;

private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
Expand Down Expand Up @@ -686,20 +686,26 @@ public Hash getHash() {
*
* @return the size in bytes of the encoded transaction.
*/
public int getSize() {
if (size == -1) {
public Optional<Integer> 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());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"Input": {
"chainId": "4844001004"
"hash": "0x31b315056d41d2e05b3a9d6b8beb4dadcffd286a62ca37dd7de0c89debe2633e"
},
"RawEncodedTransaction": "0x054500000000038c34b89390ade8350e994a5611eaab7c55b2544e70904558e649bb4f7a75f2f954b7adda21f61163b09ff39db79262ce15820b79b1fa8b039dbf2e97f4ad7dec96b920010000000000000000000000000000000000000000000000000000000000000000000000009435770000000000000000000000000000000000000000000000000000000000e876481700000000000000000000000000000000000000000000000000000040420f0000000000c00000000000000000000000000000000000000000000000000000000000000000000000d5000000d5000000005ed0b200000000000000000000000000000000000000000000000000000000d5000000010000000000000000000000000000000000000000010c30956be2a7db889757f855d3d42d4a3660a90f0c86aaef23586ac0f050b501c97b77e1fc5f69618cfbf94e0d7486ebdfab1ce1173e59c908ac5b61a12fc2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,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());
}
}

Expand Down

0 comments on commit c5731b0

Please sign in to comment.