Skip to content

Commit

Permalink
Remove EventType
Browse files Browse the repository at this point in the history
Java already has a type system that we are using.
  • Loading branch information
stevetodd committed Apr 9, 2021
1 parent 03b7281 commit a52b097
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import foundation.identity.keri.api.Version;
import foundation.identity.keri.api.crypto.StandardFormats;
import foundation.identity.keri.api.crypto.StandardSignatureAlgorithms;
import foundation.identity.keri.api.event.EventType;
import foundation.identity.keri.api.event.Format;
import foundation.identity.keri.api.event.KeyConfigurationDigest;
import foundation.identity.keri.api.event.SigningThreshold;
Expand All @@ -33,14 +32,21 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static foundation.identity.keri.Hex.hex;
import static foundation.identity.keri.Hex.hexNoPad;
import static foundation.identity.keri.QualifiedBase64.qb64;
import static foundation.identity.keri.api.event.EventFieldNames.*;
import static java.nio.charset.StandardCharsets.UTF_8;

public final class EventSerializer {
private final static byte[] KERI_BYTES = "KERI".getBytes(UTF_8);
private static final byte[] KERI_BYTES = "KERI".getBytes(UTF_8);

private static final String INCEPTION_TYPE = "icp";
private static final String ROTATION_TYPE = "rot";
private static final String INTERACTION_TYPE = "ixn";
private static final String DELEGATED_INCEPTION_TYPE = "dip";
private static final String DELEGATED_ROTATION_TYPE = "drt";
private static final String RECEIPT_FROM_BASIC_TYPE = "rct";
private static final String RECEIPT_FROM_TRANSFERABLE_TYPE = "vrc";

private static final ObjectMapper JSON = new ObjectMapper();
private static final ObjectMapper CBOR = new ObjectMapper(new CBORFactory());
Expand Down Expand Up @@ -74,18 +80,6 @@ static String format(Format f) {
};
}

static String type(EventType t) {
return switch (t) {
case INCEPTION -> "icp";
case ROTATION -> "rot";
case INTERACTION -> "ixn";
case DELEGATED_INCEPTION -> "dip";
case DELEGATED_ROTATION -> "drt";
case RECEIPT -> "rct";
case RECEIPT_FROM_TRANSFERABLE -> "vrc";
};
}

static ObjectNode seal(Seal seal, ObjectMapper mapper) {
var obj = mapper.createObjectNode();
if (seal instanceof KeyEventCoordinatesSeal) {
Expand Down Expand Up @@ -183,7 +177,7 @@ public byte[] serialize(Identifier identifier, IdentifierSpec spec) {
rootNode.put(IDENTIFIER.label(), qb64(identifier));
}
rootNode.put(SEQUENCE_NUMBER.label(), hexNoPad(0));
rootNode.put(EVENT_TYPE.label(), type(EventType.INCEPTION));
rootNode.put(EVENT_TYPE.label(), INCEPTION_TYPE);

rootNode.set(SIGNING_THRESHOLD.label(), signingThreshold(spec.signingThreshold(), mapper));

Expand Down Expand Up @@ -223,7 +217,7 @@ public byte[] serialize(RotationSpec spec) {
rootNode.put(VERSION.label(), version(Version.CURRENT, spec.format(), 0));
rootNode.put(IDENTIFIER.label(), qb64(spec.identifier()));
rootNode.put(SEQUENCE_NUMBER.label(), hexNoPad(spec.sequenceNumber()));
rootNode.put(EVENT_TYPE.label(), type(EventType.ROTATION));
rootNode.put(EVENT_TYPE.label(), ROTATION_TYPE);
rootNode.put(PRIOR_EVENT_DIGEST.label(), qb64(spec.previous().digest()));

rootNode.set(SIGNING_THRESHOLD.label(), signingThreshold(spec.signingThreshold(), mapper));
Expand Down Expand Up @@ -268,7 +262,7 @@ public byte[] serialize(InteractionSpec spec) {
rootNode.put(VERSION.label(), version(Version.CURRENT, spec.format(), 0));
rootNode.put(IDENTIFIER.label(), qb64(spec.identifier()));
rootNode.put(SEQUENCE_NUMBER.label(), hexNoPad(spec.sequenceNumber()));
rootNode.put(EVENT_TYPE.label(), type(EventType.INTERACTION));
rootNode.put(EVENT_TYPE.label(), INTERACTION_TYPE);
rootNode.put(PRIOR_EVENT_DIGEST.label(), qb64(spec.previous().digest()));

var sealsNode = mapper.createArrayNode();
Expand All @@ -288,13 +282,16 @@ public byte[] serialize(ReceiptFromTransferableIdentifierSpec spec) {
var mapper = mapper(spec.format());
var rootNode = mapper.createObjectNode();

var eventSignature = spec.signatures().stream().findFirst().get();
rootNode.put(VERSION.label(), version(Version.CURRENT, spec.format(), 0));
rootNode.put(IDENTIFIER.label(), qb64(eventSignature.event().identifier()));
rootNode.put(SEQUENCE_NUMBER.label(), hexNoPad(eventSignature.event().sequenceNumber()));
rootNode.put(EVENT_TYPE.label(), type(EventType.RECEIPT_FROM_TRANSFERABLE));
rootNode.put(EVENT_DIGEST.label(), qb64(eventSignature.event().digest()));
rootNode.put(IDENTIFIER.label(), qb64(spec.event().identifier()));
rootNode.put(SEQUENCE_NUMBER.label(), hexNoPad(spec.event().sequenceNumber()));
rootNode.put(EVENT_TYPE.label(), RECEIPT_FROM_TRANSFERABLE_TYPE);
rootNode.put(EVENT_DIGEST.label(), qb64(spec.event().digest()));

var eventSignature = spec.signatures()
.stream()
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("at least one signature is required"));
var anchorNode = mapper.createObjectNode();
anchorNode.put(IDENTIFIER.label(), qb64(eventSignature.key().establishmentEvent().identifier()));
anchorNode.put(SEQUENCE_NUMBER.label(), hexNoPad(eventSignature.key().establishmentEvent().sequenceNumber()));
Expand All @@ -317,7 +314,7 @@ public byte[] serialize(ReceiptSpec spec) {
rootNode.put(VERSION.label(), version(Version.CURRENT, spec.format(), 0));
rootNode.put(IDENTIFIER.label(), qb64(spec.event().identifier()));
rootNode.put(SEQUENCE_NUMBER.label(), hexNoPad(spec.event().sequenceNumber()));
rootNode.put(EVENT_TYPE.label(), type(EventType.RECEIPT));
rootNode.put(EVENT_TYPE.label(), RECEIPT_FROM_BASIC_TYPE);
rootNode.put(EVENT_DIGEST.label(), qb64(spec.event().digest()));

try {
Expand Down
19 changes: 18 additions & 1 deletion core/src/main/java/foundation/identity/keri/KeyEvents.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package foundation.identity.keri;

import foundation.identity.keri.api.event.AttachedEventSignature;
import foundation.identity.keri.api.event.DelegatedInceptionEvent;
import foundation.identity.keri.api.event.DelegatedRotationEvent;
import foundation.identity.keri.api.event.EstablishmentEvent;
import foundation.identity.keri.api.event.Event;
import foundation.identity.keri.api.event.KeyEvent;
Expand All @@ -11,6 +13,7 @@
import foundation.identity.keri.api.event.RotationEvent;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static java.lang.String.format;
Expand All @@ -22,7 +25,7 @@ public final class KeyEvents {

public static String toString(Event e) {
var sb = new StringBuilder();
sb.append(format("%s (KERI %s %s)", e.type(), e.version(), e.format())).append("\n");
sb.append(format("%s (KERI %s %s)", type(e.getClass()), e.version(), e.format())).append("\n");

if (e instanceof KeyEvent) {
var ie = (KeyEvent) e;
Expand Down Expand Up @@ -102,6 +105,20 @@ public static String toString(Event e) {
return sb.toString();
}

private static final Map<Class<? extends Event>, String> typeMapping = Map.of(
InceptionEvent.class, "icp",
RotationEvent.class, "rot",
InteractionEvent.class, "ixn",
DelegatedInceptionEvent.class, "dip",
DelegatedRotationEvent.class, "drt",
ReceiptFromBasicIdentifierEvent.class, "rct",
ReceiptFromTransferableIdentifierEvent.class, "vrc"
);

private static String type(Class<? extends Event> cls) {
return typeMapping.getOrDefault(cls, "?" + cls.getName());
}

private static <T> String listToString(List<T> list, Function<T, String> toString) {
return list.stream()
.map(toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@

public interface DelegatedInceptionEvent extends InceptionEvent, DelegatedEstablishmentEvent {

@Override
default EventType type() {
return EventType.DELEGATED_INCEPTION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@

public interface DelegatedRotationEvent extends RotationEvent, DelegatedEstablishmentEvent {

@Override
default EventType type() {
return EventType.DELEGATED_ROTATION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface DelegatingEventCoordinates {

BigInteger sequenceNumber();

EventType eventType();
Class<? extends Event> eventType();

KeyEventCoordinates previousEvent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ public interface Event {

Format format();

EventType type();

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ public interface InceptionEvent extends EstablishmentEvent {

Set<ConfigurationTrait> configurationTraits();

@Override
default EventType type() {
return EventType.INCEPTION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ public interface InteractionEvent extends KeyEvent, SealingEvent {

List<Seal> seals();

@Override
default EventType type() {
return EventType.INTERACTION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

public interface ReceiptFromBasicIdentifierEvent extends ReceiptEvent {

@Override
default EventType type() {
return EventType.RECEIPT;
}

Set<EventSignature> receipts();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

public interface ReceiptFromTransferableIdentifierEvent extends ReceiptEvent {

@Override
default EventType type() {
return EventType.RECEIPT_FROM_TRANSFERABLE;
}

KeyEventCoordinates keyEstablishmentEvent();

Set<AttachedEventSignature> signatures();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ public interface RotationEvent extends EstablishmentEvent, SealingEvent {

List<Seal> seals();

@Override
default EventType type() {
return EventType.ROTATION;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import foundation.identity.keri.KeyEventStore;
import foundation.identity.keri.KeyEventValidator;
import foundation.identity.keri.KeyEvents;
import foundation.identity.keri.api.KeyState;
import foundation.identity.keri.api.event.Event;
import foundation.identity.keri.api.event.KeyEvent;
Expand Down Expand Up @@ -73,7 +74,7 @@ private Publisher<? extends Event> retrieveIdentifierEvents(InetSocketAddress ad
.delayElements(Duration.ofMillis(250), Schedulers.single())
.doOnNext(e-> {
System.out.println("\n[Node] >>> EVENT >>> " + address);
EventUtils.printEvent(e);
KeyEvents.toString(e);
});
}

Expand All @@ -82,15 +83,15 @@ private Publisher<Void> handleEvents(EventInbound in, EventOutbound out) {
.doOnError(Throwable::printStackTrace)
.doOnNext(e -> {
System.out.println("\n[Node] <<< EVENT <<< " + ((Connection) in).channel().remoteAddress());
EventUtils.printEvent(e);
KeyEvents.toString(e);
})
.flatMap(e ->
out.sendEvent(
process(e)
.doOnError(Throwable::printStackTrace)
.doOnNext(e2 -> {
System.out.println("\n[Node] >>> EVENT >>> " + ((Connection) in).channel().remoteAddress());
EventUtils.printEvent(e2);
KeyEvents.toString(e2);
})
)
.then(),
Expand Down Expand Up @@ -118,20 +119,20 @@ private Flux<Event> process(Event event) {
}
}

private Flux<Event> produceChit(KeyEvent ie) {
var lastReceipt = this.keyEventStore.findLatestReceipt(this.identifier.identifier(), ie.identifier());
var lastEventSequenceNumber = lastReceipt.isPresent()
? lastReceipt.get().event().sequenceNumber()
: -1;
private Flux<Event> produceChit(KeyEvent keyEvent) {
var events = Flux.<Event> empty();
if (lastEventSequenceNumber != this.identifier.lastEvent().sequenceNumber()) {
long lastEventSequenceNumber = this.keyEventStore.findLatestReceipt(this.identifier.identifier(), keyEvent.identifier())
.map(es -> es.event().sequenceNumber())
.orElse(-1L);

if (lastEventSequenceNumber < this.identifier.lastEvent().sequenceNumber()) {
// make sure we've sent our log so they can verify the chit
System.out.println("[Node] WILL SEND LOG >>> ");
events = produceOwnLog(lastEventSequenceNumber);
}

System.out.println("[Node] WILL SEND VRC >>> ");
return Flux.concat(events, Mono.just(receiptEvent(ie)));
return Flux.concat(events, Mono.just(receiptEvent(keyEvent)));
}

private Flux<Event> produceOwnLog(long fromSequenceNumber) {
Expand Down
Loading

0 comments on commit a52b097

Please sign in to comment.