This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 371
Adds first version of validate_block
#3
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ecbf2c4
Adds `validate_block` support for `runtime`
bkchr 68100e7
Adds `test-runtime` to start working on tests
bkchr 86fc199
Make the tests pass
bkchr 80b222c
Update to latest polkadot/substrate
bkchr 2c6bf07
Start using trie db
bkchr 101d338
Adapt to latest changes in the specification
bkchr 815e466
Trying to get the tests back working
bkchr f213d5e
Fixes simple test
bkchr cbaef32
Implement test that executes extrinsics
bkchr 4fa5d35
Some docs
bkchr 391792f
Some fixes
bkchr 092f9ac
Forget the correct value
bkchr f124354
Fix review comments
bkchr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| [workspace] | ||
| members = [ "consensus" ] | ||
| members = [ "consensus", "runtime" ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,23 @@ | |
| name = "cumulus-consensus" | ||
| description = "Proxy Polkadot's consensus as a consensus engine for Substrate" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies"] | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| # substrate deps | ||
| substrate-client = { git = "https://github.com/paritytech/substrate" } | ||
| substrate-consensus-common = { git = "https://github.com/paritytech/substrate" } | ||
| substrate-primitives = { git = "https://github.com/paritytech/substrate" } | ||
| sr-primitives = { git = "https://github.com/paritytech/substrate" } | ||
| substrate-client = { git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
| substrate-consensus-common = { git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
| substrate-primitives = { git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
| sr-primitives = { git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
|
|
||
| # polkadot deps | ||
| polkadot-service = { git = "https://github.com/paritytech/polkadot" } | ||
| polkadot-primitives = { git = "https://github.com/paritytech/polkadot" } | ||
| polkadot-runtime = { git = "https://github.com/paritytech/polkadot" } | ||
| polkadot-service = { git = "https://github.com/paritytech/polkadot", branch = "bkchr-validate_block" } | ||
| polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "bkchr-validate_block" } | ||
| polkadot-runtime = { git = "https://github.com/paritytech/polkadot", branch = "bkchr-validate_block" } | ||
|
|
||
| # other deps | ||
| futures = "0.1.21" | ||
| tokio = "0.1.8" | ||
| parity-codec = "3.0" | ||
| parity-codec = "3.1" | ||
| log = "0.4" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| [package] | ||
| name = "cumulus-runtime" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| codec = { package = "parity-codec", version = "3.1", default-features = false, features = [ "derive" ] } | ||
| rstd = { package = "sr-std", git = "https://github.com/paritytech/substrate", default-features = false, branch = "bkchr-validate_block" } | ||
| runtime-primitives = { package = "sr-primitives", git = "https://github.com/paritytech/substrate", default-features = false, branch = "bkchr-validate_block" } | ||
| rio = { package = "sr-io", git = "https://github.com/paritytech/substrate", default-features = false, branch = "bkchr-validate_block" } | ||
| executive = { package = "srml-executive", git = "https://github.com/paritytech/substrate", default-features = false, branch = "bkchr-validate_block" } | ||
|
|
||
| [dev-dependencies] | ||
| keyring = { package = "substrate-keyring", git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
| primitives = { package = "substrate-primitives", git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
| executor = { package = "substrate-executor", git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
| test-runtime = { package = "substrate-test-runtime", git = "https://github.com/paritytech/substrate", branch = "bkchr-validate_block" } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "codec/std", | ||
| "rstd/std", | ||
| "rio/std", | ||
| "runtime-primitives/std", | ||
| "executive/std", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Cumulus. | ||
|
|
||
| // Cumulus is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Cumulus is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use rstd::{vec::Vec, collections::btree_map::BTreeMap}; | ||
| use codec::{Encode, Decode}; | ||
| use runtime_primitives::traits::Block as BlockT; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| #[doc(hidden)] | ||
| pub use rstd::slice; | ||
|
|
||
| #[macro_use] | ||
| pub mod validate_block; | ||
|
|
||
| type WitnessData = BTreeMap<Vec<u8>, Vec<u8>>; | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// The parachain block that is created on a collator and validated by a validator. | ||
| #[derive(Encode, Decode)] | ||
| struct ParachainBlock<B: BlockT> { | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extrinsics: Vec<<B as BlockT>::Extrinsic>, | ||
| /// The data that is required to emulate the storage accesses executed by all extrinsics. | ||
| witness_data: WitnessData, | ||
| } | ||
|
|
||
| impl<B: BlockT> ParachainBlock<B> { | ||
| #[cfg(test)] | ||
| fn new(extrinsics: Vec<<B as BlockT>::Extrinsic>, witness_data: WitnessData) -> Self { | ||
| Self { | ||
| extrinsics, | ||
| witness_data, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<B: BlockT> Default for ParachainBlock<B> { | ||
| fn default() -> Self { | ||
| Self { | ||
| extrinsics: Vec::default(), | ||
| witness_data: BTreeMap::default(), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
|
|
||
| // Substrate is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Substrate is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! A module that enables a runtime to work as parachain. | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, One}; | ||
| #[cfg(not(feature = "std"))] | ||
| use executive::ExecuteBlock; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| #[doc(hidden)] | ||
| pub mod storage_functions; | ||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| /// Register the `validate_block` function that is used by parachains to validate blocks on a validator. | ||
| /// | ||
| /// Does *nothing* when `std` feature is enabled. | ||
| /// | ||
| /// Expects as parameters the block and the block executor. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// struct Block; | ||
| /// struct BlockExecutor; | ||
| /// | ||
| /// cumulus_runtime::register_validate_block!(Block, BlockExecutor); | ||
| /// | ||
| /// # fn main() {} | ||
| /// ``` | ||
| #[macro_export] | ||
| macro_rules! register_validate_block { | ||
| ($block:ty, $block_executor:ty) => { | ||
| $crate::register_validate_block_impl!($block, $block_executor); | ||
| }; | ||
| } | ||
|
|
||
| /// The actual implementation of `register_validate_block` for `no_std`. | ||
| #[cfg(not(feature = "std"))] | ||
| #[doc(hidden)] | ||
| #[macro_export] | ||
| macro_rules! register_validate_block_impl { | ||
| ($block:ty, $block_executor:ty) => { | ||
| #[doc(hidden)] | ||
| mod parachain_validate_block { | ||
| use super::*; | ||
|
|
||
| #[no_mangle] | ||
| unsafe fn validate_block(block: *const u8, block_len: u64, prev_head: *const u8, prev_head_len: u64) { | ||
| let block = $crate::slice::from_raw_parts(block, block_len as usize); | ||
| let prev_head = $crate::slice::from_raw_parts(prev_head, prev_head_len as usize); | ||
|
|
||
| $crate::validate_block::validate_block::<$block, $block_executor>(block, prev_head); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /// The actual implementation of `register_validate_block` for `std`. | ||
| #[cfg(feature = "std")] | ||
| #[doc(hidden)] | ||
| #[macro_export] | ||
| macro_rules! register_validate_block_impl { | ||
| ($block:ty, $block_executor:ty) => {}; | ||
| } | ||
|
|
||
| /// Validate a given parachain block on a validator. | ||
| #[cfg(not(feature = "std"))] | ||
| #[doc(hidden)] | ||
| pub fn validate_block<Block: BlockT, E: ExecuteBlock<Block>>(mut block: &[u8], mut prev_head: &[u8]) { | ||
| use codec::Decode; | ||
|
|
||
| let block = crate::ParachainBlock::<Block>::decode(&mut block).expect("Could not decode parachain block."); | ||
| let parent_header = <<Block as BlockT>::Header as Decode>::decode(&mut prev_head).expect("Could not decode parent header."); | ||
|
|
||
| let _guard = unsafe { | ||
| use storage_functions as storage; | ||
| storage::STORAGE = Some(block.witness_data); | ||
| ( | ||
| // Replace storage calls with our own implementations | ||
| rio::ext_get_allocated_storage.replace_implementation(storage::ext_get_allocated_storage), | ||
| rio::ext_get_storage_into.replace_implementation(storage::ext_get_storage_into), | ||
| rio::ext_set_storage.replace_implementation(storage::ext_set_storage), | ||
| rio::ext_exists_storage.replace_implementation(storage::ext_exists_storage), | ||
| rio::ext_clear_storage.replace_implementation(storage::ext_clear_storage), | ||
| ) | ||
| }; | ||
|
|
||
| let block_number = *parent_header.number() + One::one(); | ||
| E::execute_extrinsics_without_checks(block_number, block.extrinsics); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
|
|
||
| // Substrate is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Substrate is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! All storage functions that are replaced by `validate_block` in the Substrate runtime. | ||
|
|
||
| use crate::WitnessData; | ||
| use rstd::{slice, ptr, cmp}; | ||
|
|
||
| pub static mut STORAGE: Option<WitnessData> = None; | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const STORAGE_SET_EXPECT: &str = "`STORAGE` needs to be set before calling this function."; | ||
|
|
||
| pub unsafe fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8 { | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let key = slice::from_raw_parts(key_data, key_len as usize); | ||
| match STORAGE.as_mut().expect(STORAGE_SET_EXPECT).get_mut(key) { | ||
| Some(value) => { | ||
| *written_out = value.len() as u32; | ||
| value.as_mut_ptr() | ||
| }, | ||
| None => { | ||
| *written_out = u32::max_value(); | ||
| ptr::null_mut() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub unsafe fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32) { | ||
| let key = slice::from_raw_parts(key_data, key_len as usize); | ||
| let value = slice::from_raw_parts(value_data, value_len as usize); | ||
|
|
||
| STORAGE.as_mut().map(|s| { | ||
| s.insert(key.to_vec(), value.to_vec()); | ||
bkchr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
|
|
||
| pub unsafe fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32, value_offset: u32) -> u32 { | ||
| let key = slice::from_raw_parts(key_data, key_len as usize); | ||
| let out_value = slice::from_raw_parts_mut(value_data, value_len as usize); | ||
|
|
||
| match STORAGE.as_mut().expect(STORAGE_SET_EXPECT).get_mut(key) { | ||
| Some(value) => { | ||
| let value = &value[value_offset as usize..]; | ||
| let len = cmp::min(value_len as usize, value.len()); | ||
| out_value[..len].copy_from_slice(&value[..len]); | ||
| len as u32 | ||
| }, | ||
| None => { | ||
| u32::max_value() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub unsafe fn ext_exists_storage(key_data: *const u8, key_len: u32) -> u32 { | ||
| let key = slice::from_raw_parts(key_data, key_len as usize); | ||
|
|
||
| if STORAGE.as_mut().expect(STORAGE_SET_EXPECT).contains_key(key) { | ||
| 1 | ||
| } else { | ||
| 0 | ||
| } | ||
| } | ||
|
|
||
| pub unsafe fn ext_clear_storage(key_data: *const u8, key_len: u32) { | ||
| let key = slice::from_raw_parts(key_data, key_len as usize); | ||
|
|
||
| STORAGE.as_mut().expect(STORAGE_SET_EXPECT).remove(key); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.