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
@@ -46,3 +46,4 @@ sp-keyring = { version = "7.0.0", path = "../../../primitives/keyring" }
sp-timestamp = { version = "4.0.0-dev", path = "../../../primitives/timestamp" }
sp-tracing = { version = "6.0.0", path = "../../../primitives/tracing" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" }
tokio = { version = "1.22.0" }
+36 -23
View File
@@ -633,7 +633,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use futures::executor;
use parking_lot::Mutex;
use sc_block_builder::BlockBuilderProvider;
use sc_client_api::BlockchainEvents;
@@ -659,6 +658,7 @@ mod tests {
runtime::{Header, H256},
TestClient,
};
use tokio::runtime::{Handle, Runtime};
const SLOT_DURATION_MS: u64 = 1000;
@@ -716,11 +716,20 @@ mod tests {
>;
type AuraPeer = Peer<(), PeersClient>;
#[derive(Default)]
pub struct AuraTestNet {
rt_handle: Handle,
peers: Vec<AuraPeer>,
}
impl WithRuntime for AuraTestNet {
fn with_runtime(rt_handle: Handle) -> Self {
AuraTestNet { rt_handle, peers: Vec::new() }
}
fn rt_handle(&self) -> &Handle {
&self.rt_handle
}
}
impl TestNetFactory for AuraTestNet {
type Verifier = AuraVerifier;
type PeerData = ();
@@ -772,7 +781,8 @@ mod tests {
#[test]
fn authoring_blocks() {
sp_tracing::try_init_simple();
let net = AuraTestNet::new(3);
let runtime = Runtime::new().unwrap();
let net = AuraTestNet::new(runtime.handle().clone(), 3);
let peers = &[(0, Keyring::Alice), (1, Keyring::Bob), (2, Keyring::Charlie)];
@@ -838,7 +848,7 @@ mod tests {
);
}
executor::block_on(future::select(
runtime.block_on(future::select(
future::poll_fn(move |cx| {
net.lock().poll(cx);
Poll::<()>::Pending
@@ -865,7 +875,8 @@ mod tests {
#[test]
fn current_node_authority_should_claim_slot() {
let net = AuraTestNet::new(4);
let runtime = Runtime::new().unwrap();
let net = AuraTestNet::new(runtime.handle().clone(), 4);
let mut authorities = vec![
Keyring::Alice.public().into(),
@@ -909,19 +920,20 @@ mod tests {
Default::default(),
Default::default(),
);
assert!(executor::block_on(worker.claim_slot(&head, 0.into(), &authorities)).is_none());
assert!(executor::block_on(worker.claim_slot(&head, 1.into(), &authorities)).is_none());
assert!(executor::block_on(worker.claim_slot(&head, 2.into(), &authorities)).is_none());
assert!(executor::block_on(worker.claim_slot(&head, 3.into(), &authorities)).is_some());
assert!(executor::block_on(worker.claim_slot(&head, 4.into(), &authorities)).is_none());
assert!(executor::block_on(worker.claim_slot(&head, 5.into(), &authorities)).is_none());
assert!(executor::block_on(worker.claim_slot(&head, 6.into(), &authorities)).is_none());
assert!(executor::block_on(worker.claim_slot(&head, 7.into(), &authorities)).is_some());
assert!(runtime.block_on(worker.claim_slot(&head, 0.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 1.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 2.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 3.into(), &authorities)).is_some());
assert!(runtime.block_on(worker.claim_slot(&head, 4.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 5.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 6.into(), &authorities)).is_none());
assert!(runtime.block_on(worker.claim_slot(&head, 7.into(), &authorities)).is_some());
}
#[test]
fn on_slot_returns_correct_block() {
let net = AuraTestNet::new(4);
let runtime = Runtime::new().unwrap();
let net = AuraTestNet::new(runtime.handle().clone(), 4);
let keystore_path = tempfile::tempdir().expect("Creates keystore path");
let keystore = LocalKeystore::open(keystore_path.path(), None).expect("Creates keystore.");
@@ -957,15 +969,16 @@ mod tests {
let head = client.header(&BlockId::Number(0)).unwrap().unwrap();
let res = executor::block_on(worker.on_slot(SlotInfo {
slot: 0.into(),
ends_at: Instant::now() + Duration::from_secs(100),
create_inherent_data: Box::new(()),
duration: Duration::from_millis(1000),
chain_head: head,
block_size_limit: None,
}))
.unwrap();
let res = runtime
.block_on(worker.on_slot(SlotInfo {
slot: 0.into(),
ends_at: Instant::now() + Duration::from_secs(100),
create_inherent_data: Box::new(()),
duration: Duration::from_millis(1000),
chain_head: head,
block_size_limit: None,
}))
.unwrap();
// The returned block should be imported and we should be able to get its header by now.
assert!(client.header(&BlockId::Hash(res.block.hash())).unwrap().is_some());
@@ -58,3 +58,4 @@ sc-network-test = { version = "0.8.0", path = "../../network/test" }
sp-timestamp = { version = "4.0.0-dev", path = "../../../primitives/timestamp" }
sp-tracing = { version = "6.0.0", path = "../../../primitives/tracing" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" }
tokio = "1.22.0"
@@ -32,7 +32,7 @@ sp-runtime = { version = "7.0.0", path = "../../../../primitives/runtime" }
[dev-dependencies]
serde_json = "1.0.85"
tempfile = "3.1.0"
tokio = "1.17.0"
tokio = "1.22.0"
sc-consensus = { version = "0.10.0-dev", path = "../../../consensus/common" }
sc-keystore = { version = "4.0.0-dev", path = "../../../keystore" }
sp-keyring = { version = "7.0.0", path = "../../../../primitives/keyring" }
+77 -20
View File
@@ -20,7 +20,6 @@
use super::*;
use authorship::claim_slot;
use futures::executor::block_on;
use log::debug;
use rand_chacha::{
rand_core::{RngCore, SeedableRng},
@@ -50,6 +49,7 @@ use sp_runtime::{
};
use sp_timestamp::Timestamp;
use std::{cell::RefCell, task::Poll, time::Duration};
use tokio::runtime::{Handle, Runtime};
type Item = DigestItem;
@@ -227,11 +227,20 @@ where
type BabePeer = Peer<Option<PeerData>, BabeBlockImport>;
#[derive(Default)]
pub struct BabeTestNet {
rt_handle: Handle,
peers: Vec<BabePeer>,
}
impl WithRuntime for BabeTestNet {
fn with_runtime(rt_handle: Handle) -> Self {
BabeTestNet { rt_handle, peers: Vec::new() }
}
fn rt_handle(&self) -> &Handle {
&self.rt_handle
}
}
type TestHeader = <TestBlock as BlockT>::Header;
type TestSelectChain =
@@ -361,7 +370,8 @@ impl TestNetFactory for BabeTestNet {
#[should_panic]
fn rejects_empty_block() {
sp_tracing::try_init_simple();
let mut net = BabeTestNet::new(3);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 3);
let block_builder = |builder: BlockBuilder<_, _, _>| builder.build().unwrap().block;
net.mut_peers(|peer| {
peer[0].generate_blocks(1, BlockOrigin::NetworkInitialSync, block_builder);
@@ -380,7 +390,9 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static
let mutator = Arc::new(mutator) as Mutator;
MUTATOR.with(|m| *m.borrow_mut() = mutator.clone());
let net = BabeTestNet::new(3);
let runtime = Runtime::new().unwrap();
let net = BabeTestNet::new(runtime.handle().clone(), 3);
let peers = [Sr25519Keyring::Alice, Sr25519Keyring::Bob, Sr25519Keyring::Charlie];
@@ -457,7 +469,7 @@ fn run_one_test(mutator: impl Fn(&mut TestHeader, Stage) + Send + Sync + 'static
.expect("Starts babe"),
);
}
block_on(future::select(
runtime.block_on(future::select(
futures::future::poll_fn(move |cx| {
let mut net = net.lock();
net.poll(cx);
@@ -594,8 +606,9 @@ fn propose_and_import_block<Transaction: Send + 'static>(
slot: Option<Slot>,
proposer_factory: &mut DummyFactory,
block_import: &mut BoxBlockImport<TestBlock, Transaction>,
runtime: &Runtime,
) -> Hash {
let mut proposer = block_on(proposer_factory.init(parent)).unwrap();
let mut proposer = runtime.block_on(proposer_factory.init(parent)).unwrap();
let slot = slot.unwrap_or_else(|| {
let parent_pre_digest = find_pre_digest::<TestBlock>(parent).unwrap();
@@ -611,7 +624,7 @@ fn propose_and_import_block<Transaction: Send + 'static>(
let parent_hash = parent.hash();
let mut block = block_on(proposer.propose_with(pre_digest)).unwrap().block;
let mut block = runtime.block_on(proposer.propose_with(pre_digest)).unwrap().block;
let epoch_descriptor = proposer_factory
.epoch_changes
@@ -647,7 +660,8 @@ fn propose_and_import_block<Transaction: Send + 'static>(
import
.insert_intermediate(INTERMEDIATE_KEY, BabeIntermediate::<TestBlock> { epoch_descriptor });
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
let import_result = block_on(block_import.import_block(import, Default::default())).unwrap();
let import_result =
runtime.block_on(block_import.import_block(import, Default::default())).unwrap();
match import_result {
ImportResult::Imported(_) => {},
@@ -666,13 +680,14 @@ fn propose_and_import_blocks<Transaction: Send + 'static>(
block_import: &mut BoxBlockImport<TestBlock, Transaction>,
parent_id: BlockId<TestBlock>,
n: usize,
runtime: &Runtime,
) -> Vec<Hash> {
let mut hashes = Vec::with_capacity(n);
let mut parent_header = client.header(&parent_id).unwrap().unwrap();
for _ in 0..n {
let block_hash =
propose_and_import_block(&parent_header, None, proposer_factory, block_import);
propose_and_import_block(&parent_header, None, proposer_factory, block_import, runtime);
hashes.push(block_hash);
parent_header = client.header(&BlockId::Hash(block_hash)).unwrap().unwrap();
}
@@ -682,7 +697,8 @@ fn propose_and_import_blocks<Transaction: Send + 'static>(
#[test]
fn importing_block_one_sets_genesis_epoch() {
let mut net = BabeTestNet::new(1);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
@@ -704,6 +720,7 @@ fn importing_block_one_sets_genesis_epoch() {
Some(999.into()),
&mut proposer_factory,
&mut block_import,
&runtime,
);
let genesis_epoch = Epoch::genesis(&data.link.config, 999.into());
@@ -721,7 +738,8 @@ fn importing_block_one_sets_genesis_epoch() {
#[test]
fn revert_prunes_epoch_changes_and_removes_weights() {
let mut net = BabeTestNet::new(1);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
@@ -739,7 +757,14 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, parent_id, n)
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
// Test scenario.
@@ -801,7 +826,8 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
#[test]
fn revert_not_allowed_for_finalized() {
let mut net = BabeTestNet::new(1);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
@@ -818,7 +844,14 @@ fn revert_not_allowed_for_finalized() {
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, parent_id, n)
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
let canon = propose_and_import_blocks_wrap(BlockId::Number(0), 3);
@@ -839,7 +872,8 @@ fn revert_not_allowed_for_finalized() {
#[test]
fn importing_epoch_change_block_prunes_tree() {
let mut net = BabeTestNet::new(1);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
@@ -856,7 +890,14 @@ fn importing_epoch_change_block_prunes_tree() {
};
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, parent_id, n)
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
// This is the block tree that we're going to use in this test. Each node
@@ -916,7 +957,8 @@ fn importing_epoch_change_block_prunes_tree() {
#[test]
#[should_panic]
fn verify_slots_are_strictly_increasing() {
let mut net = BabeTestNet::new(1);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
@@ -939,13 +981,20 @@ fn verify_slots_are_strictly_increasing() {
Some(999.into()),
&mut proposer_factory,
&mut block_import,
&runtime,
);
let b1 = client.header(&BlockId::Hash(b1)).unwrap().unwrap();
// we should fail to import this block since the slot number didn't increase.
// we will panic due to the `PanickingBlockImport` defined above.
propose_and_import_block(&b1, Some(999.into()), &mut proposer_factory, &mut block_import);
propose_and_import_block(
&b1,
Some(999.into()),
&mut proposer_factory,
&mut block_import,
&runtime,
);
}
#[test]
@@ -980,7 +1029,8 @@ fn babe_transcript_generation_match() {
#[test]
fn obsolete_blocks_aux_data_cleanup() {
let mut net = BabeTestNet::new(1);
let runtime = Runtime::new().unwrap();
let mut net = BabeTestNet::new(runtime.handle().clone(), 1);
let peer = net.peer(0);
let data = peer.data.as_ref().expect("babe link set up during initialization");
@@ -1003,7 +1053,14 @@ fn obsolete_blocks_aux_data_cleanup() {
let mut block_import = data.block_import.lock().take().expect("import set up during init");
let mut propose_and_import_blocks_wrap = |parent_id, n| {
propose_and_import_blocks(&client, &mut proposer_factory, &mut block_import, parent_id, n)
propose_and_import_blocks(
&client,
&mut proposer_factory,
&mut block_import,
parent_id,
n,
&runtime,
)
};
let aux_data_check = |hashes: &[Hash], expected: bool| {
@@ -42,7 +42,7 @@ sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" }
sp-timestamp = { version = "4.0.0-dev", path = "../../../primitives/timestamp" }
[dev-dependencies]
tokio = { version = "1.17.0", features = ["rt-multi-thread", "macros"] }
tokio = { version = "1.22.0", features = ["rt-multi-thread", "macros"] }
sc-basic-authorship = { version = "0.10.0-dev", path = "../../basic-authorship" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" }
substrate-test-runtime-transaction-pool = { version = "2.0.0", path = "../../../test-utils/runtime/transaction-pool" }