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

BlockId removal: refactor: ProofProvider #12519

Merged
merged 5 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions client/api/src/proof_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

//! Proof utilities
use crate::{CompactProof, StorageProof};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_runtime::traits::Block as BlockT;
use sp_state_machine::{KeyValueStates, KeyValueStorageLevel};
use sp_storage::ChildInfo;

Expand All @@ -27,15 +27,15 @@ pub trait ProofProvider<Block: BlockT> {
/// Reads storage value at a given block + key, returning read proof.
fn read_proof(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
keys: &mut dyn Iterator<Item = &[u8]>,
) -> sp_blockchain::Result<StorageProof>;

/// Reads child storage value at a given block + storage_key + key, returning
/// read proof.
fn read_child_proof(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
child_info: &ChildInfo,
keys: &mut dyn Iterator<Item = &[u8]>,
) -> sp_blockchain::Result<StorageProof>;
Expand All @@ -46,12 +46,12 @@ pub trait ProofProvider<Block: BlockT> {
/// No changes are made.
fn execution_proof(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
method: &str,
call_data: &[u8],
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)>;

/// Given a `BlockId` iterate over all storage values starting at `start_keys`.
/// Given a `Hash` iterate over all storage values starting at `start_keys`.
/// Last `start_keys` element contains last accessed key value.
/// With multiple `start_keys`, first `start_keys` element is
/// the current storage key of of the last accessed child trie.
Expand All @@ -61,12 +61,12 @@ pub trait ProofProvider<Block: BlockT> {
/// Returns combined proof and the numbers of collected keys.
fn read_proof_collection(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
start_keys: &[Vec<u8>],
size_limit: usize,
) -> sp_blockchain::Result<(CompactProof, u32)>;

/// Given a `BlockId` iterate over all storage values starting at `start_key`.
/// Given a `Hash` iterate over all storage values starting at `start_key`.
/// Returns collected keys and values.
/// Returns the collected keys values content of the top trie followed by the
/// collected keys values of child tries.
Expand All @@ -76,7 +76,7 @@ pub trait ProofProvider<Block: BlockT> {
/// end.
fn storage_collection(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
start_key: &[Vec<u8>],
size_limit: usize,
) -> sp_blockchain::Result<Vec<(KeyValueStorageLevel, bool)>>;
Expand Down
76 changes: 35 additions & 41 deletions client/network/light/src/light_client_requests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use sp_core::{
hexdisplay::HexDisplay,
storage::{ChildInfo, ChildType, PrefixedStorageKey},
};
use sp_runtime::{generic::BlockId, traits::Block};
use sp_runtime::traits::Block;
use std::{marker::PhantomData, sync::Arc};

const LOG_TARGET: &str = "light-client-request-handler";
Expand Down Expand Up @@ -172,26 +172,22 @@ where

let block = Decode::decode(&mut request.block.as_ref())?;

let response =
match self
.client
.execution_proof(&BlockId::Hash(block), &request.method, &request.data)
{
Ok((_, proof)) => {
let r = schema::v1::light::RemoteCallResponse { proof: proof.encode() };
Some(schema::v1::light::response::Response::RemoteCallResponse(r))
},
Err(e) => {
trace!(
"remote call request from {} ({} at {:?}) failed with: {}",
peer,
request.method,
request.block,
e,
);
None
},
};
let response = match self.client.execution_proof(&block, &request.method, &request.data) {
Ok((_, proof)) => {
let r = schema::v1::light::RemoteCallResponse { proof: proof.encode() };
Some(schema::v1::light::response::Response::RemoteCallResponse(r))
},
Err(e) => {
trace!(
"remote call request from {} ({} at {:?}) failed with: {}",
peer,
request.method,
request.block,
e,
);
None
},
};

Ok(schema::v1::light::Response { response })
}
Expand All @@ -215,25 +211,23 @@ where

let block = Decode::decode(&mut request.block.as_ref())?;

let response = match self
.client
.read_proof(&BlockId::Hash(block), &mut request.keys.iter().map(AsRef::as_ref))
{
Ok(proof) => {
let r = schema::v1::light::RemoteReadResponse { proof: proof.encode() };
Some(schema::v1::light::response::Response::RemoteReadResponse(r))
},
Err(error) => {
trace!(
"remote read request from {} ({} at {:?}) failed with: {}",
peer,
fmt_keys(request.keys.first(), request.keys.last()),
request.block,
error,
);
None
},
};
let response =
match self.client.read_proof(&block, &mut request.keys.iter().map(AsRef::as_ref)) {
Ok(proof) => {
let r = schema::v1::light::RemoteReadResponse { proof: proof.encode() };
Some(schema::v1::light::response::Response::RemoteReadResponse(r))
},
Err(error) => {
trace!(
"remote read request from {} ({} at {:?}) failed with: {}",
peer,
fmt_keys(request.keys.first(), request.keys.last()),
request.block,
error,
);
None
},
};

Ok(schema::v1::light::Response { response })
}
Expand Down Expand Up @@ -265,7 +259,7 @@ where
};
let response = match child_info.and_then(|child_info| {
self.client.read_child_proof(
&BlockId::Hash(block),
&block,
&child_info,
&mut request.keys.iter().map(AsRef::as_ref),
)
Expand Down
6 changes: 3 additions & 3 deletions client/network/sync/src/state_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use sc_network_common::{
config::ProtocolId,
request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig},
};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_runtime::traits::Block as BlockT;
use std::{
hash::{Hash, Hasher},
sync::Arc,
Expand Down Expand Up @@ -205,14 +205,14 @@ where

if !request.no_proof {
let (proof, _count) = self.client.read_proof_collection(
&BlockId::hash(block),
&block,
request.start.as_slice(),
MAX_RESPONSE_BYTES,
)?;
response.proof = proof.encode();
} else {
let entries = self.client.storage_collection(
&BlockId::hash(block),
&block,
request.start.as_slice(),
MAX_RESPONSE_BYTES,
)?;
Expand Down
4 changes: 2 additions & 2 deletions client/rpc/src/state/state_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ where
self.block_or_best(block)
.and_then(|block| {
self.client
.read_proof(&BlockId::Hash(block), &mut keys.iter().map(|key| key.0.as_ref()))
.read_proof(&block, &mut keys.iter().map(|key| key.0.as_ref()))
.map(|proof| proof.iter_nodes().map(|node| node.into()).collect())
.map(|proof| ReadProof { at: block, proof })
})
Expand Down Expand Up @@ -494,7 +494,7 @@ where
};
self.client
.read_child_proof(
&BlockId::Hash(block),
&block,
&child_info,
&mut keys.iter().map(|key| key.0.as_ref()),
)
Expand Down
22 changes: 9 additions & 13 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,42 +1152,39 @@ where
{
fn read_proof(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
keys: &mut dyn Iterator<Item = &[u8]>,
) -> sp_blockchain::Result<StorageProof> {
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
self.state_at(&hash)
self.state_at(hash)
.and_then(|state| prove_read(state, keys).map_err(Into::into))
}

fn read_child_proof(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
child_info: &ChildInfo,
keys: &mut dyn Iterator<Item = &[u8]>,
) -> sp_blockchain::Result<StorageProof> {
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
self.state_at(&hash)
self.state_at(hash)
.and_then(|state| prove_child_read(state, child_info, keys).map_err(Into::into))
}

fn execution_proof(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
method: &str,
call_data: &[u8],
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)> {
self.executor.prove_execution(id, method, call_data)
self.executor.prove_execution(&BlockId::Hash(*hash), method, call_data)
}

fn read_proof_collection(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
start_key: &[Vec<u8>],
size_limit: usize,
) -> sp_blockchain::Result<(CompactProof, u32)> {
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
let state = self.state_at(&hash)?;
let state = self.state_at(hash)?;
// this is a read proof, using version V0 or V1 is equivalent.
let root = state.storage_root(std::iter::empty(), StateVersion::V0).0;

Expand All @@ -1202,14 +1199,13 @@ where

fn storage_collection(
&self,
id: &BlockId<Block>,
hash: &Block::Hash,
start_key: &[Vec<u8>],
size_limit: usize,
) -> sp_blockchain::Result<Vec<(KeyValueStorageLevel, bool)>> {
if start_key.len() > MAX_NESTED_TRIE_DEPTH {
return Err(Error::Backend("Invalid start key.".to_string()))
}
let hash = self.backend.blockchain().expect_block_hash_from_id(&id)?;
let state = self.state_at(&hash)?;
let child_info = |storage_key: &Vec<u8>| -> sp_blockchain::Result<ChildInfo> {
let storage_key = PrefixedStorageKey::new_ref(storage_key);
Expand Down