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
Implement Collator #11
Merged
+2,143
−1,275
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5ea520e
Adds the collator implementation
bkchr 014a995
Adds missing build.rs
bkchr 9bef7a9
Fix compilation
bkchr ce06477
Some updates
bkchr bc4499b
Try to fix the build
bkchr 6005865
Make everything compile again
bkchr 7d1c5ab
Enable feature
bkchr 5021209
Adds very simple test for the Collator
bkchr a688806
Merge remote-tracking branch 'origin/master' into bkchr-collator
bkchr b136067
Use locked
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "cumulus-collator" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| # Substrate dependencies | ||
| runtime-primitives = { package = "sr-primitives", git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch" } | ||
| consensus-common = { package = "substrate-consensus-common", git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch" } | ||
| inherents = { package = "substrate-inherents", git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch" } | ||
|
|
||
| # Polkadot dependencies | ||
| polkadot-collator = { git = "https://github.com/paritytech/polkadot", branch = "bkchr-cumulus-branch" } | ||
| polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "bkchr-cumulus-branch" } | ||
|
|
||
| # other deps | ||
| log = "0.4.6" | ||
| codec = { package = "parity-codec", version = "3.5.1", features = [ "derive" ] } | ||
| futures = "0.1.27" |
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,126 @@ | ||
| // 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/>. | ||
|
|
||
| //! Cumulus Collator implementation for Substrate. | ||
|
|
||
| use runtime_primitives::traits::Block as BlockT; | ||
| use consensus_common::{Environment, Proposer}; | ||
|
|
||
| use polkadot_collator::{InvalidHead, ParachainContext}; | ||
| use polkadot_primitives::parachain::{self, BlockData, Message, Id as ParaId, Extrinsic}; | ||
|
|
||
| use codec::{Decode, Encode}; | ||
| use log::error; | ||
| use futures::{Future, future::IntoFuture}; | ||
|
|
||
| use std::{sync::Arc, marker::PhantomData, time::Duration}; | ||
|
|
||
| /// The head data of the parachain, stored in the relay chain. | ||
| #[derive(Decode, Encode, Debug)] | ||
| struct HeadData<Block: BlockT> { | ||
| header: Block::Header, | ||
| } | ||
|
|
||
| /// The implementation of the Cumulus `Collator`. | ||
| pub struct Collator<Block, PF> { | ||
| proposer_factory: Arc<PF>, | ||
| _phantom: PhantomData<Block>, | ||
| inherent_data_providers: inherents::InherentDataProviders, | ||
| } | ||
|
|
||
| impl<Block: BlockT, PF: Environment<Block>> Collator<Block, PF> { | ||
| /// Create a new instance. | ||
| fn new( | ||
| proposer_factory: Arc<PF>, | ||
| inherent_data_providers: inherent_data::InherentDataProviders | ||
| ) -> Self { | ||
| Self { | ||
| proposer_factory, | ||
| inherent_data_providers, | ||
| _phantom: PhantomData, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<Block, PF> Clone for Collator<Block, PF> { | ||
| fn clone(&self) -> Self { | ||
| Self { | ||
| proposer_factory: self.proposer_factory.clone(), | ||
| inherent_data_providers: self.inherent_data_providers.clone(), | ||
| _phantom: PhantomData, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<Block, PF> ParachainContext for Collator<Block, PF> | ||
| where | ||
| Block: BlockT, | ||
| PF: Environment<Block> + 'static, | ||
| PF::Error: std::fmt::Debug, | ||
| { | ||
| type ProduceCandidate = Box< | ||
| dyn Future<Item=(BlockData, parachain::HeadData, Extrinsic), Error=InvalidHead> | ||
| >; | ||
|
|
||
| fn produce_candidate<I: IntoIterator<Item=(ParaId, Message)>>( | ||
| &self, | ||
| last_head: parachain::HeadData, | ||
| _: I, | ||
| ) -> Self::ProduceCandidate { | ||
| let factory = self.proposer_factory.clone(); | ||
| let inherent_providers = self.inherent_data_providers.clone(); | ||
|
|
||
| let res = HeadData::<Block>::decode(&mut &last_head.0[..]) | ||
| .ok_or_else(|| InvalidHead).into_future() | ||
| .and_then(move |last_head| | ||
| factory.init(&last_head.header).map_err(|e| { | ||
| //TODO: Do we want to return the real error? | ||
| error!("Could not create proposer: {:?}", e); | ||
| InvalidHead | ||
| }) | ||
| ) | ||
| .and_then(move |proposer| | ||
| inherent_providers.create_inherent_data() | ||
| .map(|id| (proposer, id)) | ||
| .map_err(|e| { | ||
| error!("Failed to create inherent data: {:?}", e); | ||
| InvalidHead | ||
| }) | ||
| ) | ||
| .and_then(|(proposer, inherent_data)| { | ||
| proposer.propose( | ||
| inherent_data, | ||
| Default::default(), | ||
| //TODO: Fix this. | ||
| Duration::from_secs(6), | ||
| ) | ||
| .into_future() | ||
| .map_err(|e| { | ||
| error!("Proposing failed: {:?}", e); | ||
| InvalidHead | ||
| }) | ||
| }) | ||
| .map(|b| { | ||
| let block_data = BlockData(b.encode()); | ||
| let head_data = HeadData::<Block> { header: b.deconstruct().0 }; | ||
| let extrinsic = Extrinsic { outgoing_messages: Vec::new() }; | ||
|
|
||
| (block_data, parachain::HeadData(head_data.encode()), extrinsic) | ||
| }); | ||
|
|
||
| Box::new(res) | ||
| } | ||
| } | ||
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,27 @@ | ||
| // 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 Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use wasm_builder_runner::{build_current_project, WasmBuilderSource}; | ||
|
|
||
| fn main() { | ||
| build_current_project( | ||
| "wasm_binary.rs", | ||
| WasmBuilderSource::Git { | ||
| repo: "https://github.com/paritytech/substrate", | ||
| rev: "c7fa536d85df5d6a0fc5cdc3f82b6e5a1a2db640", | ||
| } | ||
| ); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what to do here, but it is worth noting that
polkadot-collatortimes out collation futures that take longer than a given duration. Maybe we can pass that timeout along toProduceCandidatesomehow.