Skip to content

Commit

Permalink
use stereotomy msg, add BootstrapVerifiersTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Dec 27, 2023
1 parent 1d83bdc commit 71a61d9
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.salesforce.apollo.archipelago.RouterImpl;
import com.salesforce.apollo.cryptography.Verifier;
import com.salesforce.apollo.fireflies.comm.entrance.EntranceClient;
import com.salesforce.apollo.fireflies.proto.IdentifierSequenceNumber;
import com.salesforce.apollo.fireflies.comm.entrance.Entrance;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.membership.SigningMember;
import com.salesforce.apollo.ring.SliceIterator;
import com.salesforce.apollo.stereotomy.EventCoordinates;
import com.salesforce.apollo.stereotomy.KeyState;
import com.salesforce.apollo.stereotomy.KeyStateVerifier;
import com.salesforce.apollo.stereotomy.Verifiers;
import com.salesforce.apollo.stereotomy.event.proto.IdentAndSeq;
import com.salesforce.apollo.stereotomy.event.proto.KeyState_;
import com.salesforce.apollo.stereotomy.event.protobuf.KeyStateImpl;
import com.salesforce.apollo.stereotomy.identifier.Identifier;
Expand All @@ -42,20 +43,21 @@
* @author hal.hildebrand
**/
public class BootstrapVerifiers implements Verifiers {
private final static Logger log = LoggerFactory.getLogger(
private final static Logger log = LoggerFactory.getLogger(
BootstrapVerifiers.class);
private final List<View.Participant> successors;
private final View.Node member;
private final int majority;
private final LoadingCache<EventCoordinates, KeyState> ksCoords;
private final LoadingCache<IdentifierSequence, KeyState> ksSeq;
private final RouterImpl.CommonCommunications<EntranceClient, ?> communications;
private final Duration operationTimeout;
private final Duration operationsFrequency;

public BootstrapVerifiers(View.Node member, Duration operationTimeout, List<View.Participant> successors,
int majority, Duration operationsFrequency,
RouterImpl.CommonCommunications<EntranceClient, ?> communications) {
private final List<? extends Member> successors;
private final SigningMember member;
private final int majority;
private final LoadingCache<EventCoordinates, KeyState> ksCoords;
private final LoadingCache<IdentifierSequence, KeyState> ksSeq;
private final RouterImpl.CommonCommunications<Entrance, ?> communications;
private final Duration operationTimeout;
private final Duration operationsFrequency;

public <S extends SigningMember, M extends Member> BootstrapVerifiers(S member, Duration operationTimeout,
List<M> successors, int majority,
Duration operationsFrequency,
RouterImpl.CommonCommunications<Entrance, ?> communications) {
this.member = member;
this.successors = successors;
this.majority = majority;
Expand Down Expand Up @@ -188,11 +190,11 @@ private KeyState delegate(EventCoordinates coordinates) {
}

private record IdentifierSequence(Identifier identifier, ULong seqNum) {
public IdentifierSequenceNumber toIdSeq() {
return IdentifierSequenceNumber.newBuilder()
.setIdentifier(identifier.toIdent())
.setSequenceNumber(seqNum.longValue())
.build();
public IdentAndSeq toIdSeq() {
return IdentAndSeq.newBuilder()
.setIdentifier(identifier.toIdent())
.setSequenceNumber(seqNum.longValue())
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.salesforce.apollo.archipelago.Link;
import com.salesforce.apollo.fireflies.View.Node;
import com.salesforce.apollo.fireflies.proto.*;
import com.salesforce.apollo.fireflies.proto.Gateway;
import com.salesforce.apollo.fireflies.proto.Join;
import com.salesforce.apollo.fireflies.proto.Redirect;
import com.salesforce.apollo.fireflies.proto.Registration;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.stereotomy.event.proto.EventCoords;
import com.salesforce.apollo.stereotomy.event.proto.IdentAndSeq;
import com.salesforce.apollo.stereotomy.event.proto.KeyState_;

import java.io.IOException;
Expand All @@ -30,7 +34,7 @@ public void close() throws IOException {
}

@Override
public ListenableFuture<KeyState_> getKeyState(IdentifierSequenceNumber idSeq) {
public ListenableFuture<KeyState_> getKeyState(IdentAndSeq idSeq) {
return null;
}

Expand All @@ -56,7 +60,7 @@ public ListenableFuture<Redirect> seed(Registration registration) {
};
}

ListenableFuture<KeyState_> getKeyState(IdentifierSequenceNumber idSeq);
ListenableFuture<KeyState_> getKeyState(IdentAndSeq idSeq);

ListenableFuture<KeyState_> getKeyState(EventCoords coords);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.salesforce.apollo.fireflies.proto.EntranceGrpc.EntranceFutureStub;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.stereotomy.event.proto.EventCoords;
import com.salesforce.apollo.stereotomy.event.proto.IdentAndSeq;
import com.salesforce.apollo.stereotomy.event.proto.KeyState_;

import java.time.Duration;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void close() {
}

@Override
public ListenableFuture<KeyState_> getKeyState(IdentifierSequenceNumber idSeq) {
public ListenableFuture<KeyState_> getKeyState(IdentAndSeq idSeq) {
return client.getKeyStateIdentifier(idSeq);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
import com.salesforce.apollo.fireflies.FireflyMetrics;
import com.salesforce.apollo.fireflies.View.Service;
import com.salesforce.apollo.fireflies.proto.EntranceGrpc.EntranceImplBase;
import com.salesforce.apollo.fireflies.proto.*;
import com.salesforce.apollo.fireflies.proto.Gateway;
import com.salesforce.apollo.fireflies.proto.Join;
import com.salesforce.apollo.fireflies.proto.Redirect;
import com.salesforce.apollo.fireflies.proto.Registration;
import com.salesforce.apollo.protocols.ClientIdentity;
import com.salesforce.apollo.stereotomy.EventCoordinates;
import com.salesforce.apollo.stereotomy.event.proto.EventCoords;
import com.salesforce.apollo.stereotomy.event.proto.IdentAndSeq;
import com.salesforce.apollo.stereotomy.event.proto.KeyState_;
import com.salesforce.apollo.stereotomy.identifier.Identifier;
import io.grpc.stub.StreamObserver;
Expand Down Expand Up @@ -51,7 +55,7 @@ public void getKeyStateCoords(EventCoords request, StreamObserver<KeyState_> res
}

@Override
public void getKeyStateIdentifier(IdentifierSequenceNumber request, StreamObserver<KeyState_> responseObserver) {
public void getKeyStateIdentifier(IdentAndSeq request, StreamObserver<KeyState_> responseObserver) {
Digest from = identity.getFrom();
if (from == null) {
responseObserver.onError(new IllegalStateException("Member has been removed"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.salesforce.apollo.fireflies;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import com.salesforce.apollo.archipelago.RouterImpl;
import com.salesforce.apollo.cryptography.DigestAlgorithm;
import com.salesforce.apollo.cryptography.JohnHancock;
import com.salesforce.apollo.cryptography.SignatureAlgorithm;
import com.salesforce.apollo.cryptography.proto.Digeste;
import com.salesforce.apollo.fireflies.comm.entrance.Entrance;
import com.salesforce.apollo.fireflies.comm.entrance.EntranceClient;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.membership.stereotomy.ControlledIdentifierMember;
import com.salesforce.apollo.stereotomy.StereotomyImpl;
import com.salesforce.apollo.stereotomy.event.proto.EventCoords;
import com.salesforce.apollo.stereotomy.event.proto.IdentAndSeq;
import com.salesforce.apollo.stereotomy.event.proto.KeyState_;
import com.salesforce.apollo.stereotomy.mem.MemKERL;
import com.salesforce.apollo.stereotomy.mem.MemKeyStore;
import org.joou.ULong;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.io.ByteArrayInputStream;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author hal.hildebrand
**/
public class BootstrapVerifiersTest {

@Test
public void smokin() throws Exception {
int cardinality = 5;
int majority = 3;

var entropy = SecureRandom.getInstance("SHA1PRNG");
entropy.setSeed(new byte[] { 6, 6, 6 });
var stereotomy = new StereotomyImpl(new MemKeyStore(), new MemKERL(DigestAlgorithm.DEFAULT), entropy);

var members = IntStream.range(0, cardinality)
.mapToObj(i -> stereotomy.newIdentifier())
.map(cpk -> new ControlledIdentifierMember(cpk))
.collect(Collectors.toList());
var member = members.getFirst();

Digeste testDigest = DigestAlgorithm.DEFAULT.getLast().toDigeste();
Entrance client = mock(Entrance.class);
SettableFuture<KeyState_> ks = SettableFuture.create();
ks.set(KeyState_.newBuilder().setDigest(testDigest).build());
when(client.getKeyState(any(EventCoords.class))).then(new Answer<>() {
@Override
public ListenableFuture<KeyState_> answer(InvocationOnMock invocation) throws Throwable {
return ks;
}
});
when(client.getKeyState(any(IdentAndSeq.class))).then(new Answer<>() {
@Override
public ListenableFuture<KeyState_> answer(InvocationOnMock invocation) throws Throwable {
return ks;
}
});
when(client.getMember()).then(new Answer<>() {
@Override
public Member answer(InvocationOnMock invocation) throws Throwable {
return member;
}
});

@SuppressWarnings("unchecked")
RouterImpl.CommonCommunications<Entrance, ?> comm = mock(RouterImpl.CommonCommunications.class);
when(comm.connect(any())).thenReturn(client);

RouterImpl.CommonCommunications<EntranceClient, ?> communications = null;
var verifiers = new BootstrapVerifiers(member, Duration.ofSeconds(1), members, majority, Duration.ofMillis(10),
comm);

var verifier = verifiers.verifierFor(member.getIdentifier().getIdentifier());
assertFalse(verifier.isEmpty());

JohnHancock signature = new JohnHancock(SignatureAlgorithm.DEFAULT, new byte[0], ULong.valueOf(0));
var is = new ByteArrayInputStream(new byte[0]);
var keyState = verifier.get().verify(signature, is);
assertNotNull(keyState);
}
}
7 changes: 1 addition & 6 deletions grpc/src/main/proto/fireflies.proto
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ service Entrance {
rpc seed (Registration) returns (Redirect) {}
rpc join (Join) returns (Gateway) {}
rpc getKeyStateCoords(stereotomy.EventCoords) returns (stereotomy.KeyState_) {}
rpc getKeyStateIdentifier(IdentifierSequenceNumber) returns (stereotomy.KeyState_) {}
}

message IdentifierSequenceNumber {
stereotomy.Ident identifier = 1;
uint64 sequenceNumber = 2;
rpc getKeyStateIdentifier(stereotomy.IdentAndSeq) returns (stereotomy.KeyState_) {}
}

message Registration {
Expand Down

0 comments on commit 71a61d9

Please sign in to comment.