Skip to content

Commit

Permalink
Refactor Seal to have Any message type value()
Browse files Browse the repository at this point in the history
Allows Seals of arbitrary self describing protobuf messages
  • Loading branch information
Hellblazer committed Dec 9, 2023
1 parent 5996e03 commit f77db69
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 113 deletions.
2 changes: 2 additions & 0 deletions grpc/src/main/proto/stereotomy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ option java_package = "com.salesfoce.apollo.stereotomy.event.proto";
option java_outer_classname = "StereotomyProto";
option objc_class_prefix = "Stereo";
import "crypto.proto";
import "google/protobuf/any.proto";

package stereotomy;

Expand Down Expand Up @@ -52,6 +53,7 @@ message Sealed {
EventLoc event = 3;
bytes bits = 4;
}
google.protobuf.Any value = 5;
}

// Common header for events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void register(SubContext context) {
assertNotNull(attached);
assertEquals(1, attached.seals().size());
final var extracted = attached.seals().get(0);
assertTrue(extracted instanceof Seal.DigestSeal);
assertTrue(extracted instanceof Seal.EventSeal);
// assertEquals(1, attached.endorsements().size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.salesforce.apollo.stereotomy.*;
import com.salesforce.apollo.stereotomy.event.EstablishmentEvent;
import com.salesforce.apollo.stereotomy.event.KeyEvent;
import com.salesforce.apollo.stereotomy.event.Seal.CoordinatesSeal;
import com.salesforce.apollo.stereotomy.event.Seal;
import com.salesforce.apollo.stereotomy.event.Seal.DigestSeal;
import com.salesforce.apollo.stereotomy.identifier.Identifier;
import com.salesforce.apollo.stereotomy.identifier.SelfAddressingIdentifier;
Expand Down Expand Up @@ -43,7 +43,7 @@ public class ShardedKERLTest {
@BeforeEach
public void before() throws Exception {
secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(new byte[]{0});
secureRandom.setSeed(new byte[] { 0 });
}

@Test
Expand All @@ -52,8 +52,7 @@ public void delegated() throws Exception {
Emulator emmy = new Emulator();
emmy.start(Domain.boostrapMigration());

ShardedKERL kerl = new ShardedKERL(emmy.newConnector(), emmy.getMutator(), timeout,
DigestAlgorithm.DEFAULT);
ShardedKERL kerl = new ShardedKERL(emmy.newConnector(), emmy.getMutator(), timeout, DigestAlgorithm.DEFAULT);

var ks = new MemKeyStore();
Stereotomy controller = new StereotomyImpl(ks, kerl, secureRandom);
Expand All @@ -68,15 +67,16 @@ public void delegated() throws Exception {
var sap = (SelfAddressingIdentifier) identifier.getIdentifier();
assertEquals(DigestAlgorithm.DEFAULT, sap.getDigest().getAlgorithm());
assertEquals("092126af01f80ca28e7a99bbdce229c029be3bbfcb791e29ccb7a64e8019a36f",
Hex.hex(sap.getDigest().getBytes()));
Hex.hex(sap.getDigest().getBytes()));

assertEquals(1, ((Unweighted) identifier.getSigningThreshold()).getThreshold());

// keys
assertEquals(1, identifier.getKeys().size());
assertNotNull(identifier.getKeys().get(0));

EstablishmentEvent lastEstablishmentEvent = (EstablishmentEvent) kerl.getKeyEvent(identifier.getLastEstablishmentEvent());
EstablishmentEvent lastEstablishmentEvent = (EstablishmentEvent) kerl.getKeyEvent(
identifier.getLastEstablishmentEvent());
assertEquals(identifier.getKeys().get(0), lastEstablishmentEvent.getKeys().get(0));

var keyCoordinates = KeyCoordinates.of(lastEstablishmentEvent, 0);
Expand All @@ -89,10 +89,10 @@ public void delegated() throws Exception {
var keyStoreNextKeyPair = ks.getNextKey(keyCoordinates);
assertTrue(keyStoreNextKeyPair.isPresent());
var expectedNextKeys = KeyConfigurationDigester.digest(SigningThreshold.unweighted(1),
List.of(keyStoreNextKeyPair.get().getPublic()),
identifier.getNextKeyConfigurationDigest()
.get()
.getAlgorithm());
List.of(keyStoreNextKeyPair.get().getPublic()),
identifier.getNextKeyConfigurationDigest()
.get()
.getAlgorithm());
assertEquals(expectedNextKeys, identifier.getNextKeyConfigurationDigest().get());

// witnesses
Expand All @@ -116,8 +116,7 @@ public void delegated() throws Exception {

var digest = DigestAlgorithm.BLAKE3_256.digest("digest seal".getBytes());
var event = EventCoordinates.of(kerl.getKeyEvent(identifier.getLastEstablishmentEvent()));
var seals = List.of(DigestSeal.construct(digest), DigestSeal.construct(digest),
CoordinatesSeal.construct(event));
var seals = List.of(DigestSeal.construct(digest), DigestSeal.construct(digest), Seal.construct(event));

identifier.rotate();
identifier.seal(InteractionSpecification.newBuilder());
Expand All @@ -131,17 +130,15 @@ public void direct() throws Exception {
Emulator emmy = new Emulator();
emmy.start(Domain.boostrapMigration());

ShardedKERL kerl = new ShardedKERL(emmy.newConnector(), emmy.getMutator(), timeout,
DigestAlgorithm.DEFAULT);
ShardedKERL kerl = new ShardedKERL(emmy.newConnector(), emmy.getMutator(), timeout, DigestAlgorithm.DEFAULT);

Stereotomy controller = new StereotomyImpl(new MemKeyStore(), kerl, secureRandom);

var i = controller.newIdentifier();

var digest = DigestAlgorithm.BLAKE3_256.digest("digest seal".getBytes());
var event = EventCoordinates.of(kerl.getKeyEvent(i.getLastEstablishmentEvent()));
var seals = List.of(DigestSeal.construct(digest), DigestSeal.construct(digest),
CoordinatesSeal.construct(event));
var seals = List.of(DigestSeal.construct(digest), DigestSeal.construct(digest), Seal.construct(event));

i.rotate();
i.seal(InteractionSpecification.newBuilder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.salesforce.apollo.membership.stereotomy.ControlledIdentifierMember;
import com.salesforce.apollo.stereotomy.*;
import com.salesforce.apollo.stereotomy.event.KeyEvent;
import com.salesforce.apollo.stereotomy.event.Seal.CoordinatesSeal;
import com.salesforce.apollo.stereotomy.event.Seal;
import com.salesforce.apollo.stereotomy.event.Seal.DigestSeal;
import com.salesforce.apollo.stereotomy.identifier.spec.InteractionSpecification;
import com.salesforce.apollo.stereotomy.identifier.spec.RotationSpecification;
Expand Down Expand Up @@ -79,8 +79,7 @@ public void kerl() throws Exception {

var digest = DigestAlgorithm.BLAKE3_256.digest("digest seal".getBytes());
var event = EventCoordinates.of(kel.getKeyEvent(i.getLastEstablishmentEvent()));
var seals = List.of(DigestSeal.construct(digest), DigestSeal.construct(digest),
CoordinatesSeal.construct(event));
var seals = List.of(DigestSeal.construct(digest), DigestSeal.construct(digest), Seal.construct(event));

i.rotate();
i.seal(InteractionSpecification.newBuilder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.salesforce.apollo.stereotomy.event;

import com.google.protobuf.Any;
import com.salesfoce.apollo.stereotomy.event.proto.EventLoc;
import com.salesfoce.apollo.stereotomy.event.proto.Sealed;
import com.salesforce.apollo.cryptography.Digest;
Expand All @@ -14,102 +15,168 @@

/**
* @author hal.hildebrand
*
*/
public interface Seal {
interface CoordinatesSeal extends Seal {

static CoordinatesSeal construct(EventCoordinates coordinates) {
return new CoordinatesSeal() {
static Seal from(Sealed s) {
if (s.hasEventCoordinates()) {
return construct(new EventCoordinates(s.getEventCoordinates()));
}
if (s.hasDigest()) {
return DigestSeal.construct(Digest.from(s.getDigest()));
}
if (s.hasEvent()) {
EventLoc event = s.getEvent();
return EventSeal.construct(Identifier.from(event.getIdentifier()), new Digest(event.getDigest()),
event.getSequenceNumber());
}
return ProtoSeal.construct(s.getValue());
}

static CoordinatesSeal construct(EventCoordinates coordinates) {
return new CoordinatesSealImpl(coordinates);
}

@Override
public EventCoordinates getEvent() {
return coordinates;
}
Sealed toSealed();

@Override
public Sealed toSealed() {
return Sealed.newBuilder().setEventCoordinates(coordinates.toEventCoords()).build();
}
};
}
Any value();

interface CoordinatesSeal extends Seal {

EventCoordinates getEvent();
}

interface DigestSeal extends Seal {

static DigestSeal construct(Digest digest) {
return new DigestSeal() {

@Override
public Digest getDigest() {
return digest;
}

@Override
public Sealed toSealed() {
return Sealed.newBuilder().setDigest(digest.toDigeste()).build();
}
};
return new DigestSealImpl(digest);
}

Digest getDigest();

class DigestSealImpl extends ProtoSeal implements DigestSeal {

private final Digest digest;

public DigestSealImpl(Digest digest) {
this(digest, null);
}

public DigestSealImpl(Digest digest, Any value) {
super(value);
this.digest = digest;
}

@Override
public Digest getDigest() {
return digest;
}

@Override
public Sealed toSealed() {
return Sealed.newBuilder().setDigest(digest.toDigeste()).setValue(value()).build();
}
}
}

interface EventSeal extends Seal {

static EventSeal construct(Identifier prefix, Digest digest, long sequenceNumber) {
return new EventSeal() {

@Override
public Digest getDigest() {
return digest;
}

@Override
public Identifier getPrefix() {
return prefix;
}

@Override
public long getSequenceNumber() {
return sequenceNumber;
}

@Override
public Sealed toSealed() {
return Sealed.newBuilder()
.setEvent(EventLoc.newBuilder()
.setIdentifier(prefix.toIdent())
.setSequenceNumber(sequenceNumber))
.setDigest(digest.toDigeste())
.build();
}
};
return new EventSealImpl(prefix, digest, sequenceNumber);
}

Digest getDigest();

Identifier getPrefix();

long getSequenceNumber();

class EventSealImpl extends ProtoSeal implements EventSeal {
private final Identifier prefix;
private final Digest digest;
private final long sequenceNumber;

public EventSealImpl(Identifier prefix, Digest digest, long sequenceNumber) {
this(prefix, digest, sequenceNumber, null);
}

public EventSealImpl(Identifier prefix, Digest digest, long sequenceNumber, Any value) {
super(value);
this.prefix = prefix;
this.digest = digest;
this.sequenceNumber = sequenceNumber;
}

@Override
public Digest getDigest() {
return digest;
}

@Override
public Identifier getPrefix() {
return prefix;
}

@Override
public long getSequenceNumber() {
return sequenceNumber;
}

@Override
public Sealed toSealed() {
return Sealed.newBuilder()
.setEvent(EventLoc.newBuilder()
.setDigest(digest.toDigeste())
.setIdentifier(prefix.toIdent())
.setSequenceNumber(sequenceNumber))
.setValue(value())
.build();
}
}
}

static Seal from(Sealed s) {
if (s.hasEventCoordinates()) {
return CoordinatesSeal.construct(new EventCoordinates(s.getEventCoordinates()));
class ProtoSeal implements Seal {
private final Any value;

public ProtoSeal(Any value) {
this.value = value == null ? Any.getDefaultInstance() : value;
}
if (s.hasDigest()) {
return DigestSeal.construct(Digest.from(s.getDigest()));

static ProtoSeal construct(Any value) {
return new Seal.ProtoSeal(value);
}
if (s.hasEvent()) {
EventLoc event = s.getEvent();
return EventSeal.construct(Identifier.from(event.getIdentifier()), new Digest(event.getDigest()),
event.getSequenceNumber());

@Override
public Sealed toSealed() {
return Sealed.newBuilder().setValue(value).build();
}

@Override
public Any value() {
return value;
}
throw new IllegalArgumentException("Unknown seal type");
}

Sealed toSealed();
class CoordinatesSealImpl extends ProtoSeal implements CoordinatesSeal {
private final EventCoordinates coordinates;

public CoordinatesSealImpl(EventCoordinates coordinates) {
this(coordinates, null);
}

public CoordinatesSealImpl(EventCoordinates coordinates, Any value) {
super(value);
this.coordinates = coordinates;
}

@Override
public EventCoordinates getEvent() {
return coordinates;
}

@Override
public Sealed toSealed() {
return Sealed.newBuilder().setEventCoordinates(coordinates.toEventCoords()).build();
}
}
}
Loading

0 comments on commit f77db69

Please sign in to comment.