Skip to content

Commit

Permalink
SSZ - encoding (hyperledger#4979)
Browse files Browse the repository at this point in the history
* Adding preliminary SSZ encoding/decoding of Transaction Network Payload

* Adding ssz snapshot jar, delete it before moving to main

also removing licenses....

Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com>
  • Loading branch information
gezero authored Jan 23, 2023
1 parent 1abe277 commit bd147e0
Show file tree
Hide file tree
Showing 24 changed files with 1,110 additions and 5,419 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.apache.tuweni.bytes.Bytes;
import org.junit.Test;

public class IbftExtraDataEncoderTest {
public class IbftExtraDataRLPEncoderTest {

private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
Suppliers.memoize(SignatureAlgorithmFactory::getInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public TransactionResult transaction(
address(fromAddress),
Optional.empty(),
Optional.of(bigInteger(v)),
Optional.empty(),
Optional.empty(),
Optional.empty());

return new TransactionCompleteResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ private TransactionAdapter updatePrivatePayload(final Transaction transaction) {
transaction.getSender(),
transaction.getChainId(),
Optional.ofNullable(transaction.getV()),
Optional.empty())));
transaction.getMaxFeePerData(),
transaction.getVersionedHashes(),
transaction.getBlobsWithCommitments())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.Transaction;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.evm.AccessListEntry;
import org.hyperledger.besu.plugin.data.TransactionType;

Expand Down Expand Up @@ -88,9 +88,7 @@ public TransactionPendingResult(final Transaction transaction) {
this.input = transaction.getPayload().toString();
this.nonce = Quantity.create(transaction.getNonce());
this.publicKey = transaction.getPublicKey().orElse(null);
final BytesValueRLPOutput out = new BytesValueRLPOutput();
transaction.writeTo(out);
this.raw = out.encoded().toString();
this.raw = TransactionEncoder.encodeOpaqueBytes(transaction).toString();
this.to = transaction.getTo().map(Address::toHexString).orElse(null);
this.type =
transactionType.equals(TransactionType.FRONTIER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ private static final String createValidTransactionRLP() {
"0x8411b12666f68ef74cace3615c9d5a377729d03f")), // sender public address
Optional.empty(),
Optional.of(BigInteger.valueOf(37)),
Optional.empty(),
Optional.empty(),
Optional.empty());
publicTransaction.writeTo(bvrlp);
return bvrlp.encoded().toHexString();
Expand Down
22 changes: 22 additions & 0 deletions ethereum/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,31 @@ jar {
}
}

//repositories {
// mavenLocal()
//}
//
//sourceControl {
// gitRepository("https://github.com/apache/incubator-tuweni.git") {
// producesModule("org.apache.tuweni:ssz")
// }
//}
//
//
//includeBuild('tuweni-library') {
// dependencySubstitution {
// substitute module('org.apache.tuweni:tuweni-ssz') using project(':')
// }
//}


dependencies {
api 'org.slf4j:slf4j-api'

annotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess'

// implementation fileTree(dir: '/Users/jiripeinlich/consensys/src/incubator-tuweni/ssz/build/classes', include: '*.class')

implementation project(':config')
implementation project(':crypto')
implementation project(':datatypes')
Expand All @@ -54,6 +74,8 @@ dependencies {
implementation 'org.apache.tuweni:tuweni-concurrent'
implementation 'org.apache.tuweni:tuweni-units'
implementation 'org.apache.tuweni:tuweni-rlp'
implementation 'org.apache.tuweni:tuweni-ssz'
implementation files('libs/tuweni-ssz-2.4.0-SNAPSHOT.jar')
implementation 'org.hyperledger.besu:bls12-381'
implementation 'org.immutables:value-annotations'

Expand Down
Binary file added ethereum/core/libs/tuweni-ssz-2.4.0-SNAPSHOT.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.ethereum.core;

import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void writeTo(final RLPOutput out) {
out.startList();

header.writeTo(out);
out.writeList(body.getTransactions(), Transaction::writeTo);
out.writeList(body.getTransactions(), TransactionEncoder::encodeOpaqueBytes);
out.writeList(body.getOmmers(), BlockHeader::writeTo);
body.getWithdrawals().ifPresent(withdrawals -> out.writeList(withdrawals, Withdrawal::writeTo));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.core;

import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
import org.hyperledger.besu.ethereum.rlp.RLPOutput;

Expand Down Expand Up @@ -88,7 +89,7 @@ public Optional<List<Withdrawal>> getWithdrawals() {
public void writeTo(final RLPOutput output) {
output.startList();

output.writeList(getTransactions(), Transaction::writeTo);
output.writeList(getTransactions(), TransactionEncoder::encodeOpaqueBytes);
output.writeList(getOmmers(), BlockHeader::writeTo);
withdrawals.ifPresent(withdrawals -> output.writeList(withdrawals, Withdrawal::writeTo));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.encoding.TransactionDecoder;
import org.hyperledger.besu.ethereum.core.encoding.TransactionEncoder;
import org.hyperledger.besu.ethereum.core.encoding.ssz.TransactionNetworkPayload;
import org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput;
import org.hyperledger.besu.ethereum.rlp.RLP;
import org.hyperledger.besu.ethereum.rlp.RLPInput;
Expand All @@ -48,6 +49,7 @@

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.ssz.SSZFixedSizeTypeList;
import org.apache.tuweni.units.bigints.UInt256;
import org.apache.tuweni.units.bigints.UInt256s;

Expand Down Expand Up @@ -112,8 +114,11 @@ public class Transaction
private final TransactionType transactionType;

private final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance();
private final Optional<Wei> maxFeePerData;
private final Optional<List<Hash>> versionedHashes;

private final Optional<BlobsWithCommitments> blobsWithCommitments;

public static Builder builder() {
return new Builder();
}
Expand Down Expand Up @@ -166,7 +171,9 @@ public Transaction(
final Address sender,
final Optional<BigInteger> chainId,
final Optional<BigInteger> v,
final Optional<List<Hash>> versionedHashes) {
final Optional<Wei> maxFeePerData,
final Optional<List<Hash>> versionedHashes,
final Optional<BlobsWithCommitments> blobsWithCommitments) {
if (v.isPresent() && chainId.isPresent()) {
throw new IllegalArgumentException(
String.format("chainId '%s' and v '%s' cannot both be provided", chainId.get(), v.get()));
Expand Down Expand Up @@ -212,7 +219,9 @@ public Transaction(
this.sender = sender;
this.chainId = chainId;
this.v = v;
this.maxFeePerData = maxFeePerData;
this.versionedHashes = versionedHashes;
this.blobsWithCommitments = blobsWithCommitments;
}

public Transaction(
Expand All @@ -228,7 +237,9 @@ public Transaction(
final Address sender,
final Optional<BigInteger> chainId,
final Optional<BigInteger> v,
final Optional<List<Hash>> versionedHashes) {
final Optional<Wei> maxFeePerData,
final Optional<List<Hash>> versionedHashes,
final Optional<BlobsWithCommitments> blobsWithCommitments) {
this(
TransactionType.FRONTIER,
nonce,
Expand All @@ -244,7 +255,9 @@ public Transaction(
sender,
chainId,
v,
versionedHashes);
maxFeePerData,
versionedHashes,
blobsWithCommitments);
}

public Transaction(
Expand All @@ -257,7 +270,8 @@ public Transaction(
final Bytes payload,
final Optional<BigInteger> chainId,
final Optional<BigInteger> v,
final Optional<List<Hash>> versionedHashes) {
final Optional<List<Hash>> versionedHashes,
final Optional<BlobsWithCommitments> blobsWithCommitments) {
this(
TransactionType.FRONTIER,
nonce,
Expand All @@ -273,7 +287,9 @@ public Transaction(
null,
chainId,
v,
versionedHashes);
Optional.empty(),
versionedHashes,
blobsWithCommitments);
}

/**
Expand Down Expand Up @@ -316,6 +332,8 @@ public Transaction(
sender,
chainId,
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());
}

Expand Down Expand Up @@ -348,7 +366,9 @@ public Transaction(
final Address sender,
final Optional<BigInteger> chainId,
final Optional<BigInteger> v,
final Optional<List<Hash>> versionedHashes) {
final Optional<Wei> maxFeePerData,
final Optional<List<Hash>> versionedHashes,
final Optional<BlobsWithCommitments> blobsWithCommitments) {
this(
nonce,
Optional.of(gasPrice),
Expand All @@ -362,7 +382,9 @@ public Transaction(
sender,
chainId,
v,
versionedHashes);
maxFeePerData,
versionedHashes,
blobsWithCommitments);
}

/**
Expand Down Expand Up @@ -709,8 +731,16 @@ public TransactionType getType() {
return this.transactionType;
}

public Optional<Wei> getMaxFeePerData() {
return maxFeePerData;
}

public Optional<List<Hash>> getVersionedHashes() {
return this.versionedHashes;
return versionedHashes;
}

public Optional<BlobsWithCommitments> getBlobsWithCommitments() {
return blobsWithCommitments;
}

/**
Expand Down Expand Up @@ -995,6 +1025,8 @@ public static class Builder {

protected Optional<BigInteger> v = Optional.empty();
protected List<Hash> versionedHashes = null;
private Wei maxFeePerData;
private BlobsWithCommitments blobsWithCommitments;

public Builder type(final TransactionType transactionType) {
this.transactionType = transactionType;
Expand Down Expand Up @@ -1105,7 +1137,9 @@ public Transaction build() {
sender,
chainId,
v,
Optional.ofNullable(versionedHashes));
Optional.ofNullable(maxFeePerData),
Optional.ofNullable(versionedHashes),
Optional.ofNullable(blobsWithCommitments));
}

public Transaction signAndBuild(final KeyPair keys) {
Expand Down Expand Up @@ -1133,6 +1167,34 @@ SECPSignature computeSignature(final KeyPair keys) {
chainId),
keys);
}

public Builder kzgBlobs(
final SSZFixedSizeTypeList<TransactionNetworkPayload.KZGCommitment> kzgCommitments,
final SSZFixedSizeTypeList<TransactionNetworkPayload.Blob> blobs,
final TransactionNetworkPayload.KZGProof kzgProof) {
this.blobsWithCommitments = new BlobsWithCommitments(kzgCommitments, blobs, kzgProof);
return this;
}

public Builder maxFeePerData(final Wei maxFeePerData) {
this.maxFeePerData = maxFeePerData;
return this;
}
}

public static class BlobsWithCommitments {
public final SSZFixedSizeTypeList<TransactionNetworkPayload.KZGCommitment> kzgCommitments;
public final SSZFixedSizeTypeList<TransactionNetworkPayload.Blob> blobs;
public final TransactionNetworkPayload.KZGProof kzgProof;

public BlobsWithCommitments(
final SSZFixedSizeTypeList<TransactionNetworkPayload.KZGCommitment> kzgCommitments,
final SSZFixedSizeTypeList<TransactionNetworkPayload.Blob> blobs,
final TransactionNetworkPayload.KZGProof kzgProof) {
this.kzgCommitments = kzgCommitments;
this.blobs = blobs;
this.kzgProof = kzgProof;
}
}

/**
Expand Down
Loading

0 comments on commit bd147e0

Please sign in to comment.