Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
BlockId removal: CallApiAt::state_at (#13394)
Browse files Browse the repository at this point in the history
* BlockId removal: CallApiAt::state_at

It changes the arguments of:
- `CallApiAt::state_at`,
- `Client::code_at`,
- `Client::state_at`

from: `BlockId<Block>` to: `Block::Hash`

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <git@kchr.de>

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: parity-processbot <>
  • Loading branch information
michalkucharczyk and bkchr authored Feb 16, 2023
1 parent b6c4ab8 commit 873fb68
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
8 changes: 3 additions & 5 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ where
}

/// Get the code at a given block.
pub fn code_at(&self, id: &BlockId<Block>) -> sp_blockchain::Result<Vec<u8>> {
let hash = self.backend.blockchain().expect_block_hash_from_id(id)?;
pub fn code_at(&self, hash: Block::Hash) -> sp_blockchain::Result<Vec<u8>> {
Ok(StorageProvider::storage(self, hash, &StorageKey(well_known_keys::CODE.to_vec()))?
.expect(
"None is returned if there's no value stored for the given key;\
Expand Down Expand Up @@ -1746,9 +1745,8 @@ where
CallExecutor::runtime_version(&self.executor, hash).map_err(Into::into)
}

fn state_at(&self, at: &BlockId<Block>) -> Result<Self::StateBackend, sp_api::ApiError> {
let hash = self.backend.blockchain().expect_block_hash_from_id(at)?;
self.state_at(hash).map_err(Into::into)
fn state_at(&self, at: Block::Hash) -> Result<Self::StateBackend, sp_api::ApiError> {
self.state_at(at).map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ pub trait CallApiAt<Block: BlockT> {
fn runtime_version_at(&self, at: &BlockId<Block>) -> Result<RuntimeVersion, ApiError>;

/// Get the state `at` the given block.
fn state_at(&self, at: &BlockId<Block>) -> Result<Self::StateBackend, ApiError>;
fn state_at(&self, at: Block::Hash) -> Result<Self::StateBackend, ApiError>;
}

/// Auxiliary wrapper that holds an api instance and binds it to the given lifetime.
Expand Down
4 changes: 2 additions & 2 deletions primitives/api/test/tests/runtime_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ fn record_proof_works() {
.set_execution_strategy(ExecutionStrategy::Both)
.build_with_longest_chain();

let block_id = BlockId::Number(client.chain_info().best_number);
let storage_root =
*futures::executor::block_on(longest_chain.best_chain()).unwrap().state_root();

let runtime_code = sp_core::traits::RuntimeCode {
code_fetcher: &sp_core::traits::WrappedRuntimeCode(
client.code_at(&block_id).unwrap().into(),
client.code_at(client.chain_info().best_hash).unwrap().into(),
),
hash: vec![1],
heap_pages: None,
Expand All @@ -167,6 +166,7 @@ fn record_proof_works() {
}
.into_signed_tx();

let block_id = BlockId::Hash(client.chain_info().best_hash);
// Build the block and record proof
let mut builder = client
.new_block_at(&block_id, Default::default(), true)
Expand Down

0 comments on commit 873fb68

Please sign in to comment.