mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-06 02:08:02 +00:00
Fetch changes trie roots + CHT-based proof for these roots (#896)
* build CHT for changes tries roots * collect chnages tries roots proof in key_changes_proof * flush check_changes_proof * fixed compilation * LightDataChecker now has a ref to the blockchain * continue passing proofs * new light db tests * more CHT tests * more tests for key changes proof when headers are missing * lost files
This commit is contained in:
committed by
Gav Wood
parent
7f8ee0f53b
commit
fa84cec382
@@ -48,8 +48,11 @@ pub trait Storage<Block: BlockT>: BlockchainHeaderBackend<Block> {
|
||||
/// Get last finalized header.
|
||||
fn last_finalized(&self) -> ClientResult<Block::Hash>;
|
||||
|
||||
/// Get CHT root for given block. Fails if the block is not pruned (not a part of any CHT).
|
||||
fn cht_root(&self, cht_size: u64, block: NumberFor<Block>) -> ClientResult<Block::Hash>;
|
||||
/// Get headers CHT root for given block. Fails if the block is not pruned (not a part of any CHT).
|
||||
fn header_cht_root(&self, cht_size: u64, block: NumberFor<Block>) -> ClientResult<Block::Hash>;
|
||||
|
||||
/// Get changes trie CHT root for given block. Fails if the block is not pruned (not a part of any CHT).
|
||||
fn changes_trie_cht_root(&self, cht_size: u64, block: NumberFor<Block>) -> ClientResult<Block::Hash>;
|
||||
|
||||
/// Get storage cache.
|
||||
fn cache(&self) -> Option<&BlockchainCache<Block>>;
|
||||
@@ -106,7 +109,7 @@ impl<S, F, Block> BlockchainHeaderBackend<Block> for Blockchain<S, F> where Bloc
|
||||
|
||||
self.fetcher().upgrade().ok_or(ClientErrorKind::NotAvailableOnLightClient)?
|
||||
.remote_header(RemoteHeaderRequest {
|
||||
cht_root: self.storage.cht_root(cht::SIZE, number)?,
|
||||
cht_root: self.storage.header_cht_root(cht::SIZE, number)?,
|
||||
block: number,
|
||||
retry_count: None,
|
||||
})
|
||||
@@ -155,3 +158,84 @@ impl<S, F, Block> BlockchainBackend<Block> for Blockchain<S, F> where Block: Blo
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use std::collections::HashMap;
|
||||
use test_client::runtime::{Hash, Block, Header};
|
||||
use blockchain::Info;
|
||||
use light::fetcher::tests::OkCallFetcher;
|
||||
use super::*;
|
||||
|
||||
pub type DummyBlockchain = Blockchain<DummyStorage, OkCallFetcher>;
|
||||
|
||||
pub struct DummyStorage {
|
||||
pub changes_tries_cht_roots: HashMap<u64, Hash>,
|
||||
}
|
||||
|
||||
impl DummyStorage {
|
||||
pub fn new() -> Self {
|
||||
DummyStorage {
|
||||
changes_tries_cht_roots: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockchainHeaderBackend<Block> for DummyStorage {
|
||||
fn header(&self, _id: BlockId<Block>) -> ClientResult<Option<Header>> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn info(&self) -> ClientResult<Info<Block>> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn status(&self, _id: BlockId<Block>) -> ClientResult<BlockStatus> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn number(&self, _hash: Hash) -> ClientResult<Option<NumberFor<Block>>> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn hash(&self, _number: u64) -> ClientResult<Option<Hash>> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage<Block> for DummyStorage {
|
||||
fn import_header(
|
||||
&self,
|
||||
_header: Header,
|
||||
_authorities: Option<Vec<AuthorityId>>,
|
||||
_state: NewBlockState,
|
||||
) -> ClientResult<()> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn finalize_header(&self, _block: BlockId<Block>) -> ClientResult<()> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn last_finalized(&self) -> ClientResult<Hash> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn header_cht_root(&self, _cht_size: u64, _block: u64) -> ClientResult<Hash> {
|
||||
Err(ClientErrorKind::Backend("Test error".into()).into())
|
||||
}
|
||||
|
||||
fn changes_trie_cht_root(&self, cht_size: u64, block: u64) -> ClientResult<Hash> {
|
||||
cht::block_to_cht_number(cht_size, block)
|
||||
.and_then(|cht_num| self.changes_tries_cht_roots.get(&cht_num))
|
||||
.cloned()
|
||||
.ok_or_else(|| ClientErrorKind::Backend(
|
||||
format!("Test error: CHT for block #{} not found", block)
|
||||
).into())
|
||||
}
|
||||
|
||||
fn cache(&self) -> Option<&BlockchainCache<Block>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,19 +16,22 @@
|
||||
|
||||
//! Light client data fetcher. Fetches requested data from remote full nodes.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::collections::BTreeMap;
|
||||
use std::marker::PhantomData;
|
||||
use futures::IntoFuture;
|
||||
|
||||
use hash_db::Hasher;
|
||||
use hash_db::{HashDB, Hasher};
|
||||
use heapsize::HeapSizeOf;
|
||||
use primitives::{ChangesTrieConfiguration, convert_hash};
|
||||
use runtime_primitives::traits::{As, Block as BlockT, Header as HeaderT, NumberFor};
|
||||
use state_machine::{CodeExecutor, ChangesTrieRootsStorage, ChangesTrieAnchorBlockId,
|
||||
read_proof_check, key_changes_proof_check};
|
||||
TrieBackend, read_proof_check, key_changes_proof_check, create_proof_check_backend_storage};
|
||||
|
||||
use call_executor::CallResult;
|
||||
use cht;
|
||||
use error::{Error as ClientError, ErrorKind as ClientErrorKind, Result as ClientResult};
|
||||
use light::blockchain::{Blockchain, Storage as BlockchainStorage};
|
||||
use light::call_executor::check_execution_proof;
|
||||
|
||||
/// Remote call request.
|
||||
@@ -83,15 +86,30 @@ pub struct RemoteChangesRequest<Header: HeaderT> {
|
||||
/// Only use digests from blocks up to this hash. Should be last_block OR come
|
||||
/// after this block and be the part of the same fork.
|
||||
pub max_block: (Header::Number, Header::Hash),
|
||||
// TODO: get rid of this + preserve change_trie_roots when replacing headers with CHT!!!
|
||||
/// Changes trie roots for the range of blocks [first_block..max_block].
|
||||
pub tries_roots: Vec<Header::Hash>,
|
||||
/// Known changes trie roots for the range of blocks [tries_roots.0..max_block].
|
||||
/// Proofs for roots of ascendants of tries_roots.0 are provided by the remote node.
|
||||
pub tries_roots: (Header::Number, Header::Hash, Vec<Header::Hash>),
|
||||
/// Storage key to read.
|
||||
pub key: Vec<u8>,
|
||||
/// Number of times to retry request. None means that default RETRY_COUNT is used.
|
||||
pub retry_count: Option<usize>,
|
||||
}
|
||||
|
||||
/// Key changes read proof.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct ChangesProof<Header: HeaderT> {
|
||||
/// Max block that has been used in changes query.
|
||||
pub max_block: Header::Number,
|
||||
/// All touched nodes of all changes tries.
|
||||
pub proof: Vec<Vec<u8>>,
|
||||
/// All changes tries roots that have been touched AND are missing from
|
||||
/// the requester' node. It is a map of block number => changes trie root.
|
||||
pub roots: BTreeMap<Header::Number, Header::Hash>,
|
||||
/// The proofs for all changes tries roots that have been touched AND are
|
||||
/// missing from the requester' node. It is a map of CHT number => proof.
|
||||
pub roots_proof: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
/// Light client data fetcher. Implementations of this trait must check if remote data
|
||||
/// is correct (see FetchedDataChecker) and return already checked data.
|
||||
pub trait Fetcher<Block: BlockT>: Send + Sync {
|
||||
@@ -117,8 +135,8 @@ pub trait Fetcher<Block: BlockT>: Send + Sync {
|
||||
|
||||
/// Light client remote data checker.
|
||||
///
|
||||
/// Implementations of this trait should not use any blockchain data except that is
|
||||
/// passed to its methods.
|
||||
/// Implementations of this trait should not use any prunable blockchain data
|
||||
/// except that is passed to its methods.
|
||||
pub trait FetchChecker<Block: BlockT>: Send + Sync {
|
||||
/// Check remote header proof.
|
||||
fn check_header_proof(
|
||||
@@ -143,32 +161,158 @@ pub trait FetchChecker<Block: BlockT>: Send + Sync {
|
||||
fn check_changes_proof(
|
||||
&self,
|
||||
request: &RemoteChangesRequest<Block::Header>,
|
||||
remote_max: NumberFor<Block>,
|
||||
remote_proof: Vec<Vec<u8>>
|
||||
proof: ChangesProof<Block::Header>
|
||||
) -> ClientResult<Vec<(NumberFor<Block>, u32)>>;
|
||||
}
|
||||
|
||||
/// Remote data checker.
|
||||
pub struct LightDataChecker<E, H> {
|
||||
pub struct LightDataChecker<E, H, B: BlockT, S: BlockchainStorage<B>, F> {
|
||||
blockchain: Arc<Blockchain<S, F>>,
|
||||
executor: E,
|
||||
_hasher: PhantomData<H>,
|
||||
_hasher: PhantomData<(B, H)>,
|
||||
}
|
||||
|
||||
impl<E, H> LightDataChecker<E, H> {
|
||||
impl<E, H, B: BlockT, S: BlockchainStorage<B>, F> LightDataChecker<E, H, B, S, F> {
|
||||
/// Create new light data checker.
|
||||
pub fn new(executor: E) -> Self {
|
||||
pub fn new(blockchain: Arc<Blockchain<S, F>>, executor: E) -> Self {
|
||||
Self {
|
||||
executor, _hasher: PhantomData
|
||||
blockchain, executor, _hasher: PhantomData
|
||||
}
|
||||
}
|
||||
|
||||
/// Check remote changes query proof assuming that CHT-s are of given size.
|
||||
fn check_changes_proof_with_cht_size(
|
||||
&self,
|
||||
request: &RemoteChangesRequest<B::Header>,
|
||||
remote_proof: ChangesProof<B::Header>,
|
||||
cht_size: u64,
|
||||
) -> ClientResult<Vec<(NumberFor<B>, u32)>>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + HeapSizeOf,
|
||||
{
|
||||
// since we need roots of all changes tries for the range begin..max
|
||||
// => remote node can't use max block greater that one that we have passed
|
||||
if remote_proof.max_block > request.max_block.0 || remote_proof.max_block < request.last_block.0 {
|
||||
return Err(ClientErrorKind::ChangesTrieAccessFailed(format!(
|
||||
"Invalid max_block used by the remote node: {}. Local: {}..{}..{}",
|
||||
remote_proof.max_block, request.first_block.0, request.last_block.0, request.max_block.0,
|
||||
)).into());
|
||||
}
|
||||
|
||||
// check if remote node has responded with extra changes trie roots proofs
|
||||
// all changes tries roots must be in range [request.first_block.0; request.tries_roots.0)
|
||||
let is_extra_first_root = remote_proof.roots.keys().next()
|
||||
.map(|first_root| *first_root < request.first_block.0
|
||||
|| *first_root >= request.tries_roots.0)
|
||||
.unwrap_or(false);
|
||||
let is_extra_last_root = remote_proof.roots.keys().next_back()
|
||||
.map(|last_root| *last_root >= request.tries_roots.0)
|
||||
.unwrap_or(false);
|
||||
if is_extra_first_root || is_extra_last_root {
|
||||
return Err(ClientErrorKind::ChangesTrieAccessFailed(format!(
|
||||
"Extra changes tries roots proofs provided by the remote node: [{:?}..{:?}]. Expected in range: [{}; {})",
|
||||
remote_proof.roots.keys().next(), remote_proof.roots.keys().next_back(),
|
||||
request.first_block.0, request.tries_roots.0,
|
||||
)).into());
|
||||
}
|
||||
|
||||
// if request has been composed when some required headers were already pruned
|
||||
// => remote node has sent us CHT-based proof of required changes tries roots
|
||||
// => check that this proof is correct before proceeding with changes proof
|
||||
let remote_max_block = remote_proof.max_block;
|
||||
let remote_roots = remote_proof.roots;
|
||||
let remote_roots_proof = remote_proof.roots_proof;
|
||||
let remote_proof = remote_proof.proof;
|
||||
if !remote_roots.is_empty() {
|
||||
self.check_changes_tries_proof(
|
||||
cht_size,
|
||||
&remote_roots,
|
||||
remote_roots_proof,
|
||||
)?;
|
||||
}
|
||||
|
||||
// and now check the key changes proof + get the changes
|
||||
key_changes_proof_check::<_, H>(
|
||||
&request.changes_trie_config,
|
||||
&RootsStorage {
|
||||
roots: (request.tries_roots.0, &request.tries_roots.2),
|
||||
prev_roots: remote_roots,
|
||||
},
|
||||
remote_proof,
|
||||
request.first_block.0.as_(),
|
||||
&ChangesTrieAnchorBlockId {
|
||||
hash: convert_hash(&request.last_block.1),
|
||||
number: request.last_block.0.as_(),
|
||||
},
|
||||
remote_max_block.as_(),
|
||||
&request.key)
|
||||
.map(|pairs| pairs.into_iter().map(|(b, x)| (As::sa(b), x)).collect())
|
||||
.map_err(|err| ClientErrorKind::ChangesTrieAccessFailed(err).into())
|
||||
}
|
||||
|
||||
/// Check CHT-based proof for changes tries roots.
|
||||
fn check_changes_tries_proof(
|
||||
&self,
|
||||
cht_size: u64,
|
||||
remote_roots: &BTreeMap<NumberFor<B>, B::Hash>,
|
||||
remote_roots_proof: Vec<Vec<u8>>,
|
||||
) -> ClientResult<()>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + HeapSizeOf,
|
||||
{
|
||||
// all the checks are sharing the same storage
|
||||
let storage = create_proof_check_backend_storage(remote_roots_proof);
|
||||
|
||||
// we remote_roots.keys() are sorted => we can use this to group changes tries roots
|
||||
// that are belongs to the same CHT
|
||||
let blocks = remote_roots.keys().cloned();
|
||||
cht::for_each_cht_group::<B::Header, _, _, _>(cht_size, blocks, |mut storage, _, cht_blocks| {
|
||||
// get local changes trie CHT root for given CHT
|
||||
// it should be there, because it is never pruned AND request has been composed
|
||||
// when required header has been pruned (=> replaced with CHT)
|
||||
let first_block = cht_blocks.first().cloned()
|
||||
.expect("for_each_cht_group never calls callback with empty groups");
|
||||
let local_cht_root = self.blockchain.storage().changes_trie_cht_root(cht_size, first_block)?;
|
||||
|
||||
// check changes trie root for every block within CHT range
|
||||
for block in cht_blocks {
|
||||
// check if the proofs storage contains the root
|
||||
// normally this happens in when the proving backend is created, but since
|
||||
// we share the storage for multiple checks, do it here
|
||||
let mut cht_root = H::Out::default();
|
||||
cht_root.as_mut().copy_from_slice(local_cht_root.as_ref());
|
||||
if !storage.contains(&cht_root) {
|
||||
return Err(ClientErrorKind::InvalidCHTProof.into());
|
||||
}
|
||||
|
||||
// check proof for single changes trie root
|
||||
let proving_backend = TrieBackend::new(storage, cht_root);
|
||||
let remote_changes_trie_root = remote_roots[&block];
|
||||
cht::check_proof_on_proving_backend::<B::Header, H>(
|
||||
local_cht_root,
|
||||
block,
|
||||
remote_changes_trie_root,
|
||||
&proving_backend)?;
|
||||
|
||||
// and return the storage to use in following checks
|
||||
storage = proving_backend.into_storage();
|
||||
}
|
||||
|
||||
Ok(storage)
|
||||
}, storage)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, Block, H> FetchChecker<Block> for LightDataChecker<E, H>
|
||||
impl<E, Block, H, S, F> FetchChecker<Block> for LightDataChecker<E, H, Block, S, F>
|
||||
where
|
||||
Block: BlockT,
|
||||
E: CodeExecutor<H>,
|
||||
H: Hasher,
|
||||
H::Out: Ord + HeapSizeOf,
|
||||
S: BlockchainStorage<Block>,
|
||||
F: Send + Sync,
|
||||
{
|
||||
fn check_header_proof(
|
||||
&self,
|
||||
@@ -177,7 +321,7 @@ impl<E, Block, H> FetchChecker<Block> for LightDataChecker<E, H>
|
||||
remote_proof: Vec<Vec<u8>>
|
||||
) -> ClientResult<Block::Header> {
|
||||
let remote_header = remote_header.ok_or_else(||
|
||||
ClientError::from(ClientErrorKind::InvalidHeaderProof))?;
|
||||
ClientError::from(ClientErrorKind::InvalidCHTProof))?;
|
||||
let remote_header_hash = remote_header.hash();
|
||||
cht::check_proof::<Block::Header, H>(
|
||||
request.cht_root,
|
||||
@@ -207,55 +351,39 @@ impl<E, Block, H> FetchChecker<Block> for LightDataChecker<E, H>
|
||||
fn check_changes_proof(
|
||||
&self,
|
||||
request: &RemoteChangesRequest<Block::Header>,
|
||||
remote_max: NumberFor<Block>,
|
||||
remote_proof: Vec<Vec<u8>>
|
||||
remote_proof: ChangesProof<Block::Header>
|
||||
) -> ClientResult<Vec<(NumberFor<Block>, u32)>> {
|
||||
// since we need roots of all changes tries for the range begin..max
|
||||
// => remote node can't use max block greater that one that we have passed
|
||||
if remote_max > request.max_block.0 || remote_max < request.last_block.0 {
|
||||
return Err(ClientErrorKind::ChangesTrieAccessFailed(format!(
|
||||
"Invalid max_block used by the remote node: {}. Local: {}..{}..{}",
|
||||
remote_max, request.first_block.0, request.last_block.0, request.max_block.0,
|
||||
)).into());
|
||||
}
|
||||
|
||||
let first_number = request.first_block.0.as_();
|
||||
key_changes_proof_check::<_, H>(
|
||||
&request.changes_trie_config,
|
||||
&RootsStorage {
|
||||
first: first_number,
|
||||
roots: &request.tries_roots,
|
||||
},
|
||||
remote_proof,
|
||||
first_number,
|
||||
&ChangesTrieAnchorBlockId {
|
||||
hash: convert_hash(&request.last_block.1),
|
||||
number: request.last_block.0.as_(),
|
||||
},
|
||||
remote_max.as_(),
|
||||
&request.key)
|
||||
.map(|pairs| pairs.into_iter().map(|(b, x)| (As::sa(b), x)).collect())
|
||||
.map_err(|err| ClientErrorKind::ChangesTrieAccessFailed(err).into())
|
||||
self.check_changes_proof_with_cht_size(request, remote_proof, cht::SIZE)
|
||||
}
|
||||
}
|
||||
|
||||
/// A view of HashMap<Number, Hash> as a changes trie roots storage.
|
||||
struct RootsStorage<'a, Hash: 'a> {
|
||||
first: u64,
|
||||
roots: &'a [Hash],
|
||||
/// A view of BTreeMap<Number, Hash> as a changes trie roots storage.
|
||||
struct RootsStorage<'a, Number: As<u64>, Hash: 'a> {
|
||||
roots: (Number, &'a [Hash]),
|
||||
prev_roots: BTreeMap<Number, Hash>,
|
||||
}
|
||||
|
||||
impl<'a, H, Hash> ChangesTrieRootsStorage<H> for RootsStorage<'a, Hash>
|
||||
impl<'a, H, Number, Hash> ChangesTrieRootsStorage<H> for RootsStorage<'a, Number, Hash>
|
||||
where
|
||||
H: Hasher,
|
||||
Number: Send + Sync + Eq + ::std::cmp::Ord + Copy + As<u64>,
|
||||
Hash: 'a + Send + Sync + Clone + AsRef<[u8]>,
|
||||
{
|
||||
fn root(&self, _anchor: &ChangesTrieAnchorBlockId<H::Out>, block: u64) -> Result<Option<H::Out>, String> {
|
||||
// we can't ask for roots from parallel forks here => ignore anchor
|
||||
Ok(block.checked_sub(self.first)
|
||||
.and_then(|index| self.roots.get(index as usize))
|
||||
.cloned()
|
||||
.map(|root| convert_hash(&root)))
|
||||
let root = if block < self.roots.0.as_() {
|
||||
self.prev_roots.get(&As::sa(block)).cloned()
|
||||
} else {
|
||||
block.checked_sub(self.roots.0.as_())
|
||||
.and_then(|index| self.roots.1.get(index as usize))
|
||||
.cloned()
|
||||
};
|
||||
|
||||
Ok(root.map(|root| {
|
||||
let mut hasher_root: H::Out = Default::default();
|
||||
hasher_root.as_mut().copy_from_slice(root.as_ref());
|
||||
hasher_root
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,18 +391,20 @@ impl<'a, H, Hash> ChangesTrieRootsStorage<H> for RootsStorage<'a, Hash>
|
||||
pub mod tests {
|
||||
use futures::future::{ok, err, FutureResult};
|
||||
use parking_lot::Mutex;
|
||||
use keyring::Keyring;
|
||||
use call_executor::CallResult;
|
||||
use client::tests::prepare_client_with_key_changes;
|
||||
use executor::{self, NativeExecutionDispatch};
|
||||
use error::Error as ClientError;
|
||||
use test_client::{self, TestClient};
|
||||
use test_client::{self, TestClient, blockchain::HeaderBackend};
|
||||
use test_client::runtime::{self, Hash, Block, Header};
|
||||
use consensus::BlockOrigin;
|
||||
|
||||
use in_mem::{Blockchain as InMemoryBlockchain};
|
||||
use light::fetcher::{Fetcher, FetchChecker, LightDataChecker,
|
||||
RemoteCallRequest, RemoteHeaderRequest};
|
||||
use primitives::{Blake2Hasher};
|
||||
use light::blockchain::tests::{DummyStorage, DummyBlockchain};
|
||||
use primitives::{twox_128, Blake2Hasher};
|
||||
use primitives::storage::well_known_keys;
|
||||
use runtime_primitives::generic::BlockId;
|
||||
use state_machine::Backend;
|
||||
@@ -305,10 +435,9 @@ pub mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_for_read_proof_check() -> (
|
||||
LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher>,
|
||||
Header, Vec<Vec<u8>>, usize)
|
||||
{
|
||||
type TestChecker = LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher, Block, DummyStorage, OkCallFetcher>;
|
||||
|
||||
fn prepare_for_read_proof_check() -> (TestChecker, Header, Vec<Vec<u8>>, usize) {
|
||||
// prepare remote client
|
||||
let remote_client = test_client::new();
|
||||
let remote_block_id = BlockId::Number(0);
|
||||
@@ -330,21 +459,19 @@ pub mod tests {
|
||||
::backend::NewBlockState::Final,
|
||||
).unwrap();
|
||||
let local_executor = test_client::LocalExecutor::new();
|
||||
let local_checker = LightDataChecker::new(local_executor);
|
||||
let local_checker = LightDataChecker::new(Arc::new(DummyBlockchain::new(DummyStorage::new())), local_executor);
|
||||
(local_checker, remote_block_header, remote_read_proof, authorities_len)
|
||||
}
|
||||
|
||||
fn prepare_for_header_proof_check(insert_cht: bool) -> (
|
||||
LightDataChecker<executor::NativeExecutor<test_client::LocalExecutor>, Blake2Hasher>,
|
||||
Hash, Header, Vec<Vec<u8>>)
|
||||
{
|
||||
fn prepare_for_header_proof_check(insert_cht: bool) -> (TestChecker, Hash, Header, Vec<Vec<u8>>) {
|
||||
// prepare remote client
|
||||
let remote_client = test_client::new();
|
||||
let mut local_headers_hashes = Vec::new();
|
||||
for i in 0..4 {
|
||||
let builder = remote_client.new_block().unwrap();
|
||||
remote_client.justify_and_import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
|
||||
local_headers_hashes.push(remote_client.block_hash(i + 1).unwrap());
|
||||
local_headers_hashes.push(remote_client.block_hash(i + 1)
|
||||
.map_err(|_| ClientErrorKind::Backend("TestError".into()).into()));
|
||||
}
|
||||
|
||||
// 'fetch' header proof from remote node
|
||||
@@ -353,12 +480,12 @@ pub mod tests {
|
||||
|
||||
// check remote read proof locally
|
||||
let local_storage = InMemoryBlockchain::<Block>::new();
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(4, 0, local_headers_hashes.into_iter()).unwrap();
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(4, 0, local_headers_hashes).unwrap();
|
||||
if insert_cht {
|
||||
local_storage.insert_cht_root(1, local_cht_root);
|
||||
}
|
||||
let local_executor = test_client::LocalExecutor::new();
|
||||
let local_checker = LightDataChecker::new(local_executor);
|
||||
let local_checker = LightDataChecker::new(Arc::new(DummyBlockchain::new(DummyStorage::new())), local_executor);
|
||||
(local_checker, local_cht_root, remote_block_header, remote_header_proof)
|
||||
}
|
||||
|
||||
@@ -406,10 +533,12 @@ pub mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changes_proof_is_generated_and_checked() {
|
||||
fn changes_proof_is_generated_and_checked_when_headers_are_not_pruned() {
|
||||
let (remote_client, local_roots, test_cases) = prepare_client_with_key_changes();
|
||||
let local_checker = LightDataChecker::<_, Blake2Hasher>::new(
|
||||
test_client::LocalExecutor::new());
|
||||
let local_checker = TestChecker::new(
|
||||
Arc::new(DummyBlockchain::new(DummyStorage::new())),
|
||||
test_client::LocalExecutor::new()
|
||||
);
|
||||
let local_checker = &local_checker as &FetchChecker<Block>;
|
||||
let max = remote_client.info().unwrap().chain.best_number;
|
||||
let max_hash = remote_client.info().unwrap().chain.best_hash;
|
||||
@@ -419,8 +548,8 @@ pub mod tests {
|
||||
let end_hash = remote_client.block_hash(end).unwrap().unwrap();
|
||||
|
||||
// 'fetch' changes proof from remote node
|
||||
let (remote_max, remote_proof) = remote_client.key_changes_proof(
|
||||
begin_hash, end_hash, max_hash, &key
|
||||
let remote_proof = remote_client.key_changes_proof(
|
||||
begin_hash, end_hash, begin_hash, max_hash, &key
|
||||
).unwrap();
|
||||
|
||||
// check proof on local client
|
||||
@@ -430,12 +559,16 @@ pub mod tests {
|
||||
first_block: (begin, begin_hash),
|
||||
last_block: (end, end_hash),
|
||||
max_block: (max, max_hash),
|
||||
tries_roots: local_roots_range,
|
||||
tries_roots: (begin, begin_hash, local_roots_range),
|
||||
key: key,
|
||||
retry_count: None,
|
||||
};
|
||||
let local_result = local_checker.check_changes_proof(
|
||||
&request, remote_max, remote_proof).unwrap();
|
||||
let local_result = local_checker.check_changes_proof(&request, ChangesProof {
|
||||
max_block: remote_proof.max_block,
|
||||
proof: remote_proof.proof,
|
||||
roots: remote_proof.roots,
|
||||
roots_proof: remote_proof.roots_proof,
|
||||
}).unwrap();
|
||||
|
||||
// ..and ensure that result is the same as on remote node
|
||||
match local_result == expected_result {
|
||||
@@ -446,11 +579,60 @@ pub mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changes_proof_is_generated_and_checked_when_headers_are_pruned() {
|
||||
// we're testing this test case here:
|
||||
// (1, 4, dave.clone(), vec![(4, 0), (1, 1), (1, 0)]),
|
||||
let (remote_client, remote_roots, _) = prepare_client_with_key_changes();
|
||||
let dave = twox_128(&runtime::system::balance_of_key(Keyring::Dave.to_raw_public().into())).to_vec();
|
||||
|
||||
// 'fetch' changes proof from remote node:
|
||||
// we're fetching changes for range b1..b4
|
||||
// we do not know changes trie roots before b3 (i.e. we only know b3+b4)
|
||||
// but we have changes trie CHT root for b1...b4
|
||||
let b1 = remote_client.block_hash_from_id(&BlockId::Number(1)).unwrap().unwrap();
|
||||
let b3 = remote_client.block_hash_from_id(&BlockId::Number(3)).unwrap().unwrap();
|
||||
let b4 = remote_client.block_hash_from_id(&BlockId::Number(4)).unwrap().unwrap();
|
||||
let remote_proof = remote_client.key_changes_proof_with_cht_size(
|
||||
b1, b4, b3, b4, &dave, 4
|
||||
).unwrap();
|
||||
|
||||
// prepare local checker, having a root of changes trie CHT#0
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
|
||||
let mut local_storage = DummyStorage::new();
|
||||
local_storage.changes_tries_cht_roots.insert(0, local_cht_root);
|
||||
let local_checker = TestChecker::new(
|
||||
Arc::new(DummyBlockchain::new(local_storage)),
|
||||
test_client::LocalExecutor::new()
|
||||
);
|
||||
|
||||
// check proof on local client
|
||||
let request = RemoteChangesRequest::<Header> {
|
||||
changes_trie_config: runtime::changes_trie_config(),
|
||||
first_block: (1, b1),
|
||||
last_block: (4, b4),
|
||||
max_block: (4, b4),
|
||||
tries_roots: (3, b3, vec![remote_roots[2].clone(), remote_roots[3].clone()]),
|
||||
key: dave,
|
||||
retry_count: None,
|
||||
};
|
||||
let local_result = local_checker.check_changes_proof_with_cht_size(&request, ChangesProof {
|
||||
max_block: remote_proof.max_block,
|
||||
proof: remote_proof.proof,
|
||||
roots: remote_proof.roots,
|
||||
roots_proof: remote_proof.roots_proof,
|
||||
}, 4).unwrap();
|
||||
|
||||
assert_eq!(local_result, vec![(4, 0), (1, 1), (1, 0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_changes_proof_fails_if_proof_is_wrong() {
|
||||
let (remote_client, local_roots, test_cases) = prepare_client_with_key_changes();
|
||||
let local_checker = LightDataChecker::<_, Blake2Hasher>::new(
|
||||
test_client::LocalExecutor::new());
|
||||
let local_checker = TestChecker::new(
|
||||
Arc::new(DummyBlockchain::new(DummyStorage::new())),
|
||||
test_client::LocalExecutor::new()
|
||||
);
|
||||
let local_checker = &local_checker as &FetchChecker<Block>;
|
||||
let max = remote_client.info().unwrap().chain.best_number;
|
||||
let max_hash = remote_client.info().unwrap().chain.best_hash;
|
||||
@@ -460,24 +642,86 @@ pub mod tests {
|
||||
let end_hash = remote_client.block_hash(end).unwrap().unwrap();
|
||||
|
||||
// 'fetch' changes proof from remote node
|
||||
let (remote_max, mut remote_proof) = remote_client.key_changes_proof(
|
||||
begin_hash, end_hash, max_hash, &key).unwrap();
|
||||
let remote_proof = remote_client.key_changes_proof(
|
||||
begin_hash, end_hash, begin_hash, max_hash, &key).unwrap();
|
||||
|
||||
let local_roots_range = local_roots.clone()[(begin - 1) as usize..].to_vec();
|
||||
let request = RemoteChangesRequest::<Header> {
|
||||
changes_trie_config: runtime::changes_trie_config(),
|
||||
first_block: (begin, begin_hash),
|
||||
last_block: (end, end_hash),
|
||||
max_block: (max, max_hash),
|
||||
tries_roots: local_roots_range.clone(),
|
||||
tries_roots: (begin, begin_hash, local_roots_range.clone()),
|
||||
key: key,
|
||||
retry_count: None,
|
||||
};
|
||||
|
||||
// check proof on local client using max from the future
|
||||
assert!(local_checker.check_changes_proof(&request, remote_max + 1, remote_proof.clone()).is_err());
|
||||
assert!(local_checker.check_changes_proof(&request, ChangesProof {
|
||||
max_block: remote_proof.max_block + 1,
|
||||
proof: remote_proof.proof.clone(),
|
||||
roots: remote_proof.roots.clone(),
|
||||
roots_proof: remote_proof.roots_proof.clone(),
|
||||
}).is_err());
|
||||
|
||||
// check proof on local client using broken proof
|
||||
remote_proof = local_roots_range.into_iter().map(|v| v.as_bytes().to_vec()).collect();
|
||||
assert!(local_checker.check_changes_proof(&request, remote_max, remote_proof).is_err());
|
||||
assert!(local_checker.check_changes_proof(&request, ChangesProof {
|
||||
max_block: remote_proof.max_block,
|
||||
proof: local_roots_range.clone().into_iter().map(|v| v.as_ref().to_vec()).collect(),
|
||||
roots: remote_proof.roots,
|
||||
roots_proof: remote_proof.roots_proof,
|
||||
}).is_err());
|
||||
|
||||
// extra roots proofs are provided
|
||||
assert!(local_checker.check_changes_proof(&request, ChangesProof {
|
||||
max_block: remote_proof.max_block,
|
||||
proof: remote_proof.proof.clone(),
|
||||
roots: vec![(begin - 1, Default::default())].into_iter().collect(),
|
||||
roots_proof: vec![],
|
||||
}).is_err());
|
||||
assert!(local_checker.check_changes_proof(&request, ChangesProof {
|
||||
max_block: remote_proof.max_block,
|
||||
proof: remote_proof.proof.clone(),
|
||||
roots: vec![(end + 1, Default::default())].into_iter().collect(),
|
||||
roots_proof: vec![],
|
||||
}).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_changes_tries_proof_fails_if_proof_is_wrong() {
|
||||
// we're testing this test case here:
|
||||
// (1, 4, dave.clone(), vec![(4, 0), (1, 1), (1, 0)]),
|
||||
let (remote_client, remote_roots, _) = prepare_client_with_key_changes();
|
||||
let local_cht_root = cht::compute_root::<Header, Blake2Hasher, _>(
|
||||
4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
|
||||
let dave = twox_128(&runtime::system::balance_of_key(Keyring::Dave.to_raw_public().into())).to_vec();
|
||||
|
||||
// 'fetch' changes proof from remote node:
|
||||
// we're fetching changes for range b1..b4
|
||||
// we do not know changes trie roots before b3 (i.e. we only know b3+b4)
|
||||
// but we have changes trie CHT root for b1...b4
|
||||
let b1 = remote_client.block_hash_from_id(&BlockId::Number(1)).unwrap().unwrap();
|
||||
let b3 = remote_client.block_hash_from_id(&BlockId::Number(3)).unwrap().unwrap();
|
||||
let b4 = remote_client.block_hash_from_id(&BlockId::Number(4)).unwrap().unwrap();
|
||||
let remote_proof = remote_client.key_changes_proof_with_cht_size(
|
||||
b1, b4, b3, b4, &dave, 4
|
||||
).unwrap();
|
||||
|
||||
// fails when changes trie CHT is missing from the local db
|
||||
let local_checker = TestChecker::new(
|
||||
Arc::new(DummyBlockchain::new(DummyStorage::new())),
|
||||
test_client::LocalExecutor::new()
|
||||
);
|
||||
assert!(local_checker.check_changes_tries_proof(4, &remote_proof.roots,
|
||||
remote_proof.roots_proof.clone()).is_err());
|
||||
|
||||
// fails when proof is broken
|
||||
let mut local_storage = DummyStorage::new();
|
||||
local_storage.changes_tries_cht_roots.insert(0, local_cht_root);
|
||||
let local_checker = TestChecker::new(
|
||||
Arc::new(DummyBlockchain::new(local_storage)),
|
||||
test_client::LocalExecutor::new()
|
||||
);
|
||||
assert!(local_checker.check_changes_tries_proof(4, &remote_proof.roots, vec![]).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,13 +65,14 @@ where
|
||||
}
|
||||
|
||||
/// Create an instance of fetch data checker.
|
||||
pub fn new_fetch_checker<E, H>(
|
||||
pub fn new_fetch_checker<E, H, B: BlockT, S: BlockchainStorage<B>, F>(
|
||||
blockchain: Arc<Blockchain<S, F>>,
|
||||
executor: E,
|
||||
) -> LightDataChecker<E, H>
|
||||
) -> LightDataChecker<E, H, B, S, F>
|
||||
where
|
||||
E: CodeExecutor<H>,
|
||||
H: Hasher,
|
||||
|
||||
{
|
||||
LightDataChecker::new(executor)
|
||||
LightDataChecker::new(blockchain, executor)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user