Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blob tx network size and hash #5015

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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 @@ -688,20 +688,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 @@ -116,6 +116,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 @@ -81,7 +81,7 @@ private static Bytes encodeForEth68(final List<Transaction> transactions) {
transactions.forEach(
transaction -> {
types.add(transaction.getType());
sizes.add(transaction.getSize());
sizes.add(transaction.getNetworkSize().orElseThrow());
hashes.add(transaction.getHash());
});

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 @@ -366,7 +366,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