-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Split execute_and_import_block in client into two functions #1455
Changes from 7 commits
064c15f
b22d845
9b95c51
14e5a6a
5293120
469eee2
1d174fa
d84ab70
d2ba44f
eda2567
1a989d4
e37cfc8
c345f14
fdb4263
ec29c58
664b2a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ use state_machine::{ | |
DBValue, Backend as StateBackend, CodeExecutor, ChangesTrieAnchorBlockId, | ||
ExecutionStrategy, ExecutionManager, prove_read, | ||
ChangesTrieRootsStorage, ChangesTrieStorage, | ||
key_changes, key_changes_proof, OverlayedChanges | ||
key_changes, key_changes_proof, OverlayedChanges, | ||
}; | ||
|
||
use crate::backend::{self, BlockImportOperation}; | ||
|
@@ -66,6 +66,9 @@ pub type ImportNotifications<Block> = mpsc::UnboundedReceiver<BlockImportNotific | |
/// A stream of block finality notifications. | ||
pub type FinalityNotifications<Block> = mpsc::UnboundedReceiver<FinalityNotification<Block>>; | ||
|
||
type StorageUpdate<B, Block> = <<<B as backend::Backend<Block, Blake2Hasher>>::BlockImportOperation as backend::BlockImportOperation<Block, Blake2Hasher>>::State as state_machine::Backend<Blake2Hasher>>::Transaction; | ||
type ChangesUpdate = trie::MemoryDB<Blake2Hasher>; | ||
|
||
/// Substrate Client | ||
pub struct Client<B, E, Block, RA> where Block: BlockT { | ||
backend: Arc<B>, | ||
|
@@ -588,39 +591,9 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
} | ||
|
||
let mut transaction = self.backend.begin_operation(BlockId::Hash(parent_hash))?; | ||
let (storage_update, changes_update, storage_changes) = match transaction.state()? { | ||
Some(transaction_state) => { | ||
let mut overlay = Default::default(); | ||
let r = self.executor.call_at_state( | ||
transaction_state, | ||
&mut overlay, | ||
"Core_execute_block", | ||
&<Block as BlockT>::new(import_headers.pre().clone(), body.clone().unwrap_or_default()).encode(), | ||
match (origin, self.block_execution_strategy) { | ||
(BlockOrigin::NetworkInitialSync, _) | (_, ExecutionStrategy::NativeWhenPossible) => | ||
ExecutionManager::NativeWhenPossible, | ||
(_, ExecutionStrategy::AlwaysWasm) => ExecutionManager::AlwaysWasm, | ||
_ => ExecutionManager::Both(|wasm_result, native_result| { | ||
let header = import_headers.post(); | ||
warn!("Consensus error between wasm and native block execution at block {}", hash); | ||
warn!(" Header {:?}", header); | ||
warn!(" Native result {:?}", native_result); | ||
warn!(" Wasm result {:?}", wasm_result); | ||
telemetry!("block.execute.consensus_failure"; | ||
"hash" => ?hash, | ||
"origin" => ?origin, | ||
"header" => ?header | ||
); | ||
wasm_result | ||
}), | ||
}, | ||
); | ||
let (_, storage_update, changes_update) = r?; | ||
overlay.commit_prospective(); | ||
(Some(storage_update), Some(changes_update), Some(overlay.into_committed().collect())) | ||
}, | ||
None => (None, None, None) | ||
}; | ||
|
||
// TODO: correct path logic | ||
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. Every |
||
let (storage_update,changes_update,storage_changes) = self.block_execution(&import_headers, origin, hash, body.clone(), &transaction)?; | ||
|
||
// TODO: non longest-chain rule. | ||
let is_new_best = finalized || match fork_choice { | ||
|
@@ -691,6 +664,54 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
Ok(ImportResult::Queued) | ||
} | ||
|
||
fn block_execution( | ||
&self, | ||
import_headers: &PrePostHeader<Block::Header>, | ||
origin: BlockOrigin, | ||
hash: Block::Hash, | ||
body: Option<Vec<Block::Extrinsic>>, | ||
transaction: &B::BlockImportOperation, | ||
) -> error::Result<(Option<StorageUpdate<B, Block>>, Option<Option<ChangesUpdate>>, Option<Vec<(Vec<u8>, Option<Vec<u8>>)>>)> where | ||
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. style: could split the tuple args each onto their own line with another level of indentation 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. resolved |
||
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + Clone, | ||
{ | ||
match transaction.state()? { | ||
Some(transaction_state) => { | ||
let mut overlay = Default::default(); | ||
let mut r = self.executor.call_at_state( | ||
transaction_state, | ||
&mut overlay, | ||
"Core_execute_block", | ||
&<Block as BlockT>::new(import_headers.pre().clone(), body.clone().unwrap_or_default()).encode(), | ||
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. I don't think 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. Good catch, will fix 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. resolved 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. I think we should open an issue for that. I would like to see some function |
||
match (origin, self.block_execution_strategy) { | ||
(BlockOrigin::NetworkInitialSync, _) | (_, ExecutionStrategy::NativeWhenPossible) => | ||
ExecutionManager::NativeWhenPossible, | ||
(_, ExecutionStrategy::AlwaysWasm) => ExecutionManager::AlwaysWasm, | ||
_ => ExecutionManager::Both(|wasm_result, native_result| { | ||
let header = import_headers.post(); | ||
warn!("Consensus error between wasm and native block execution at block {}", hash); | ||
warn!(" Header {:?}", header); | ||
warn!(" Native result {:?}", native_result); | ||
warn!(" Wasm result {:?}", wasm_result); | ||
telemetry!("block.execute.consensus_failure"; | ||
"hash" => ?hash, | ||
"origin" => ?origin, | ||
"header" => ?header | ||
); | ||
wasm_result | ||
}), | ||
}, | ||
); | ||
let (_, storage_update, changes_update) = r?; | ||
overlay.commit_prospective(); | ||
|
||
Ok((Some(storage_update), Some(changes_update), Some(overlay.into_committed().collect()))) | ||
}, | ||
|
||
None => Ok((None, None, None)) | ||
} | ||
|
||
} | ||
|
||
/// Finalizes all blocks up to given. If a justification is provided it is | ||
/// stored with the given finalized block (any other finalized blocks are | ||
/// left unjustified). | ||
|
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.