Skip to content

Commit

Permalink
Verify signature of votes and proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Feb 21, 2024
1 parent 097aa42 commit 859e00c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 21 deletions.
9 changes: 9 additions & 0 deletions code/driver/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ impl Validity {
pub fn is_valid(self) -> bool {
self == Validity::Valid
}

/// Returns `Valid` if given true, `Invalid` if given false.
pub fn from_valid(valid: bool) -> Self {
if valid {
Validity::Valid
} else {
Validity::Invalid
}
}
}
29 changes: 15 additions & 14 deletions code/node/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::sync::Arc;
use std::time::Duration;

use malachite_node::config::Config;
use malachite_node::network::broadcast;
use malachite_node::network::broadcast::PeerInfo;
use malachite_node::node::{Node, Params};
use malachite_node::peers::Peers;
use malachite_node::timers;
use malachite_test::utils::{make_validators, FixedProposer};
use malachite_test::{Address, Height, PrivateKey, TestContext, ValidatorSet, Value};
use tracing::info;

mod cli;
use cli::Cli;
mod config;
use config::{Config, PeerConfig};

#[tokio::main(flavor = "current_thread")]
pub async fn main() {
Expand Down Expand Up @@ -43,29 +43,30 @@ pub async fn main() {
let peer_info = peer_config.peer_info();
let vs = ValidatorSet::new(vs);

let node = make_node(vs, my_sk, my_addr, peer_info, &config.peers).await;
let node = make_node(vs, my_sk, my_addr, peer_info, config.into()).await;

info!("[{}] Starting...", args.peer_id);

node.run().await;
}

pub async fn make_node(
vs: ValidatorSet,
pk: PrivateKey,
addr: Address,
validator_set: ValidatorSet,
private_key: PrivateKey,
address: Address,
peer_info: PeerInfo,
peers: &[PeerConfig],
peers: Peers<TestContext>,
) -> Node<TestContext, broadcast::Handle> {
let height = Height::new(1);
let ctx = TestContext::new(pk);
let sel = Arc::new(FixedProposer::new(vs.validators[0].address));
let start_height = Height::new(1);
let ctx = TestContext::new(private_key);
let proposer_selector = Arc::new(FixedProposer::new(validator_set.validators[0].address));

let params = Params {
start_height: height,
proposer_selector: sel,
validator_set: vs,
address: addr,
start_height,
proposer_selector,
validator_set,
address,
peers: peers.clone(),
threshold_params: Default::default(),
};

Expand Down
5 changes: 3 additions & 2 deletions code/node/bin/config.rs → code/node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::net::SocketAddr;

use serde::{Deserialize, Serialize};

use malachite_node::network::{broadcast::PeerInfo, PeerId};
use malachite_test::PublicKey;

use crate::network::{broadcast::PeerInfo, PeerId};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub peers: Vec<PeerConfig>,
Expand Down Expand Up @@ -34,7 +35,7 @@ pub mod de {
pub mod peer_id {
use super::*;

use malachite_node::network::PeerId;
use crate::network::PeerId;

pub fn serialize<S>(id: &PeerId, s: S) -> Result<S::Ok, S::Error>
where
Expand Down
2 changes: 2 additions & 0 deletions code/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod config;
pub mod network;
pub mod node;
pub mod peers;
pub mod timers;
25 changes: 20 additions & 5 deletions code/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use std::time::Duration;

use tokio::sync::mpsc;
use tracing::{debug, info, Instrument};
use tracing::{debug, info, warn, Instrument};

use malachite_common::{
Context, Height, Proposal, Round, SignedProposal, SignedVote, Timeout, TimeoutStep, Vote,
Expand All @@ -15,6 +15,7 @@ use malachite_vote::ThresholdParams;

use crate::network::Msg as NetworkMsg;
use crate::network::{Network, PeerId};
use crate::peers::Peers;
use crate::timers::{self, Timers};

pub struct Params<Ctx: Context> {
Expand All @@ -23,6 +24,7 @@ pub struct Params<Ctx: Context> {
pub validator_set: Ctx::ValidatorSet,
pub address: Ctx::Address,
pub threshold_params: ThresholdParams,
pub peers: Peers<Ctx>,
}

type TxInput<Ctx> = mpsc::UnboundedSender<Input<Ctx>>;
Expand Down Expand Up @@ -197,14 +199,27 @@ where
match msg {
NetworkMsg::Vote(signed_vote) => {
let signed_vote = SignedVote::<Ctx>::from_proto(signed_vote).unwrap();
// self.ctx.verify_signed_vote(signed_vote);
tx_input.send(Input::Vote(signed_vote.vote)).unwrap();
let peer = self.params.peers.get(&peer_id).unwrap(); // FIXME

if self.ctx.verify_signed_vote(&signed_vote, &peer.public_key) {
tx_input.send(Input::Vote(signed_vote.vote)).unwrap();
} else {
warn!("Invalid vote from peer {peer_id}: {signed_vote:?}");
}
}
NetworkMsg::Proposal(proposal) => {
let signed_proposal = SignedProposal::<Ctx>::from_proto(proposal).unwrap();
let validity = Validity::Valid; // self.ctx.verify_proposal(proposal);
let peer = self.params.peers.get(&peer_id).unwrap(); // FIXME

let valid = self
.ctx
.verify_signed_proposal(&signed_proposal, &peer.public_key);

tx_input
.send(Input::Proposal(signed_proposal.proposal, validity))
.send(Input::Proposal(
signed_proposal.proposal,
Validity::from_valid(valid),
))
.unwrap();
}

Expand Down
62 changes: 62 additions & 0 deletions code/node/src/peers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::net::SocketAddr;

use derive_where::derive_where;
use malachite_common::{Context, PublicKey};
use malachite_test::TestContext;

use crate::config::{Config, PeerConfig};
use crate::network::broadcast::PeerInfo;
use crate::network::PeerId;

#[derive_where(Clone, Debug)]
pub struct Peers<Ctx: Context> {
pub peers: Vec<Peer<Ctx>>,
}

impl<Ctx: Context> Peers<Ctx> {
pub fn get(&self, id: &PeerId) -> Option<&Peer<Ctx>> {
self.peers.iter().find(|p| &p.id == id)
}

pub fn iter(&self) -> impl Iterator<Item = &Peer<Ctx>> {
self.peers.iter()
}

pub fn except<'a>(&'a self, id: &'a PeerId) -> impl Iterator<Item = &Peer<Ctx>> + 'a {
self.iter().filter(move |p| &p.id != id)
}
}

impl From<Config> for Peers<TestContext> {
fn from(config: Config) -> Self {
Self {
peers: config.peers.into_iter().map(Peer::from).collect(),
}
}
}

#[derive_where(Clone, Debug)]
pub struct Peer<Ctx: Context> {
pub id: PeerId,
pub addr: SocketAddr,
pub public_key: PublicKey<Ctx>,
}

impl From<PeerConfig> for Peer<TestContext> {
fn from(peer: PeerConfig) -> Self {
Self {
id: peer.id,
addr: peer.addr,
public_key: peer.public_key,
}
}
}

impl Peer<TestContext> {
pub fn peer_info(&self) -> PeerInfo {
PeerInfo {
id: self.id.clone(),
addr: self.addr,
}
}
}

0 comments on commit 859e00c

Please sign in to comment.