Skip to content

Commit 1eba5b7

Browse files
michalkucharczykbkchr
authored andcommitted
BlockId removal: refactor: CallExecutor trait (paritytech#13173)
* BlockId removal: refactor: CallExecutor trait It changes the arguments of CallExecutor methods: - `call`, 'contextual_call', 'runtime_version', 'prove_execution' from: `BlockId<Block>` to: `Block::Hash` This PR is part of BlockId::Number refactoring analysis (paritytech/substrate#11292) * Apply suggestions from code review Co-authored-by: Bastian Köcher <git@kchr.de> * ".git/.scripts/commands/fmt/fmt.sh" Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: command-bot <>
1 parent ba960cd commit 1eba5b7

File tree

8 files changed

+55
-56
lines changed

8 files changed

+55
-56
lines changed

client/api/src/call_executor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! A method call executor interface.
2020
2121
use sc_executor::{RuntimeVersion, RuntimeVersionOf};
22-
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
22+
use sp_runtime::traits::Block as BlockT;
2323
use sp_state_machine::{ExecutionStrategy, OverlayedChanges, StorageProof};
2424
use std::cell::RefCell;
2525

@@ -54,7 +54,7 @@ pub trait CallExecutor<B: BlockT>: RuntimeVersionOf {
5454
/// No changes are made.
5555
fn call(
5656
&self,
57-
id: &BlockId<B>,
57+
at_hash: B::Hash,
5858
method: &str,
5959
call_data: &[u8],
6060
strategy: ExecutionStrategy,
@@ -67,7 +67,7 @@ pub trait CallExecutor<B: BlockT>: RuntimeVersionOf {
6767
/// of the execution context.
6868
fn contextual_call(
6969
&self,
70-
at: &BlockId<B>,
70+
at_hash: B::Hash,
7171
method: &str,
7272
call_data: &[u8],
7373
changes: &RefCell<OverlayedChanges>,
@@ -83,14 +83,14 @@ pub trait CallExecutor<B: BlockT>: RuntimeVersionOf {
8383
/// Extract RuntimeVersion of given block
8484
///
8585
/// No changes are made.
86-
fn runtime_version(&self, id: &BlockId<B>) -> Result<RuntimeVersion, sp_blockchain::Error>;
86+
fn runtime_version(&self, at_hash: B::Hash) -> Result<RuntimeVersion, sp_blockchain::Error>;
8787

8888
/// Prove the execution of the given `method`.
8989
///
9090
/// No changes are made.
9191
fn prove_execution(
9292
&self,
93-
at: &BlockId<B>,
93+
at_hash: B::Hash,
9494
method: &str,
9595
call_data: &[u8],
9696
) -> Result<(Vec<u8>, StorageProof), sp_blockchain::Error>;

client/finality-grandpa/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -464,18 +464,18 @@ pub trait GenesisAuthoritySetProvider<Block: BlockT> {
464464
fn get(&self) -> Result<AuthorityList, ClientError>;
465465
}
466466

467-
impl<Block: BlockT, E> GenesisAuthoritySetProvider<Block>
468-
for Arc<dyn ExecutorProvider<Block, Executor = E>>
467+
impl<Block: BlockT, E, Client> GenesisAuthoritySetProvider<Block> for Arc<Client>
469468
where
470469
E: CallExecutor<Block>,
470+
Client: ExecutorProvider<Block, Executor = E> + HeaderBackend<Block>,
471471
{
472472
fn get(&self) -> Result<AuthorityList, ClientError> {
473473
// This implementation uses the Grandpa runtime API instead of reading directly from the
474474
// `GRANDPA_AUTHORITIES_KEY` as the data may have been migrated since the genesis block of
475475
// the chain, whereas the runtime API is backwards compatible.
476476
self.executor()
477477
.call(
478-
&BlockId::Number(Zero::zero()),
478+
self.expect_block_hash_from_id(&BlockId::Number(Zero::zero()))?,
479479
"GrandpaApi_grandpa_authorities",
480480
&[],
481481
ExecutionStrategy::NativeElseWasm,

client/rpc-spec-v2/src/chain_head/chain_head.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ where
744744
let res = client
745745
.executor()
746746
.call(
747-
&BlockId::Hash(hash),
747+
hash,
748748
&function,
749749
&call_parameters,
750750
client.execution_extensions().strategies().other,

client/rpc/src/state/state_full.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ where
203203
self.client
204204
.executor()
205205
.call(
206-
&BlockId::Hash(block),
206+
block,
207207
&method,
208208
&call_data,
209209
self.client.execution_extensions().strategies().other,

client/service/src/client/call_executor.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,32 @@ where
8181
fn check_override<'a>(
8282
&'a self,
8383
onchain_code: RuntimeCode<'a>,
84-
id: &BlockId<Block>,
84+
hash: <Block as BlockT>::Hash,
8585
) -> sp_blockchain::Result<(RuntimeCode<'a>, RuntimeVersion)>
8686
where
8787
Block: BlockT,
8888
B: backend::Backend<Block>,
8989
{
90-
let on_chain_version = self.on_chain_runtime_version(id)?;
90+
let on_chain_version = self.on_chain_runtime_version(hash)?;
9191
let code_and_version = if let Some(d) = self.wasm_override.as_ref().as_ref().and_then(|o| {
9292
o.get(
9393
&on_chain_version.spec_version,
9494
onchain_code.heap_pages,
9595
&on_chain_version.spec_name,
9696
)
9797
}) {
98-
log::debug!(target: "wasm_overrides", "using WASM override for block {}", id);
98+
log::debug!(target: "wasm_overrides", "using WASM override for block {}", hash);
9999
d
100100
} else if let Some(s) =
101101
self.wasm_substitutes
102-
.get(on_chain_version.spec_version, onchain_code.heap_pages, id)
102+
.get(on_chain_version.spec_version, onchain_code.heap_pages, hash)
103103
{
104-
log::debug!(target: "wasm_substitutes", "Using WASM substitute for block {:?}", id);
104+
log::debug!(target: "wasm_substitutes", "Using WASM substitute for block {:?}", hash);
105105
s
106106
} else {
107107
log::debug!(
108108
target: "wasm_overrides",
109-
"Neither WASM override nor substitute available for block {id}, using onchain code",
109+
"Neither WASM override nor substitute available for block {hash}, using onchain code",
110110
);
111111
(onchain_code, on_chain_version)
112112
};
@@ -115,14 +115,10 @@ where
115115
}
116116

117117
/// Returns the on chain runtime version.
118-
fn on_chain_runtime_version(
119-
&self,
120-
id: &BlockId<Block>,
121-
) -> sp_blockchain::Result<RuntimeVersion> {
118+
fn on_chain_runtime_version(&self, hash: Block::Hash) -> sp_blockchain::Result<RuntimeVersion> {
122119
let mut overlay = OverlayedChanges::default();
123120

124-
let at_hash = self.backend.blockchain().expect_block_hash_from_id(id)?;
125-
let state = self.backend.state_at(at_hash)?;
121+
let state = self.backend.state_at(hash)?;
126122
let mut cache = StorageTransactionCache::<Block, B::State>::default();
127123
let mut ext = Ext::new(&mut overlay, &mut cache, &state, None);
128124
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
@@ -166,21 +162,21 @@ where
166162

167163
fn call(
168164
&self,
169-
at: &BlockId<Block>,
165+
at_hash: Block::Hash,
170166
method: &str,
171167
call_data: &[u8],
172168
strategy: ExecutionStrategy,
173169
) -> sp_blockchain::Result<Vec<u8>> {
174170
let mut changes = OverlayedChanges::default();
175-
let at_hash = self.backend.blockchain().expect_block_hash_from_id(at)?;
176-
let at_number = self.backend.blockchain().expect_block_number_from_id(at)?;
171+
let at_number =
172+
self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(at_hash))?;
177173
let state = self.backend.state_at(at_hash)?;
178174

179175
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
180176
let runtime_code =
181177
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
182178

183-
let runtime_code = self.check_override(runtime_code, at)?.0;
179+
let runtime_code = self.check_override(runtime_code, at_hash)?.0;
184180

185181
let extensions = self.execution_extensions.extensions(
186182
at_hash,
@@ -206,7 +202,7 @@ where
206202

207203
fn contextual_call(
208204
&self,
209-
at: &BlockId<Block>,
205+
at_hash: Block::Hash,
210206
method: &str,
211207
call_data: &[u8],
212208
changes: &RefCell<OverlayedChanges>,
@@ -216,8 +212,8 @@ where
216212
) -> Result<Vec<u8>, sp_blockchain::Error> {
217213
let mut storage_transaction_cache = storage_transaction_cache.map(|c| c.borrow_mut());
218214

219-
let at_hash = self.backend.blockchain().expect_block_hash_from_id(at)?;
220-
let at_number = self.backend.blockchain().expect_block_number_from_id(at)?;
215+
let at_number =
216+
self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(at_hash))?;
221217
let state = self.backend.state_at(at_hash)?;
222218

223219
let (execution_manager, extensions) =
@@ -232,7 +228,7 @@ where
232228

233229
let runtime_code =
234230
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
235-
let runtime_code = self.check_override(runtime_code, at)?.0;
231+
let runtime_code = self.check_override(runtime_code, at_hash)?.0;
236232

237233
match recorder {
238234
Some(recorder) => {
@@ -275,32 +271,31 @@ where
275271
.map_err(Into::into)
276272
}
277273

278-
fn runtime_version(&self, id: &BlockId<Block>) -> sp_blockchain::Result<RuntimeVersion> {
279-
let at_hash = self.backend.blockchain().expect_block_hash_from_id(id)?;
274+
fn runtime_version(&self, at_hash: Block::Hash) -> sp_blockchain::Result<RuntimeVersion> {
280275
let state = self.backend.state_at(at_hash)?;
281276
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
282277

283278
let runtime_code =
284279
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
285-
self.check_override(runtime_code, id).map(|(_, v)| v)
280+
self.check_override(runtime_code, at_hash).map(|(_, v)| v)
286281
}
287282

288283
fn prove_execution(
289284
&self,
290-
at: &BlockId<Block>,
285+
at_hash: Block::Hash,
291286
method: &str,
292287
call_data: &[u8],
293288
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)> {
294-
let at_hash = self.backend.blockchain().expect_block_hash_from_id(at)?;
295-
let at_number = self.backend.blockchain().expect_block_number_from_id(at)?;
289+
let at_number =
290+
self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(at_hash))?;
296291
let state = self.backend.state_at(at_hash)?;
297292

298293
let trie_backend = state.as_trie_backend();
299294

300295
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_backend);
301296
let runtime_code =
302297
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
303-
let runtime_code = self.check_override(runtime_code, at)?.0;
298+
let runtime_code = self.check_override(runtime_code, at_hash)?.0;
304299

305300
sp_state_machine::prove_execution_on_trie_backend(
306301
trie_backend,
@@ -340,7 +335,7 @@ where
340335
E: CodeExecutor + RuntimeVersionOf + Clone + 'static,
341336
Block: BlockT,
342337
{
343-
fn runtime_version(&self, at: &BlockId<Block>) -> Result<sp_version::RuntimeVersion, String> {
338+
fn runtime_version(&self, at: Block::Hash) -> Result<sp_version::RuntimeVersion, String> {
344339
CallExecutor::runtime_version(self, at).map_err(|e| e.to_string())
345340
}
346341
}
@@ -359,6 +354,7 @@ where
359354
#[cfg(test)]
360355
mod tests {
361356
use super::*;
357+
use backend::Backend;
362358
use sc_client_api::in_mem;
363359
use sc_executor::{NativeElseWasmExecutor, WasmExecutionMethod};
364360
use sp_core::{
@@ -432,7 +428,7 @@ mod tests {
432428
};
433429

434430
let check = call_executor
435-
.check_override(onchain_code, &BlockId::Number(Default::default()))
431+
.check_override(onchain_code, backend.blockchain().info().genesis_hash)
436432
.expect("RuntimeCode override")
437433
.0;
438434

client/service/src/client/client.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ where
493493

494494
/// Get the RuntimeVersion at a given block.
495495
pub fn runtime_version_at(&self, id: &BlockId<Block>) -> sp_blockchain::Result<RuntimeVersion> {
496-
CallExecutor::runtime_version(&self.executor, id)
496+
let hash = self.backend.blockchain().expect_block_hash_from_id(id)?;
497+
CallExecutor::runtime_version(&self.executor, hash)
497498
}
498499

499500
/// Apply a checked and validated block to an operation. If a justification is provided
@@ -1241,7 +1242,7 @@ where
12411242
method: &str,
12421243
call_data: &[u8],
12431244
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)> {
1244-
self.executor.prove_execution(&BlockId::Hash(hash), method, call_data)
1245+
self.executor.prove_execution(hash, method, call_data)
12451246
}
12461247

12471248
fn read_proof_collection(
@@ -1726,9 +1727,10 @@ where
17261727
&self,
17271728
params: CallApiAtParams<Block, B::State>,
17281729
) -> Result<Vec<u8>, sp_api::ApiError> {
1730+
let at_hash = self.expect_block_hash_from_id(params.at)?;
17291731
self.executor
17301732
.contextual_call(
1731-
params.at,
1733+
at_hash,
17321734
params.function,
17331735
&params.arguments,
17341736
params.overlayed_changes,
@@ -1740,7 +1742,8 @@ where
17401742
}
17411743

17421744
fn runtime_version_at(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, sp_api::ApiError> {
1743-
CallExecutor::runtime_version(&self.executor, at).map_err(Into::into)
1745+
let hash = self.backend.blockchain().expect_block_hash_from_id(at)?;
1746+
CallExecutor::runtime_version(&self.executor, hash).map_err(Into::into)
17441747
}
17451748

17461749
fn state_at(&self, at: &BlockId<Block>) -> Result<Self::StateBackend, sp_api::ApiError> {

client/service/src/client/wasm_substitutes.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ use sc_client_api::backend;
2222
use sc_executor::RuntimeVersionOf;
2323
use sp_blockchain::{HeaderBackend, Result};
2424
use sp_core::traits::{FetchRuntimeCode, RuntimeCode, WrappedRuntimeCode};
25-
use sp_runtime::{
26-
generic::BlockId,
27-
traits::{Block as BlockT, NumberFor},
28-
};
25+
use sp_runtime::traits::{Block as BlockT, NumberFor};
2926
use sp_state_machine::BasicExternalities;
3027
use sp_version::RuntimeVersion;
3128
use std::{
@@ -54,10 +51,13 @@ impl<Block: BlockT> WasmSubstitute<Block> {
5451
RuntimeCode { code_fetcher: self, hash: self.hash.clone(), heap_pages }
5552
}
5653

57-
/// Returns `true` when the substitute matches for the given `block_id`.
58-
fn matches(&self, block_id: &BlockId<Block>, backend: &impl backend::Backend<Block>) -> bool {
59-
let requested_block_number =
60-
backend.blockchain().block_number_from_id(block_id).ok().flatten();
54+
/// Returns `true` when the substitute matches for the given `hash`.
55+
fn matches(
56+
&self,
57+
hash: <Block as BlockT>::Hash,
58+
backend: &impl backend::Backend<Block>,
59+
) -> bool {
60+
let requested_block_number = backend.blockchain().number(hash).ok().flatten();
6161

6262
Some(self.block_number) <= requested_block_number
6363
}
@@ -147,10 +147,10 @@ where
147147
&self,
148148
spec: u32,
149149
pages: Option<u64>,
150-
block_id: &BlockId<Block>,
150+
hash: Block::Hash,
151151
) -> Option<(RuntimeCode<'_>, RuntimeVersion)> {
152152
let s = self.substitutes.get(&spec)?;
153-
s.matches(block_id, &*self.backend)
153+
s.matches(hash, &*self.backend)
154154
.then(|| (s.runtime_code(pages), s.version.clone()))
155155
}
156156

primitives/version/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub use sp_runtime::{create_runtime_str, StateVersion};
4848
pub use sp_std;
4949

5050
#[cfg(feature = "std")]
51-
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
51+
use sp_runtime::traits::Block as BlockT;
5252

5353
#[cfg(feature = "std")]
5454
pub mod embed;
@@ -370,14 +370,14 @@ pub trait GetNativeVersion {
370370
#[cfg(feature = "std")]
371371
pub trait GetRuntimeVersionAt<Block: BlockT> {
372372
/// Returns the version of runtime at the given block.
373-
fn runtime_version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, String>;
373+
fn runtime_version(&self, at: <Block as BlockT>::Hash) -> Result<RuntimeVersion, String>;
374374
}
375375

376376
#[cfg(feature = "std")]
377377
impl<T: GetRuntimeVersionAt<Block>, Block: BlockT> GetRuntimeVersionAt<Block>
378378
for std::sync::Arc<T>
379379
{
380-
fn runtime_version(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, String> {
380+
fn runtime_version(&self, at: <Block as BlockT>::Hash) -> Result<RuntimeVersion, String> {
381381
(&**self).runtime_version(at)
382382
}
383383
}

0 commit comments

Comments
 (0)