Skip to content

Commit

Permalink
Size calculation should use opaque bytes instead of rlp
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <gabriel.trintinalia@consensys.net>
  • Loading branch information
Gabriel-Trintinalia committed Sep 8, 2023
1 parent 80a10b8 commit 30eca31
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 30eca31

Please sign in to comment.