Skip to content

Commit

Permalink
Add HexBloom diadem to the mix. Some cleanup
Browse files Browse the repository at this point in the history
The diadem is the standin for the view context. This maps to the Fireflies View diadem that succinctly defines the membership of a Context
  • Loading branch information
Hellblazer committed Jan 27, 2024
1 parent 9715a12 commit 488ad62
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 138 deletions.
54 changes: 34 additions & 20 deletions choam/src/main/java/com/salesforce/apollo/choam/CHOAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ public class CHOAM {
private final Combine.Transitions transitions;
private final TransSubmission txnSubmission = new TransSubmission();
private final AtomicReference<HashedCertifiedBlock> view = new AtomicReference<>();
private final AtomicReference<HexBloom> diadem = new AtomicReference<>();

public CHOAM(Parameters params) {
this.store = new Store(params.digestAlgorithm(), params.mvBuilder().clone().build());
this.params = params;
diadem.set(new HexBloom(params.digestAlgorithm().getLast(), 0));
executions = Executors.newVirtualThreadPerTaskExecutor();

nextView();
Expand Down Expand Up @@ -173,10 +175,10 @@ public static Checkpoint checkpoint(DigestAlgorithm algo, File state, int segmen
return cp;
}

public static Block genesis(Digest id, Map<Member, Join> joins, HashedBlock head, Context<Member> context,
HashedBlock lastViewChange, Parameters params, HashedBlock lastCheckpoint,
Iterable<Transaction> initialization) {
var reconfigure = reconfigure(id, joins, context, params, params.checkpointBlockDelta());
public static Block genesis(Digest id, HexBloom diadem, Map<Member, Join> joins, HashedBlock head,
Context<Member> context, HashedBlock lastViewChange, Parameters params,
HashedBlock lastCheckpoint, Iterable<Transaction> initialization) {
var reconfigure = reconfigure(id, diadem, joins, context, params, params.checkpointBlockDelta());
return Block.newBuilder()
.setHeader(buildHeader(params.digestAlgorithm(), reconfigure, head.hash, ULong.valueOf(0),
lastCheckpoint.height(), lastCheckpoint.hash, lastViewChange.height(),
Expand All @@ -194,9 +196,12 @@ public static String print(Join join, DigestAlgorithm da) {
+ "certifications: " + join.getEndorsementsList().stream().map(c -> ViewContext.print(c, da)).toList() + "]";
}

public static Reconfigure reconfigure(Digest nextViewId, Map<Member, Join> joins, Context<Member> context,
Parameters params, int checkpointTarget) {
var builder = Reconfigure.newBuilder().setCheckpointTarget(checkpointTarget).setId(nextViewId.toDigeste());
public static Reconfigure reconfigure(Digest nextViewId, HexBloom diadem, Map<Member, Join> joins,
Context<Member> context, Parameters params, int checkpointTarget) {
var builder = Reconfigure.newBuilder()
.setCheckpointTarget(checkpointTarget)
.setId(nextViewId.toDigeste())
.setView(diadem.toHexBloome());

// Canonical labeling of the view members for Ethereal
var remapped = rosterMap(context, joins.keySet());
Expand All @@ -207,14 +212,14 @@ public static Reconfigure reconfigure(Digest nextViewId, Map<Member, Join> joins
return reconfigure;
}

public static Block reconfigure(Digest nextViewId, Map<Member, Join> joins, HashedBlock head,
public static Block reconfigure(Digest nextViewId, HexBloom diadem, Map<Member, Join> joins, HashedBlock head,
Context<Member> context, HashedBlock lastViewChange, Parameters params,
HashedBlock lastCheckpoint) {
final Block lvc = lastViewChange.block;
int lastTarget = lvc.hasGenesis() ? lvc.getGenesis().getInitialView().getCheckpointTarget()
: lvc.getReconfigure().getCheckpointTarget();
int checkpointTarget = lastTarget == 0 ? params.checkpointBlockDelta() : lastTarget - 1;
var reconfigure = reconfigure(nextViewId, joins, context, params, checkpointTarget);
var reconfigure = reconfigure(nextViewId, diadem, joins, context, params, checkpointTarget);
return Block.newBuilder()
.setHeader(buildHeader(params.digestAlgorithm(), reconfigure, head.hash, head.height().add(1),
lastCheckpoint.height(), lastCheckpoint.hash, lastViewChange.height(),
Expand Down Expand Up @@ -294,6 +299,10 @@ public String logState() {
params.member().getId());
}

public void setDiadem(HexBloom diadem) {
this.diadem.set(diadem);
}

public void start() {
if (!started.compareAndSet(false, true)) {
return;
Expand Down Expand Up @@ -467,7 +476,7 @@ public Block checkpoint() {
public Block genesis(Map<Member, Join> joining, Digest nextViewId, HashedBlock previous) {
final HashedCertifiedBlock cp = checkpoint.get();
final HashedCertifiedBlock v = view.get();
return CHOAM.genesis(nextViewId, joining, previous, params.context(), v, params, cp,
return CHOAM.genesis(nextViewId, diadem.get(), joining, previous, params.context(), v, params, cp,
params.genesisData().apply(joining));
}

Expand Down Expand Up @@ -504,7 +513,8 @@ public void publish(CertifiedBlock cb) {
public Block reconfigure(Map<Member, Join> joining, Digest nextViewId, HashedBlock previous,
HashedBlock checkpoint) {
final HashedCertifiedBlock v = view.get();
return CHOAM.reconfigure(nextViewId, joining, previous, params.context(), v, params, checkpoint);
return CHOAM.reconfigure(nextViewId, diadem.get(), joining, previous, params.context(), v, params,
checkpoint);
}
};
}
Expand Down Expand Up @@ -612,11 +622,13 @@ private void nextView() {
log.error("Unable to generate and sign consensus key on: {}", params.member().getId());
return;
}
log.trace("Generated next view consensus key: {} sig: {} on: {}",
var current = diadem.get();
log.trace("Generated next view consensus key: {} sig: {} diadem: {} on: {}",
params.digestAlgorithm().digest(pubKey.getEncoded()),
params.digestAlgorithm().digest(signed.toSig().toByteString()), params.member().getId());
params.digestAlgorithm().digest(signed.toSig().toByteString()), current, params.member().getId());
next.set(new nextView(ViewMember.newBuilder()
.setId(params.member().getId().toDigeste())
.setDiadem(current.toHexBloome())
.setConsensusKey(pubKey)
.setSignature(signed.toSig())
.build(), keyPair));
Expand All @@ -631,8 +643,9 @@ private void process() {
case ASSEMBLE: {
params.processor().beginBlock(h.height(), h.hash);
nextViewId.set(Digest.from(h.block.getAssemble().getNextView()));
log.info("Next view id: {} on: {}", nextViewId.get(), params.member().getId());
c.assembled();
var diadem = HexBloom.from(h.block.getAssemble().getDiadem());
log.info("Next view id: {} diadem: {} on: {}", nextViewId.get(), diadem.compact(), params.member().getId());
c.assembled(diadem);
break;
}
case RECONFIGURE: {
Expand Down Expand Up @@ -1188,14 +1201,15 @@ private class Associate extends Administration {
params.digestAlgorithm().digest(nextView.member.getSignature().toByteString()), viewId,
params.member().getId());
Signer signer = new SignerImpl(nextView.consensusKeyPair.getPrivate(), ULong.MIN);
producer = new Producer(new ViewContext(context, params, signer, validators, constructBlock()), head.get(),
checkpoint.get(), comm, getLabel());
producer = new Producer(
new ViewContext(context, () -> diadem.get(), params, signer, validators, constructBlock()), head.get(),
checkpoint.get(), comm, getLabel());
producer.start();
}

@Override
public void assembled() {
producer.assembled();
public void assembled(HexBloom diadem) {
producer.assembled(diadem);
}

@Override
Expand Down Expand Up @@ -1231,7 +1245,7 @@ private Formation() {
params.digestAlgorithm().digest(c.member.getSignature().toByteString()),
params.member().getId());
Signer signer = new SignerImpl(c.consensusKeyPair.getPrivate(), ULong.MIN);
ViewContext vc = new GenesisContext(formation, params, signer, constructBlock());
ViewContext vc = new GenesisContext(formation, () -> diadem.get(), params, signer, constructBlock());
assembly = new GenesisAssembly(vc, comm, next.get().member, getLabel());
nextViewId.set(params.genesisViewId());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import com.salesforce.apollo.choam.proto.*;
import com.salesforce.apollo.choam.proto.SubmitResult.Result;
import com.salesforce.apollo.choam.support.HashedCertifiedBlock;
import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.cryptography.DigestAlgorithm;
import com.salesforce.apollo.cryptography.JohnHancock;
import com.salesforce.apollo.cryptography.Verifier;
import com.salesforce.apollo.cryptography.*;
import com.salesforce.apollo.cryptography.Verifier.DefaultVerifier;
import com.salesforce.apollo.membership.Context;
import com.salesforce.apollo.membership.ContextImpl;
Expand Down Expand Up @@ -63,7 +60,7 @@ static Set<Member> viewMembersOf(Digest hash, Context<? super Member> baseContex

void accept(HashedCertifiedBlock next);

default void assembled() {
default void assembled(HexBloom diadem) {
}

void complete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@

import com.salesforce.apollo.choam.CHOAM.BlockProducer;
import com.salesforce.apollo.choam.proto.Validate;
import com.salesforce.apollo.cryptography.HexBloom;
import com.salesforce.apollo.cryptography.Signer;
import com.salesforce.apollo.cryptography.Verifier;
import com.salesforce.apollo.membership.Context;
import com.salesforce.apollo.membership.Member;

import java.util.Collections;
import java.util.function.Supplier;

/**
* @author hal.hildebrand
*/
public class GenesisContext extends ViewContext {

public GenesisContext(Context<Member> context, Parameters params, Signer signer, BlockProducer blockProducer) {
super(context, params, signer, Collections.emptyMap(), blockProducer);
public GenesisContext(Context<Member> context, Supplier<HexBloom> diadem, Parameters params, Signer signer,
BlockProducer blockProducer) {
super(context, diadem, params, signer, Collections.emptyMap(), blockProducer);
}

@Override
Expand Down
16 changes: 10 additions & 6 deletions choam/src/main/java/com/salesforce/apollo/choam/Producer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.salesforce.apollo.choam.support.HashedCertifiedBlock;
import com.salesforce.apollo.choam.support.TxDataSource;
import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.cryptography.HexBloom;
import com.salesforce.apollo.ethereal.Config;
import com.salesforce.apollo.ethereal.Config.Builder;
import com.salesforce.apollo.ethereal.Ethereal;
Expand Down Expand Up @@ -109,8 +110,8 @@ public Producer(ViewContext view, HashedBlock lastBlock, HashedBlock checkpoint,
log.debug("Roster for: {} is: {} on: {}", getViewId(), view.roster(), params().member().getId());
}

public void assembled() {
transitions.assembled();
public void assembled(HexBloom diadem) {
transitions.assembled(diadem);
}

public Digest getNextViewId() {
Expand Down Expand Up @@ -247,6 +248,8 @@ private void produceAssemble() {
Assemble.newBuilder()
.setNextView(
vlb.hash.toDigeste())
.setDiadem(view.diadem()
.toHexBloome())
.build(),
checkpoint.get()));
previousBlock.set(assemble);
Expand Down Expand Up @@ -367,14 +370,15 @@ public void produceAssemble() {
}

@Override
public void reconfigure() {
log.debug("Starting view reconfiguration for: {} on: {}", nextViewId, params().member().getId());
public void reconfigure(HexBloom diadem) {
log.debug("Starting view reconfiguration: {} diadem: {} on: {}", nextViewId, diadem.compact(),
params().member().getId());
assembly.set(new ViewAssembly(nextViewId, view, Producer.this::addReassemble, comms) {
@Override
public void complete() {
super.complete();
log.debug("View reconfiguration: {} gathered: {} complete on: {}", nextViewId, getSlate().size(),
params().member().getId());
log.debug("View reconfiguration: {} diadem: {} gathered: {} complete on: {}", nextViewId,
diadem.compact(), getSlate().size(), params().member().getId());
assembled.set(true);
Producer.this.transitions.viewComplete();
}
Expand Down
11 changes: 9 additions & 2 deletions choam/src/main/java/com/salesforce/apollo/choam/ViewContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import static com.salesforce.apollo.cryptography.QualifiedBase64.publicKey;

Expand All @@ -34,15 +35,17 @@ public class ViewContext {
private final Map<Digest, Short> roster;
private final Signer signer;
private final Map<Member, Verifier> validators;
private final Supplier<HexBloom> diadem;

public ViewContext(Context<Member> context, Parameters params, Signer signer, Map<Member, Verifier> validators,
BlockProducer blockProducer) {
public ViewContext(Context<Member> context, Supplier<HexBloom> diadem, Parameters params, Signer signer,
Map<Member, Verifier> validators, BlockProducer blockProducer) {
this.blockProducer = blockProducer;
this.context = context;
this.roster = new HashMap<>();
this.params = params;
this.signer = signer;
this.validators = validators;
this.diadem = diadem;

var remapped = CHOAM.rosterMap(params.context(), context.allMembers().toList());
short pid = 0;
Expand Down Expand Up @@ -74,6 +77,10 @@ public Context<Member> context() {
return context;
}

public HexBloom diadem() {
return diadem.get();
}

public Validate generateValidation(HashedBlock block) {
log.trace("Signing block: {} height: {} on: {}", block.hash, block.height(), params.member().getId());
JohnHancock signature = signer.sign(block.block.getHeader().toByteString());
Expand Down
13 changes: 7 additions & 6 deletions choam/src/main/java/com/salesforce/apollo/choam/fsm/Driven.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.chiralbehaviors.tron.Entry;
import com.chiralbehaviors.tron.FsmExecutor;
import com.google.protobuf.ByteString;
import com.salesforce.apollo.cryptography.HexBloom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -37,14 +38,14 @@ public interface Driven {

void produceAssemble();

void reconfigure();
void reconfigure(HexBloom diadem);

void startProduction();

enum Earner implements Driven.Transitions {
AWAIT_VIEW {
@Override
public Transitions assembled() {
public Transitions assembled(HexBloom diadem) {
context().assembled();
return null;
}
Expand Down Expand Up @@ -93,7 +94,7 @@ public Transitions start() {
}
}, PROTOCOL_FAILURE {
@Override
public Transitions assembled() {
public Transitions assembled(HexBloom diadem) {
return null;
}

Expand Down Expand Up @@ -129,8 +130,8 @@ public void terminate() {
}
}, SPICE {
@Override
public Transitions assembled() {
context().reconfigure();
public Transitions assembled(HexBloom diadem) {
context().reconfigure(diadem);
return null;
}

Expand Down Expand Up @@ -163,7 +164,7 @@ public Transitions viewComplete() {
interface Transitions extends FsmExecutor<Driven, Transitions> {
Logger log = LoggerFactory.getLogger(Transitions.class);

default Transitions assembled() {
default Transitions assembled(HexBloom diadem) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package com.salesforce.apollo.choam;

import com.salesforce.apollo.choam.proto.*;
import com.salesforce.apollo.cryptography.proto.PubKey;
import com.salesforce.apollo.archipelago.LocalServer;
import com.salesforce.apollo.archipelago.Router;
import com.salesforce.apollo.archipelago.ServerConnectionCache;
Expand All @@ -18,10 +16,13 @@
import com.salesforce.apollo.choam.comm.Terminal;
import com.salesforce.apollo.choam.comm.TerminalClient;
import com.salesforce.apollo.choam.comm.TerminalServer;
import com.salesforce.apollo.choam.proto.*;
import com.salesforce.apollo.choam.support.HashedBlock;
import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.cryptography.DigestAlgorithm;
import com.salesforce.apollo.cryptography.HexBloom;
import com.salesforce.apollo.cryptography.Signer;
import com.salesforce.apollo.cryptography.proto.PubKey;
import com.salesforce.apollo.membership.Context;
import com.salesforce.apollo.membership.ContextImpl;
import com.salesforce.apollo.membership.Member;
Expand Down Expand Up @@ -138,8 +139,8 @@ public Block checkpoint() {

@Override
public Block genesis(Map<Member, Join> joining, Digest nextViewId, HashedBlock previous) {
return CHOAM.genesis(viewId, joining, previous, committee, previous, built, previous,
Collections.emptyList());
return CHOAM.genesis(viewId, new HexBloom(), joining, previous, committee, previous, built,
previous, Collections.emptyList());
}

@Override
Expand All @@ -163,7 +164,8 @@ public Block reconfigure(Map<Member, Join> joining, Digest nextViewId, HashedBlo
return null;
}
};
var view = new GenesisContext(committee, built, sm, reconfigure);
HexBloom diadem = new HexBloom();
var view = new GenesisContext(committee, () -> diadem, built, sm, reconfigure);

KeyPair keyPair = params.getViewSigAlgorithm().generateKeyPair();
final PubKey consensus = bs(keyPair.getPublic());
Expand Down
Loading

0 comments on commit 488ad62

Please sign in to comment.