Skip to content

Commit

Permalink
adding test for nonblob blob transaction (#5008)
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com>

Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
(cherry picked from commit 6085b23)
(cherry picked from commit d2d761b389ea811a7d62b26c6af7e74bb1c15564)
  • Loading branch information
gezero authored and jflo committed Jun 16, 2023
1 parent 2b5eefa commit a63e71f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void setAddress(final Optional<Address> address) {
public static class Data implements SSZReadable, SSZWritable {
public static final int MAX_CALL_DATA_SIZE = 16777216; // 2**24

Bytes data;
Bytes data = Bytes.EMPTY;

@Override
public boolean isFixed() {
Expand All @@ -319,7 +319,7 @@ public void populateFromReader(final SSZReader reader) {

@Override
public void writeTo(final SSZWriter writer) {
if (data != null) {
if (data != Bytes.EMPTY) {
writer.writeBytes(data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class TransactionSSZEncodingTest {
private static final String BLOB_TRANSACTIONS_TEST_VECTORS_JSON =
"org/hyperledger/besu/ethereum/core/encoding/blob_transactions_test_vectors.json";

private static final String BLOB_TRANSACTIONS_WITHOUT_BLOBS_TEST_VECTORS_JSON =
"org/hyperledger/besu/ethereum/core/encoding/blob_transactions_without_blobs_test_vectors.json";

private static Collection<Object[]> blobTransactionsTestVectors() throws IOException {

ClassLoader classLoader = TransactionSSZEncodingTest.class.getClassLoader();
Expand All @@ -63,6 +66,29 @@ private static Collection<Object[]> blobTransactionsTestVectors() throws IOExcep
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}

private static Collection<Object[]> blobTransactionsTestVectorsWithoutBlobs() throws IOException {

ClassLoader classLoader = TransactionSSZEncodingTest.class.getClassLoader();
InputStream inputStream =
classLoader.getResourceAsStream(BLOB_TRANSACTIONS_WITHOUT_BLOBS_TEST_VECTORS_JSON);
JsonParser parser = new JsonFactory().createParser(inputStream);
ObjectMapper mapper =
JsonMapper.builder()
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
.build();
List<ModelWithoutBlobs> models = mapper.readValue(parser, new TypeReference<>() {});

return models.stream()
.map(
model ->
new Object[] {
generateNameWithoutblobs(model.getInput()),
model.getInput(),
model.getRawEncodedTransaction()
})
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}

private static String generateName(final Input input) {
return " To: "
+ input.getTo()
Expand All @@ -76,6 +102,10 @@ private static String generateName(final Input input) {
+ input.getNonce();
}

private static String generateNameWithoutblobs(final InputWithoutBlobs input) {
return " chainId: " + input.getChainId();
}

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("blobTransactionsTestVectors")
void shouldDecodeSSZTransactions(
Expand All @@ -92,6 +122,20 @@ void shouldDecodeSSZTransactions(
assertThat(encodedBytes.toHexString()).isEqualTo(rawTransaction);
}

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("blobTransactionsTestVectorsWithoutBlobs")
void shouldDecodeSSZTransactionsWithoutBlobs(
final String name, final InputWithoutBlobs input, final String rawTransaction) {
final Bytes bytes = Bytes.fromHexString(rawTransaction);
final Transaction transaction = TransactionDecoder.decodeOpaqueBytes(bytes);

assertThat(transaction).isNotNull();
assertThat(transaction.getPayload()).isNotNull();
final Bytes encodedBytes = TransactionEncoder.encodeOpaqueBytes(transaction);
assertThat(encodedBytes).isNotNull();
assertThat(encodedBytes.toHexString()).isEqualTo(rawTransaction);
}

public static class Model {
private Input input;
private String rawEncodedTransaction;
Expand All @@ -113,6 +157,27 @@ public void setRawEncodedTransaction(final String rawEncodedTransaction) {
}
}

public static class ModelWithoutBlobs {
private InputWithoutBlobs input;
private String rawEncodedTransaction;

public InputWithoutBlobs getInput() {
return input;
}

public void setInput(final InputWithoutBlobs input) {
this.input = input;
}

public String getRawEncodedTransaction() {
return rawEncodedTransaction;
}

public void setRawEncodedTransaction(final String rawEncodedTransaction) {
this.rawEncodedTransaction = rawEncodedTransaction;
}
}

public static class Input {
String privateKey;
String to;
Expand Down Expand Up @@ -196,4 +261,16 @@ public void setData(final String data) {
this.data = data;
}
}

public static class InputWithoutBlobs {
String chainId;

public String getChainId() {
return chainId;
}

public void setChainId(final String chainId) {
this.chainId = chainId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"Input": {
"chainId": "4844001004"
},
"RawEncodedTransaction": "0x054500000000038c34b89390ade8350e994a5611eaab7c55b2544e70904558e649bb4f7a75f2f954b7adda21f61163b09ff39db79262ce15820b79b1fa8b039dbf2e97f4ad7dec96b920010000000000000000000000000000000000000000000000000000000000000000000000009435770000000000000000000000000000000000000000000000000000000000e876481700000000000000000000000000000000000000000000000000000040420f0000000000c00000000000000000000000000000000000000000000000000000000000000000000000d5000000d5000000005ed0b200000000000000000000000000000000000000000000000000000000d5000000010000000000000000000000000000000000000000010c30956be2a7db889757f855d3d42d4a3660a90f0c86aaef23586ac0f050b501c97b77e1fc5f69618cfbf94e0d7486ebdfab1ce1173e59c908ac5b61a12fc2"
}
]

0 comments on commit a63e71f

Please sign in to comment.