Skip to content

Commit

Permalink
Add Client for getting a value and validate a proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 3, 2023
1 parent 828f7af commit fa2211c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
52 changes: 35 additions & 17 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,32 @@ where
TimeoutElapsed(Timeout),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message<Ctx>
where
Ctx: Context,
{
Propose(Ctx::Proposal),
Vote(SignedVote<Ctx>),
Decide(Round, Ctx::Value),
ScheduleTimeout(Timeout),
}

pub trait Client<Ctx>
where
Ctx: Context,
{
fn get_value(&self) -> Ctx::Value;
fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool;
}

#[derive(Clone, Debug)]
pub struct Executor<Ctx>
pub struct Executor<Ctx, Client>
where
Ctx: Context,
Client: self::Client<Ctx>,
{
client: Client,
height: Ctx::Height,
private_key: Secret<PrivateKey<Ctx>>,
address: Ctx::Address,
Expand All @@ -41,22 +62,13 @@ where
round_states: BTreeMap<Round, RoundState<Ctx>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message<Ctx>
where
Ctx: Context,
{
Propose(Ctx::Proposal),
Vote(SignedVote<Ctx>),
Decide(Round, Ctx::Value),
ScheduleTimeout(Timeout),
}

impl<Ctx> Executor<Ctx>
impl<Ctx, Client> Executor<Ctx, Client>
where
Ctx: Context,
Client: self::Client<Ctx>,
{
pub fn new(
client: Client,
height: Ctx::Height,
validator_set: Ctx::ValidatorSet,
private_key: PrivateKey<Ctx>,
Expand All @@ -69,6 +81,7 @@ where
);

Self {
client,
height,
private_key: Secret::new(private_key),
address,
Expand All @@ -79,9 +92,12 @@ where
}
}

pub fn get_value(&self) -> Ctx::Value {
// TODO - add external interface to get the value
Ctx::DUMMY_VALUE
fn get_value(&self) -> Ctx::Value {
self.client.get_value()
}

fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool {
self.client.validate_proposal(proposal)
}

pub fn execute(&mut self, msg: Event<Ctx>) -> Option<Message<Ctx>> {
Expand Down Expand Up @@ -145,7 +161,9 @@ where
}

fn apply_proposal(&mut self, proposal: Ctx::Proposal) -> Option<RoundMessage<Ctx>> {
// TODO: Check for invalid proposal
if !self.validate_proposal(&proposal) {
todo!("invalid proposal");
}

// Check that there is an ongoing round
let Some(round_state) = self.round_states.get(&self.round) else {
Expand Down
24 changes: 24 additions & 0 deletions Code/test/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use malachite_consensus::executor::Client;

use crate::{Proposal, TestContext, Value};

pub struct TestClient {
pub value: Value,
pub is_valid: fn(&Proposal) -> bool,
}

impl TestClient {
pub fn new(value: Value, is_valid: fn(&Proposal) -> bool) -> Self {
Self { value, is_valid }
}
}

impl Client<TestContext> for TestClient {
fn get_value(&self) -> Value {
self.value.clone()
}

fn validate_proposal(&self, proposal: &Proposal) -> bool {
(self.is_valid)(proposal)
}
}
2 changes: 2 additions & 0 deletions Code/test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![forbid(unsafe_code)]
#![deny(trivial_casts, trivial_numeric_casts)]

mod client;
mod context;
mod height;
mod proposal;
Expand All @@ -9,6 +10,7 @@ mod validator_set;
mod value;
mod vote;

pub use crate::client::*;
pub use crate::context::*;
pub use crate::height::*;
pub use crate::proposal::*;
Expand Down
12 changes: 7 additions & 5 deletions Code/test/tests/consensus_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use malachite_consensus::executor::{Event, Executor, Message};
use malachite_round::state::{RoundValue, State, Step};

use malachite_test::{
Address, Height, PrivateKey, Proposal, TestContext, Validator, ValidatorSet, Vote,
Address, Height, PrivateKey, Proposal, TestClient, TestContext, Validator, ValidatorSet, Vote,
};
use rand::rngs::StdRng;
use rand::SeedableRng;
Expand Down Expand Up @@ -46,8 +46,8 @@ fn executor_steps_proposer() {
let (my_sk, my_addr) = (sk1, addr1);

let vs = ValidatorSet::new(vec![v1, v2.clone(), v3.clone()]);

let mut executor = Executor::new(Height::new(1), vs, my_sk.clone(), my_addr);
let client = TestClient::new(value.clone(), |_| true);
let mut executor = Executor::new(client, Height::new(1), vs, my_sk.clone(), my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));

Expand Down Expand Up @@ -228,7 +228,8 @@ fn executor_steps_not_proposer() {
let (my_sk, my_addr) = (sk2, addr2);

let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let mut executor = Executor::new(Height::new(1), vs, my_sk.clone(), my_addr);
let client = TestClient::new(value.clone(), |_| true);
let mut executor = Executor::new(client, Height::new(1), vs, my_sk.clone(), my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));

Expand Down Expand Up @@ -409,7 +410,8 @@ fn executor_steps_not_proposer_timeout() {
let (my_sk, my_addr) = (sk2, addr2);

let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let mut executor = Executor::new(Height::new(1), vs, my_sk.clone(), my_addr);
let client = TestClient::new(value.clone(), |_| true);
let mut executor = Executor::new(client, Height::new(1), vs, my_sk.clone(), my_addr);

let steps = vec![
// Start round 0, we are not the proposer
Expand Down

0 comments on commit fa2211c

Please sign in to comment.