Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
482a074
reshuffle consensus libraries
rphmeier Feb 8, 2018
917b092
polkadot-useful type definitions for statement table
rphmeier Feb 8, 2018
8e2fd3c
begin BftService
rphmeier Feb 10, 2018
776cf13
Merge branch 'master' into rh-split-bft-table
rphmeier Feb 10, 2018
6abfed4
primary selection logic
rphmeier Feb 12, 2018
fc18524
bft service implementation without I/O
rphmeier Feb 12, 2018
017fd51
extract out `BlockImport` trait
rphmeier Feb 12, 2018
25990ee
Merge branch 'master' into rh-split-bft-table
rphmeier Feb 12, 2018
c33c3ff
allow bft primitives to compile on wasm
rphmeier Feb 12, 2018
acab9a3
Block builder (substrate)
gavofyork Feb 12, 2018
1830fa7
take polkadot-consensus down to the core.
rphmeier Feb 12, 2018
767a9d9
test for preemption
rphmeier Feb 12, 2018
7fc4b4d
fix test build
rphmeier Feb 12, 2018
9acd3f9
Fix wasm build
gavofyork Feb 12, 2018
ca5900f
Bulid on any block
gavofyork Feb 13, 2018
d11cfe1
Test for block builder.
gavofyork Feb 13, 2018
b973ccc
Block import tests for client.
gavofyork Feb 13, 2018
ec61865
Tidy ups
gavofyork Feb 13, 2018
23638cd
clean up block builder instantiation
rphmeier Feb 15, 2018
dda6d24
Merge branch 'rh-split-bft-table' into rh-justification-verification
rphmeier Feb 15, 2018
340ce39
justification verification logic
rphmeier Feb 15, 2018
170b0d1
JustifiedHeader and import
rphmeier Feb 15, 2018
6a1a851
Propert block generation for tests
arkpar Feb 15, 2018
1352765
network and tablerouter trait
rphmeier Feb 15, 2018
2758503
use statement import to drive creation of further statements
rphmeier Feb 15, 2018
a1247bd
Fixed rpc tests
arkpar Feb 15, 2018
a1a19b6
custom error type for consensus
rphmeier Feb 15, 2018
40a9496
create proposer
rphmeier Feb 15, 2018
9e4f273
asynchronous proposal evaluation
rphmeier Feb 15, 2018
673fc2c
Merge branch 'master' into rh-justification-verification
rphmeier Feb 15, 2018
8636b77
Merge branch 'rh-justification-verification' into rh-polkadot-propose
rphmeier Feb 15, 2018
a5c09c8
inherent transactions in polkadot runtime
rphmeier Feb 16, 2018
7b1a563
fix tests to match real polkadot block constraints
rphmeier Feb 16, 2018
8d08573
implicitly generate inherent functions
rphmeier Feb 16, 2018
2abbe6c
add inherent transaction functionality to block body
rphmeier Feb 20, 2018
5bace3a
Merge branch 'master' into rh-polkadot-propose
rphmeier Feb 20, 2018
a87afa7
block builder logic for polkadot
rphmeier Feb 20, 2018
e891649
some tests for the polkadot API
rphmeier Feb 20, 2018
5b3556c
avoid redundancy in native code compatibility check
rphmeier Feb 21, 2018
ad8a576
helper for extracting nonce
rphmeier Feb 21, 2018
40b5e4c
transaction pool implementation
rphmeier Feb 21, 2018
760aeff
transaction pool
rphmeier Feb 23, 2018
198ff7b
integrate transaction pool with proposer
rphmeier Feb 23, 2018
3d052fb
Merge branch 'master' into rh-transaction-pool
rphmeier Feb 25, 2018
fd8e624
indentation
rphmeier Feb 25, 2018
8458389
kill storage keys module
rphmeier Feb 25, 2018
81ff2ad
accept new transactions to replace old
rphmeier Mar 1, 2018
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
Prev Previous commit
Next Next commit
justification verification logic
  • Loading branch information
rphmeier committed Feb 15, 2018
commit 340ce39a66915acfca42fa54e21bf5b1ea63d04a
89 changes: 77 additions & 12 deletions substrate/bft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
use client::{BlockId, Client};
use client::backend::Backend;
use codec::Slicable;
use ed25519::Signature;
use ed25519::LocalizedSignature;
use primitives::bft::{Message as PrimitiveMessage, Action as PrimitiveAction};
use primitives::block::{Block, Header, HeaderHash};
use primitives::AuthorityId;
use state_machine::CodeExecutor;
Expand All @@ -63,20 +64,23 @@ pub type LocalizedMessage = generic::LocalizedMessage<
Block,
HeaderHash,
AuthorityId,
Signature
LocalizedSignature
>;

/// Justification of some hash.
pub type Justification = generic::Justification<HeaderHash, Signature>;
pub type Justification = generic::Justification<HeaderHash, LocalizedSignature>;

/// Justification of a prepare message.
pub type PrepareJustification = generic::PrepareJustification<HeaderHash, Signature>;
pub type PrepareJustification = generic::PrepareJustification<HeaderHash, LocalizedSignature>;

/// Unchecked justification.
pub type UncheckedJustification = generic::UncheckedJustification<HeaderHash, LocalizedSignature>;

/// Result of a committed round of BFT
pub type Committed = generic::Committed<Block, HeaderHash, Signature>;
pub type Committed = generic::Committed<Block, HeaderHash, LocalizedSignature>;

/// Communication between BFT participants.
pub type Communication = generic::Communication<Block, HeaderHash, AuthorityId, Signature>;
pub type Communication = generic::Communication<Block, HeaderHash, AuthorityId, LocalizedSignature>;

/// Logic for a proposer.
///
Expand Down Expand Up @@ -131,7 +135,6 @@ impl<B, E> Authorities for Client<B, E>
}
}


/// Instance of BFT agreement.
struct BftInstance<P> {
key: Arc<ed25519::Pair>,
Expand All @@ -145,7 +148,7 @@ struct BftInstance<P> {
impl<P: Proposer> generic::Context for BftInstance<P> {
type AuthorityId = AuthorityId;
type Digest = HeaderHash;
type Signature = Signature;
type Signature = LocalizedSignature;
type Candidate = Block;
type RoundTimeout = Box<Future<Item=(),Error=Error> + Send>;
type CreateProposal = <P::CreateProposal as IntoFuture>::Future;
Expand All @@ -163,8 +166,6 @@ impl<P: Proposer> generic::Context for BftInstance<P> {
}

fn sign_local(&self, message: Message) -> LocalizedMessage {
use primitives::bft::{Message as PrimitiveMessage, Action as PrimitiveAction};

let action = match message.clone() {
::generic::Message::Propose(r, p) => PrimitiveAction::Propose(r as u32, p),
::generic::Message::Prepare(r, h) => PrimitiveAction::Prepare(r as u32, h),
Expand All @@ -178,7 +179,10 @@ impl<P: Proposer> generic::Context for BftInstance<P> {
};

let to_sign = Slicable::encode(&primitive);
let signature = self.key.sign(&to_sign);
let signature = LocalizedSignature {
signer: self.key.public(),
signature: self.key.sign(&to_sign),
};

LocalizedMessage {
message,
Expand Down Expand Up @@ -327,7 +331,7 @@ impl<P, E, I> BftService<P, E, I>
// TODO: check key is one of the authorities.
let authorities = self.client.authorities(&BlockId::Hash(hash))?;
let n = authorities.len();
let max_faulty = n.saturating_sub(1) / 3;
let max_faulty = max_faulty_of(n);

let bft_instance = BftInstance {
proposer,
Expand Down Expand Up @@ -372,6 +376,57 @@ impl<P, E, I> BftService<P, E, I>
}
}

/// Given a total number of authorities, yield the maximum faulty that would be allowed.
/// This will always be under 1/3.
pub fn max_faulty_of(n: usize) -> usize {
n.saturating_sub(1) / 3
}

fn check_justification_signed_message(authorities: &[AuthorityId], message: &[u8], just: UncheckedJustification)
-> Result<Justification, UncheckedJustification>
{
just.check(authorities.len() - max_faulty_of(authorities.len()), |_, _, sig| {
let auth_id = sig.signer.0;
if !authorities.contains(&auth_id) { return None }

if ed25519::verify_strong(&sig.signature, message, &sig.signer) {
Some(sig.signer.0)
} else {
None
}
})
}

/// Check a full justification for a header hash.
/// Provide all valid authorities.
///
/// On failure, returns the justification back.
pub fn check_justification(authorities: &[AuthorityId], parent: HeaderHash, just: UncheckedJustification)
-> Result<Justification, UncheckedJustification>
{
let message = Slicable::encode(&PrimitiveMessage {
parent,
action: PrimitiveAction::Commit(just.round_number as u32, just.digest),
});

check_justification_signed_message(authorities, &message[..], just)
}

/// Check a prepare justification for a header hash.
/// Provide all valid authorities.
///
/// On failure, returns the justification back.
pub fn check_prepare_justification(authorities: &[AuthorityId], parent: HeaderHash, just: UncheckedJustification)
-> Result<PrepareJustification, UncheckedJustification>
{
let message = Slicable::encode(&PrimitiveMessage {
parent,
action: PrimitiveAction::Prepare(just.round_number as u32, just.digest),
});

check_justification_signed_message(authorities, &message[..], just)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -470,4 +525,14 @@ mod tests {

core.turn(Some(::std::time::Duration::from_millis(100)));
}

#[test]
fn max_faulty() {
assert_eq!(max_faulty_of(3), 0);
assert_eq!(max_faulty_of(4), 1);
assert_eq!(max_faulty_of(100), 33);
assert_eq!(max_faulty_of(0), 0);
assert_eq!(max_faulty_of(11), 3);
assert_eq!(max_faulty_of(99), 32);
}
}
19 changes: 17 additions & 2 deletions substrate/ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ extern crate untrusted;
use ring::{rand, signature};
use primitives::hash::H512;

/// Alias to 520-bit hash when used in the context of a signature on the relay chain.
/// Alias to 512-bit hash when used in the context of a signature on the relay chain.
pub type Signature = H512;

/// A localized signature also contains sender information.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct LocalizedSignature {
/// The signer of the signature.
pub signer: Public,
/// The signature itself.
pub signature: Signature,
}

/// Verify a message without type checking the parameters' types for the right size.
pub fn verify(sig: &[u8], message: &[u8], public: &[u8]) -> bool {
let public_key = untrusted::Input::from(public);
Expand All @@ -40,7 +49,7 @@ pub fn verify(sig: &[u8], message: &[u8], public: &[u8]) -> bool {
}

/// A public key.
#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Public(pub [u8; 32]);

/// A key pair.
Expand Down Expand Up @@ -152,6 +161,12 @@ impl Verifiable for Signature {
}
}

impl Verifiable for LocalizedSignature {
fn verify(&self, message: &[u8], pubkey: &Public) -> bool {
pubkey == &self.signer && self.signature.verify(message, pubkey)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down