-
Notifications
You must be signed in to change notification settings - Fork 1.1k
slot-based-collator: Implement dedicated block import #6481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
33d47e0
slot-based-collator: Implement dedicated block import
bkchr 649ecaf
Merge remote-tracking branch 'refs/remotes/origin/master'
bkchr 444dad2
Review comments
bkchr b7c57a1
Apply suggestions from code review
bkchr 1447ca7
Mention the issue
bkchr e0a1254
Update from bkchr running command 'prdoc --audience node_dev --bump m…
actions-user 830c2ec
Merge branch 'master' into bkchr-slot-based-block-import
bkchr 16ac24d
Merge remote-tracking branch 'origin/master' into bkchr-slot-based-bl…
bkchr c0f6b43
Merge remote-tracking branch 'refs/remotes/origin/master'
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
144 changes: 144 additions & 0 deletions
144
cumulus/client/consensus/aura/src/collators/slot_based/block_import.rs
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,144 @@ | ||
| // Copyright (C) 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/>. | ||
|
|
||
| use futures::{stream::FusedStream, StreamExt}; | ||
| use sc_consensus::{BlockImport, StateAction}; | ||
| use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; | ||
| use sp_api::{ApiExt, CallApiAt, CallContext, Core, ProvideRuntimeApi, StorageProof}; | ||
| use sp_runtime::traits::{Block as BlockT, Header as _}; | ||
| use sp_trie::proof_size_extension::ProofSizeExt; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Handle for receiving the block and the storage proof from the [`SlotBasedBlockImport`]. | ||
| /// | ||
| /// This handle should be passed to [`Params`](super::Params) or can also be dropped if the node is | ||
| /// not running as collator. | ||
| pub struct SlotBasedBlockImportHandle<Block> { | ||
| receiver: TracingUnboundedReceiver<(Block, StorageProof)>, | ||
| } | ||
|
|
||
| impl<Block> SlotBasedBlockImportHandle<Block> { | ||
| /// Returns the next item. | ||
| /// | ||
| /// The future will never return when the internal channel is closed. | ||
| pub async fn next(&mut self) -> (Block, StorageProof) { | ||
| loop { | ||
| if self.receiver.is_terminated() { | ||
| futures::pending!() | ||
| } else if let Some(res) = self.receiver.next().await { | ||
| return res | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Special block import for the slot based collator. | ||
| pub struct SlotBasedBlockImport<Block, BI, Client> { | ||
| inner: BI, | ||
| client: Arc<Client>, | ||
| sender: TracingUnboundedSender<(Block, StorageProof)>, | ||
| } | ||
|
|
||
| impl<Block, BI, Client> SlotBasedBlockImport<Block, BI, Client> { | ||
| /// Create a new instance. | ||
| /// | ||
| /// The returned [`SlotBasedBlockImportHandle`] needs to be passed to the | ||
| /// [`Params`](super::Params), so that this block import instance can communicate with the | ||
| /// collation task. If the node is not running as a collator, just dropping the handle is fine. | ||
| pub fn new(inner: BI, client: Arc<Client>) -> (Self, SlotBasedBlockImportHandle<Block>) { | ||
| let (sender, receiver) = tracing_unbounded("SlotBasedBlockImportChannel", 1000); | ||
|
|
||
| (Self { sender, client, inner }, SlotBasedBlockImportHandle { receiver }) | ||
| } | ||
| } | ||
|
|
||
| impl<Block, BI: Clone, Client> Clone for SlotBasedBlockImport<Block, BI, Client> { | ||
| fn clone(&self) -> Self { | ||
| Self { inner: self.inner.clone(), client: self.client.clone(), sender: self.sender.clone() } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl<Block, BI, Client> BlockImport<Block> for SlotBasedBlockImport<Block, BI, Client> | ||
| where | ||
| Block: BlockT, | ||
| BI: BlockImport<Block> + Send + Sync, | ||
| BI::Error: Into<sp_consensus::Error>, | ||
| Client: ProvideRuntimeApi<Block> + CallApiAt<Block> + Send + Sync, | ||
| Client::StateBackend: Send, | ||
| Client::Api: Core<Block>, | ||
| { | ||
| type Error = sp_consensus::Error; | ||
|
|
||
| async fn check_block( | ||
| &self, | ||
| block: sc_consensus::BlockCheckParams<Block>, | ||
| ) -> Result<sc_consensus::ImportResult, Self::Error> { | ||
| self.inner.check_block(block).await.map_err(Into::into) | ||
| } | ||
|
|
||
| async fn import_block( | ||
| &self, | ||
| mut params: sc_consensus::BlockImportParams<Block>, | ||
| ) -> Result<sc_consensus::ImportResult, Self::Error> { | ||
| // If the channel exists and it is required to execute the block, we will execute the block | ||
| // here. This is done to collect the storage proof and to prevent re-execution, we push | ||
| // downwards the state changes. `StateAction::ApplyChanges` is ignored, because it either | ||
| // means that the node produced the block itself or the block was imported via state sync. | ||
| if !self.sender.is_closed() && !matches!(params.state_action, StateAction::ApplyChanges(_)) | ||
| { | ||
| let mut runtime_api = self.client.runtime_api(); | ||
|
|
||
| runtime_api.set_call_context(CallContext::Onchain); | ||
|
|
||
| runtime_api.record_proof(); | ||
| let recorder = runtime_api | ||
| .proof_recorder() | ||
| .expect("Proof recording is enabled in the line above; qed."); | ||
| runtime_api.register_extension(ProofSizeExt::new(recorder)); | ||
|
|
||
| let parent_hash = *params.header.parent_hash(); | ||
|
|
||
| let block = Block::new(params.header.clone(), params.body.clone().unwrap_or_default()); | ||
|
|
||
| runtime_api | ||
| .execute_block(parent_hash, block.clone()) | ||
| .map_err(|e| Box::new(e) as Box<_>)?; | ||
|
|
||
| let storage_proof = | ||
| runtime_api.extract_proof().expect("Proof recording was enabled above; qed"); | ||
|
|
||
| let state = self.client.state_at(parent_hash).map_err(|e| Box::new(e) as Box<_>)?; | ||
| let gen_storage_changes = runtime_api | ||
| .into_storage_changes(&state, parent_hash) | ||
| .map_err(sp_consensus::Error::ChainLookup)?; | ||
|
|
||
| if params.header.state_root() != &gen_storage_changes.transaction_storage_root { | ||
| return Err(sp_consensus::Error::Other(Box::new( | ||
| sp_blockchain::Error::InvalidStateRoot, | ||
| ))) | ||
| } | ||
|
|
||
| params.state_action = StateAction::ApplyChanges(sc_consensus::StorageChanges::Changes( | ||
| gen_storage_changes, | ||
| )); | ||
|
|
||
| let _ = self.sender.unbounded_send((block, storage_proof)); | ||
| } | ||
|
|
||
| self.inner.import_block(params).await.map_err(Into::into) | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -47,6 +47,8 @@ pub struct Params<Block: BlockT, RClient, CS> { | |
| pub collator_service: CS, | ||
| /// Receiver channel for communication with the block builder task. | ||
| pub collator_receiver: TracingUnboundedReceiver<CollatorMessage<Block>>, | ||
| /// The handle from the special slot based block import. | ||
| pub block_import_handle: super::SlotBasedBlockImportHandle<Block>, | ||
| } | ||
|
|
||
| /// Asynchronously executes the collation task for a parachain. | ||
|
|
@@ -55,28 +57,49 @@ pub struct Params<Block: BlockT, RClient, CS> { | |
| /// collations to the relay chain. It listens for new best relay chain block notifications and | ||
| /// handles collator messages. If our parachain is scheduled on a core and we have a candidate, | ||
| /// the task will build a collation and send it to the relay chain. | ||
| pub async fn run_collation_task<Block, RClient, CS>(mut params: Params<Block, RClient, CS>) | ||
| where | ||
| pub async fn run_collation_task<Block, RClient, CS>( | ||
| Params { | ||
| relay_client, | ||
| collator_key, | ||
| para_id, | ||
| reinitialize, | ||
| collator_service, | ||
| mut collator_receiver, | ||
| mut block_import_handle, | ||
| }: Params<Block, RClient, CS>, | ||
| ) where | ||
| Block: BlockT, | ||
| CS: CollatorServiceInterface<Block> + Send + Sync + 'static, | ||
| RClient: RelayChainInterface + Clone + 'static, | ||
| { | ||
| let Ok(mut overseer_handle) = params.relay_client.overseer_handle() else { | ||
| let Ok(mut overseer_handle) = relay_client.overseer_handle() else { | ||
| tracing::error!(target: LOG_TARGET, "Failed to get overseer handle."); | ||
| return | ||
| }; | ||
|
|
||
| cumulus_client_collator::initialize_collator_subsystems( | ||
| &mut overseer_handle, | ||
| params.collator_key, | ||
| params.para_id, | ||
| params.reinitialize, | ||
| collator_key, | ||
| para_id, | ||
| reinitialize, | ||
| ) | ||
| .await; | ||
|
|
||
| let collator_service = params.collator_service; | ||
| while let Some(collator_message) = params.collator_receiver.next().await { | ||
| handle_collation_message(collator_message, &collator_service, &mut overseer_handle).await; | ||
| loop { | ||
| futures::select! { | ||
| collator_message = collator_receiver.next() => { | ||
| let Some(message) = collator_message else { | ||
| return; | ||
| }; | ||
|
|
||
| handle_collation_message(message, &collator_service, &mut overseer_handle).await; | ||
| }, | ||
| block_import_msg = block_import_handle.next().fuse() => { | ||
| // TODO: Implement me. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: is there some ticket number to refer here? |
||
| // Issue: https://github.com/paritytech/polkadot-sdk/issues/6495 | ||
| let _ = block_import_msg; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.