Fix key collision for child trie (#4162)

* In progress, runtime io must switch to future proof root +
child_specific (unique id) + u32 type.

* Switch interface, sr-io seems ok, rpc could use similar interface to
sr-io, genesis json broken if there is child trie in existing encoding
genesis.

* test from previous implementation.

* fix proving test.

* Restore Keyspacedb from other branch, only apply to child trie.

* Removing unneeded child_info from child root (child info are stored
if things changed, otherwhise the root does not change).

* Switch rpc to use same format as ext: more future proof.

* use root from child info for trie backend essence.

* Breaking long lines.

* Update doc and clean pr a bit.

* fix error type

* Restore removed doc on merge and update sr-io doc.

* Switch child storage api to use directly unique id, if managed id
where to be put in place, the api will change at this time.

* Clean deprecated host interface from child.

* Removing assertion on child info (can fail depending on root
memoization).

* merging child info in the overlay when possible.

* child iteration by prefix using child_info.

* Using ChainInfo in frame support. ChainInfo gets redesign to avoid
buffers allocation on every calls.

* Add length of root to the data of child info.

* comments

* Encode compact.

* Remove child info with root.

* Fix try_update condition.

* Comment Ext child root caching.

* Replace tuples by struct with field

* remove StorageTuple alias.

* Fix doc tests, and remove StorageOverlay and ChildStorageOverlay
aliases.
This commit is contained in:
cheme
2019-12-14 03:11:19 +01:00
committed by Gavin Wood
parent 7121837f84
commit 0ece5d9e17
53 changed files with 2121 additions and 918 deletions
+5 -2
View File
@@ -23,7 +23,8 @@ use consensus::{BlockImport, BlockStatus, Error as ConsensusError};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_runtime::generic::{BlockId};
use sp_runtime::Justification;
use primitives::{H256, Blake2Hasher, storage::StorageKey};
use primitives::{H256, Blake2Hasher};
use primitives::storage::{StorageKey, ChildInfo};
/// Local client abstraction for the network.
pub trait Client<Block: BlockT>: Send + Sync {
@@ -57,6 +58,7 @@ pub trait Client<Block: BlockT>: Send + Sync {
&self,
block: &Block::Hash,
storage_key: &[u8],
child_info: ChildInfo,
keys: &[Vec<u8>],
) -> Result<StorageProof, Error>;
@@ -135,10 +137,11 @@ impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
&self,
block: &Block::Hash,
storage_key: &[u8],
child_info: ChildInfo,
keys: &[Vec<u8>],
) -> Result<StorageProof, Error> {
(self as &SubstrateClient<B, E, Block, RA>)
.read_child_proof(&BlockId::Hash(block.clone()), storage_key, keys)
.read_child_proof(&BlockId::Hash(block.clone()), storage_key, child_info, keys)
}
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, StorageProof), Error> {
+35 -17
View File
@@ -24,7 +24,7 @@ use libp2p::{Multiaddr, PeerId};
use libp2p::core::{ConnectedPoint, nodes::Substream, muxing::StreamMuxerBox};
use libp2p::swarm::{ProtocolsHandler, IntoProtocolsHandler};
use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use primitives::storage::StorageKey;
use primitives::storage::{StorageKey, ChildInfo};
use consensus::{
BlockOrigin,
block_validation::BlockAnnounceValidator,
@@ -238,12 +238,16 @@ impl<'a, B: BlockT> LightDispatchNetwork<B> for LightDispatchIn<'a> {
id: RequestId,
block: <B as BlockT>::Hash,
storage_key: Vec<u8>,
child_info: Vec<u8>,
child_type: u32,
keys: Vec<Vec<u8>>,
) {
let message: Message<B> = message::generic::Message::RemoteReadChildRequest(message::RemoteReadChildRequest {
id,
block,
storage_key,
child_info,
child_type,
keys,
});
@@ -1554,23 +1558,37 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
trace!(target: "sync", "Remote read child request {} from {} ({} {} at {})",
request.id, who, request.storage_key.to_hex::<String>(), keys_str(), request.block);
let proof = match self.context_data.chain.read_child_proof(
&request.block,
&request.storage_key,
&request.keys,
) {
Ok(proof) => proof,
Err(error) => {
trace!(target: "sync", "Remote read child request {} from {} ({} {} at {}) failed with: {}",
request.id,
who,
request.storage_key.to_hex::<String>(),
keys_str(),
request.block,
error
);
StorageProof::empty()
let proof = if let Some(child_info) = ChildInfo::resolve_child_info(request.child_type, &request.child_info[..]) {
match self.context_data.chain.read_child_proof(
&request.block,
&request.storage_key,
child_info,
&request.keys,
) {
Ok(proof) => proof,
Err(error) => {
trace!(target: "sync", "Remote read child request {} from {} ({} {} at {}) failed with: {}",
request.id,
who,
request.storage_key.to_hex::<String>(),
keys_str(),
request.block,
error
);
StorageProof::empty()
}
}
} else {
trace!(target: "sync", "Remote read child request {} from {} ({} {} at {}) failed with: {}",
request.id,
who,
request.storage_key.to_hex::<String>(),
keys_str(),
request.block,
"invalid child info and type",
);
StorageProof::empty()
};
self.send_message(
&who,
@@ -69,6 +69,8 @@ pub trait LightDispatchNetwork<B: BlockT> {
id: RequestId,
block: <B as BlockT>::Hash,
storage_key: Vec<u8>,
child_info: Vec<u8>,
child_type: u32,
keys: Vec<Vec<u8>>,
);
@@ -622,6 +624,8 @@ impl<Block: BlockT> Request<Block> {
self.id,
data.block,
data.storage_key.clone(),
data.child_info.clone(),
data.child_type,
data.keys.clone(),
),
RequestData::RemoteCall(ref data, _) =>
@@ -677,6 +681,7 @@ pub mod tests {
use std::sync::Arc;
use std::time::Instant;
use futures::{Future, sync::oneshot};
use primitives::storage::ChildInfo;
use sp_runtime::traits::{Block as BlockT, NumberFor, Header as HeaderT};
use sp_blockchain::{Error as ClientError, Result as ClientResult};
use client_api::{FetchChecker, RemoteHeaderRequest,
@@ -808,7 +813,7 @@ pub mod tests {
fn send_header_request(&mut self, _: &PeerId, _: RequestId, _: <<B as BlockT>::Header as HeaderT>::Number) {}
fn send_read_request(&mut self, _: &PeerId, _: RequestId, _: <B as BlockT>::Hash, _: Vec<Vec<u8>>) {}
fn send_read_child_request(&mut self, _: &PeerId, _: RequestId, _: <B as BlockT>::Hash, _: Vec<u8>,
_: Vec<Vec<u8>>) {}
_: Vec<u8>, _: u32, _: Vec<Vec<u8>>) {}
fn send_call_request(&mut self, _: &PeerId, _: RequestId, _: <B as BlockT>::Hash, _: String, _: Vec<u8>) {}
fn send_changes_request(&mut self, _: &PeerId, _: RequestId, _: <B as BlockT>::Hash, _: <B as BlockT>::Hash,
_: <B as BlockT>::Hash, _: <B as BlockT>::Hash, _: Option<Vec<u8>>, _: Vec<u8>) {}
@@ -1027,10 +1032,14 @@ pub mod tests {
light_dispatch.on_connect(&mut network_interface, peer0.clone(), Roles::FULL, 1000);
let (tx, response) = oneshot::channel();
let child_info = ChildInfo::new_default(b"unique_id_1");
let (child_info, child_type) = child_info.info();
light_dispatch.add_request(&mut network_interface, RequestData::RemoteReadChild(RemoteReadChildRequest {
header: dummy_header(),
block: Default::default(),
storage_key: b":child_storage:sub".to_vec(),
child_info: child_info.to_vec(),
child_type,
keys: vec![b":key".to_vec()],
retry_count: None,
}, tx));
@@ -368,6 +368,11 @@ pub mod generic {
pub block: H,
/// Child Storage key.
pub storage_key: Vec<u8>,
/// Child trie source information.
pub child_info: Vec<u8>,
/// Child type, its required to resolve `child_info`
/// content and choose child implementation.
pub child_type: u32,
/// Storage key.
pub keys: Vec<Vec<u8>>,
}