Improve state related logs to use a more uniform format (#9452)

* Improve `state` related logs to use a more uniform format

The logging before wasn't that uniform and not that great to read/parse.
Now we are using a uniform format for all the logs. Besides these
changes, there are some minor changes around the code that calls the
state machine.

* Make CI happy

* Use HexDisplay for `ext_id`
This commit is contained in:
Bastian Köcher
2021-07-29 11:43:03 +02:00
committed by GitHub
parent 76611ba6a3
commit f07f69301a
9 changed files with 250 additions and 224 deletions
@@ -18,7 +18,7 @@
use super::{client::ClientConfig, wasm_override::WasmOverride, wasm_substitutes::WasmSubstitutes};
use codec::{Decode, Encode};
use sc_client_api::{backend, call_executor::CallExecutor};
use sc_client_api::{backend, call_executor::CallExecutor, HeaderBackend};
use sc_executor::{NativeVersion, RuntimeInfo, RuntimeVersion};
use sp_api::{ProofRecorder, StorageTransactionCache};
use sp_core::{
@@ -28,7 +28,7 @@ use sp_core::{
use sp_externalities::Extensions;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, HashFor, NumberFor},
traits::{Block as BlockT, NumberFor},
};
use sp_state_machine::{
self, backend::Backend as _, ExecutionManager, ExecutionStrategy, Ext, OverlayedChanges,
@@ -146,7 +146,7 @@ where
fn call(
&self,
id: &BlockId<Block>,
at: &BlockId<Block>,
method: &str,
call_data: &[u8],
strategy: ExecutionStrategy,
@@ -154,12 +154,17 @@ where
) -> sp_blockchain::Result<Vec<u8>> {
let mut changes = OverlayedChanges::default();
let changes_trie =
backend::changes_tries_state_at_block(id, self.backend.changes_trie_storage())?;
let state = self.backend.state_at(*id)?;
backend::changes_tries_state_at_block(at, self.backend.changes_trie_storage())?;
let state = self.backend.state_at(*at)?;
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
let runtime_code = self.check_override(runtime_code, id)?;
let runtime_code = self.check_override(runtime_code, at)?;
let at_hash = self.backend.blockchain().block_hash_from_id(at)?.ok_or_else(|| {
sp_blockchain::Error::UnknownBlock(format!("Could not find block hash for {:?}", at))
})?;
let return_data = StateMachine::new(
&state,
@@ -172,6 +177,7 @@ where
&runtime_code,
self.spawn_handle.clone(),
)
.set_parent_hash(at_hash)
.execute_using_consensus_failure_handler::<_, NeverNativeValue, fn() -> _>(
strategy.get_manager(),
None,
@@ -210,6 +216,10 @@ where
let changes = &mut *changes.borrow_mut();
let at_hash = self.backend.blockchain().block_hash_from_id(at)?.ok_or_else(|| {
sp_blockchain::Error::UnknownBlock(format!("Could not find block hash for {:?}", at))
})?;
match recorder {
Some(recorder) => {
let trie_state = state.as_trie_backend().ok_or_else(|| {
@@ -240,7 +250,8 @@ where
extensions.unwrap_or_default(),
&runtime_code,
self.spawn_handle.clone(),
);
)
.set_parent_hash(at_hash);
// TODO: https://github.com/paritytech/substrate/issues/4455
// .with_storage_transaction_cache(storage_transaction_cache.as_mut().map(|c| &mut **c))
state_machine.execute_using_consensus_failure_handler(
@@ -267,7 +278,8 @@ where
)
.with_storage_transaction_cache(
storage_transaction_cache.as_mut().map(|c| &mut **c),
);
)
.set_parent_hash(at_hash);
state_machine.execute_using_consensus_failure_handler(
execution_manager,
native_call.map(|n| || (n)().map_err(|e| Box::new(e) as Box<_>)),
@@ -292,19 +304,27 @@ where
.map_err(|e| sp_blockchain::Error::VersionInvalid(format!("{:?}", e)).into())
}
fn prove_at_trie_state<S: sp_state_machine::TrieBackendStorage<HashFor<Block>>>(
fn prove_execution(
&self,
trie_state: &sp_state_machine::TrieBackend<S, HashFor<Block>>,
overlay: &mut OverlayedChanges,
at: &BlockId<Block>,
method: &str,
call_data: &[u8],
) -> Result<(Vec<u8>, StorageProof), sp_blockchain::Error> {
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_state);
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)> {
let mut state = self.backend.state_at(*at)?;
let trie_backend = state.as_trie_backend().ok_or_else(|| {
Box::new(sp_state_machine::ExecutionError::UnableToGenerateProof)
as Box<dyn sp_state_machine::Error>
})?;
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_backend);
let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
let runtime_code = self.check_override(runtime_code, at)?;
sp_state_machine::prove_execution_on_trie_backend::<_, _, NumberFor<Block>, _, _>(
trie_state,
overlay,
&trie_backend,
&mut Default::default(),
&self.executor,
self.spawn_handle.clone(),
method,
@@ -46,7 +46,7 @@ use sc_client_api::{
CallExecutor, ExecutorProvider, KeyIterator, ProofProvider, UsageProvider,
};
use sc_executor::RuntimeVersion;
use sc_light::{call_executor::prove_execution, fetcher::ChangesProof};
use sc_light::fetcher::ChangesProof;
use sc_telemetry::{telemetry, TelemetryHandle, SUBSTRATE_INFO};
use sp_api::{
ApiExt, ApiRef, CallApiAt, CallApiAtParams, ConstructRuntimeApi, Core as CoreApi,
@@ -1312,8 +1312,8 @@ where
&mut [well_known_keys::CODE, well_known_keys::HEAP_PAGES].iter().map(|v| *v),
)?;
let state = self.state_at(id)?;
prove_execution(state, &self.executor, method, call_data)
self.executor
.prove_execution(id, method, call_data)
.map(|(r, p)| (r, StorageProof::merge(vec![p, code_proof])))
}