diff --git a/demo/cli/src/lib.rs b/demo/cli/src/lib.rs index 298f7228f3d59..58ddadf7c4272 100644 --- a/demo/cli/src/lib.rs +++ b/demo/cli/src/lib.rs @@ -125,10 +125,6 @@ pub fn run(args: I) -> error::Result<()> where let prepare_genesis = || { storage = genesis_config.build_externalities(); let block = genesis::construct_genesis_block(&storage); - with_externalities(&mut storage, || - // TODO: use api.rs to dispatch instead - demo_runtime::System::initialise_genesis_state(&block.header) - ); (primitives::block::Header::decode(&mut block.header.encode().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect()) }; let client = Arc::new(client::new_in_mem(executor, prepare_genesis)?); diff --git a/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm b/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm index d9377e58e1adb..5275ded0aac1f 100644 Binary files a/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm and b/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.compact.wasm differ diff --git a/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm b/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm index bdcb8d5a84375..76d1188dc5891 100755 Binary files a/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm and b/demo/runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm differ diff --git a/polkadot/api/src/lib.rs b/polkadot/api/src/lib.rs index 98e439875aa2f..21b11a0d3678a 100644 --- a/polkadot/api/src/lib.rs +++ b/polkadot/api/src/lib.rs @@ -192,7 +192,16 @@ impl PolkadotApi for Client> } fn duty_roster(&self, at: &CheckedId) -> Result { - with_runtime!(self, at, ::runtime::Parachains::calculate_duty_roster) + // duty roster can only be queried at the start of a block, + // so we create a dummy. + let id = at.block_id(); + let parent_hash = self.block_hash_from_id(id)?.ok_or(ErrorKind::UnknownBlock(*id))?; + let number = self.block_number_from_id(id)?.ok_or(ErrorKind::UnknownBlock(*id))? + 1; + + with_runtime!(self, at, || { + ::runtime::System::initialise(&number, &parent_hash, &Default::default()); + ::runtime::Parachains::calculate_duty_roster() + }) } fn timestamp(&self, at: &CheckedId) -> Result { @@ -203,7 +212,7 @@ impl PolkadotApi for Client> with_runtime!(self, at, || ::runtime::Executive::execute_block(block)) } - fn index(&self, at: &Self::CheckedBlockId, account: AccountId) -> Result { + fn index(&self, at: &CheckedId, account: AccountId) -> Result { with_runtime!(self, at, || ::runtime::System::account_index(account)) } @@ -381,10 +390,6 @@ mod tests { || { let mut storage = genesis_config.build_externalities(); let block = ::client::genesis::construct_genesis_block(&storage); - with_externalities(&mut storage, || - // TODO: use api.rs to dispatch instead - runtime::System::initialise_genesis_state(&block.header) - ); (substrate_primitives::block::Header::decode(&mut block.header.encode().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect()) } ).unwrap() diff --git a/polkadot/consensus/src/error.rs b/polkadot/consensus/src/error.rs index 38ba4ab60716f..312c88f13e728 100644 --- a/polkadot/consensus/src/error.rs +++ b/polkadot/consensus/src/error.rs @@ -16,8 +16,7 @@ //! Errors that can occur during the consensus process. -use primitives::block::HeaderHash; - +use primitives::block::{HeaderHash, Number}; error_chain! { links { PolkadotApi(::polkadot_api::Error, ::polkadot_api::ErrorKind); @@ -41,6 +40,10 @@ error_chain! { description("Proposal had wrong parent hash."), display("Proposal had wrong parent hash. Expected {:?}, got {:?}", expected, got), } + WrongNumber(expected: Number, got: Number) { + description("Proposal had wrong number."), + display("Proposal had wrong number. Expected {:?}, got {:?}", expected, got), + } ProposalTooLarge(size: usize) { description("Proposal exceeded the maximum size."), display( diff --git a/polkadot/consensus/src/lib.rs b/polkadot/consensus/src/lib.rs index 53cf39b6c8f23..978181be1ec77 100644 --- a/polkadot/consensus/src/lib.rs +++ b/polkadot/consensus/src/lib.rs @@ -575,7 +575,7 @@ impl bft::Proposer for Proposer { let substrate_block = Slicable::decode(&mut polkadot_block.encode().as_slice()) .expect("polkadot blocks defined to serialize to substrate blocks correctly; qed"); - assert!(evaluate_proposal(&substrate_block, &*self.client, current_timestamp(), &self.parent_hash, &self.parent_id).is_ok()); + assert!(evaluate_proposal(&substrate_block, &*self.client, current_timestamp(), &self.parent_hash, self.parent_number, &self.parent_id).is_ok()); Ok(substrate_block) } @@ -583,7 +583,7 @@ impl bft::Proposer for Proposer { // TODO: certain kinds of errors here should lead to a misbehavior report. fn evaluate(&self, proposal: &SubstrateBlock) -> Result { debug!(target: "bft", "evaluating block on top of parent ({}, {:?})", self.parent_number, self.parent_hash); - match evaluate_proposal(proposal, &*self.client, current_timestamp(), &self.parent_hash, &self.parent_id) { + match evaluate_proposal(proposal, &*self.client, current_timestamp(), &self.parent_hash, self.parent_number, &self.parent_id) { Ok(x) => Ok(x), Err(e) => match *e.kind() { ErrorKind::PolkadotApi(polkadot_api::ErrorKind::Executor(_)) => Ok(false), @@ -656,6 +656,7 @@ fn evaluate_proposal( client: &C, now: Timestamp, parent_hash: &HeaderHash, + parent_number: BlockNumber, parent_id: &C::CheckedBlockId, ) -> Result { const MAX_TIMESTAMP_DRIFT: Timestamp = 4; @@ -677,9 +678,9 @@ fn evaluate_proposal( bail!(ErrorKind::WrongParentHash(*parent_hash, proposal.header.parent_hash)); } - // no need to check number because - // a) we assume the parent is valid. - // b) the runtime checks that `proposal.parent_hash` == `block_hash(proposal.number - 1)` + if proposal.header.number != parent_number + 1 { + bail!(ErrorKind::WrongNumber(parent_number + 1, proposal.header.number)) + } let block_timestamp = proposal.timestamp(); diff --git a/polkadot/consensus/src/service.rs b/polkadot/consensus/src/service.rs index 3671edfb90f3e..ce4d9e56b1134 100644 --- a/polkadot/consensus/src/service.rs +++ b/polkadot/consensus/src/service.rs @@ -249,12 +249,13 @@ fn start_bft( return; } }; + let input = messages.select(hash, network.bft_messages(), authorities).map_err(|e| e.into()); let output = BftSink { network: network, parent_hash: hash.clone(), _e: Default::default() }; match bft_service.build_upon(&header, input, output) { Ok(Some(bft)) => handle.spawn(bft), Ok(None) => {}, - Err(e) => debug!("BFT agreement error: {:?}", e), + Err(e) => debug!(target: "bft","BFT agreement error: {:?}", e), } } diff --git a/polkadot/runtime/wasm/genesis.wasm b/polkadot/runtime/wasm/genesis.wasm index 13aa5149366f6..bc0861d31f35d 100644 Binary files a/polkadot/runtime/wasm/genesis.wasm and b/polkadot/runtime/wasm/genesis.wasm differ diff --git a/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm b/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm index 13aa5149366f6..bc0861d31f35d 100644 Binary files a/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm and b/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.compact.wasm differ diff --git a/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm b/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm index 3c4a32a5a9536..98f0bf055e032 100755 Binary files a/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm and b/polkadot/runtime/wasm/target/wasm32-unknown-unknown/release/polkadot_runtime.wasm differ diff --git a/polkadot/service/src/lib.rs b/polkadot/service/src/lib.rs index da5a59c5aad9c..45ac157aba56f 100644 --- a/polkadot/service/src/lib.rs +++ b/polkadot/service/src/lib.rs @@ -192,11 +192,6 @@ impl Service { let prepare_genesis = || { storage = genesis_config.build_externalities(); let block = genesis::construct_genesis_block(&storage); - with_externalities(&mut storage, || { - // TODO: use api.rs to dispatch instead - polkadot_runtime::System::initialise_genesis_state(&block.header); - info!("Genesis header hash: {}", polkadot_runtime::System::block_hash(0)); - }); (primitives::block::Header::decode(&mut block.header.encode().as_ref()).expect("to_vec() always gives a valid serialisation; qed"), storage.into_iter().collect()) }; diff --git a/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm b/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm index 1d065a3690093..6f29446e089cb 100644 Binary files a/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm and b/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm differ diff --git a/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm b/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm index 9ba001aada9be..06dbcefbc13df 100755 Binary files a/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm and b/substrate/executor/wasm/target/wasm32-unknown-unknown/release/runtime_test.wasm differ diff --git a/substrate/runtime/executive/src/lib.rs b/substrate/runtime/executive/src/lib.rs index e273db25ed05f..35982f754b5d3 100644 --- a/substrate/runtime/executive/src/lib.rs +++ b/substrate/runtime/executive/src/lib.rs @@ -104,9 +104,6 @@ impl< // any final checks Self::final_checks(&header); - - // any stuff that we do after taking the storage root. - Self::post_finalise(&header); } /// Finalise the block - it is up the caller to ensure that all header fields are valid @@ -116,11 +113,7 @@ impl< // setup extrinsics >::derive_extrinsics(); - - let header = >::finalise(); - Self::post_finalise(&header); - - header + >::finalise() } /// Apply outside of the block execution function. @@ -170,12 +163,6 @@ impl< header.state_root().check_equal(&storage_root); assert!(header.state_root() == &storage_root, "Storage root must match that calculated."); } - - fn post_finalise(header: &System::Header) { - // store the header hash in storage; we can't do it before otherwise there would be a - // cyclic dependency. - >::record_block_hash(header); - } } #[cfg(test)] diff --git a/substrate/runtime/system/src/lib.rs b/substrate/runtime/system/src/lib.rs index a8abc82977151..864a007726238 100644 --- a/substrate/runtime/system/src/lib.rs +++ b/substrate/runtime/system/src/lib.rs @@ -94,6 +94,7 @@ impl Module { // populate environment. >::put(number); >::put(parent_hash); + >::insert(*number - One::one(), parent_hash); >::put(txs_root); >::put(Self::calculate_random()); >::put(0); @@ -119,21 +120,6 @@ impl Module { >::put(l); } - /// Records a particular block number and hash combination. - pub fn record_block_hash>(header: &H) { - // store the header hash in storage; we can't do it before otherwise there would be a - // cyclic dependency. - let h = T::Hashing::hash_of(header); - >::insert(header.number(), &h); - - Self::initialise(&(*header.number() + One::one()), &h, &Default::default()); - } - - /// Initializes the state following the determination of the genesis block. - pub fn initialise_genesis_state>(header: &H) { - Self::record_block_hash(header); - } - /// Calculate the current block's random seed. fn calculate_random() -> T::Hash { assert!(Self::block_number() > Zero::zero(), "Block number may never be zero"); diff --git a/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm b/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm index bd198235855d2..b56b76b0ab746 100644 Binary files a/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm and b/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm differ diff --git a/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.wasm b/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.wasm index f4674c935bebb..a3ffd5df44f23 100755 Binary files a/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.wasm and b/substrate/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.wasm differ