Upgrade tokio to 1.22.0 and replace async-std with tokio (#12646)

* Replace deprecated libp2p feature specs with correct ones

* Bump tokio to 1.21.2

* Replace async-std libp2p primitives with tokio ones

* minor: rustfmt

* Fix TestNet to run initialization in the tokio context

* Convert telemetry test from async-std to tokio

* Convert notifications tests from async-std to tokio

* Convert chain sync tests from async-std to tokio

* Ditch async-std completely

* Make executor mandatory

* Bump tokio to 1.22.0

* minor: rustfmt

* Explicitly use tokio runtime in tests

* Move more tests to explicit tokio runtime

* Explicitly set multithreaded runtime in tokio test

* minor: rustfmt

* minor: fix comment

* Replace async-std with tokio in MMR tests
This commit is contained in:
Dmitry Markin
2022-12-05 11:18:46 +03:00
committed by GitHub
parent 1943e25cb9
commit 5eb84f9cc6
60 changed files with 747 additions and 634 deletions
+57 -31
View File
@@ -47,7 +47,7 @@ use sc_consensus::{
use sc_network::{config::RequestResponseConfig, ProtocolName};
use sc_network_test::{
Block, BlockImportAdapter, FullPeerConfig, PassThroughVerifier, Peer, PeersClient,
PeersFullClient, TestNetFactory,
PeersFullClient, TestNetFactory, WithRuntime,
};
use sc_utils::notification::NotificationReceiver;
use serde::{Deserialize, Serialize};
@@ -64,7 +64,10 @@ use sp_runtime::{
};
use std::{collections::HashMap, marker::PhantomData, sync::Arc, task::Poll};
use substrate_test_runtime_client::{runtime::Header, ClientExt};
use tokio::{runtime::Runtime, time::Duration};
use tokio::{
runtime::{Handle, Runtime},
time::Duration,
};
const GENESIS_HASH: H256 = H256::zero();
fn beefy_gossip_proto_name() -> ProtocolName {
@@ -103,14 +106,23 @@ pub(crate) struct PeerData {
Mutex<Option<BeefyJustifsRequestHandler<Block, PeersFullClient>>>,
}
#[derive(Default)]
pub(crate) struct BeefyTestNet {
rt_handle: Handle,
peers: Vec<BeefyPeer>,
}
impl WithRuntime for BeefyTestNet {
fn with_runtime(rt_handle: Handle) -> Self {
BeefyTestNet { rt_handle, peers: Vec::new() }
}
fn rt_handle(&self) -> &Handle {
&self.rt_handle
}
}
impl BeefyTestNet {
pub(crate) fn new(n_authority: usize) -> Self {
let mut net = BeefyTestNet { peers: Vec::with_capacity(n_authority) };
pub(crate) fn new(rt_handle: Handle, n_authority: usize) -> Self {
let mut net = BeefyTestNet::with_runtime(rt_handle);
for i in 0..n_authority {
let (rx, cfg) = on_demand_justifications_protocol_config(GENESIS_HASH, None);
@@ -145,6 +157,7 @@ impl BeefyTestNet {
session_length: u64,
validator_set: &BeefyValidatorSet,
include_mmr_digest: bool,
runtime: &mut Runtime,
) {
self.peer(0).generate_blocks(count, BlockOrigin::File, |builder| {
let mut block = builder.build().unwrap().block;
@@ -162,7 +175,7 @@ impl BeefyTestNet {
block
});
self.block_until_sync();
runtime.block_on(self.wait_until_sync());
}
}
@@ -534,14 +547,14 @@ fn beefy_finalizing_blocks() {
let session_len = 10;
let min_block_delta = 4;
let mut net = BeefyTestNet::new(2);
let mut net = BeefyTestNet::new(runtime.handle().clone(), 2);
let api = Arc::new(two_validators::TestApi {});
let beefy_peers = peers.iter().enumerate().map(|(id, key)| (id, key, api.clone())).collect();
runtime.spawn(initialize_beefy(&mut net, beefy_peers, min_block_delta));
// push 42 blocks including `AuthorityChange` digests every 10 blocks.
net.generate_blocks_and_sync(42, session_len, &validator_set, true);
net.generate_blocks_and_sync(42, session_len, &validator_set, true, &mut runtime);
let net = Arc::new(Mutex::new(net));
@@ -574,13 +587,13 @@ fn lagging_validators() {
let session_len = 30;
let min_block_delta = 1;
let mut net = BeefyTestNet::new(2);
let mut net = BeefyTestNet::new(runtime.handle().clone(), 2);
let api = Arc::new(two_validators::TestApi {});
let beefy_peers = peers.iter().enumerate().map(|(id, key)| (id, key, api.clone())).collect();
runtime.spawn(initialize_beefy(&mut net, beefy_peers, min_block_delta));
// push 62 blocks including `AuthorityChange` digests every 30 blocks.
net.generate_blocks_and_sync(62, session_len, &validator_set, true);
net.generate_blocks_and_sync(62, session_len, &validator_set, true, &mut runtime);
let net = Arc::new(Mutex::new(net));
@@ -657,7 +670,7 @@ fn correct_beefy_payload() {
let session_len = 20;
let min_block_delta = 2;
let mut net = BeefyTestNet::new(4);
let mut net = BeefyTestNet::new(runtime.handle().clone(), 4);
// Alice, Bob, Charlie will vote on good payloads
let good_api = Arc::new(four_validators::TestApi {});
@@ -674,7 +687,7 @@ fn correct_beefy_payload() {
runtime.spawn(initialize_beefy(&mut net, bad_peers, min_block_delta));
// push 12 blocks
net.generate_blocks_and_sync(12, session_len, &validator_set, false);
net.generate_blocks_and_sync(12, session_len, &validator_set, false, &mut runtime);
let net = Arc::new(Mutex::new(net));
let peers = peers.into_iter().enumerate();
@@ -713,13 +726,15 @@ fn correct_beefy_payload() {
#[test]
fn beefy_importing_blocks() {
use futures::{executor::block_on, future::poll_fn, task::Poll};
use futures::{future::poll_fn, task::Poll};
use sc_block_builder::BlockBuilderProvider;
use sc_client_api::BlockBackend;
sp_tracing::try_init_simple();
let mut net = BeefyTestNet::new(2);
let runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 2);
let client = net.peer(0).client().clone();
let (mut block_import, _, peer_data) = net.make_block_import(client.clone());
@@ -744,11 +759,15 @@ fn beefy_importing_blocks() {
// Import without justifications.
let mut justif_recv = justif_stream.subscribe();
assert_eq!(
block_on(block_import.import_block(params(block.clone(), None), HashMap::new())).unwrap(),
runtime
.block_on(block_import.import_block(params(block.clone(), None), HashMap::new()))
.unwrap(),
ImportResult::Imported(ImportedAux { is_new_best: true, ..Default::default() }),
);
assert_eq!(
block_on(block_import.import_block(params(block, None), HashMap::new())).unwrap(),
runtime
.block_on(block_import.import_block(params(block, None), HashMap::new()))
.unwrap(),
ImportResult::AlreadyInChain
);
// Verify no BEEFY justifications present:
@@ -762,7 +781,7 @@ fn beefy_importing_blocks() {
None
);
// and none sent to BEEFY worker.
block_on(poll_fn(move |cx| {
runtime.block_on(poll_fn(move |cx| {
assert_eq!(justif_recv.poll_next_unpin(cx), Poll::Pending);
Poll::Ready(())
}));
@@ -783,7 +802,9 @@ fn beefy_importing_blocks() {
let hashof2 = block.header.hash();
let mut justif_recv = justif_stream.subscribe();
assert_eq!(
block_on(block_import.import_block(params(block, justif), HashMap::new())).unwrap(),
runtime
.block_on(block_import.import_block(params(block, justif), HashMap::new()))
.unwrap(),
ImportResult::Imported(ImportedAux {
bad_justification: false,
is_new_best: true,
@@ -802,7 +823,7 @@ fn beefy_importing_blocks() {
);
// but sent to BEEFY worker
// (worker will append it to backend when all previous mandatory justifs are there as well).
block_on(poll_fn(move |cx| {
runtime.block_on(poll_fn(move |cx| {
match justif_recv.poll_next_unpin(cx) {
Poll::Ready(Some(_justification)) => (),
v => panic!("unexpected value: {:?}", v),
@@ -826,7 +847,9 @@ fn beefy_importing_blocks() {
let hashof3 = block.header.hash();
let mut justif_recv = justif_stream.subscribe();
assert_eq!(
block_on(block_import.import_block(params(block, justif), HashMap::new())).unwrap(),
runtime
.block_on(block_import.import_block(params(block, justif), HashMap::new()))
.unwrap(),
ImportResult::Imported(ImportedAux {
// Still `false` because we don't want to fail import on bad BEEFY justifications.
bad_justification: false,
@@ -845,7 +868,7 @@ fn beefy_importing_blocks() {
None
);
// and none sent to BEEFY worker.
block_on(poll_fn(move |cx| {
runtime.block_on(poll_fn(move |cx| {
assert_eq!(justif_recv.poll_next_unpin(cx), Poll::Pending);
Poll::Ready(())
}));
@@ -865,13 +888,13 @@ fn voter_initialization() {
// Should vote on all mandatory blocks no matter the `min_block_delta`.
let min_block_delta = 10;
let mut net = BeefyTestNet::new(2);
let mut net = BeefyTestNet::new(runtime.handle().clone(), 2);
let api = Arc::new(two_validators::TestApi {});
let beefy_peers = peers.iter().enumerate().map(|(id, key)| (id, key, api.clone())).collect();
runtime.spawn(initialize_beefy(&mut net, beefy_peers, min_block_delta));
// push 26 blocks
net.generate_blocks_and_sync(26, session_len, &validator_set, false);
net.generate_blocks_and_sync(26, session_len, &validator_set, false, &mut runtime);
let net = Arc::new(Mutex::new(net));
// Finalize multiple blocks at once to get a burst of finality notifications right from start.
@@ -897,7 +920,7 @@ fn on_demand_beefy_justification_sync() {
let session_len = 5;
let min_block_delta = 5;
let mut net = BeefyTestNet::new(4);
let mut net = BeefyTestNet::new(runtime.handle().clone(), 4);
// Alice, Bob, Charlie start first and make progress through voting.
let api = Arc::new(four_validators::TestApi {});
@@ -914,7 +937,7 @@ fn on_demand_beefy_justification_sync() {
let dave_index = 3;
// push 30 blocks
net.generate_blocks_and_sync(30, session_len, &validator_set, false);
net.generate_blocks_and_sync(30, session_len, &validator_set, false, &mut runtime);
let fast_peers = fast_peers.into_iter().enumerate();
let net = Arc::new(Mutex::new(net));
@@ -968,11 +991,12 @@ fn on_demand_beefy_justification_sync() {
fn should_initialize_voter_at_genesis() {
let keys = &[BeefyKeyring::Alice];
let validator_set = ValidatorSet::new(make_beefy_ids(keys), 0).unwrap();
let mut net = BeefyTestNet::new(1);
let mut runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let backend = net.peer(0).client().as_backend();
// push 15 blocks with `AuthorityChange` digests every 10 blocks
net.generate_blocks_and_sync(15, 10, &validator_set, false);
net.generate_blocks_and_sync(15, 10, &validator_set, false, &mut runtime);
let mut finality = net.peer(0).client().as_client().finality_notification_stream().fuse();
@@ -1013,11 +1037,12 @@ fn should_initialize_voter_at_genesis() {
fn should_initialize_voter_when_last_final_is_session_boundary() {
let keys = &[BeefyKeyring::Alice];
let validator_set = ValidatorSet::new(make_beefy_ids(keys), 0).unwrap();
let mut net = BeefyTestNet::new(1);
let mut runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let backend = net.peer(0).client().as_backend();
// push 15 blocks with `AuthorityChange` digests every 10 blocks
net.generate_blocks_and_sync(15, 10, &validator_set, false);
net.generate_blocks_and_sync(15, 10, &validator_set, false, &mut runtime);
let mut finality = net.peer(0).client().as_client().finality_notification_stream().fuse();
@@ -1073,11 +1098,12 @@ fn should_initialize_voter_when_last_final_is_session_boundary() {
fn should_initialize_voter_at_latest_finalized() {
let keys = &[BeefyKeyring::Alice];
let validator_set = ValidatorSet::new(make_beefy_ids(keys), 0).unwrap();
let mut net = BeefyTestNet::new(1);
let mut runtime = Runtime::new().unwrap();
let mut net = BeefyTestNet::new(runtime.handle().clone(), 1);
let backend = net.peer(0).client().as_backend();
// push 15 blocks with `AuthorityChange` digests every 10 blocks
net.generate_blocks_and_sync(15, 10, &validator_set, false);
net.generate_blocks_and_sync(15, 10, &validator_set, false, &mut runtime);
let mut finality = net.peer(0).client().as_client().finality_notification_stream().fuse();