Skip to content

Commit

Permalink
fix(daml-rpc): compress batches across the wire
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin O'Donnell <[email protected]>
  • Loading branch information
scealiontach committed Nov 2, 2020
1 parent e414af0 commit 56465a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public static Batch makeSawtoothBatch(final KeyManager keyManager, final Collect
public static Transaction makeSawtoothTransaction(final KeyManager keyManager, final String familyName,
final String familyVersion, final Collection<String> inputAddresses, final Collection<String> outputAddresses,
final Collection<String> dependentTransactionIds, final ByteString payload) {
String payloadHash = getHash(payload);
ByteString wrappedPayload = SawtoothClientUtils.wrap(payload);
String payloadHash = getHash(wrappedPayload);
TransactionHeader.Builder txnHeaderBldr = TransactionHeader.newBuilder().setFamilyName(familyName)
.setFamilyVersion(familyVersion).clearBatcherPublicKey().setBatcherPublicKey(keyManager.getPublicKeyInHex())
.setNonce(SawtoothClientUtils.generateNonce()).setPayloadSha512(payloadHash).addAllInputs(inputAddresses)
Expand All @@ -109,7 +110,7 @@ public static Transaction makeSawtoothTransaction(final KeyManager keyManager, f

String signedHeader = keyManager.sign(txnHeader.toByteArray());
return Transaction.newBuilder().setHeader(txnHeader.toByteString()).setHeaderSignature(signedHeader)
.setPayload(payload).build();
.setPayload(wrappedPayload).build();
}

/**
Expand Down Expand Up @@ -181,7 +182,9 @@ public static String getHash(final byte[] arg) {
}

public static ByteString wrap(final ByteString value) throws InternalError {
return VersionedEnvelope.newBuilder().setData(compressByteString(value)).build().toByteString();
ByteString v = VersionedEnvelope.newBuilder().setData(compressByteString(value)).build().toByteString();
LOGGER.error("Wrapping bytes size={} new size={}", value.size(), v.size());
return v;
}

public static ByteString unwrap(final ByteString wrappedVal) throws InternalError {
Expand All @@ -191,7 +194,9 @@ public static ByteString unwrap(final ByteString wrappedVal) throws InternalErro
switch (envelope.getVersion()) {
case "":
case "1":
return uncompressByteString(envelope.getData());
ByteString v = uncompressByteString(envelope.getData());
LOGGER.error("Unwrapping bytes size={} new size={}", wrappedVal.size(), v.size());
return v;
default:
LOGGER.error("Envelope specified an unknown version: " + envelope.getVersion());
throw new InternalError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.blockchaintp.sawtooth.daml.protobuf.DamlOperation;
import com.blockchaintp.sawtooth.daml.protobuf.DamlOperationBatch;
import com.blockchaintp.sawtooth.daml.protobuf.DamlTransaction;
import com.blockchaintp.utils.SawtoothClientUtils;
import com.codahale.metrics.SharedMetricRegistries;
import com.daml.caching.Cache;
import com.daml.ledger.participant.state.kvutils.DamlKvutils.DamlLogEntryId;
Expand Down Expand Up @@ -99,7 +100,8 @@ public void apply(final TpProcessRequest tpProcessRequest, final Context state)
final LedgerState<String> ledgerState = new ContextLedgerState(state);

try {
final DamlOperationBatch batch = DamlOperationBatch.parseFrom(tpProcessRequest.getPayload());
ByteString unwrappedPayload = SawtoothClientUtils.unwrap(tpProcessRequest.getPayload());
final DamlOperationBatch batch = DamlOperationBatch.parseFrom(unwrappedPayload);
for (final DamlOperation operation : batch.getOperationsList()) {
final String participantId = operation.getSubmittingParticipant();
if (operation.hasTransaction()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.blockchaintp.sawtooth.timekeeper.protobuf.TimeKeeperParticipant;
import com.blockchaintp.sawtooth.timekeeper.protobuf.TimeKeeperRecord;
import com.blockchaintp.sawtooth.timekeeper.protobuf.TimeKeeperUpdate;
import com.blockchaintp.utils.SawtoothClientUtils;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Timestamp;
Expand Down Expand Up @@ -85,7 +86,8 @@ public void apply(final TpProcessRequest transactionRequest, final Context state
basicRequestChecks(transactionRequest);
String signerPublicKey = transactionRequest.getHeader().getSignerPublicKey();
try {
TimeKeeperUpdate update = TimeKeeperUpdate.parseFrom(transactionRequest.getPayload());
ByteString unwrappedPayload = SawtoothClientUtils.unwrap(transactionRequest.getPayload());
TimeKeeperUpdate update = TimeKeeperUpdate.parseFrom(unwrappedPayload);
String myRecordAddr = Namespace.makeAddress(this.namespace, signerPublicKey);
LOGGER.debug("Getting global record state");
Map<String, ByteString> sourceData = state
Expand Down

0 comments on commit 56465a3

Please sign in to comment.