Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client for getting a value and validate a proposal #43

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@ where
TimeoutElapsed(Timeout),
}

#[derive(Clone, Debug)]
pub struct Executor<Ctx>
where
Ctx: Context,
{
height: Ctx::Height,
private_key: Secret<PrivateKey<Ctx>>,
address: Ctx::Address,
validator_set: Ctx::ValidatorSet,
pub round: Round,
votes: VoteKeeper<Ctx>,
round_states: BTreeMap<Round, RoundState<Ctx>>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message<Ctx>
where
Expand All @@ -53,11 +39,37 @@ where
NewRound(Round),
}

impl<Ctx> Executor<Ctx>
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, Client>
where
Ctx: Context,
Client: self::Client<Ctx>,
{
pub client: Client,
pub height: Ctx::Height,
pub private_key: Secret<PrivateKey<Ctx>>,
pub address: Ctx::Address,
pub validator_set: Ctx::ValidatorSet,
pub round: Round,
pub votes: VoteKeeper<Ctx>,
pub round_states: BTreeMap<Round, RoundState<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 @@ -66,6 +78,7 @@ where
let votes = VoteKeeper::new(validator_set.total_voting_power());

Self {
client,
height,
private_key: Secret::new(private_key),
address,
Expand All @@ -76,9 +89,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 @@ -143,7 +159,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 @@ -47,8 +47,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 @@ -229,7 +229,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 @@ -410,7 +411,8 @@ fn executor_steps_not_proposer_timeout_multiple_rounds() {
let (my_sk, my_addr) = (sk3, addr3);

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, v3, are not the proposer
Expand Down
Loading