Skip to content

Commit

Permalink
feat: ChonkyBFT types (#209)
Browse files Browse the repository at this point in the history
Implemented all types for ChonkyBFT. Work on this is all on the roles
crate. Tests in roles crate are passing. Note that I'm not keeping the
old types, that's because `ReplicaCommit` has no changes to it (I
checked and serialization is the same for old and new, old signatures
still work) as well as `CommitQC` and `FinalBlock`. Any type that an EN
could receive, or that ends on our database, doesn't suffer any
alterations. So it seems that the upgrade to ChonkyBFT is
backwards-compatible for ENs. Since we only have one validator, we can
upgrade the whole network without a planned hard fork.

Part of BFT-452
  • Loading branch information
brunoffranca authored Oct 21, 2024
1 parent 74407e6 commit f6168f8
Show file tree
Hide file tree
Showing 39 changed files with 2,849 additions and 1,911 deletions.
24 changes: 12 additions & 12 deletions node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/actors/bft/src/leader/replica_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl StateMachine {
.retain(|view_number, _| active_views.contains(view_number));

// Now we check if we have enough weight to continue.
if weight < self.config.genesis().validators.threshold() {
if weight < self.config.genesis().validators.quorum_threshold() {
return Ok(());
};

Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/leader/replica_prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl StateMachine {
.retain(|view_number, _| active_views.contains(view_number));

// Now we check if we have enough weight to continue.
if weight < self.config.genesis().validators.threshold() {
if weight < self.config.genesis().validators.quorum_threshold() {
return Ok(());
}

Expand Down
4 changes: 2 additions & 2 deletions node/actors/bft/src/leader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ async fn replica_prepare_different_messages() {

let mut replica_commit_result = None;
// The rest of the validators until threshold sign other_replica_prepare
for i in validators / 2..util.genesis().validators.threshold() as usize {
for i in validators / 2..util.genesis().validators.quorum_threshold() as usize {
replica_commit_result = util
.process_replica_prepare(ctx, util.keys[i].sign_msg(other_replica_prepare.clone()))
.await
Expand Down Expand Up @@ -564,7 +564,7 @@ async fn replica_commit_bad_chain() {
assert_matches!(
res,
Err(replica_commit::Error::InvalidMessage(
validator::ReplicaCommitVerifyError::View(_)
validator::ReplicaCommitVerifyError::BadView(_)
))
);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/replica/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ async fn leader_commit_bad_chain() {
res,
Err(leader_commit::Error::InvalidMessage(
validator::CommitQCVerifyError::InvalidMessage(
validator::ReplicaCommitVerifyError::View(_)
validator::ReplicaCommitVerifyError::BadView(_)
)
))
);
Expand Down
4 changes: 2 additions & 2 deletions node/actors/bft/src/testonly/ut_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl UTHarness {
let res = self.process_replica_prepare(ctx, msg).await;
match (
(i + 1) as u64 * self.genesis().validators.iter().next().unwrap().weight
< self.genesis().validators.threshold(),
< self.genesis().validators.quorum_threshold(),
first_match,
) {
(true, _) => assert!(res.unwrap().is_none()),
Expand Down Expand Up @@ -256,7 +256,7 @@ impl UTHarness {
.process_replica_commit(ctx, key.sign_msg(msg.clone()));
match (
(i + 1) as u64 * self.genesis().validators.iter().next().unwrap().weight
< self.genesis().validators.threshold(),
< self.genesis().validators.quorum_threshold(),
first_match,
) {
(true, _) => res.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion node/actors/bft/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn run_test(behavior: Behavior, network: Network) {
let mut nodes = vec![(behavior, 1u64); NODES];
// validator::threshold(NODES) will calculate required nodes to validate a message
// given each node weight is 1
let honest_nodes_amount = validator::threshold(NODES as u64) as usize;
let honest_nodes_amount = validator::quorum_threshold(NODES as u64) as usize;
for n in &mut nodes[0..honest_nodes_amount] {
n.0 = Behavior::Honest;
}
Expand Down
2 changes: 1 addition & 1 deletion node/libs/crypto/src/keccak256/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod test;
pub mod testonly;

/// Keccak256 hash.
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Keccak256(pub(crate) [u8; 32]);

impl Keccak256 {
Expand Down
50 changes: 31 additions & 19 deletions node/libs/roles/src/proto/validator/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,69 @@ message Block {
message View {
reserved 1,2;
reserved "protocol_version","fork";

optional GenesisHash genesis = 4; // required
optional uint64 number = 3; // required; ViewNumber
}

message ConsensusMsg {
oneof t {// required
ReplicaPrepare replica_prepare = 1;
reserved 1, 3, 4;
reserved "replica_prepare", "leader_prepare", "leader_commit";

oneof t { // required
ReplicaCommit replica_commit = 2;
LeaderPrepare leader_prepare = 3;
LeaderCommit leader_commit = 4;
ReplicaTimeout replica_timeout = 5;
ReplicaNewView replica_new_view = 6;
LeaderProposal leader_proposal = 7;
}
}

message ReplicaPrepare {
message ReplicaCommit {
optional View view = 1; // required
optional BlockHeader proposal = 2; // required
}

message ReplicaTimeout {
optional View view = 1; // required
optional ReplicaCommit high_vote = 2; // optional
optional CommitQC high_qc = 3; // optional
}

message ReplicaCommit {
optional View view = 1; // required
optional BlockHeader proposal = 2; // required
message ReplicaNewView {
optional ProposalJustification justification = 1; // required
}

message LeaderPrepare {
message LeaderProposal {
optional BlockHeader proposal = 1; // required
optional bytes proposal_payload = 2; // optional (depending on justification)
optional PrepareQC justification = 3; // required
optional ProposalJustification justification = 3; // required
}

message LeaderCommit {
optional CommitQC justification = 1; // required
message CommitQC {
optional ReplicaCommit msg = 1; // required
optional std.BitVector signers = 2; // required
optional AggregateSignature sig = 3; // required
}

message PrepareQC {
message TimeoutQC {
optional View view = 4; // required
repeated ReplicaPrepare msgs = 1; // required
repeated ReplicaTimeout msgs = 1; // required
repeated std.BitVector signers = 2; // required
optional AggregateSignature sig = 3; // required
}

message CommitQC {
optional ReplicaCommit msg = 1; // required
optional std.BitVector signers = 2; // required
optional AggregateSignature sig = 3; // required
message ProposalJustification {
oneof t { // required
CommitQC commit_qc = 1;
TimeoutQC timeout_qc = 2;
}
}

message Phase {
oneof t {
oneof t { // required
std.Void prepare = 1;
std.Void commit = 2;
std.Void timeout = 3;
}
}

Expand Down
Loading

0 comments on commit f6168f8

Please sign in to comment.