|
17 | 17 | use std::{marker::PhantomData, sync::Arc};
|
18 | 18 |
|
19 | 19 | use sc_consensus::{
|
20 |
| - import_queue::{BasicQueue, Verifier as VerifierT}, |
21 |
| - BlockImport, BlockImportParams, |
| 20 | + import_queue::{BasicQueue, Verifier as VerifierT}, |
| 21 | + BlockImport, BlockImportParams, |
22 | 22 | };
|
23 | 23 | use sp_api::ProvideRuntimeApi;
|
24 | 24 | use sp_block_builder::BlockBuilder as BlockBuilderApi;
|
25 | 25 | use sp_blockchain::Result as ClientResult;
|
26 | 26 | use sp_consensus::{error::Error as ConsensusError, CacheKeyId};
|
27 | 27 | use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
|
28 | 28 | use sp_runtime::{
|
29 |
| - generic::BlockId, |
30 |
| - traits::{Block as BlockT, Header as HeaderT}, |
| 29 | + generic::BlockId, |
| 30 | + traits::{Block as BlockT, Header as HeaderT}, |
31 | 31 | };
|
32 | 32 |
|
33 | 33 | /// A verifier that just checks the inherents.
|
34 | 34 | pub struct Verifier<Client, Block, CIDP> {
|
35 |
| - client: Arc<Client>, |
36 |
| - create_inherent_data_providers: CIDP, |
37 |
| - _marker: PhantomData<Block>, |
| 35 | + client: Arc<Client>, |
| 36 | + create_inherent_data_providers: CIDP, |
| 37 | + _marker: PhantomData<Block>, |
38 | 38 | }
|
39 | 39 |
|
40 | 40 | impl<Client, Block, CIDP> Verifier<Client, Block, CIDP> {
|
41 |
| - /// Create a new instance. |
42 |
| - pub fn new(client: Arc<Client>, create_inherent_data_providers: CIDP) -> Self { |
43 |
| - Self { |
44 |
| - client, |
45 |
| - create_inherent_data_providers, |
46 |
| - _marker: PhantomData, |
47 |
| - } |
48 |
| - } |
| 41 | + /// Create a new instance. |
| 42 | + pub fn new(client: Arc<Client>, create_inherent_data_providers: CIDP) -> Self { |
| 43 | + Self { client, create_inherent_data_providers, _marker: PhantomData } |
| 44 | + } |
49 | 45 | }
|
50 | 46 |
|
51 | 47 | #[async_trait::async_trait]
|
52 | 48 | impl<Client, Block, CIDP> VerifierT<Block> for Verifier<Client, Block, CIDP>
|
53 | 49 | where
|
54 |
| - Block: BlockT, |
55 |
| - Client: ProvideRuntimeApi<Block> + Send + Sync, |
56 |
| - <Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>, |
57 |
| - CIDP: CreateInherentDataProviders<Block, ()>, |
| 50 | + Block: BlockT, |
| 51 | + Client: ProvideRuntimeApi<Block> + Send + Sync, |
| 52 | + <Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>, |
| 53 | + CIDP: CreateInherentDataProviders<Block, ()>, |
58 | 54 | {
|
59 |
| - async fn verify( |
60 |
| - &mut self, |
61 |
| - mut block_params: BlockImportParams<Block, ()>, |
62 |
| - ) -> Result< |
63 |
| - ( |
64 |
| - BlockImportParams<Block, ()>, |
65 |
| - Option<Vec<(CacheKeyId, Vec<u8>)>>, |
66 |
| - ), |
67 |
| - String, |
68 |
| - > { |
69 |
| - if let Some(inner_body) = block_params.body.take() { |
70 |
| - let inherent_data_providers = self |
71 |
| - .create_inherent_data_providers |
72 |
| - .create_inherent_data_providers(*block_params.header.parent_hash(), ()) |
73 |
| - .await |
74 |
| - .map_err(|e| e.to_string())?; |
75 |
| - |
76 |
| - let inherent_data = inherent_data_providers |
77 |
| - .create_inherent_data() |
78 |
| - .map_err(|e| e.to_string())?; |
79 |
| - |
80 |
| - let block = Block::new(block_params.header.clone(), inner_body); |
81 |
| - |
82 |
| - let inherent_res = self |
83 |
| - .client |
84 |
| - .runtime_api() |
85 |
| - .check_inherents( |
86 |
| - &BlockId::Hash(*block.header().parent_hash()), |
87 |
| - block.clone(), |
88 |
| - inherent_data, |
89 |
| - ) |
90 |
| - .map_err(|e| e.to_string())?; |
91 |
| - |
92 |
| - if !inherent_res.ok() { |
93 |
| - for (i, e) in inherent_res.into_errors() { |
94 |
| - match inherent_data_providers.try_handle_error(&i, &e).await { |
95 |
| - Some(r) => r.map_err(|e| e.to_string())?, |
96 |
| - None => Err(format!( |
97 |
| - "Unhandled inherent error from `{}`.", |
98 |
| - String::from_utf8_lossy(&i) |
99 |
| - ))?, |
100 |
| - } |
101 |
| - } |
102 |
| - } |
103 |
| - |
104 |
| - let (_, inner_body) = block.deconstruct(); |
105 |
| - block_params.body = Some(inner_body); |
106 |
| - } |
107 |
| - |
108 |
| - block_params.post_hash = Some(block_params.header.hash()); |
109 |
| - |
110 |
| - Ok((block_params, None)) |
111 |
| - } |
| 55 | + async fn verify( |
| 56 | + &mut self, |
| 57 | + mut block_params: BlockImportParams<Block, ()>, |
| 58 | + ) -> Result<(BlockImportParams<Block, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> { |
| 59 | + if let Some(inner_body) = block_params.body.take() { |
| 60 | + let inherent_data_providers = self |
| 61 | + .create_inherent_data_providers |
| 62 | + .create_inherent_data_providers(*block_params.header.parent_hash(), ()) |
| 63 | + .await |
| 64 | + .map_err(|e| e.to_string())?; |
| 65 | + |
| 66 | + let inherent_data = |
| 67 | + inherent_data_providers.create_inherent_data().map_err(|e| e.to_string())?; |
| 68 | + |
| 69 | + let block = Block::new(block_params.header.clone(), inner_body); |
| 70 | + |
| 71 | + let inherent_res = self |
| 72 | + .client |
| 73 | + .runtime_api() |
| 74 | + .check_inherents( |
| 75 | + &BlockId::Hash(*block.header().parent_hash()), |
| 76 | + block.clone(), |
| 77 | + inherent_data, |
| 78 | + ) |
| 79 | + .map_err(|e| e.to_string())?; |
| 80 | + |
| 81 | + if !inherent_res.ok() { |
| 82 | + for (i, e) in inherent_res.into_errors() { |
| 83 | + match inherent_data_providers.try_handle_error(&i, &e).await { |
| 84 | + Some(r) => r.map_err(|e| e.to_string())?, |
| 85 | + None => Err(format!( |
| 86 | + "Unhandled inherent error from `{}`.", |
| 87 | + String::from_utf8_lossy(&i) |
| 88 | + ))?, |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + let (_, inner_body) = block.deconstruct(); |
| 94 | + block_params.body = Some(inner_body); |
| 95 | + } |
| 96 | + |
| 97 | + block_params.post_hash = Some(block_params.header.hash()); |
| 98 | + |
| 99 | + Ok((block_params, None)) |
| 100 | + } |
112 | 101 | }
|
113 | 102 |
|
114 | 103 | /// Start an import queue for a Cumulus collator that does not uses any special authoring logic.
|
115 | 104 | pub fn import_queue<Client, Block: BlockT, I, CIDP>(
|
116 |
| - client: Arc<Client>, |
117 |
| - block_import: I, |
118 |
| - create_inherent_data_providers: CIDP, |
119 |
| - spawner: &impl sp_core::traits::SpawnEssentialNamed, |
120 |
| - registry: Option<&substrate_prometheus_endpoint::Registry>, |
| 105 | + client: Arc<Client>, |
| 106 | + block_import: I, |
| 107 | + create_inherent_data_providers: CIDP, |
| 108 | + spawner: &impl sp_core::traits::SpawnEssentialNamed, |
| 109 | + registry: Option<&substrate_prometheus_endpoint::Registry>, |
121 | 110 | ) -> ClientResult<BasicQueue<Block, I::Transaction>>
|
122 | 111 | where
|
123 |
| - I: BlockImport<Block, Error = ConsensusError> + Send + Sync + 'static, |
124 |
| - I::Transaction: Send, |
125 |
| - Client: ProvideRuntimeApi<Block> + Send + Sync + 'static, |
126 |
| - <Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>, |
127 |
| - CIDP: CreateInherentDataProviders<Block, ()> + 'static, |
| 112 | + I: BlockImport<Block, Error = ConsensusError> + Send + Sync + 'static, |
| 113 | + I::Transaction: Send, |
| 114 | + Client: ProvideRuntimeApi<Block> + Send + Sync + 'static, |
| 115 | + <Client as ProvideRuntimeApi<Block>>::Api: BlockBuilderApi<Block>, |
| 116 | + CIDP: CreateInherentDataProviders<Block, ()> + 'static, |
128 | 117 | {
|
129 |
| - let verifier = Verifier::new(client, create_inherent_data_providers); |
130 |
| - |
131 |
| - Ok(BasicQueue::new( |
132 |
| - verifier, |
133 |
| - Box::new(cumulus_client_consensus_common::ParachainBlockImport::new( |
134 |
| - block_import, |
135 |
| - )), |
136 |
| - None, |
137 |
| - spawner, |
138 |
| - registry, |
139 |
| - )) |
| 118 | + let verifier = Verifier::new(client, create_inherent_data_providers); |
| 119 | + |
| 120 | + Ok(BasicQueue::new( |
| 121 | + verifier, |
| 122 | + Box::new(cumulus_client_consensus_common::ParachainBlockImport::new(block_import)), |
| 123 | + None, |
| 124 | + spawner, |
| 125 | + registry, |
| 126 | + )) |
140 | 127 | }
|
0 commit comments