Skip to content

Commit

Permalink
Merge branch 'main' into anca/bs_propandparts
Browse files Browse the repository at this point in the history
  • Loading branch information
ancazamfir committed Nov 22, 2024
2 parents a6c228e + ced48f9 commit d3ca820
Show file tree
Hide file tree
Showing 46 changed files with 614 additions and 455 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ target/
lcov.info

# JetBrains projects
.idea
.idea

# Vim temporary files
*.swp
26 changes: 15 additions & 11 deletions code/Cargo.lock

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

10 changes: 7 additions & 3 deletions code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
# Test
"crates/test",
"crates/test/mbt",
"crates/driver/test-utils",

# Starknet
"crates/starknet/*",
Expand All @@ -50,7 +51,7 @@ inherits = "release"
debug = true

[workspace.lints.rust]
unused_crate_dependencies = "warn"
# None for now

[workspace.dependencies]
malachite-actors = { version = "0.1.0", path = "crates/actors" }
Expand All @@ -67,11 +68,14 @@ malachite-metrics = { version = "0.1.0", path = "crates/metrics" }
malachite-node = { version = "0.1.0", path = "crates/node" }
malachite-proto = { version = "0.1.0", path = "crates/proto" }
malachite-round = { version = "0.1.0", path = "crates/round" }
malachite-test = { version = "0.1.0", path = "crates/test" }
malachite-test-mbt = { version = "0.1.0", path = "crates/test/mbt" }
malachite-vote = { version = "0.1.0", path = "crates/vote" }
malachite-signing-ed25519 = { version = "0.1.0", path = "crates/signing-ed25519" }

# Test
malachite-test = { version = "0.1.0", path = "crates/test" }
malachite-test-mbt = { version = "0.1.0", path = "crates/test/mbt" }
malachite-driver-test-utils = { version = "0.1.0", path = "crates/driver/test-utils" }

# Starknet
malachite-starknet-host = { version = "0.1.0", path = "crates/starknet/host" }
malachite-starknet-app = { version = "0.1.0", path = "crates/starknet/app" }
Expand Down
18 changes: 3 additions & 15 deletions code/crates/actors/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,7 @@ where
if connected_peers == total_peers {
info!(count = %connected_peers, "Enough peers connected to start consensus");

let height = state.consensus.driver.height();

let result = self
.process_input(
&myself,
state,
ConsensusInput::StartHeight(height, validator_set.clone()),
)
.await;

if let Err(e) = result {
error!("Error when starting height {height}: {e:?}");
}
self.host.cast(HostMsg::ConsensusReady(myself.clone()))?;
}
}

Expand Down Expand Up @@ -527,7 +515,7 @@ where
}

Effect::StartRound(height, round, proposer) => {
self.host.cast(HostMsg::StartRound {
self.host.cast(HostMsg::StartedRound {
height,
round,
proposer,
Expand Down Expand Up @@ -602,7 +590,7 @@ where
let height = certificate.height;

self.host
.cast(HostMsg::Decide {
.cast(HostMsg::Decided {
certificate,
consensus: myself.clone(),
})
Expand Down
7 changes: 5 additions & 2 deletions code/crates/actors/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ pub type HostRef<Ctx> = ActorRef<HostMsg<Ctx>>;

/// Messages that need to be handled by the host actor.
pub enum HostMsg<Ctx: Context> {
/// Consensus is ready
ConsensusReady(ConsensusRef<Ctx>),

/// Consensus has started a new round.
StartRound {
StartedRound {
height: Ctx::Height,
round: Round,
proposer: Ctx::Address,
Expand Down Expand Up @@ -89,7 +92,7 @@ pub enum HostMsg<Ctx: Context> {
},

// Consensus has decided on a value
Decide {
Decided {
certificate: CommitCertificate<Ctx>,
consensus: ConsensusRef<Ctx>,
},
Expand Down
2 changes: 1 addition & 1 deletion code/crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![no_std]
#![forbid(unsafe_code)]
#![deny(unused_crate_dependencies, trivial_casts, trivial_numeric_casts)]
#![deny(trivial_casts, trivial_numeric_casts)]
#![warn(
missing_docs,
rustdoc::broken_intra_doc_links,
Expand Down
3 changes: 3 additions & 0 deletions code/crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ tracing = { workspace = true }

[lints]
workspace = true

[dev-dependencies]
malachite-test = { workspace = true }
16 changes: 13 additions & 3 deletions code/crates/consensus/src/handle/decide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ where
let proposal_round = proposal.round();
let value = proposal.value();

// Restore the commits. Note that they will be removed from `state`
let commits = state.restore_precommits(height, proposal_round, value);
// We only decide proposals for the current height
assert_eq!(height, state.driver.height());

// Clean proposals and values
state.remove_full_proposals(height);
Expand Down Expand Up @@ -46,7 +46,17 @@ where
}
}

let certificate = CommitCertificate::new(height, proposal_round, value.id(), commits);
// Look for an existing certificate
let certificate = state
.driver
.get_certificate(proposal_round, value.id())
.cloned()
.unwrap_or_else(|| {
// Restore the commits. Note that they will be removed from `state`
let commits = state.restore_precommits(height, proposal_round, value);
// TODO: should we verify we have 2/3rd commits?
CommitCertificate::new(height, proposal_round, value.id(), commits)
});

perform!(co, Effect::Decide { certificate });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use malachite_actors::host::ProposedValue;
use malachite_common::{Context, Round, SignedProposal, Validity, ValueOrigin};
use malachite_consensus::{FullProposal, FullProposalKeeper, Input};
use malachite_consensus::{FullProposal, FullProposalKeeper, Input, ProposedValue};
use malachite_test::utils::validators::make_validators;
use malachite_test::{Address, Proposal, Value};
use malachite_test::{Height, TestContext};
Expand Down
10 changes: 7 additions & 3 deletions code/crates/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ debug = ["std", "malachite-round/debug"]
workspace = true

[dependencies]
malachite-common = { version = "0.1.0", path = "../common" }
malachite-round = { version = "0.1.0", path = "../round" }
malachite-vote = { version = "0.1.0", path = "../vote" }
malachite-common = { workspace = true }
malachite-round = { workspace = true }
malachite-vote = { workspace = true }

derive-where = { workspace = true }
thiserror = { workspace = true, default-features = false }

[dev-dependencies]
malachite-test = { workspace = true }
malachite-driver-test-utils = { workspace = true }
19 changes: 15 additions & 4 deletions code/crates/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::fmt;

use malachite_common::{
CommitCertificate, Context, Proposal, Round, SignedProposal, SignedVote, Timeout, TimeoutStep,
Validator, ValidatorSet, Validity, Vote,
Validator, ValidatorSet, Validity, ValueId, Vote,
};
use malachite_round::input::Input as RoundInput;
use malachite_round::output::Output as RoundOutput;
Expand Down Expand Up @@ -38,6 +38,9 @@ where
/// The validator set at the current height
validator_set: Ctx::ValidatorSet,

/// The proposer for the current round, None for round nil.
proposer: Option<Ctx::Address>,

/// The proposals to decide on.
pub(crate) proposal_keeper: ProposalKeeper<Ctx>,

Expand All @@ -50,9 +53,6 @@ where
/// The state of the round state machine.
pub(crate) round_state: RoundState<Ctx>,

/// The proposer for the current round, None for round nil.
proposer: Option<Ctx::Address>,

/// The pending inputs to be processed next, if any.
/// The first element of the tuple is the round at which that input has been emitted.
pending_inputs: Vec<(Round, RoundInput<Ctx>)>,
Expand Down Expand Up @@ -176,6 +176,17 @@ where
}
}

/// Get a commit certificate for the given round and value id.
pub fn get_certificate(
&self,
round: Round,
value_id: ValueId<Ctx>,
) -> Option<&CommitCertificate<Ctx>> {
self.certificates
.iter()
.find(|c| c.round == round && c.value_id == value_id)
}

/// Process the given input, returning the outputs to be broadcast to the network.
pub fn process(&mut self, msg: Input<Ctx>) -> Result<Vec<Output<Ctx>>, Error<Ctx>> {
let round_output = match self.apply(msg)? {
Expand Down
2 changes: 1 addition & 1 deletion code/crates/driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Driver for the state machine of the Malachite consensus engine
#![forbid(unsafe_code)]
#![deny(unused_crate_dependencies, trivial_casts, trivial_numeric_casts)]
#![deny(trivial_casts, trivial_numeric_casts)]
#![warn(
missing_docs,
rustdoc::broken_intra_doc_links,
Expand Down
18 changes: 10 additions & 8 deletions code/crates/driver/src/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,10 @@ where

// We have a valid proposal. Check if there is already a certificate for it.
// L49
if self
.certificates
.iter()
.any(|c| c.value_id == proposal.value().id() && proposal.round() == c.round)
&& self.round_state.decision.is_none()
if self.round_state.decision.is_none()
&& self
.get_certificate(proposal.round(), proposal.value().id())
.is_some()
{
return Some(RoundInput::ProposalAndPrecommitValue(proposal));
}
Expand Down Expand Up @@ -189,15 +188,18 @@ where
// Should only receive proposals for our height.
assert_eq!(self.height(), certificate.height);

let certificate_round = certificate.round;
let certificate_value_id = certificate.value_id.clone();

// Store the certificate
self.certificates.push(certificate.clone());
self.certificates.push(certificate);

if let Some((signed_proposal, validity)) = self
.proposal_keeper
.get_proposal_and_validity_for_round(certificate.round)
.get_proposal_and_validity_for_round(certificate_round)
{
let proposal = &signed_proposal.message;
if proposal.value().id() == certificate.value_id && validity.is_valid() {
if proposal.value().id() == certificate_value_id && validity.is_valid() {
return Some(RoundInput::ProposalAndPrecommitValue(proposal.clone()));
}
}
Expand Down
18 changes: 18 additions & 0 deletions code/crates/driver/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "malachite-driver-test-utils"
description = "Test utilities for the `malachite-driver`"
publish = false

version.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true
rust-version.workspace = true


[dependencies]
malachite-common = { workspace = true }
malachite-round = { workspace = true }
malachite-vote = { workspace = true }
malachite-test = { workspace = true }
malachite-driver = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use malachite_common::{NilOrVal, Round, SignedProposal, SignedVote, Timeout, Validity};
use malachite_driver::{Input, Output};
use malachite_round::state::{RoundValue, State, Step};

use crate::{Address, Height, Proposal, Signature, TestContext, Value, Vote};
use malachite_test::{Address, Height, Proposal, Signature, TestContext, Value, Vote};

pub fn new_round_input(round: Round, proposer: Address) -> Input<TestContext> {
Input::NewRound(Height::new(1), round, proposer)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use malachite_common::{Round, Validity};
use malachite_driver::{Driver, Input, Output};
use malachite_round::state::State;

use malachite_test::utils::driver::*;
use malachite_driver_test_utils::*;
use malachite_test::utils::validators::make_validators;
use malachite_test::{Height, Proposal, TestContext, ValidatorSet, Value};

Expand Down
2 changes: 0 additions & 2 deletions code/crates/gossip-consensus/test/tests/discovery.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(unused_crate_dependencies)]

use std::{time::Duration, vec};

use malachite_discovery_test::{Expected, Test, TestNode};
Expand Down
2 changes: 1 addition & 1 deletion code/crates/round/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Per-round consensus state machine
#![forbid(unsafe_code)]
#![deny(unused_crate_dependencies, trivial_casts, trivial_numeric_casts)]
#![deny(trivial_casts, trivial_numeric_casts)]
#![warn(
missing_docs,
rustdoc::broken_intra_doc_links,
Expand Down
Loading

0 comments on commit d3ca820

Please sign in to comment.