Skip to content
Open
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
4 changes: 2 additions & 2 deletions primitives/src/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl SerializedSize for TendermintStep {
}

/// Unique identifier for a single instance of TendermintAggregation
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct TendermintIdentifier {
/// Network ID this tendermint vote is meant for.
pub network: NetworkId,
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<T: SerializeContent> TendermintProposal<T> {
// that can be included plain text as the proof alongside it also contains it.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TendermintVote {
/// Hash of the proposed macro block
/// Hash of the proposed macro or skip block
pub proposal_hash: Option<Blake2sHash>,
/// Identifier to this votes aggregation
pub id: TendermintIdentifier,
Expand Down
27 changes: 17 additions & 10 deletions validator/src/aggregation/tendermint/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nimiq_block::{MacroBody, MacroHeader, MicroBlock};
use nimiq_blockchain::Blockchain;
use nimiq_blockchain_interface::AbstractBlockchain;
use nimiq_hash::{Blake2sHash, Hash};
use nimiq_keys::Ed25519Signature as SchnorrSignature;
use nimiq_keys::{Address, Ed25519Signature as SchnorrSignature};
use nimiq_network_interface::{
network::Network,
request::{Handle, RequestCommon, RequestMarker},
Expand Down Expand Up @@ -59,10 +59,11 @@ impl SignedProposal {
/// via GossipSub, i.e. produced by this node itself.
pub fn into_tendermint_signed_message<Id>(
self,
address: Address,
id: Option<Id>,
) -> SignedProposalMessage<Header<Id>, (SchnorrSignature, u16)> {
) -> SignedProposalMessage<Header<Id>, (SchnorrSignature, Address, u16)> {
SignedProposalMessage {
signature: (self.signature, self.signer),
signature: (self.signature, address, self.signer),
message: ProposalMessage {
proposal: Header(self.proposal, id),
round: self.round,
Expand All @@ -78,15 +79,15 @@ impl SignedProposal {
&self,
predecessor: MicroBlock,
blockchain: &Blockchain,
) -> bool {
) -> Result<Address, ()> {
// Make sure the proposal references the predecessor as its parent hash
if predecessor.hash() != self.proposal.parent_hash {
return false;
return Err(());
}

// Make sure the height of the predecessor fits
if predecessor.block_number() + 1 != self.proposal.block_number {
return false;
return Err(());
}
// Get the active validators.
let validators = blockchain.current_validators().unwrap();
Expand All @@ -106,18 +107,24 @@ impl SignedProposal {
.validator;

// Compare the expected and the actual validator
*assumed_validator == actual_validator
if *assumed_validator != actual_validator {
return Err(());
}

Ok(actual_validator.address)
}
}

impl<Id> From<SignedProposalMessage<Header<Id>, (SchnorrSignature, u16)>> for SignedProposal {
fn from(value: SignedProposalMessage<Header<Id>, (SchnorrSignature, u16)>) -> Self {
impl<Id> From<SignedProposalMessage<Header<Id>, (SchnorrSignature, Address, u16)>>
for SignedProposal
{
fn from(value: SignedProposalMessage<Header<Id>, (SchnorrSignature, Address, u16)>) -> Self {
Self {
proposal: value.message.proposal.0,
valid_round: value.message.valid_round,
round: value.message.round,
signature: value.signature.0,
signer: value.signature.1,
signer: value.signature.2,
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions validator/src/aggregation/tendermint/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::sync::Arc;

use nimiq_block::MultiSignature;
use nimiq_handel::{
evaluator::WeightedVote, partitioner::BinomialPartitioner, protocol::Protocol,
store::ReplaceStore,
};
use nimiq_primitives::{policy::Policy, TendermintIdentifier};
use nimiq_tendermint::Aggregation;
use nimiq_primitives::{policy::Policy, TendermintIdentifier, TendermintVote};
use nimiq_tendermint::Aggregation as _;
use parking_lot::RwLock;

use super::{
super::registry::ValidatorRegistry, contribution::TendermintContribution,
verifier::TendermintVerifier,
};

#[derive(std::fmt::Debug)]
pub(crate) struct TendermintAggregationProtocol {
verifier: Arc<<Self as Protocol<TendermintIdentifier>>::Verifier>,
partitioner: Arc<<Self as Protocol<TendermintIdentifier>>::Partitioner>,
Expand All @@ -30,6 +30,7 @@ impl TendermintAggregationProtocol {
registry: Arc<ValidatorRegistry>,
node_id: usize,
id: TendermintIdentifier,
observe_valid_vote: Arc<dyn Fn(&TendermintVote, &MultiSignature) + Send + Sync>,
) -> Self {
let partitioner = Arc::new(BinomialPartitioner::new(node_id, registry.len()));

Expand All @@ -53,7 +54,11 @@ impl TendermintAggregationProtocol {
},
));

let verifier = Arc::new(TendermintVerifier::new(registry.clone(), id.clone()));
let verifier = Arc::new(TendermintVerifier::new(
registry.clone(),
id.clone(),
observe_valid_vote,
));

Self {
verifier,
Expand Down
7 changes: 4 additions & 3 deletions validator/src/aggregation/tendermint/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use nimiq_block::{MacroBody, MacroHeader};
use nimiq_database_value_derive::DbSerializable;
use nimiq_hash::Blake2sHash;
use nimiq_keys::Ed25519Signature as SchnorrSignature;
use nimiq_keys::{Address, Ed25519Signature as SchnorrSignature};
use nimiq_serde::{Deserialize, Serialize};
use nimiq_tendermint::{State as TendermintState, Step};
use nimiq_validator_network::{PubsubId, ValidatorNetwork};
Expand All @@ -23,7 +23,8 @@ pub struct MacroState {
round_number: u32,
step: Step,
known_proposals: BTreeMap<Blake2sHash, MacroHeader>,
round_proposals: BTreeMap<u32, BTreeMap<Blake2sHash, (Option<u32>, (SchnorrSignature, u16))>>,
round_proposals:
BTreeMap<u32, BTreeMap<Blake2sHash, (Option<u32>, (SchnorrSignature, Address, u16))>>,
votes: BTreeMap<(u32, Step), Option<Blake2sHash>>,
best_votes: BTreeMap<(u32, Step), TendermintContribution>,
inherents: BTreeMap<Blake2sHash, MacroBody>,
Expand Down Expand Up @@ -177,7 +178,7 @@ impl MacroState {
round: round_number,
valid_round,
signature: signature.0,
signer: signature.1,
signer: signature.2,
})
}
}
12 changes: 10 additions & 2 deletions validator/src/aggregation/tendermint/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use async_trait::async_trait;
use nimiq_block::MultiSignature;
use nimiq_bls::AggregatePublicKey;
use nimiq_handel::{
identity::IdentityRegistry,
Expand All @@ -13,17 +14,22 @@ use tokio::task;

use super::contribution::TendermintContribution;

#[derive(Debug)]
pub(crate) struct TendermintVerifier<I: IdentityRegistry> {
identity_registry: Arc<I>,
id: TendermintIdentifier,
observe_valid_vote: Arc<dyn Fn(&TendermintVote, &MultiSignature) + Send + Sync>,
}

impl<I: IdentityRegistry> TendermintVerifier<I> {
pub(crate) fn new(identity_registry: Arc<I>, id: TendermintIdentifier) -> Self {
pub(crate) fn new(
identity_registry: Arc<I>,
id: TendermintIdentifier,
observe_valid_vote: Arc<dyn Fn(&TendermintVote, &MultiSignature) + Send + Sync>,
) -> Self {
Self {
identity_registry,
id,
observe_valid_vote,
}
}
}
Expand Down Expand Up @@ -58,11 +64,13 @@ impl<I: IdentityRegistry + Sync + Send + 'static> Verifier for TendermintVerifie

params.push((aggregated_public_key, vote, multi_sig.clone()));
}
let observe_valid_vote = Arc::clone(&self.observe_valid_vote);
let result = task::spawn_blocking(move || {
params
.into_par_iter()
.map(|(aggregated_public_key, vote, contribution)| {
if aggregated_public_key.verify_hash(vote.hash(), &contribution.signature) {
observe_valid_vote(&vote, &contribution);
Ok(())
} else {
Err(())
Expand Down
97 changes: 97 additions & 0 deletions validator/src/double_proposal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::{
collections::{btree_map, BTreeMap},
mem,
};

use nimiq_block::{DoubleProposalProof, MacroHeader};
use nimiq_hash::Blake2sHash;
use nimiq_keys::{Address, Ed25519Signature as SchnorrSignature};
use nimiq_primitives::{networks::NetworkId, TendermintProposal};

struct Proposal {
proposer: Address,
hash: Blake2sHash,
proposal: TendermintProposal<MacroHeader>,
signature: SchnorrSignature,
}

enum Round {
Seen(Proposal),
Reported(Address),
}

impl Round {
fn proposer(&self) -> &Address {
match self {
Round::Seen(Proposal { proposer, .. }) => proposer,
Round::Reported(proposer) => proposer,
}
}
}

pub struct DoubleProposalDetector {
network: NetworkId,
block_number: u32,
rounds: BTreeMap<u32, Round>,
}

impl DoubleProposalDetector {
// TODO: add network_id
// TODO: record one proposal per validator per height
pub fn new(
network: NetworkId,
block_number: u32,
) -> DoubleProposalDetector {
DoubleProposalDetector {
network,
block_number,
rounds: BTreeMap::new(),
}
}
pub fn observe_valid_proposal(
&mut self,
proposer: Address,
proposal: TendermintProposal<MacroHeader>,
signature: SchnorrSignature,
) -> Option<DoubleProposalProof> {
assert_eq!(proposal.proposal.network, self.network);
assert_eq!(proposal.proposal.block_number, self.block_number);
match self.rounds.entry(proposal.round) {
btree_map::Entry::Vacant(v) => {
v.insert(Round::Seen(Proposal {
proposer,
hash: proposal.hash(),
proposal,
signature,
}));
None
}
btree_map::Entry::Occupied(o) => {
let round = o.into_mut();
assert_eq!(
*round.proposer(),
proposer,
"Only one address can propose in a round"
);
match round {
Round::Reported(_) => return None,
Round::Seen(Proposal { hash: old_hash, .. }) => {
if *old_hash == proposal.hash() {
return None;
}
}
}
match mem::replace(round, Round::Reported(proposer.clone())) {
Round::Reported(_) => unreachable!(),
Round::Seen(old_proposal) => Some(DoubleProposalProof::new(
proposer,
old_proposal.proposal,
old_proposal.signature,
proposal,
signature,
)),
}
}
}
}
}
Loading