Run cargo fmt on the whole code base (#9394)

* Run cargo fmt on the whole code base

* Second run

* Add CI check

* Fix compilation

* More unnecessary braces

* Handle weights

* Use --all

* Use correct attributes...

* Fix UI tests

* AHHHHHHHHH

* 🤦

* Docs

* Fix compilation

* 🤷

* Please stop

* 🤦 x 2

* More

* make rustfmt.toml consistent with polkadot

Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use sp_core::offchain::{OffchainStorage, storage::InMemOffchainStorage};
use sp_core::offchain::{storage::InMemOffchainStorage, OffchainStorage};
use std::sync::Arc;
type TestBackend = sc_client_api::in_mem::Backend<substrate_test_runtime::Block>;
@@ -32,12 +32,13 @@ fn test_leaves_with_complex_block_tree() {
fn test_blockchain_query_by_number_gets_canonical() {
let backend = Arc::new(TestBackend::new());
substrate_test_runtime_client::trait_tests::test_blockchain_query_by_number_gets_canonical(backend);
substrate_test_runtime_client::trait_tests::test_blockchain_query_by_number_gets_canonical(
backend,
);
}
#[test]
fn in_memory_offchain_storage() {
let mut storage = InMemOffchainStorage::default();
assert_eq!(storage.get(b"A", b"B"), None);
assert_eq!(storage.get(b"B", b"A"), None);
+332 -247
View File
@@ -16,53 +16,52 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use sc_light::{
call_executor::{
GenesisCallExecutor,
check_execution_proof,
},
fetcher::LightDataChecker,
blockchain::{BlockchainCache, Blockchain},
backend::{Backend, GenesisOrUnavailableState},
use super::prepare_client_with_key_changes;
use parity_scale_codec::{Decode, Encode};
use parking_lot::Mutex;
use sc_block_builder::BlockBuilderProvider;
use sc_client_api::{
backend::NewBlockState,
blockchain::Info,
cht,
in_mem::{Backend as InMemBackend, Blockchain as InMemoryBlockchain},
AuxStore, Backend as ClientBackend, BlockBackend, BlockImportOperation, CallExecutor,
ChangesProof, ExecutionStrategy, FetchChecker, ProofProvider, ProvideChtRoots,
RemoteBodyRequest, RemoteCallRequest, RemoteChangesRequest, RemoteHeaderRequest,
RemoteReadChildRequest, RemoteReadRequest, Storage, StorageProof, StorageProvider,
};
use std::sync::Arc;
use sc_executor::{NativeExecutor, NativeVersion, RuntimeVersion, WasmExecutionMethod};
use sc_light::{
backend::{Backend, GenesisOrUnavailableState},
blockchain::{Blockchain, BlockchainCache},
call_executor::{check_execution_proof, GenesisCallExecutor},
fetcher::LightDataChecker,
};
use sp_api::{ProofRecorder, StorageTransactionCache};
use sp_blockchain::{
well_known_cache_keys, BlockStatus, CachedHeaderMetadata, Error as ClientError, HeaderBackend,
Result as ClientResult,
};
use sp_consensus::BlockOrigin;
use sp_core::{testing::TaskExecutor, NativeOrEncoded, H256};
use sp_externalities::Extensions;
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as _, HashFor, Header as HeaderT, NumberFor},
Digest, Justifications,
};
use std::collections::HashMap;
use parking_lot::Mutex;
use sp_state_machine::{ExecutionManager, OverlayedChanges};
use std::{cell::RefCell, collections::HashMap, panic::UnwindSafe, sync::Arc};
use substrate_test_runtime_client::{
runtime::{Hash, Block, Header}, TestClient, ClientBlockImportExt,
};
use sp_api::{StorageTransactionCache, ProofRecorder};
use sp_consensus::BlockOrigin;
use sc_executor::{NativeExecutor, WasmExecutionMethod, RuntimeVersion, NativeVersion};
use sp_core::{H256, NativeOrEncoded, testing::TaskExecutor};
use sc_client_api::{
blockchain::Info, backend::NewBlockState, Backend as ClientBackend, ProofProvider,
in_mem::{Backend as InMemBackend, Blockchain as InMemoryBlockchain}, ProvideChtRoots,
AuxStore, Storage, CallExecutor, cht, ExecutionStrategy, StorageProof, BlockImportOperation,
RemoteCallRequest, StorageProvider, ChangesProof, RemoteBodyRequest, RemoteReadRequest,
RemoteChangesRequest, FetchChecker, RemoteReadChildRequest, RemoteHeaderRequest, BlockBackend,
};
use sp_externalities::Extensions;
use sc_block_builder::BlockBuilderProvider;
use sp_blockchain::{
BlockStatus, Result as ClientResult, Error as ClientError, CachedHeaderMetadata,
HeaderBackend, well_known_cache_keys
};
use std::panic::UnwindSafe;
use std::cell::RefCell;
use sp_state_machine::{OverlayedChanges, ExecutionManager};
use parity_scale_codec::{Decode, Encode};
use super::prepare_client_with_key_changes;
use substrate_test_runtime_client::{
AccountKeyring, runtime::{self, Extrinsic},
runtime::{self, Block, Extrinsic, Hash, Header},
AccountKeyring, ClientBlockImportExt, TestClient,
};
use sp_core::{blake2_256, ChangesTrieConfiguration, storage::{well_known_keys, StorageKey, ChildInfo}};
use sp_core::{
blake2_256,
storage::{well_known_keys, ChildInfo, StorageKey},
ChangesTrieConfiguration,
};
use sp_state_machine::Backend as _;
pub type DummyBlockchain = Blockchain<DummyStorage>;
@@ -115,7 +114,8 @@ impl sp_blockchain::HeaderMetadata<Block> for DummyStorage {
type Error = ClientError;
fn header_metadata(&self, hash: Hash) -> Result<CachedHeaderMetadata<Block>, Self::Error> {
self.header(BlockId::hash(hash))?.map(|header| CachedHeaderMetadata::from(&header))
self.header(BlockId::hash(hash))?
.map(|header| CachedHeaderMetadata::from(&header))
.ok_or(ClientError::UnknownBlock("header not found".to_owned()))
}
fn insert_header_metadata(&self, _hash: Hash, _metadata: CachedHeaderMetadata<Block>) {}
@@ -127,9 +127,13 @@ impl AuxStore for DummyStorage {
'a,
'b: 'a,
'c: 'a,
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
D: IntoIterator<Item=&'a &'b [u8]>,
>(&self, insert: I, _delete: D) -> ClientResult<()> {
I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
D: IntoIterator<Item = &'a &'b [u8]>,
>(
&self,
insert: I,
_delete: D,
) -> ClientResult<()> {
for (k, v) in insert.into_iter() {
self.aux_store.lock().insert(k.to_vec(), v.to_vec());
}
@@ -182,9 +186,10 @@ impl ProvideChtRoots<Block> for DummyStorage {
cht::block_to_cht_number(cht_size, block)
.and_then(|cht_num| self.changes_tries_cht_roots.get(&cht_num))
.cloned()
.ok_or_else(|| ClientError::Backend(
format!("Test error: CHT for block #{} not found", block)
).into())
.ok_or_else(|| {
ClientError::Backend(format!("Test error: CHT for block #{} not found", block))
.into()
})
.map(Some)
}
}
@@ -210,7 +215,7 @@ impl CallExecutor<Block> for DummyCallExecutor {
fn contextual_call<
EM: Fn(
Result<NativeOrEncoded<R>, Self::Error>,
Result<NativeOrEncoded<R>, Self::Error>
Result<NativeOrEncoded<R>, Self::Error>,
) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> Result<R, sp_api::ApiError> + UnwindSafe,
@@ -220,17 +225,22 @@ impl CallExecutor<Block> for DummyCallExecutor {
_method: &str,
_call_data: &[u8],
_changes: &RefCell<OverlayedChanges>,
_storage_transaction_cache: Option<&RefCell<
StorageTransactionCache<
Block,
<Self::Backend as sc_client_api::backend::Backend<Block>>::State,
>
>>,
_storage_transaction_cache: Option<
&RefCell<
StorageTransactionCache<
Block,
<Self::Backend as sc_client_api::backend::Backend<Block>>::State,
>,
>,
>,
_execution_manager: ExecutionManager<EM>,
_native_call: Option<NC>,
_proof_recorder: &Option<ProofRecorder<Block>>,
_extensions: Option<Extensions>,
) -> ClientResult<NativeOrEncoded<R>> where ExecutionManager<EM>: Clone {
) -> ClientResult<NativeOrEncoded<R>>
where
ExecutionManager<EM>: Clone,
{
unreachable!()
}
@@ -243,7 +253,7 @@ impl CallExecutor<Block> for DummyCallExecutor {
_trie_state: &sp_state_machine::TrieBackend<S, HashFor<Block>>,
_overlay: &mut OverlayedChanges,
_method: &str,
_call_data: &[u8]
_call_data: &[u8],
) -> Result<(Vec<u8>, StorageProof), ClientError> {
unreachable!()
}
@@ -260,11 +270,11 @@ fn local_executor() -> NativeExecutor<substrate_test_runtime_client::LocalExecut
#[test]
fn local_state_is_created_when_genesis_state_is_available() {
let def = Default::default();
let header0 = substrate_test_runtime_client::runtime::Header::new(0, def, def, def, Default::default());
let header0 =
substrate_test_runtime_client::runtime::Header::new(0, def, def, def, Default::default());
let backend: Backend<_, BlakeTwo256> = Backend::new(
Arc::new(DummyBlockchain::new(DummyStorage::new())),
);
let backend: Backend<_, BlakeTwo256> =
Backend::new(Arc::new(DummyBlockchain::new(DummyStorage::new())));
let mut op = backend.begin_operation().unwrap();
op.set_block_data(header0, None, None, None, NewBlockState::Final).unwrap();
op.set_genesis_state(Default::default(), true).unwrap();
@@ -278,9 +288,8 @@ fn local_state_is_created_when_genesis_state_is_available() {
#[test]
fn unavailable_state_is_created_when_genesis_state_is_unavailable() {
let backend: Backend<_, BlakeTwo256> = Backend::new(
Arc::new(DummyBlockchain::new(DummyStorage::new())),
);
let backend: Backend<_, BlakeTwo256> =
Backend::new(Arc::new(DummyBlockchain::new(DummyStorage::new())));
match backend.state_at(BlockId::Number(0)).unwrap() {
GenesisOrUnavailableState::Unavailable => (),
@@ -305,11 +314,8 @@ fn execution_proof_is_generated_and_checked() {
let remote_header = remote_client.header(&remote_block_id).unwrap().unwrap();
// 'fetch' execution proof from remote node
let (remote_result, remote_execution_proof) = remote_client.execution_proof(
&remote_block_id,
method,
&[]
).unwrap();
let (remote_result, remote_execution_proof) =
remote_client.execution_proof(&remote_block_id, method, &[]).unwrap();
// check remote execution proof locally
let local_result = check_execution_proof::<_, _, BlakeTwo256>(
@@ -323,7 +329,8 @@ fn execution_proof_is_generated_and_checked() {
retry_count: None,
},
remote_execution_proof,
).unwrap();
)
.unwrap();
(remote_result, local_result)
}
@@ -333,17 +340,20 @@ fn execution_proof_is_generated_and_checked() {
let remote_header = remote_client.header(&remote_block_id).unwrap().unwrap();
// 'fetch' execution proof from remote node
let (_, remote_execution_proof) = remote_client.execution_proof(
&remote_block_id,
"Core_initialize_block",
&Header::new(
at,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
).encode(),
).unwrap();
let (_, remote_execution_proof) = remote_client
.execution_proof(
&remote_block_id,
"Core_initialize_block",
&Header::new(
at,
Default::default(),
Default::default(),
Default::default(),
Default::default(),
)
.encode(),
)
.unwrap();
// check remote execution proof locally
let execution_result = check_execution_proof::<_, _, BlakeTwo256>(
@@ -359,7 +369,8 @@ fn execution_proof_is_generated_and_checked() {
Default::default(),
remote_header.hash(),
remote_header.digest().clone(), // this makes next header wrong
).encode(),
)
.encode(),
retry_count: None,
},
remote_execution_proof,
@@ -379,7 +390,8 @@ fn execution_proof_is_generated_and_checked() {
BlockOrigin::Own,
remote_client.new_block(digest).unwrap().build().unwrap().block,
Justifications::from((*b"TEST", Default::default())),
)).unwrap();
))
.unwrap();
}
// check method that doesn't requires environment
@@ -401,22 +413,26 @@ fn execution_proof_is_generated_and_checked() {
fn code_is_executed_at_genesis_only() {
let backend = Arc::new(InMemBackend::<Block>::new());
let def = H256::default();
let header0 = substrate_test_runtime_client::runtime::Header::new(0, def, def, def, Default::default());
let header0 =
substrate_test_runtime_client::runtime::Header::new(0, def, def, def, Default::default());
let hash0 = header0.hash();
let header1 = substrate_test_runtime_client::runtime::Header::new(1, def, def, hash0, Default::default());
let header1 =
substrate_test_runtime_client::runtime::Header::new(1, def, def, hash0, Default::default());
let hash1 = header1.hash();
backend.blockchain().insert(hash0, header0, None, None, NewBlockState::Final).unwrap();
backend.blockchain().insert(hash1, header1, None, None, NewBlockState::Final).unwrap();
backend
.blockchain()
.insert(hash0, header0, None, None, NewBlockState::Final)
.unwrap();
backend
.blockchain()
.insert(hash1, header1, None, None, NewBlockState::Final)
.unwrap();
let genesis_executor = GenesisCallExecutor::new(backend, DummyCallExecutor);
assert_eq!(
genesis_executor.call(
&BlockId::Number(0),
"test_method",
&[],
ExecutionStrategy::NativeElseWasm,
None,
).unwrap(),
genesis_executor
.call(&BlockId::Number(0), "test_method", &[], ExecutionStrategy::NativeElseWasm, None,)
.unwrap(),
vec![42],
);
@@ -434,7 +450,6 @@ fn code_is_executed_at_genesis_only() {
}
}
type TestChecker = LightDataChecker<
NativeExecutor<substrate_test_runtime_client::LocalExecutor>,
BlakeTwo256,
@@ -448,27 +463,28 @@ fn prepare_for_read_proof_check() -> (TestChecker, Header, StorageProof, u32) {
let remote_block_id = BlockId::Number(0);
let remote_block_hash = remote_client.block_hash(0).unwrap().unwrap();
let mut remote_block_header = remote_client.header(&remote_block_id).unwrap().unwrap();
remote_block_header.state_root = remote_client.state_at(&remote_block_id).unwrap()
.storage_root(::std::iter::empty()).0.into();
remote_block_header.state_root = remote_client
.state_at(&remote_block_id)
.unwrap()
.storage_root(::std::iter::empty())
.0
.into();
// 'fetch' read proof from remote node
let heap_pages = remote_client.storage(&remote_block_id, &StorageKey(well_known_keys::HEAP_PAGES.to_vec()))
let heap_pages = remote_client
.storage(&remote_block_id, &StorageKey(well_known_keys::HEAP_PAGES.to_vec()))
.unwrap()
.and_then(|v| Decode::decode(&mut &v.0[..]).ok()).unwrap();
let remote_read_proof = remote_client.read_proof(
&remote_block_id,
&mut std::iter::once(well_known_keys::HEAP_PAGES),
).unwrap();
.and_then(|v| Decode::decode(&mut &v.0[..]).ok())
.unwrap();
let remote_read_proof = remote_client
.read_proof(&remote_block_id, &mut std::iter::once(well_known_keys::HEAP_PAGES))
.unwrap();
// check remote read proof locally
let local_storage = InMemoryBlockchain::<Block>::new();
local_storage.insert(
remote_block_hash,
remote_block_header.clone(),
None,
None,
NewBlockState::Final,
).unwrap();
local_storage
.insert(remote_block_hash, remote_block_header.clone(), None, None, NewBlockState::Final)
.unwrap();
let local_checker = LightDataChecker::new(
Arc::new(DummyBlockchain::new(DummyStorage::new())),
local_executor(),
@@ -478,45 +494,39 @@ fn prepare_for_read_proof_check() -> (TestChecker, Header, StorageProof, u32) {
}
fn prepare_for_read_child_proof_check() -> (TestChecker, Header, StorageProof, Vec<u8>) {
use substrate_test_runtime_client::DefaultTestClientBuilderExt;
use substrate_test_runtime_client::TestClientBuilderExt;
use substrate_test_runtime_client::{DefaultTestClientBuilderExt, TestClientBuilderExt};
let child_info = ChildInfo::new_default(b"child1");
let child_info = &child_info;
// prepare remote client
let remote_client = substrate_test_runtime_client::TestClientBuilder::new()
.add_extra_child_storage(
child_info,
b"key1".to_vec(),
b"value1".to_vec(),
).build();
.add_extra_child_storage(child_info, b"key1".to_vec(), b"value1".to_vec())
.build();
let remote_block_id = BlockId::Number(0);
let remote_block_hash = remote_client.block_hash(0).unwrap().unwrap();
let mut remote_block_header = remote_client.header(&remote_block_id).unwrap().unwrap();
remote_block_header.state_root = remote_client.state_at(&remote_block_id).unwrap()
.storage_root(::std::iter::empty()).0.into();
remote_block_header.state_root = remote_client
.state_at(&remote_block_id)
.unwrap()
.storage_root(::std::iter::empty())
.0
.into();
// 'fetch' child read proof from remote node
let child_value = remote_client.child_storage(
&remote_block_id,
child_info,
&StorageKey(b"key1".to_vec()),
).unwrap().unwrap().0;
let child_value = remote_client
.child_storage(&remote_block_id, child_info, &StorageKey(b"key1".to_vec()))
.unwrap()
.unwrap()
.0;
assert_eq!(b"value1"[..], child_value[..]);
let remote_read_proof = remote_client.read_child_proof(
&remote_block_id,
child_info,
&mut std::iter::once("key1".as_bytes()),
).unwrap();
let remote_read_proof = remote_client
.read_child_proof(&remote_block_id, child_info, &mut std::iter::once("key1".as_bytes()))
.unwrap();
// check locally
let local_storage = InMemoryBlockchain::<Block>::new();
local_storage.insert(
remote_block_hash,
remote_block_header.clone(),
None,
None,
NewBlockState::Final,
).unwrap();
local_storage
.insert(remote_block_hash, remote_block_header.clone(), None, None, NewBlockState::Final)
.unwrap();
let local_checker = LightDataChecker::new(
Arc::new(DummyBlockchain::new(DummyStorage::new())),
local_executor(),
@@ -533,18 +543,21 @@ fn prepare_for_header_proof_check(insert_cht: bool) -> (TestChecker, Hash, Heade
let block = remote_client.new_block(Default::default()).unwrap().build().unwrap().block;
futures::executor::block_on(remote_client.import(BlockOrigin::Own, block)).unwrap();
local_headers_hashes.push(
remote_client.block_hash(i + 1)
.map_err(|_| ClientError::Backend("TestError".into()))
remote_client
.block_hash(i + 1)
.map_err(|_| ClientError::Backend("TestError".into())),
);
}
// 'fetch' header proof from remote node
let remote_block_id = BlockId::Number(1);
let (remote_block_header, remote_header_proof) = remote_client.header_proof_with_cht_size(&remote_block_id, 4).unwrap();
let (remote_block_header, remote_header_proof) =
remote_client.header_proof_with_cht_size(&remote_block_id, 4).unwrap();
// check remote read proof locally
let local_storage = InMemoryBlockchain::<Block>::new();
let local_cht_root = cht::compute_root::<Header, BlakeTwo256, _>(4, 0, local_headers_hashes).unwrap();
let local_cht_root =
cht::compute_root::<Header, BlakeTwo256, _>(4, 0, local_headers_hashes).unwrap();
if insert_cht {
local_storage.insert_cht_root(1, local_cht_root);
}
@@ -557,7 +570,7 @@ fn prepare_for_header_proof_check(insert_cht: bool) -> (TestChecker, Hash, Heade
}
fn header_with_computed_extrinsics_root(extrinsics: Vec<Extrinsic>) -> Header {
use sp_trie::{TrieConfiguration, trie_types::Layout};
use sp_trie::{trie_types::Layout, TrieConfiguration};
let iter = extrinsics.iter().map(Encode::encode);
let extrinsics_root = Layout::<BlakeTwo256>::ordered_trie_root(iter);
@@ -567,66 +580,106 @@ fn header_with_computed_extrinsics_root(extrinsics: Vec<Extrinsic>) -> Header {
#[test]
fn storage_read_proof_is_generated_and_checked() {
let (local_checker, remote_block_header, remote_read_proof, heap_pages) = prepare_for_read_proof_check();
assert_eq!((&local_checker as &dyn FetchChecker<Block>).check_read_proof(&RemoteReadRequest::<Header> {
block: remote_block_header.hash(),
header: remote_block_header,
keys: vec![well_known_keys::HEAP_PAGES.to_vec()],
retry_count: None,
}, remote_read_proof).unwrap().remove(well_known_keys::HEAP_PAGES).unwrap().unwrap()[0], heap_pages as u8);
let (local_checker, remote_block_header, remote_read_proof, heap_pages) =
prepare_for_read_proof_check();
assert_eq!(
(&local_checker as &dyn FetchChecker<Block>)
.check_read_proof(
&RemoteReadRequest::<Header> {
block: remote_block_header.hash(),
header: remote_block_header,
keys: vec![well_known_keys::HEAP_PAGES.to_vec()],
retry_count: None,
},
remote_read_proof
)
.unwrap()
.remove(well_known_keys::HEAP_PAGES)
.unwrap()
.unwrap()[0],
heap_pages as u8
);
}
#[test]
fn storage_child_read_proof_is_generated_and_checked() {
let child_info = ChildInfo::new_default(&b"child1"[..]);
let (
local_checker,
remote_block_header,
remote_read_proof,
result,
) = prepare_for_read_child_proof_check();
assert_eq!((&local_checker as &dyn FetchChecker<Block>).check_read_child_proof(
&RemoteReadChildRequest::<Header> {
block: remote_block_header.hash(),
header: remote_block_header,
storage_key: child_info.prefixed_storage_key(),
keys: vec![b"key1".to_vec()],
retry_count: None,
},
remote_read_proof
).unwrap().remove(b"key1".as_ref()).unwrap().unwrap(), result);
let (local_checker, remote_block_header, remote_read_proof, result) =
prepare_for_read_child_proof_check();
assert_eq!(
(&local_checker as &dyn FetchChecker<Block>)
.check_read_child_proof(
&RemoteReadChildRequest::<Header> {
block: remote_block_header.hash(),
header: remote_block_header,
storage_key: child_info.prefixed_storage_key(),
keys: vec![b"key1".to_vec()],
retry_count: None,
},
remote_read_proof
)
.unwrap()
.remove(b"key1".as_ref())
.unwrap()
.unwrap(),
result
);
}
#[test]
fn header_proof_is_generated_and_checked() {
let (local_checker, local_cht_root, remote_block_header, remote_header_proof) = prepare_for_header_proof_check(true);
assert_eq!((&local_checker as &dyn FetchChecker<Block>).check_header_proof(&RemoteHeaderRequest::<Header> {
cht_root: local_cht_root,
block: 1,
retry_count: None,
}, Some(remote_block_header.clone()), remote_header_proof).unwrap(), remote_block_header);
let (local_checker, local_cht_root, remote_block_header, remote_header_proof) =
prepare_for_header_proof_check(true);
assert_eq!(
(&local_checker as &dyn FetchChecker<Block>)
.check_header_proof(
&RemoteHeaderRequest::<Header> {
cht_root: local_cht_root,
block: 1,
retry_count: None,
},
Some(remote_block_header.clone()),
remote_header_proof
)
.unwrap(),
remote_block_header
);
}
#[test]
fn check_header_proof_fails_if_cht_root_is_invalid() {
let (local_checker, _, mut remote_block_header, remote_header_proof) = prepare_for_header_proof_check(true);
let (local_checker, _, mut remote_block_header, remote_header_proof) =
prepare_for_header_proof_check(true);
remote_block_header.number = 100;
assert!((&local_checker as &dyn FetchChecker<Block>).check_header_proof(&RemoteHeaderRequest::<Header> {
cht_root: Default::default(),
block: 1,
retry_count: None,
}, Some(remote_block_header.clone()), remote_header_proof).is_err());
assert!((&local_checker as &dyn FetchChecker<Block>)
.check_header_proof(
&RemoteHeaderRequest::<Header> {
cht_root: Default::default(),
block: 1,
retry_count: None,
},
Some(remote_block_header.clone()),
remote_header_proof
)
.is_err());
}
#[test]
fn check_header_proof_fails_if_invalid_header_provided() {
let (local_checker, local_cht_root, mut remote_block_header, remote_header_proof) = prepare_for_header_proof_check(true);
let (local_checker, local_cht_root, mut remote_block_header, remote_header_proof) =
prepare_for_header_proof_check(true);
remote_block_header.number = 100;
assert!((&local_checker as &dyn FetchChecker<Block>).check_header_proof(&RemoteHeaderRequest::<Header> {
cht_root: local_cht_root,
block: 1,
retry_count: None,
}, Some(remote_block_header.clone()), remote_header_proof).is_err());
assert!((&local_checker as &dyn FetchChecker<Block>)
.check_header_proof(
&RemoteHeaderRequest::<Header> {
cht_root: local_cht_root,
block: 1,
retry_count: None,
},
Some(remote_block_header.clone()),
remote_header_proof
)
.is_err());
}
#[test]
@@ -647,9 +700,9 @@ fn changes_proof_is_generated_and_checked_when_headers_are_not_pruned() {
// 'fetch' changes proof from remote node
let key = StorageKey(key);
let remote_proof = remote_client.key_changes_proof(
begin_hash, end_hash, begin_hash, max_hash, None, &key
).unwrap();
let remote_proof = remote_client
.key_changes_proof(begin_hash, end_hash, begin_hash, max_hash, None, &key)
.unwrap();
// check proof on local client
let local_roots_range = local_roots.clone()[(begin - 1) as usize..].to_vec();
@@ -668,20 +721,23 @@ fn changes_proof_is_generated_and_checked_when_headers_are_not_pruned() {
storage_key: None,
retry_count: None,
};
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();
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
if local_result != expected_result {
panic!(
"Failed test {}: local = {:?}, expected = {:?}",
index,
local_result,
expected_result,
index, local_result, expected_result,
);
}
}
@@ -702,12 +758,17 @@ fn changes_proof_is_generated_and_checked_when_headers_are_pruned() {
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, None, &dave, 4
).unwrap();
let remote_proof = remote_client
.key_changes_proof_with_cht_size(b1, b4, b3, b4, None, &dave, 4)
.unwrap();
// prepare local checker, having a root of changes trie CHT#0
let local_cht_root = cht::compute_root::<Header, BlakeTwo256, _>(4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
let local_cht_root = cht::compute_root::<Header, BlakeTwo256, _>(
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(
@@ -732,12 +793,18 @@ fn changes_proof_is_generated_and_checked_when_headers_are_pruned() {
key: dave.0,
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();
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)]);
}
@@ -760,8 +827,9 @@ fn check_changes_proof_fails_if_proof_is_wrong() {
// 'fetch' changes proof from remote node
let key = StorageKey(key);
let remote_proof = remote_client.key_changes_proof(
begin_hash, end_hash, begin_hash, max_hash, None, &key).unwrap();
let remote_proof = remote_client
.key_changes_proof(begin_hash, end_hash, begin_hash, max_hash, None, &key)
.unwrap();
let local_roots_range = local_roots.clone()[(begin - 1) as usize..].to_vec();
let config = ChangesTrieConfiguration::new(4, 2);
@@ -781,34 +849,54 @@ fn check_changes_proof_fails_if_proof_is_wrong() {
};
// check proof on local client using max from the future
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());
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
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());
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: StorageProof::empty(),
}).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: StorageProof::empty(),
}).is_err());
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: StorageProof::empty(),
}
)
.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: StorageProof::empty(),
}
)
.is_err());
}
#[test]
@@ -817,7 +905,11 @@ fn check_changes_tries_proof_fails_if_proof_is_wrong() {
// (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, BlakeTwo256, _>(
4, 0, remote_roots.iter().cloned().map(|ct| Ok(Some(ct)))).unwrap();
4,
0,
remote_roots.iter().cloned().map(|ct| Ok(Some(ct))),
)
.unwrap();
let dave = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Dave.into())).to_vec();
let dave = StorageKey(dave);
@@ -828,9 +920,9 @@ fn check_changes_tries_proof_fails_if_proof_is_wrong() {
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, None, &dave, 4
).unwrap();
let remote_proof = remote_client
.key_changes_proof_with_cht_size(b1, b4, b3, b4, None, &dave, 4)
.unwrap();
// fails when changes trie CHT is missing from the local db
let local_checker = TestChecker::new(
@@ -838,8 +930,9 @@ fn check_changes_tries_proof_fails_if_proof_is_wrong() {
local_executor(),
Box::new(TaskExecutor::new()),
);
assert!(local_checker.check_changes_tries_proof(4, &remote_proof.roots,
remote_proof.roots_proof.clone()).is_err());
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();
@@ -849,17 +942,15 @@ fn check_changes_tries_proof_fails_if_proof_is_wrong() {
local_executor(),
Box::new(TaskExecutor::new()),
);
let result = local_checker.check_changes_tries_proof(
4, &remote_proof.roots, StorageProof::empty()
);
let result =
local_checker.check_changes_tries_proof(4, &remote_proof.roots, StorageProof::empty());
assert!(result.is_err());
}
#[test]
fn check_body_proof_faulty() {
let header = header_with_computed_extrinsics_root(
vec![Extrinsic::IncludeData(vec![1, 2, 3, 4])]
);
let header =
header_with_computed_extrinsics_root(vec![Extrinsic::IncludeData(vec![1, 2, 3, 4])]);
let block = Block::new(header.clone(), Vec::new());
let local_checker = TestChecker::new(
@@ -868,10 +959,7 @@ fn check_body_proof_faulty() {
Box::new(TaskExecutor::new()),
);
let body_request = RemoteBodyRequest {
header: header.clone(),
retry_count: None,
};
let body_request = RemoteBodyRequest { header: header.clone(), retry_count: None };
assert!(
local_checker.check_body_proof(&body_request, block.extrinsics).is_err(),
@@ -892,10 +980,7 @@ fn check_body_proof_of_same_data_should_succeed() {
Box::new(TaskExecutor::new()),
);
let body_request = RemoteBodyRequest {
header: header.clone(),
retry_count: None,
};
let body_request = RemoteBodyRequest { header: header.clone(), retry_count: None };
assert!(local_checker.check_body_proof(&body_request, block.extrinsics).is_ok());
}
File diff suppressed because it is too large Load Diff
+153 -133
View File
@@ -18,38 +18,27 @@
//! Service integration test utils.
use std::iter;
use std::sync::Arc;
use std::net::Ipv4Addr;
use std::pin::Pin;
use std::time::Duration;
use log::{info, debug};
use futures01::{Future, Stream, Poll};
use futures::{FutureExt as _, TryFutureExt as _};
use tempfile::TempDir;
use tokio::{runtime::Runtime, prelude::FutureExt};
use tokio::timer::Interval;
use sc_service::{
TaskManager,
SpawnTaskHandle,
GenericChainSpec,
ChainSpecExtension,
Configuration,
KeepBlocks, TransactionStorageMode,
config::{BasePath, DatabaseConfig, KeystoreConfig},
RuntimeGenesis,
Role,
Error,
TaskExecutor,
client::Client,
};
use sp_blockchain::HeaderBackend;
use sc_network::{multiaddr, Multiaddr};
use sc_network::config::{NetworkConfiguration, TransportConfig};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sc_transaction_pool_api::TransactionPool;
use sc_client_api::{Backend, CallExecutor};
use futures01::{Future, Poll, Stream};
use log::{debug, info};
use parking_lot::Mutex;
use sc_client_api::{Backend, CallExecutor};
use sc_network::{
config::{NetworkConfiguration, TransportConfig},
multiaddr, Multiaddr,
};
use sc_service::{
client::Client,
config::{BasePath, DatabaseConfig, KeystoreConfig},
ChainSpecExtension, Configuration, Error, GenericChainSpec, KeepBlocks, Role, RuntimeGenesis,
SpawnTaskHandle, TaskExecutor, TaskManager, TransactionStorageMode,
};
use sc_transaction_pool_api::TransactionPool;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use std::{iter, net::Ipv4Addr, pin::Pin, sync::Arc, time::Duration};
use tempfile::TempDir;
use tokio::{prelude::FutureExt, runtime::Runtime, timer::Interval};
#[cfg(test)]
mod client;
@@ -67,7 +56,9 @@ struct TestNet<G, E, F, L, U> {
nodes: usize,
}
pub trait TestNetNode: Clone + Future<Item = (), Error = sc_service::Error> + Send + 'static {
pub trait TestNetNode:
Clone + Future<Item = (), Error = sc_service::Error> + Send + 'static
{
type Block: BlockT;
type Backend: Backend<Self::Block>;
type Executor: CallExecutor<Self::Block> + Send + Sync;
@@ -76,7 +67,9 @@ pub trait TestNetNode: Clone + Future<Item = (), Error = sc_service::Error> + Se
fn client(&self) -> Arc<Client<Self::Backend, Self::Executor, Self::Block, Self::RuntimeApi>>;
fn transaction_pool(&self) -> Arc<Self::TransactionPool>;
fn network(&self) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>>;
fn network(
&self,
) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>>;
fn spawn_handle(&self) -> SpawnTaskHandle;
}
@@ -88,23 +81,21 @@ pub struct TestNetComponents<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> {
}
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool>
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool> {
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
{
pub fn new(
task_manager: TaskManager,
client: Arc<Client<TBackend, TExec, TBl, TRtApi>>,
network: Arc<sc_network::NetworkService<TBl, <TBl as BlockT>::Hash>>,
transaction_pool: Arc<TExPool>,
) -> Self {
Self {
client, transaction_pool, network,
task_manager: Arc::new(Mutex::new(task_manager)),
}
Self { client, transaction_pool, network, task_manager: Arc::new(Mutex::new(task_manager)) }
}
}
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Clone for
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool> {
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Clone
for TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
{
fn clone(&self) -> Self {
Self {
task_manager: self.task_manager.clone(),
@@ -115,8 +106,8 @@ TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool> {
}
}
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Future for
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Future
for TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
{
type Item = ();
type Error = sc_service::Error;
@@ -126,14 +117,14 @@ impl<TBl: BlockT, TBackend, TExec, TRtApi, TExPool> Future for
}
}
impl<TBl, TBackend, TExec, TRtApi, TExPool> TestNetNode for
TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
where
TBl: BlockT,
TBackend: sc_client_api::Backend<TBl> + Send + Sync + 'static,
TExec: CallExecutor<TBl> + Send + Sync + 'static,
TRtApi: Send + Sync + 'static,
TExPool: TransactionPool<Block = TBl> + Send + Sync + 'static,
impl<TBl, TBackend, TExec, TRtApi, TExPool> TestNetNode
for TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
where
TBl: BlockT,
TBackend: sc_client_api::Backend<TBl> + Send + Sync + 'static,
TExec: CallExecutor<TBl> + Send + Sync + 'static,
TRtApi: Send + Sync + 'static,
TExPool: TransactionPool<Block = TBl> + Send + Sync + 'static,
{
type Block = TBl;
type Backend = TBackend;
@@ -147,7 +138,9 @@ TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
fn transaction_pool(&self) -> Arc<Self::TransactionPool> {
self.transaction_pool.clone()
}
fn network(&self) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>> {
fn network(
&self,
) -> Arc<sc_network::NetworkService<Self::Block, <Self::Block as BlockT>::Hash>> {
self.network.clone()
}
fn spawn_handle(&self) -> SpawnTaskHandle {
@@ -156,33 +149,32 @@ TestNetComponents<TBl, TBackend, TExec, TRtApi, TExPool>
}
impl<G, E, F, L, U> TestNet<G, E, F, L, U>
where F: Clone + Send + 'static, L: Clone + Send +'static, U: Clone + Send + 'static
where
F: Clone + Send + 'static,
L: Clone + Send + 'static,
U: Clone + Send + 'static,
{
pub fn run_until_all_full<FP, LP>(
&mut self,
full_predicate: FP,
light_predicate: LP,
)
where
FP: Send + Fn(usize, &F) -> bool + 'static,
LP: Send + Fn(usize, &L) -> bool + 'static,
pub fn run_until_all_full<FP, LP>(&mut self, full_predicate: FP, light_predicate: LP)
where
FP: Send + Fn(usize, &F) -> bool + 'static,
LP: Send + Fn(usize, &L) -> bool + 'static,
{
let full_nodes = self.full_nodes.clone();
let light_nodes = self.light_nodes.clone();
let interval = Interval::new_interval(Duration::from_millis(100))
.map_err(|_| ())
.for_each(move |_| {
let full_ready = full_nodes.iter().all(|&(ref id, ref service, _, _)|
full_predicate(*id, service)
);
let full_ready = full_nodes
.iter()
.all(|&(ref id, ref service, _, _)| full_predicate(*id, service));
if !full_ready {
return Ok(());
return Ok(())
}
let light_ready = light_nodes.iter().all(|&(ref id, ref service, _)|
light_predicate(*id, service)
);
let light_ready = light_nodes
.iter()
.all(|&(ref id, ref service, _)| light_predicate(*id, service));
if !light_ready {
Ok(())
@@ -200,7 +192,10 @@ where F: Clone + Send + 'static, L: Clone + Send +'static, U: Clone + Send + 'st
}
}
fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'static + Send + Sync> (
fn node_config<
G: RuntimeGenesis + 'static,
E: ChainSpecExtension + Clone + 'static + Send + Sync,
>(
index: usize,
spec: &GenericChainSpec<G, E>,
role: Role,
@@ -208,8 +203,7 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
key_seed: Option<String>,
base_port: u16,
root: &TempDir,
) -> Configuration
{
) -> Configuration {
let root = root.path().join(format!("node-{}", index));
let mut network_config = NetworkConfiguration::new(
@@ -224,7 +218,7 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
network_config.listen_addresses.push(
iter::once(multiaddr::Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
.chain(iter::once(multiaddr::Protocol::Tcp(base_port + index as u16)))
.collect()
.collect(),
);
network_config.transport = TransportConfig::Normal {
@@ -241,14 +235,8 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
transaction_pool: Default::default(),
network: network_config,
keystore_remote: Default::default(),
keystore: KeystoreConfig::Path {
path: root.join("key"),
password: None
},
database: DatabaseConfig::RocksDb {
path: root.join("db"),
cache_size: 128,
},
keystore: KeystoreConfig::Path { path: root.join("key"), password: None },
database: DatabaseConfig::RocksDb { path: root.join("db"), cache_size: 128 },
state_cache_size: 16777216,
state_cache_child_ratio: None,
state_pruning: Default::default(),
@@ -284,7 +272,8 @@ fn node_config<G: RuntimeGenesis + 'static, E: ChainSpecExtension + Clone + 'sta
}
}
impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
impl<G, E, F, L, U> TestNet<G, E, F, L, U>
where
F: TestNetNode,
L: TestNetNode,
E: ChainSpecExtension + Clone + 'static + Send + Sync,
@@ -295,11 +284,8 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
spec: GenericChainSpec<G, E>,
full: impl Iterator<Item = impl FnOnce(Configuration) -> Result<(F, U), Error>>,
light: impl Iterator<Item = impl FnOnce(Configuration) -> Result<L, Error>>,
authorities: impl Iterator<Item = (
String,
impl FnOnce(Configuration) -> Result<(F, U), Error>
)>,
base_port: u16
authorities: impl Iterator<Item = (String, impl FnOnce(Configuration) -> Result<(F, U), Error>)>,
base_port: u16,
) -> TestNet<G, E, F, L, U> {
sp_tracing::try_init_simple();
fdlimit::raise_fd_limit();
@@ -322,7 +308,7 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
temp: &TempDir,
full: impl Iterator<Item = impl FnOnce(Configuration) -> Result<(F, U), Error>>,
light: impl Iterator<Item = impl FnOnce(Configuration) -> Result<L, Error>>,
authorities: impl Iterator<Item = (String, impl FnOnce(Configuration) -> Result<(F, U), Error>)>
authorities: impl Iterator<Item = (String, impl FnOnce(Configuration) -> Result<(F, U), Error>)>,
) {
let executor = self.runtime.executor();
let task_executor: TaskExecutor = {
@@ -330,7 +316,8 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
(move |fut: Pin<Box<dyn futures::Future<Output = ()> + Send>>, _| {
executor.spawn(fut.unit_error().compat());
async {}
}).into()
})
.into()
};
for (key, authority) in authorities {
@@ -344,10 +331,12 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
&temp,
);
let addr = node_config.network.listen_addresses.iter().next().unwrap().clone();
let (service, user_data) = authority(node_config).expect("Error creating test node service");
let (service, user_data) =
authority(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
let addr = addr.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
let addr = addr
.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.authority_nodes.push((self.nodes, service, user_data, addr));
self.nodes += 1;
}
@@ -366,7 +355,8 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
let (service, user_data) = full(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
let addr = addr.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
let addr = addr
.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.full_nodes.push((self.nodes, service, user_data, addr));
self.nodes += 1;
}
@@ -385,7 +375,8 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
let service = light(node_config).expect("Error creating test node service");
executor.spawn(service.clone().map_err(|_| ()));
let addr = addr.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
let addr = addr
.with(multiaddr::Protocol::P2p(service.network().local_peer_id().clone().into()));
self.light_nodes.push((self.nodes, service, addr));
self.nodes += 1;
}
@@ -393,7 +384,10 @@ impl<G, E, F, L, U> TestNet<G, E, F, L, U> where
}
fn tempdir_with_prefix(prefix: &str) -> TempDir {
tempfile::Builder::new().prefix(prefix).tempdir().expect("Error creating test dir")
tempfile::Builder::new()
.prefix(prefix)
.tempdir()
.expect("Error creating test dir")
}
pub fn connectivity<G, E, Fb, F, Lb, L>(
@@ -420,8 +414,8 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
let mut network = TestNet::new(
&temp,
spec.clone(),
(0..NUM_FULL_NODES).map(|_| { |cfg| full_builder(cfg).map(|s| (s, ())) }),
(0..NUM_LIGHT_NODES).map(|_| { |cfg| light_builder(cfg) }),
(0..NUM_FULL_NODES).map(|_| |cfg| full_builder(cfg).map(|s| (s, ()))),
(0..NUM_LIGHT_NODES).map(|_| |cfg| light_builder(cfg)),
// Note: this iterator is empty but we can't just use `iter::empty()`, otherwise
// the type of the closure cannot be inferred.
(0..0).map(|_| (String::new(), { |cfg| full_builder(cfg).map(|s| (s, ())) })),
@@ -430,11 +424,15 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
info!("Checking star topology");
let first_address = network.full_nodes[0].3.clone();
for (_, service, _, _) in network.full_nodes.iter().skip(1) {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
@@ -464,8 +462,8 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
let mut network = TestNet::new(
&temp,
spec,
(0..NUM_FULL_NODES).map(|_| { |cfg| full_builder(cfg).map(|s| (s, ())) }),
(0..NUM_LIGHT_NODES).map(|_| { |cfg| light_builder(cfg) }),
(0..NUM_FULL_NODES).map(|_| |cfg| full_builder(cfg).map(|s| (s, ()))),
(0..NUM_LIGHT_NODES).map(|_| |cfg| light_builder(cfg)),
// Note: this iterator is empty but we can't just use `iter::empty()`, otherwise
// the type of the closure cannot be inferred.
(0..0).map(|_| (String::new(), { |cfg| full_builder(cfg).map(|s| (s, ())) })),
@@ -477,14 +475,18 @@ pub fn connectivity<G, E, Fb, F, Lb, L>(
for i in 0..max_nodes {
if i != 0 {
if let Some((_, service, _, node_id)) = network.full_nodes.get(i) {
service.network().add_reserved_peer(address.to_string())
service
.network()
.add_reserved_peer(address.to_string())
.expect("Error adding reserved peer");
address = node_id.clone();
}
}
if let Some((_, service, node_id)) = network.light_nodes.get(i) {
service.network().add_reserved_peer(address.to_string())
service
.network()
.add_reserved_peer(address.to_string())
.expect("Error adding reserved peer");
address = node_id.clone();
}
@@ -512,7 +514,7 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
full_builder: Fb,
light_builder: Lb,
mut make_block_and_import: B,
mut extrinsic_factory: ExF
mut extrinsic_factory: ExF,
) where
Fb: Fn(Configuration) -> Result<(F, U), Error>,
F: TestNetNode,
@@ -532,8 +534,8 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
let mut network = TestNet::new(
&temp,
spec,
(0..NUM_FULL_NODES).map(|_| { |cfg| full_builder(cfg) }),
(0..NUM_LIGHT_NODES).map(|_| { |cfg| light_builder(cfg) }),
(0..NUM_FULL_NODES).map(|_| |cfg| full_builder(cfg)),
(0..NUM_LIGHT_NODES).map(|_| |cfg| light_builder(cfg)),
// Note: this iterator is empty but we can't just use `iter::empty()`, otherwise
// the type of the closure cannot be inferred.
(0..0).map(|_| (String::new(), { |cfg| full_builder(cfg) })),
@@ -542,7 +544,7 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
info!("Checking block sync");
let first_address = {
let &mut (_, ref first_service, ref mut first_user_data, _) = &mut network.full_nodes[0];
for i in 0 .. NUM_BLOCKS {
for i in 0..NUM_BLOCKS {
if i % 128 == 0 {
info!("Generating #{}", i + 1);
}
@@ -550,24 +552,29 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
make_block_and_import(&first_service, first_user_data);
}
let info = network.full_nodes[0].1.client().info();
network.full_nodes[0].1.network().new_best_block_imported(info.best_hash, info.best_number);
network.full_nodes[0]
.1
.network()
.new_best_block_imported(info.best_hash, info.best_number);
network.full_nodes[0].3.clone()
};
info!("Running sync");
for (_, service, _, _) in network.full_nodes.iter().skip(1) {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
|_index, service|
service.client().info().best_number == (NUM_BLOCKS as u32).into(),
|_index, service|
service.client().info().best_number == (NUM_BLOCKS as u32).into(),
|_index, service| service.client().info().best_number == (NUM_BLOCKS as u32).into(),
|_index, service| service.client().info().best_number == (NUM_BLOCKS as u32).into(),
);
info!("Checking extrinsic propagation");
@@ -577,9 +584,12 @@ pub fn sync<G, E, Fb, F, Lb, L, B, ExF, U>(
let extrinsic = extrinsic_factory(&first_service, first_user_data);
let source = sc_transaction_pool_api::TransactionSource::External;
futures::executor::block_on(
first_service.transaction_pool().submit_one(&best_block, source, extrinsic)
).expect("failed to submit extrinsic");
futures::executor::block_on(first_service.transaction_pool().submit_one(
&best_block,
source,
extrinsic,
))
.expect("failed to submit extrinsic");
network.run_until_all_full(
|_index, service| service.transaction_pool().ready().count() == 1,
@@ -591,7 +601,7 @@ pub fn consensus<G, E, Fb, F, Lb, L>(
spec: GenericChainSpec<G, E>,
full_builder: Fb,
light_builder: Lb,
authorities: impl IntoIterator<Item = String>
authorities: impl IntoIterator<Item = String>,
) where
Fb: Fn(Configuration) -> Result<F, Error>,
F: TestNetNode,
@@ -607,54 +617,64 @@ pub fn consensus<G, E, Fb, F, Lb, L>(
let mut network = TestNet::new(
&temp,
spec,
(0..NUM_FULL_NODES / 2).map(|_| { |cfg| full_builder(cfg).map(|s| (s, ())) }),
(0..NUM_LIGHT_NODES / 2).map(|_| { |cfg| light_builder(cfg) }),
authorities.into_iter().map(|key| (key, { |cfg| full_builder(cfg).map(|s| (s, ())) })),
(0..NUM_FULL_NODES / 2).map(|_| |cfg| full_builder(cfg).map(|s| (s, ()))),
(0..NUM_LIGHT_NODES / 2).map(|_| |cfg| light_builder(cfg)),
authorities
.into_iter()
.map(|key| (key, { |cfg| full_builder(cfg).map(|s| (s, ())) })),
30600,
);
info!("Checking consensus");
let first_address = network.authority_nodes[0].3.clone();
for (_, service, _, _) in network.full_nodes.iter() {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _, _) in network.authority_nodes.iter().skip(1) {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
|_index, service|
service.client().info().finalized_number >= (NUM_BLOCKS as u32 / 2).into(),
|_index, service|
service.client().info().best_number >= (NUM_BLOCKS as u32 / 2).into(),
|_index, service| {
service.client().info().finalized_number >= (NUM_BLOCKS as u32 / 2).into()
},
|_index, service| service.client().info().best_number >= (NUM_BLOCKS as u32 / 2).into(),
);
info!("Adding more peers");
network.insert_nodes(
&temp,
(0..NUM_FULL_NODES / 2).map(|_| { |cfg| full_builder(cfg).map(|s| (s, ())) }),
(0..NUM_LIGHT_NODES / 2).map(|_| { |cfg| light_builder(cfg) }),
(0..NUM_FULL_NODES / 2).map(|_| |cfg| full_builder(cfg).map(|s| (s, ()))),
(0..NUM_LIGHT_NODES / 2).map(|_| |cfg| light_builder(cfg)),
// Note: this iterator is empty but we can't just use `iter::empty()`, otherwise
// the type of the closure cannot be inferred.
(0..0).map(|_| (String::new(), { |cfg| full_builder(cfg).map(|s| (s, ())) })),
);
for (_, service, _, _) in network.full_nodes.iter() {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
for (_, service, _) in network.light_nodes.iter() {
service.network().add_reserved_peer(first_address.to_string())
service
.network()
.add_reserved_peer(first_address.to_string())
.expect("Error adding reserved peer");
}
network.run_until_all_full(
|_index, service|
service.client().info().finalized_number >= (NUM_BLOCKS as u32).into(),
|_index, service|
service.client().info().best_number >= (NUM_BLOCKS as u32).into(),
|_index, service| service.client().info().finalized_number >= (NUM_BLOCKS as u32).into(),
|_index, service| service.client().info().best_number >= (NUM_BLOCKS as u32).into(),
);
}