Skip to content

Commit

Permalink
use bftSlice for gorgoneion/maat successors
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Jun 2, 2024
1 parent c19e043 commit 91e0d86
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,8 @@ private SignedNonce generateNonce(KERL_ application) {
.setTimestamp(Timestamp.newBuilder().setSeconds(now.getEpochSecond()).setNanos(now.getNano()))
.build();

var successors = context.size() == 1 ? Collections.singletonList(member) : Context.uniqueSuccessors(context,
digestOf(
ident,
parameters.digestAlgorithm()));
var successors = context.size() == 1 ? Collections.singletonList(member)
: context.bftSubset(digestOf(ident, parameters.digestAlgorithm()));
final var majority = context.size() == 1 ? 1 : context.majority();
final var redirecting = new SliceIterator<>("Nonce Endorsement", member, successors, endorsementComm);
Set<MemberSignature> endorsements = Collections.newSetFromMap(new ConcurrentHashMap<>());
Expand Down Expand Up @@ -222,8 +220,7 @@ private void notarize(Credentials credentials, Validations validations) {
.setValidations(validations)
.build();

var successors = Context.uniqueSuccessors(context,
digestOf(identifier.toIdent(), parameters.digestAlgorithm()));
var successors = context.bftSubset(digestOf(identifier.toIdent(), parameters.digestAlgorithm()));
final var majority = context.size() == 1 ? 1 : context.majority();
SliceIterator<Endorsement> redirecting = new SliceIterator<>("Enrollment", member, successors, endorsementComm);
var completed = new HashSet<Member>();
Expand All @@ -249,8 +246,7 @@ private Validations register(Credentials request) {

var validated = new CompletableFuture<Validations>();

var successors = Context.uniqueSuccessors(context,
digestOf(identifier.toIdent(), parameters.digestAlgorithm()));
var successors = context.bftSubset(digestOf(identifier.toIdent(), parameters.digestAlgorithm()));
final var majority = context.size() == 1 ? 1 : context.majority();
final var redirecting = new SliceIterator<>("Credential verification", member, successors, endorsementComm);
var verifications = new HashSet<Validation_>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.salesforce.apollo.membership.Util;
import org.apache.commons.math3.random.BitsStreamGenerator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -34,18 +33,6 @@ public interface Context<T extends Member> {

double DEFAULT_EPSILON = 0.99999;

static List<Member> uniqueSuccessors(Context<Member> context, Digest digest) {
Set<Member> post = new HashSet<>();
context.successors(digest, m -> {
if (post.size() == context.getRingCount()) {
return false;
}
return post.add(m);
});
var successors = new ArrayList<>(post);
return successors;
}

static Digest hashFor(Digest ctxId, int ring, Digest d) {
return d.prefix(ctxId, ring);
}
Expand Down Expand Up @@ -516,11 +503,22 @@ default int toleranceLevel() {
Iterable<T> traverse(int ring, T member);

/**
* collect the list of successors to the key on each ring that pass the provided predicate test and provide a unique
* member per ring if possible.
* collect the list of successors to the key on each ring that pass the provided predicate test, and providing a
* unique member per ring if possible.
*/
void uniqueSuccessors(Digest key, Predicate<T> test, Set<T> collector);

default Set<T> uniqueSuccessors(Digest digest) {
var collected = new HashSet<T>();
uniqueSuccessors(digest, collected);
return collected;
}

/**
* collect the list of successors to the key on each ring, providing a unique member per ring if possible.
*/
void uniqueSuccessors(Digest key, Set<T> collector);

boolean validRing(int ring);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ public void uniqueSuccessors(Digest key, Predicate<T> test, Set<T> collector) {
delegate.uniqueSuccessors(key, test, collector);
}

@Override
public void uniqueSuccessors(Digest key, Set<T> collector) {
delegate.uniqueSuccessors(key, collector);
}

@Override
public boolean validRing(int ring) {
return delegate.validRing(ring);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,6 @@ public Iterable<T> traverse(int ring, T member) {
*/
@Override
public void uniqueSuccessors(Digest key, Predicate<T> test, Set<T> collector) {
Set<T> successors = new HashSet<>();
for (Ring<T> ring : rings) {
T successor = ring.successor(key, m -> !collector.contains(m) && test.test(m));
if (successor != null) {
Expand All @@ -741,6 +740,11 @@ public void uniqueSuccessors(Digest key, Predicate<T> test, Set<T> collector) {
}
}

@Override
public void uniqueSuccessors(Digest key, Set<T> collector) {
uniqueSuccessors(key, t -> true, collector);
}

@Override
public boolean validRing(int ring) {
return ring >= 0 && ring < rings.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ public void uniqueSuccessors(Digest key, Predicate<T> test, Set<T> collector) {
}
}

@Override
public void uniqueSuccessors(Digest key, Set<T> collector) {
uniqueSuccessors(key, t -> true, collector);
}

@Override
public boolean validRing(int ring) {
return ring >= 0 && ring < rings.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import java.io.IOException;
import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -41,20 +39,20 @@ public class SliceIterator<Comm extends Link> {
private Member current;
private Iterator<? extends Member> currentIteration;

public SliceIterator(String label, SigningMember member, List<? extends Member> slice,
public SliceIterator(String label, SigningMember member, Collection<? extends Member> slice,
CommonCommunications<Comm, ?> comm) {
this(label, member, slice, comm, Executors.newScheduledThreadPool(1, Thread.ofVirtual().factory()));
}

public SliceIterator(String label, SigningMember member, List<? extends Member> slice,
public SliceIterator(String label, SigningMember member, Collection<? extends Member> s,
CommonCommunications<Comm, ?> comm, ScheduledExecutorService scheduler) {
assert member != null && slice != null && comm != null;
assert member != null && s != null && comm != null;
this.label = label;
this.member = member;
this.slice = slice;
this.slice = new ArrayList<>(s);
this.comm = comm;
this.scheduler = scheduler;
Entropy.secureShuffle(slice);
Entropy.secureShuffle(this.slice);
this.currentIteration = slice.iterator();
log.debug("Slice for: <{}> is: {} on: {}", label, slice.stream().map(m -> m.getId()).toList(), member.getId());
}
Expand Down
8 changes: 4 additions & 4 deletions thoth/src/main/java/com/salesforce/apollo/thoth/Maat.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public boolean validate(EstablishmentEvent event) {
return false;
}
final Context<Member> ctx = context;
var successors = Context.uniqueSuccessors(ctx, digestOf(event.getIdentifier().toIdent(), digest.getAlgorithm()))
.stream()
.map(m -> m.getId())
.collect(Collectors.toSet());
var successors = ctx.bftSubset(digestOf(event.getIdentifier().toIdent(), digest.getAlgorithm()))
.stream()
.map(m -> m.getId())
.collect(Collectors.toSet());

record validator(EstablishmentEvent validating, JohnHancock signature) {
}
Expand Down

0 comments on commit 91e0d86

Please sign in to comment.