Skip to content

Commit

Permalink
refactor: separate out sawtooth-utils and rearrange protos
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin O'Donnell <[email protected]>
  • Loading branch information
scealiontach committed Jun 4, 2021
1 parent 07e2618 commit 7db5ff7
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ syntax = "proto3";

option java_multiple_files = true;
package com.blockchaintp.sawtooth.daml.protobuf;
import "google/protobuf/timestamp.proto";

message DamlOperationBatch {
string version = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -13,6 +13,9 @@
import sawtooth.sdk.signing.PrivateKey;
import sawtooth.sdk.signing.PublicKey;

/**
* Abstract base class for all KeyManagers.
*/
public abstract class BaseKeyManager implements KeyManager {

private static final Logger LOGGER = LoggerFactory.getLogger(BaseKeyManager.class);
Expand All @@ -28,47 +31,47 @@ public abstract class BaseKeyManager implements KeyManager {
}

@Override
public PublicKey getPublicKey() {
public final PublicKey getPublicKey() {
return getPublicKey(DEFAULT);
}

@Override
public String getPublicKeyInHex() {
return getPublicKey(DEFAULT).hex();
public final PublicKey getPublicKey(final String id) {
return this.publicKeyMap.get(id);
}

@Override
public PublicKey getPublicKey(final String id) {
return this.publicKeyMap.get(id);
public final String getPublicKeyInHex() {
return getPublicKey(DEFAULT).hex();
}

@Override
public String getPublicKeyInHex(final String id) {
public final String getPublicKeyInHex(final String id) {
return getPublicKey(id).hex();
}

@Override
public String sign(final byte[] item) {
public final String sign(final byte[] item) {
return sign(DEFAULT, item);
}

@Override
public String sign(final String id, final byte[] item) {
public final String sign(final String id, final byte[] item) {
PrivateKey privKey = getPrivateKey(id);
if (privKey == null) {
throw new KeyManagerRuntimeException(String.format("No private key with id %s is available", id));
}
LOGGER.debug("Signing array of size={} for id={}", item.length, id);
return getContextForKey(privKey).sign(item, privKey);
return getContextForKey(privKey).sign(item, privKey);
}

@Override
public boolean verify(final byte[] item, final String signature) {
public final boolean verify(final byte[] item, final String signature) {
return verify(DEFAULT, item, signature);
}

@Override
public boolean verify(final String id, final byte[] item, final String signature) {
public final boolean verify(final String id, final byte[] item, final String signature) {
var pubKey = this.getPublicKey(id);
if (pubKey == null) {
throw new KeyManagerRuntimeException(String.format("No public key with id %s is available", id));
Expand All @@ -77,27 +80,27 @@ public boolean verify(final String id, final byte[] item, final String signature
}

@Override
public boolean verify(final PublicKey pubKey, final byte[] item, final String signature) {
public final boolean verify(final PublicKey pubKey, final byte[] item, final String signature) {
return getContextForKey(pubKey).verify(signature, item, pubKey);
}

protected PrivateKey putKey(PrivateKey key) {
protected final PrivateKey putKey(final PrivateKey key) {
return putKey(DEFAULT, key);
}

protected PrivateKey putKey(String id, PrivateKey key) {
protected final PrivateKey putKey(final String id, final PrivateKey key) {
return this.privateKeyMap.put(id, key);
}

protected PublicKey putKey(PublicKey key) {
protected final PublicKey putKey(final PublicKey key) {
return putKey(DEFAULT, key);
}

protected PublicKey putKey(String id, PublicKey key) {
protected final PublicKey putKey(final String id, final PublicKey key) {
return this.publicKeyMap.put(id, key);
}

protected PrivateKey getPrivateKey(final String id) {
protected final PrivateKey getPrivateKey(final String id) {
return this.privateKeyMap.get(id);
}

Expand All @@ -110,39 +113,39 @@ private Context getContextForKey(final PrivateKey key) {
return CryptoFactory.createContext(key.getAlgorithmName());
}

protected boolean hasDefaultPrivateKey() {
protected final boolean hasDefaultPrivateKey() {
return this.privateKeyMap.containsKey(DEFAULT);
}

protected boolean hasPrivateKey(String id) {
protected final boolean hasPrivateKey(final String id) {
return this.privateKeyMap.containsKey(id);
}

protected boolean hasDefaultPublicKey() {
protected final boolean hasDefaultPublicKey() {
return this.publicKeyMap.containsKey(DEFAULT);
}

protected boolean hasPublicKey(String id) {
protected final boolean hasPublicKey(final String id) {
return this.publicKeyMap.containsKey(id);
}

protected Set<Entry<String,PrivateKey>> privateKeys() {
protected final Set<Entry<String, PrivateKey>> privateKeys() {
return this.privateKeyMap.entrySet();
}

protected Set<Entry<String,PublicKey>> publicKeys() {
protected final Set<Entry<String, PublicKey>> publicKeys() {
return this.publicKeyMap.entrySet();
}

protected void fillPublicKeyForDefaultPrivate() {
protected final void fillPublicKeyForDefaultPrivate() {
fillPublicKeyForPrivate(DEFAULT);
}

protected void fillPublicKeyForPrivate(String id) {
protected final void fillPublicKeyForPrivate(final String id) {
fillPublicKeyForPrivate(id, false);
}

protected void fillPublicKeyForPrivate(String id, boolean force) {
protected final void fillPublicKeyForPrivate(final String id, final boolean force) {
if (force || !hasPublicKey(id)) {
var pk = getPrivateKey(id);
String algorithmName = pk.getAlgorithmName();
Expand All @@ -152,7 +155,7 @@ protected void fillPublicKeyForPrivate(String id, boolean force) {
}
}

protected void fillRandomPrivateKey(String id, String algorithm) {
protected final void fillRandomPrivateKey(final String id, final String algorithm) {
if (!hasPrivateKey(id)) {
var ctx = CryptoFactory.createContext(algorithm);
var privKey = ctx.newRandomPrivateKey();
Expand All @@ -161,7 +164,7 @@ protected void fillRandomPrivateKey(String id, String algorithm) {
}
}

protected void fillRandomPrivateKey(String algorithm) {
protected final void fillRandomPrivateKey(final String algorithm) {
fillRandomPrivateKey(DEFAULT, algorithm);
}

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<module>daml-on-sawtooth-protobufs</module>
<module>timekeeper</module>
<module>keymanager</module>
<module>sawtooth-utils</module>
<module>sawtooth-daml-tp</module>
<module>sawtooth-daml-rpc</module>
<module>sawtooth-daml-common</module>
Expand Down
5 changes: 5 additions & 0 deletions sawtooth-daml-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<artifactId>keymanager</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>daml-on-sawtooth</groupId>
<artifactId>sawtooth-utils</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- Hyperledger Sawtooth -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.blockchaintp.sawtooth.daml.rpc;

import static sawtooth.sdk.processor.Utils.hash512;
import static com.blockchaintp.sawtooth.timekeeper.Namespace.TIMEKEEPER_GLOBAL_RECORD;
import static sawtooth.sdk.processor.Utils.hash512;

import java.net.InetAddress;
import java.net.UnknownHostException;
Expand All @@ -16,6 +16,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.blockchaintp.keymanager.KeyManager;
import com.blockchaintp.sawtooth.SawtoothClientUtils;
import com.blockchaintp.sawtooth.daml.DamlEngineSingleton;
import com.blockchaintp.sawtooth.daml.Namespace;
Expand All @@ -27,7 +28,6 @@
import com.blockchaintp.sawtooth.daml.protobuf.DamlTransaction;
import com.blockchaintp.sawtooth.daml.protobuf.DamlTransactionFragment;
import com.blockchaintp.sawtooth.messaging.ZmqStream;
import com.blockchaintp.keymanager.KeyManager;
import com.codahale.metrics.SharedMetricRegistries;
import com.daml.ledger.api.health.HealthStatus;
import com.daml.ledger.participant.state.kvutils.DamlKvutils.DamlLogEntryId;
Expand All @@ -51,7 +51,6 @@
import sawtooth.sdk.processor.exceptions.ValidatorConnectionError;
import sawtooth.sdk.protobuf.Batch;
import sawtooth.sdk.protobuf.ClientBatchSubmitResponse;
import sawtooth.sdk.protobuf.ClientBatchSubmitResponse.Status;
import sawtooth.sdk.protobuf.Transaction;
import scala.collection.JavaConverters;
import scala.concurrent.ExecutionContext;
Expand Down Expand Up @@ -361,7 +360,7 @@ public void run() {
}
}
LOGGER.trace("Flushed {} futures", flushCount);
if (outStandingFutures.isEmpty()|| flushCount > 0) {
if (outStandingFutures.isEmpty() || flushCount > 0) {
accepted = outStandingFutures.offer(submitBatch);
}
} catch (ValidatorConnectionError | InterruptedException | SawtoothWriteException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import java.util.Map;
import java.util.concurrent.TimeoutException;

import com.blockchaintp.sawtooth.SawtoothClientUtils;
import com.blockchaintp.sawtooth.daml.EventConstants;
import com.blockchaintp.sawtooth.daml.exceptions.DamlSawtoothRuntimeException;
import com.blockchaintp.utils.protobuf.VersionedEnvelope;
import com.blockchaintp.sawtooth.messaging.ZmqStream;
import com.blockchaintp.utils.VersionedEnvelopeUtils;
import com.blockchaintp.utils.protobuf.VersionedEnvelope;
import com.daml.ledger.participant.state.kvutils.KVOffset;
import com.daml.ledger.participant.state.kvutils.api.LedgerRecord;
import com.daml.ledger.participant.state.v1.Offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

package com.blockchaintp.sawtooth.daml.processor;

import static sawtooth.sdk.processor.Utils.hash512;
import static com.blockchaintp.sawtooth.timekeeper.Namespace.TIMEKEEPER_GLOBAL_RECORD;
import static sawtooth.sdk.processor.Utils.hash512;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -24,22 +25,23 @@
import java.util.Map;
import java.util.Map.Entry;

import com.blockchaintp.sawtooth.SawtoothClientUtils;
import com.blockchaintp.sawtooth.daml.EventConstants;
import com.blockchaintp.sawtooth.daml.Namespace;
import com.blockchaintp.sawtooth.daml.exceptions.DamlSawtoothRuntimeException;
import com.blockchaintp.sawtooth.daml.protobuf.DamlTransaction;
import com.blockchaintp.sawtooth.daml.protobuf.DamlTransactionFragment;
import com.blockchaintp.utils.protobuf.VersionedEnvelope;
import com.blockchaintp.sawtooth.timekeeper.protobuf.TimeKeeperGlobalRecord;
import com.blockchaintp.utils.VersionedEnvelopeUtils;
import com.blockchaintp.utils.protobuf.VersionedEnvelope;
import com.daml.ledger.validator.LedgerStateOperations;
import com.daml.lf.data.Time.Timestamp;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.Timestamps;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.logstash.logback.encoder.org.apache.commons.lang3.ArrayUtils;
import sawtooth.sdk.processor.Context;
import sawtooth.sdk.processor.exceptions.InternalError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;

import com.blockchaintp.sawtooth.SawtoothClientUtils;
import com.blockchaintp.sawtooth.daml.DamlEngineSingleton;
import com.blockchaintp.sawtooth.daml.Namespace;
import com.blockchaintp.sawtooth.daml.exceptions.DamlSawtoothRuntimeException;
Expand All @@ -41,8 +40,10 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sawtooth.sdk.processor.Context;
import sawtooth.sdk.processor.TransactionHandler;
import sawtooth.sdk.processor.exceptions.InternalError;
Expand Down
Loading

0 comments on commit 7db5ff7

Please sign in to comment.