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
25 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
a1247bd
Fixed rpc tests
arkpar Feb 15, 2018
673fc2c
Merge branch 'master' into rh-justification-verification
rphmeier Feb 15, 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
Propert block generation for tests
  • Loading branch information
arkpar committed Feb 15, 2018
commit 6a1a851a680d6650f2ad1152e3195b067bd86d44
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions substrate/bft/src/generic/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ impl<D, S> UncheckedJustification<D, S> {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Justification<D,S>(UncheckedJustification<D,S>);

impl<D, S> Justification<D, S> {
/// Convert this justification back to unchecked.
pub fn uncheck(self) -> UncheckedJustification<D, S> {
self.0
}
}

impl<D, S> ::std::ops::Deref for Justification<D, S> {
type Target = UncheckedJustification<D, S>;

Expand Down
10 changes: 10 additions & 0 deletions substrate/bft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ impl From<PrimitiveJustification> for UncheckedJustification {
}
}

impl From<UncheckedJustification> for PrimitiveJustification {
fn from(just: UncheckedJustification) -> Self {
PrimitiveJustification {
round_number: just.round_number as u32,
hash: just.digest,
signatures: just.signatures.into_iter().map(|s| (s.signer.into(), s.signature)).collect(),
}
}
}

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

Expand Down
4 changes: 3 additions & 1 deletion substrate/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ substrate-primitives = { path = "../primitives" }
substrate-runtime-io = { path = "../runtime-io" }
substrate-runtime-support = { path = "../runtime-support" }
substrate-state-machine = { path = "../state-machine" }
substrate-test-runtime = { path = "../test-runtime" }
substrate-keyring = { path = "../../substrate/keyring" }

[dev-dependencies]
substrate-test-runtime = { path = "../test-runtime" }
3 changes: 2 additions & 1 deletion substrate/client/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use state_machine;
use error;
use primitives::block::{self, Id as BlockId};
use primitives;

/// Block insertion operation. Keeps hold if the inserted block state and data.
pub trait BlockImportOperation {
Expand All @@ -28,7 +29,7 @@ pub trait BlockImportOperation {
/// Returns pending state.
fn state(&self) -> error::Result<&Self::State>;
/// Append block data to the transaction.
fn set_block_data(&mut self, header: block::Header, body: Option<block::Body>, is_new_best: bool) -> error::Result<()>;
fn set_block_data(&mut self, header: block::Header, body: Option<block::Body>, justification: Option<primitives::bft::Justification>, is_new_best: bool) -> error::Result<()>;
/// Inject storage data into the database.
fn set_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
/// Inject storage data into the database.
Expand Down
3 changes: 3 additions & 0 deletions substrate/client/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! Polkadot blockchain trait

use primitives::block::{self, Id as BlockId};
use primitives;
use error::Result;


Expand All @@ -26,6 +27,8 @@ pub trait Backend: Send + Sync {
fn header(&self, id: BlockId) -> Result<Option<block::Header>>;
/// Get block body. Returns `None` if block is not found.
fn body(&self, id: BlockId) -> Result<Option<block::Body>>;
/// Get block justification. Returns `None` if justification does not exist.
fn justification(&self, id: BlockId) -> Result<Option<primitives::bft::Justification>>;
/// Get blockchain info.
fn info(&self) -> Result<Info>;
/// Get block status.
Expand Down
47 changes: 11 additions & 36 deletions substrate/client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use state_machine;
use error;
use backend;
use runtime_support::Hashable;
use primitives;
use primitives::block::{self, Id as BlockId, HeaderHash};
use blockchain::{self, BlockStatus};
use state_machine::backend::Backend as StateBackend;
Expand All @@ -38,6 +39,7 @@ struct PendingBlock {
#[derive(PartialEq, Eq, Clone)]
struct Block {
header: block::Header,
justification: Option<primitives::bft::Justification>,
body: Option<block::Body>,
}

Expand Down Expand Up @@ -90,12 +92,13 @@ impl Blockchain {
}
}

fn insert(&self, hash: HeaderHash, header: block::Header, body: Option<block::Body>, is_new_best: bool) {
fn insert(&self, hash: HeaderHash, header: block::Header, justification: Option<primitives::bft::Justification>, body: Option<block::Body>, is_new_best: bool) {
let number = header.number;
let mut storage = self.storage.write();
storage.blocks.insert(hash, Block {
header: header,
body: body,
justification: justification,
});
storage.hashes.insert(number, hash);
if is_new_best {
Expand Down Expand Up @@ -132,6 +135,10 @@ impl blockchain::Backend for Blockchain {
Ok(self.id(id).and_then(|hash| self.storage.read().blocks.get(&hash).and_then(|b| b.body.clone())))
}

fn justification(&self, id: BlockId) -> error::Result<Option<primitives::bft::Justification>> {
Ok(self.id(id).and_then(|hash| self.storage.read().blocks.get(&hash).and_then(|b| b.justification.clone())))
}

fn info(&self) -> error::Result<blockchain::Info> {
let storage = self.storage.read();
Ok(blockchain::Info {
Expand Down Expand Up @@ -160,12 +167,13 @@ impl backend::BlockImportOperation for BlockImportOperation {
Ok(&self.pending_state)
}

fn set_block_data(&mut self, header: block::Header, body: Option<block::Body>, is_new_best: bool) -> error::Result<()> {
fn set_block_data(&mut self, header: block::Header, body: Option<block::Body>, justification: Option<primitives::bft::Justification>, is_new_best: bool) -> error::Result<()> {
assert!(self.pending_block.is_none(), "Only one block per operation is allowed");
self.pending_block = Some(PendingBlock {
block: Block {
header: header,
body: body,
justification: justification,
},
is_best: is_new_best,
});
Expand Down Expand Up @@ -197,39 +205,6 @@ impl Backend {
blockchain: Blockchain::new(),
}
}

/// Generate and import a sequence of blocks. A user supplied function is allowed to modify each block header. Useful for testing.
pub fn generate_blocks<F>(&self, count: usize, edit_header: F) where F: Fn(&mut block::Header) {
use backend::{Backend, BlockImportOperation};
let info = blockchain::Backend::info(&self.blockchain).expect("In-memory backend never fails");
let mut best_num = info.best_number;
let mut best_hash = info.best_hash;
let state_root = blockchain::Backend::header(&self.blockchain, BlockId::Hash(best_hash))
.expect("In-memory backend never fails")
.expect("Best header always exists in the blockchain")
.state_root;
for _ in 0 .. count {
best_num = best_num + 1;
let mut header = block::Header {
parent_hash: best_hash,
number: best_num,
state_root: state_root,
transaction_root: Default::default(),
digest: Default::default(),
};
edit_header(&mut header);

let mut tx = self.begin_operation(BlockId::Hash(best_hash)).expect("In-memory backend does not fail");
best_hash = header_hash(&header);
tx.set_block_data(header, Some(vec![]), true).expect("In-memory backend does not fail");
self.commit_operation(tx).expect("In-memory backend does not fail");
}
}

/// Generate and import a sequence of blocks. Useful for testing.
pub fn push_blocks(&self, count: usize) {
self.generate_blocks(count, |_| {})
}
}

impl backend::Backend for Backend {
Expand All @@ -253,7 +228,7 @@ impl backend::Backend for Backend {
if let Some(pending_block) = operation.pending_block {
let hash = header_hash(&pending_block.block.header);
self.states.write().insert(hash, operation.pending_state);
self.blockchain.insert(hash, pending_block.block.header, pending_block.block.body, pending_block.is_best);
self.blockchain.insert(hash, pending_block.block.header, pending_block.block.justification, pending_block.block.body, pending_block.is_best);
}
Ok(())
}
Expand Down
13 changes: 10 additions & 3 deletions substrate/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use blockchain::Backend as BlockchainBackend;
use backend::BlockImportOperation;
use state_machine::backend::Backend as StateBackend;
use state_machine::{Ext, OverlayedChanges};
use runtime_support::Hashable;

/// Polkadot Client
#[derive(Debug)]
Expand Down Expand Up @@ -154,7 +155,7 @@ impl<B, E> Client<B, E> where
let (genesis_header, genesis_store) = build_genesis();
let mut op = backend.begin_operation(BlockId::Hash(block::HeaderHash::default()))?;
op.reset_storage(genesis_store.into_iter())?;
op.set_block_data(genesis_header, Some(vec![]), true)?;
op.set_block_data(genesis_header, Some(vec![]), None, true)?;
backend.commit_operation(op)?;
}
Ok(Client {
Expand Down Expand Up @@ -267,7 +268,7 @@ impl<B, E> Client<B, E> where
// TODO: import lock
// TODO: validate block
// TODO: import justification.
let (header, _) = header.into_inner();
let (header, justification) = header.into_inner();
match self.backend.blockchain().status(BlockId::Hash(header.parent_hash))? {
blockchain::BlockStatus::InChain => (),
blockchain::BlockStatus::Unknown => return Ok(ImportResult::UnknownParent),
Expand All @@ -285,7 +286,8 @@ impl<B, E> Client<B, E> where
)?;

let is_new_best = header.number == self.backend.blockchain().info()?.best_number + 1;
transaction.set_block_data(header, body, is_new_best)?;
trace!("Imported {}, (#{}), best={}", block::HeaderHash::from(header.blake2_256()), header.number, is_new_best);
transaction.set_block_data(header, body, Some(justification.uncheck().into()), is_new_best)?;
transaction.set_storage(overlay.drain())?;
self.backend.commit_operation(transaction)?;
Ok(ImportResult::Queued)
Expand Down Expand Up @@ -340,6 +342,11 @@ impl<B, E> Client<B, E> where
pub fn body(&self, id: &BlockId) -> error::Result<Option<block::Body>> {
self.backend.blockchain().body(*id)
}

/// Get block justification set by id.
pub fn justification(&self, id: &BlockId) -> error::Result<Option<primitives::bft::Justification>> {
self.backend.blockchain().justification(*id)
}
}

impl<B, E> bft::BlockImport for Client<B, E>
Expand Down
6 changes: 6 additions & 0 deletions substrate/ed25519/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl AsRef<[u8]> for Public {
}
}

impl Into<[u8; 32]> for Public {
fn into(self) -> [u8; 32] {
self.0
}
}

impl Pair {
/// Generate new secure (random) key pair.
pub fn new() -> Pair {
Expand Down
5 changes: 5 additions & 0 deletions substrate/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ substrate-primitives = { path = "../../substrate/primitives" }
substrate-client = { path = "../../substrate/client" }
substrate-state-machine = { path = "../../substrate/state-machine" }
substrate-serializer = { path = "../../substrate/serializer" }
substrate-runtime-support = { path = "../../substrate/runtime-support" }

[dev-dependencies]
substrate-test-runtime = { path = "../test-runtime" }
substrate-executor = { path = "../../substrate/executor" }
substrate-keyring = { path = "../../substrate/keyring" }
substrate-codec = { path = "../../substrate/codec" }
substrate-bft = { path = "../bft" }
env_logger = "0.4"
7 changes: 7 additions & 0 deletions substrate/network/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub trait Client: Send + Sync {

/// Get block body.
fn body(&self, id: &BlockId) -> Result<Option<block::Body>, Error>;

/// Get block justification.
fn justification(&self, id: &BlockId) -> Result<Option<Justification>, Error>;
}

impl<B, E> Client for PolkadotClient<B, E> where
Expand Down Expand Up @@ -72,4 +75,8 @@ impl<B, E> Client for PolkadotClient<B, E> where
fn body(&self, id: &BlockId) -> Result<Option<block::Body>, Error> {
(self as &PolkadotClient<B, E>).body(id)
}

fn justification(&self, id: &BlockId) -> Result<Option<Justification>, Error> {
(self as &PolkadotClient<B, E>).justification(id)
}
}
19 changes: 11 additions & 8 deletions substrate/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ extern crate substrate_primitives as primitives;
extern crate substrate_state_machine as state_machine;
extern crate substrate_serializer as ser;
extern crate substrate_client as client;
extern crate substrate_runtime_support as runtime_support;
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate log;
#[macro_use] extern crate bitflags;
#[macro_use] extern crate error_chain;

#[cfg(test)] extern crate env_logger;
#[cfg(test)] extern crate substrate_test_runtime as test_runtime;
#[cfg(test)] extern crate substrate_keyring as keyring;
#[cfg(test)] #[macro_use] extern crate substrate_executor as executor;
#[cfg(test)] extern crate substrate_codec as codec;
#[cfg(test)] extern crate substrate_bft as bft;

mod service;
mod sync;
mod protocol;
Expand All @@ -44,13 +52,7 @@ mod config;
mod chain;
mod blocks;

#[cfg(test)]
mod test;

#[cfg(test)]
extern crate substrate_executor;
#[cfg(test)]
extern crate env_logger;
#[cfg(test)] mod test;

pub use service::Service;
pub use protocol::{ProtocolStatus};
Expand All @@ -59,5 +61,6 @@ pub use network::{NonReservedPeerMode, ConnectionFilter, ConnectionDirection, Ne

// TODO: move it elsewhere
fn header_hash(header: &primitives::Header) -> primitives::block::HeaderHash {
primitives::hashing::blake2_256(&ser::encode(header)).into()
use runtime_support::Hashable;
header.blake2_256().into()
}
6 changes: 3 additions & 3 deletions substrate/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ impl Protocol {
};
let max = cmp::min(request.max.unwrap_or(u32::max_value()), MAX_BLOCK_DATA_RESPONSE) as usize;
// TODO: receipts, etc.
let (mut get_header, mut get_body) = (false, false);
let (mut get_header, mut get_body, mut get_justification) = (false, false, false);
for a in request.fields {
match a {
message::BlockAttribute::Header => get_header = true,
message::BlockAttribute::Body => get_body = true,
message::BlockAttribute::Receipt => unimplemented!(),
message::BlockAttribute::MessageQueue => unimplemented!(),
message::BlockAttribute::Justification => unimplemented!(),
message::BlockAttribute::Justification => get_justification = true,
}
}
while let Some(header) = self.chain.header(&id).unwrap_or(None) {
Expand All @@ -244,7 +244,7 @@ impl Protocol {
body: if get_body { self.chain.body(&BlockId::Hash(hash)).unwrap_or(None) } else { None },
receipt: None,
message_queue: None,
justification: None,
justification: if get_justification { self.chain.justification(&BlockId::Hash(hash)).unwrap_or(None) } else { None },
};
blocks.push(block_data);
match request.direction {
Expand Down
Loading