Create opaque struct for StorageProof. (#3834)

Passing around Vec<Vec<u8>> everywhere is gross and confusing and
breaks encapsulation.
This commit is contained in:
Jim Posen
2019-10-31 11:02:29 +01:00
committed by Bastian Köcher
parent 073040a053
commit a167f37b91
20 changed files with 246 additions and 141 deletions
+12 -9
View File
@@ -18,7 +18,7 @@
use client::{self, Client as SubstrateClient, ClientInfo, CallExecutor};
use client::error::Error;
use client::light::fetcher::ChangesProof;
use client::light::fetcher::{ChangesProof, StorageProof};
use consensus::{BlockImport, BlockStatus, Error as ConsensusError};
use sr_primitives::traits::{Block as BlockT, Header as HeaderT};
use sr_primitives::generic::{BlockId};
@@ -46,10 +46,11 @@ pub trait Client<Block: BlockT>: Send + Sync {
fn justification(&self, id: &BlockId<Block>) -> Result<Option<Justification>, Error>;
/// Get block header proof.
fn header_proof(&self, block_number: <Block::Header as HeaderT>::Number) -> Result<(Block::Header, Vec<Vec<u8>>), Error>;
fn header_proof(&self, block_number: <Block::Header as HeaderT>::Number)
-> Result<(Block::Header, StorageProof), Error>;
/// Get storage read execution proof.
fn read_proof(&self, block: &Block::Hash, keys: &[Vec<u8>]) -> Result<Vec<Vec<u8>>, Error>;
fn read_proof(&self, block: &Block::Hash, keys: &[Vec<u8>]) -> Result<StorageProof, Error>;
/// Get child storage read execution proof.
fn read_child_proof(
@@ -57,10 +58,10 @@ pub trait Client<Block: BlockT>: Send + Sync {
block: &Block::Hash,
storage_key: &[u8],
keys: &[Vec<u8>],
) -> Result<Vec<Vec<u8>>, Error>;
) -> Result<StorageProof, Error>;
/// Get method execution proof.
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, Vec<Vec<u8>>), Error>;
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, StorageProof), Error>;
/// Get key changes proof.
fn key_changes_proof(
@@ -120,11 +121,13 @@ impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
(self as &SubstrateClient<B, E, Block, RA>).justification(id)
}
fn header_proof(&self, block_number: <Block::Header as HeaderT>::Number) -> Result<(Block::Header, Vec<Vec<u8>>), Error> {
fn header_proof(&self, block_number: <Block::Header as HeaderT>::Number)
-> Result<(Block::Header, StorageProof), Error>
{
(self as &SubstrateClient<B, E, Block, RA>).header_proof(&BlockId::Number(block_number))
}
fn read_proof(&self, block: &Block::Hash, keys: &[Vec<u8>]) -> Result<Vec<Vec<u8>>, Error> {
fn read_proof(&self, block: &Block::Hash, keys: &[Vec<u8>]) -> Result<StorageProof, Error> {
(self as &SubstrateClient<B, E, Block, RA>).read_proof(&BlockId::Hash(block.clone()), keys)
}
@@ -133,12 +136,12 @@ impl<B, E, Block, RA> Client<Block> for SubstrateClient<B, E, Block, RA> where
block: &Block::Hash,
storage_key: &[u8],
keys: &[Vec<u8>],
) -> Result<Vec<Vec<u8>>, Error> {
) -> Result<StorageProof, Error> {
(self as &SubstrateClient<B, E, Block, RA>)
.read_child_proof(&BlockId::Hash(block.clone()), storage_key, keys)
}
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, Vec<Vec<u8>>), Error> {
fn execution_proof(&self, block: &Block::Hash, method: &str, data: &[u8]) -> Result<(Vec<u8>, StorageProof), Error> {
(self as &SubstrateClient<B, E, Block, RA>).execution_proof(&BlockId::Hash(block.clone()), method, data)
}
+6 -6
View File
@@ -48,7 +48,7 @@ use std::sync::Arc;
use std::{cmp, num::NonZeroUsize, time};
use log::{trace, debug, warn, error};
use crate::chain::{Client, FinalityProofProvider};
use client::light::fetcher::{FetchChecker, ChangesProof};
use client::light::fetcher::{FetchChecker, ChangesProof, StorageProof};
use crate::error;
use util::LruHashSet;
@@ -1226,7 +1226,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
error
);
self.peerset_handle.report_peer(who.clone(), RPC_FAILED_REPUTATION_CHANGE);
Default::default()
StorageProof::empty()
}
};
@@ -1343,7 +1343,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
request.block,
error
);
Default::default()
StorageProof::empty()
}
};
self.send_message(
@@ -1386,7 +1386,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
request.block,
error
);
Default::default()
StorageProof::empty()
}
};
self.send_message(
@@ -1426,7 +1426,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
request.block,
error
);
(Default::default(), Default::default())
(Default::default(), StorageProof::empty())
}
};
self.send_message(
@@ -1495,7 +1495,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
max_block: Zero::zero(),
proof: vec![],
roots: BTreeMap::new(),
roots_proof: vec![],
roots_proof: StorageProof::empty(),
}
}
};
@@ -28,7 +28,7 @@ use linked_hash_map::{Entry, LinkedHashMap};
use client::error::Error as ClientError;
use client::light::fetcher::{FetchChecker, RemoteHeaderRequest,
RemoteCallRequest, RemoteReadRequest, RemoteChangesRequest, ChangesProof,
RemoteReadChildRequest, RemoteBodyRequest};
RemoteReadChildRequest, RemoteBodyRequest, StorageProof};
use crate::message::{self, BlockAttributes, Direction, FromBlock, RequestId};
use libp2p::PeerId;
use crate::config::Roles;
@@ -174,7 +174,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
&self,
_request: &RemoteHeaderRequest<Block::Header>,
_remote_header: Option<Block::Header>,
_remote_proof: Vec<Vec<u8>>
_remote_proof: StorageProof,
) -> Result<Block::Header, ClientError> {
Err(ClientError::Msg("AlwaysBadChecker".into()))
}
@@ -182,7 +182,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
fn check_read_proof(
&self,
_request: &RemoteReadRequest<Block::Header>,
_remote_proof: Vec<Vec<u8>>
_remote_proof: StorageProof,
) -> Result<HashMap<Vec<u8>,Option<Vec<u8>>>, ClientError> {
Err(ClientError::Msg("AlwaysBadChecker".into()))
}
@@ -190,7 +190,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
fn check_read_child_proof(
&self,
_request: &RemoteReadChildRequest<Block::Header>,
_remote_proof: Vec<Vec<u8>>
_remote_proof: StorageProof,
) -> Result<HashMap<Vec<u8>, Option<Vec<u8>>>, ClientError> {
Err(ClientError::Msg("AlwaysBadChecker".into()))
}
@@ -198,7 +198,7 @@ impl<Block: BlockT> FetchChecker<Block> for AlwaysBadChecker {
fn check_execution_proof(
&self,
_request: &RemoteCallRequest<Block::Header>,
_remote_proof: Vec<Vec<u8>>
_remote_proof: StorageProof,
) -> Result<Vec<u8>, ClientError> {
Err(ClientError::Msg("AlwaysBadChecker".into()))
}
@@ -684,7 +684,7 @@ pub mod tests {
use crate::config::Roles;
use crate::message::{self, BlockAttributes, Direction, FromBlock, RequestId};
use libp2p::PeerId;
use super::{REQUEST_TIMEOUT, LightDispatch, LightDispatchNetwork, RequestData};
use super::{REQUEST_TIMEOUT, LightDispatch, LightDispatchNetwork, RequestData, StorageProof};
use test_client::runtime::{changes_trie_config, Block, Extrinsic, Header};
struct DummyFetchChecker { ok: bool }
@@ -694,7 +694,7 @@ pub mod tests {
&self,
_request: &RemoteHeaderRequest<Header>,
header: Option<Header>,
_remote_proof: Vec<Vec<u8>>
_remote_proof: StorageProof,
) -> ClientResult<Header> {
match self.ok {
true if header.is_some() => Ok(header.unwrap()),
@@ -705,7 +705,7 @@ pub mod tests {
fn check_read_proof(
&self,
request: &RemoteReadRequest<Header>,
_: Vec<Vec<u8>>,
_: StorageProof,
) -> ClientResult<HashMap<Vec<u8>, Option<Vec<u8>>>> {
match self.ok {
true => Ok(request.keys
@@ -721,7 +721,7 @@ pub mod tests {
fn check_read_child_proof(
&self,
request: &RemoteReadChildRequest<Header>,
_: Vec<Vec<u8>>
_: StorageProof,
) -> ClientResult<HashMap<Vec<u8>, Option<Vec<u8>>>> {
match self.ok {
true => Ok(request.keys
@@ -734,7 +734,7 @@ pub mod tests {
}
}
fn check_execution_proof(&self, _: &RemoteCallRequest<Header>, _: Vec<Vec<u8>>) -> ClientResult<Vec<u8>> {
fn check_execution_proof(&self, _: &RemoteCallRequest<Header>, _: StorageProof) -> ClientResult<Vec<u8>> {
match self.ok {
true => Ok(vec![42]),
false => Err(ClientError::Backend("Test error".into())),
@@ -780,7 +780,7 @@ pub mod tests {
) {
light_dispatch.on_remote_call_response(network_interface, peer, message::RemoteCallResponse {
id: id,
proof: vec![vec![2]],
proof: StorageProof::empty(),
});
}
@@ -943,7 +943,7 @@ pub mod tests {
light_dispatch.on_remote_read_response(&mut network_interface, peer0.clone(), message::RemoteReadResponse {
id: 0,
proof: vec![vec![2]],
proof: StorageProof::empty(),
});
assert_disconnected_peer(&network_interface);
assert_eq!(light_dispatch.pending_requests.len(), 1);
@@ -1013,7 +1013,7 @@ pub mod tests {
light_dispatch.on_remote_read_response(&mut network_interface, peer0.clone(), message::RemoteReadResponse {
id: 0,
proof: vec![vec![2]],
proof: StorageProof::empty(),
});
assert_eq!(response.wait().unwrap().unwrap().remove(b":key".as_ref()).unwrap(), Some(vec![42]));
}
@@ -1037,7 +1037,7 @@ pub mod tests {
light_dispatch.on_remote_read_response(&mut network_interface,
peer0.clone(), message::RemoteReadResponse {
id: 0,
proof: vec![vec![2]],
proof: StorageProof::empty(),
});
assert_eq!(response.wait().unwrap().unwrap().remove(b":key".as_ref()).unwrap(), Some(vec![42]));
}
@@ -1065,7 +1065,7 @@ pub mod tests {
extrinsics_root: Default::default(),
digest: Default::default(),
}),
proof: vec![vec![2]],
proof: StorageProof::empty(),
});
assert_eq!(
response.wait().unwrap().unwrap().hash(),
@@ -1097,7 +1097,7 @@ pub mod tests {
max: 1000,
proof: vec![vec![2]],
roots: vec![],
roots_proof: vec![],
roots_proof: StorageProof::empty(),
});
assert_eq!(response.wait().unwrap().unwrap(), vec![(100, 2)]);
}
@@ -1145,7 +1145,7 @@ pub mod tests {
light_dispatch.on_remote_header_response(&mut network_interface, peer1.clone(), message::RemoteHeaderResponse {
id: 0,
header: Some(dummy_header()),
proof: vec![],
proof: StorageProof::empty(),
});
assert!(!light_dispatch.idle_peers.iter().any(|_| true));
@@ -26,6 +26,7 @@ pub use self::generic::{
FinalityProofRequest, FinalityProofResponse,
FromBlock, RemoteReadChildRequest,
};
use client::light::fetcher::StorageProof;
/// A unique ID of a request.
pub type RequestId = u64;
@@ -122,7 +123,7 @@ pub struct RemoteCallResponse {
/// Id of a request this response was made for.
pub id: RequestId,
/// Execution proof.
pub proof: Vec<Vec<u8>>,
pub proof: StorageProof,
}
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
@@ -131,7 +132,7 @@ pub struct RemoteReadResponse {
/// Id of a request this response was made for.
pub id: RequestId,
/// Read proof.
pub proof: Vec<Vec<u8>>,
pub proof: StorageProof,
}
/// Generic types.
@@ -142,7 +143,7 @@ pub mod generic {
use super::{
RemoteReadResponse, Transactions, Direction,
RequestId, BlockAttributes, RemoteCallResponse, ConsensusEngineId,
BlockState,
BlockState, StorageProof,
};
/// Consensus is mostly opaque to us
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
@@ -359,7 +360,7 @@ pub mod generic {
/// Header. None if proof generation has failed (e.g. header is unknown).
pub header: Option<Header>,
/// Header proof.
pub proof: Vec<Vec<u8>>,
pub proof: StorageProof,
}
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
@@ -395,7 +396,7 @@ pub mod generic {
/// Changes tries roots missing on the requester' node.
pub roots: Vec<(N, H)>,
/// Missing changes tries roots proof.
pub roots_proof: Vec<Vec<u8>>,
pub roots_proof: StorageProof,
}
#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]