Make BlockImport and Verifier async (#8472)

* Make grandpa work

* Introduce `SharedData`

* Add test and fix bugs

* Switch to `SharedData`

* Make grandpa tests working

* More Babe work

* Make it async

* Fix fix

* Use `async_trait` in sc-consensus-slots

This makes the code a little bit easier to read and also expresses that
there can always only be one call at a time to `on_slot`.

* Make grandpa tests compile

* More Babe tests work

* Fix network test

* Start fixing service test

* Finish service-test

* Fix sc-consensus-aura

* Fix fix fix

* More fixes

* Make everything compile *yeah*

* Fix build when we have Rust 1.51

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/common/src/shared_data.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/babe/src/tests.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Update client/consensus/babe/src/tests.rs

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>

* Fix warning

Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2021-03-30 11:19:49 +02:00
committed by GitHub
parent 2998b42311
commit 217c4be226
65 changed files with 1241 additions and 715 deletions
+31 -36
View File
@@ -28,9 +28,9 @@ use sc_network_test::{
use sc_network::config::ProtocolConfig;
use parking_lot::{RwLock, Mutex};
use futures_timer::Delay;
use futures::executor::block_on;
use tokio::runtime::{Runtime, Handle};
use sp_keyring::Ed25519Keyring;
use sc_client_api::backend::TransactionFor;
use sp_blockchain::Result;
use sp_api::{ApiRef, ProvideRuntimeApi};
use substrate_test_runtime_client::runtime::BlockNumber;
@@ -43,7 +43,9 @@ use sp_runtime::{Justifications, traits::{Block as BlockT, Header as HeaderT}};
use sp_runtime::generic::{BlockId, DigestItem};
use sp_core::H256;
use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore};
use sp_finality_grandpa::{GRANDPA_ENGINE_ID, AuthorityList, EquivocationProof, GrandpaApi, OpaqueKeyOwnershipProof};
use sp_finality_grandpa::{
GRANDPA_ENGINE_ID, AuthorityList, EquivocationProof, GrandpaApi, OpaqueKeyOwnershipProof,
};
use authorities::AuthoritySet;
use sc_block_builder::BlockBuilderProvider;
@@ -54,7 +56,13 @@ use sp_application_crypto::key_types::GRANDPA;
type TestLinkHalf =
LinkHalf<Block, PeersFullClient, LongestChain<substrate_test_runtime_client::Backend, Block>>;
type PeerData = Mutex<Option<TestLinkHalf>>;
type GrandpaPeer = Peer<PeerData>;
type GrandpaPeer = Peer<PeerData, GrandpaBlockImport>;
type GrandpaBlockImport = crate::GrandpaBlockImport<
substrate_test_runtime_client::Backend,
Block,
PeersFullClient,
LongestChain<substrate_test_runtime_client::Backend, Block>
>;
struct GrandpaTestNet {
peers: Vec<GrandpaPeer>,
@@ -93,6 +101,7 @@ impl GrandpaTestNet {
impl TestNetFactory for GrandpaTestNet {
type Verifier = PassThroughVerifier;
type PeerData = PeerData;
type BlockImport = GrandpaBlockImport;
/// Create new test network with peers and given config.
fn from_config(_config: &ProtocolConfig) -> Self {
@@ -124,9 +133,9 @@ impl TestNetFactory for GrandpaTestNet {
PassThroughVerifier::new(false) // use non-instant finality.
}
fn make_block_import<Transaction>(&self, client: PeersClient)
fn make_block_import(&self, client: PeersClient)
-> (
BlockImportAdapter<Transaction>,
BlockImportAdapter<Self::BlockImport>,
Option<BoxJustificationImport<Block>>,
PeerData,
)
@@ -141,7 +150,7 @@ impl TestNetFactory for GrandpaTestNet {
).expect("Could not create block import for fresh peer.");
let justification_import = Box::new(import.clone());
(
BlockImportAdapter::new_full(import),
BlockImportAdapter::new(import),
Some(justification_import),
Mutex::new(Some(link)),
)
@@ -820,11 +829,7 @@ fn allows_reimporting_change_blocks() {
let mut net = GrandpaTestNet::new(api.clone(), 3, 0);
let client = net.peer(0).client().clone();
let (mut block_import, ..) = net.make_block_import::<
TransactionFor<substrate_test_runtime_client::Backend, Block>
>(
client.clone(),
);
let (mut block_import, ..) = net.make_block_import(client.clone());
let full_client = client.as_full().unwrap();
let builder = full_client.new_block_at(&BlockId::Number(0), Default::default(), false).unwrap();
@@ -844,7 +849,7 @@ fn allows_reimporting_change_blocks() {
};
assert_eq!(
block_import.import_block(block(), HashMap::new()).unwrap(),
block_on(block_import.import_block(block(), HashMap::new())).unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: true,
clear_justification_requests: false,
@@ -855,7 +860,7 @@ fn allows_reimporting_change_blocks() {
);
assert_eq!(
block_import.import_block(block(), HashMap::new()).unwrap(),
block_on(block_import.import_block(block(), HashMap::new())).unwrap(),
ImportResult::AlreadyInChain
);
}
@@ -869,11 +874,7 @@ fn test_bad_justification() {
let mut net = GrandpaTestNet::new(api.clone(), 3, 0);
let client = net.peer(0).client().clone();
let (mut block_import, ..) = net.make_block_import::<
TransactionFor<substrate_test_runtime_client::Backend, Block>
>(
client.clone(),
);
let (mut block_import, ..) = net.make_block_import(client.clone());
let full_client = client.as_full().expect("only full clients are used in test");
let builder = full_client.new_block_at(&BlockId::Number(0), Default::default(), false).unwrap();
@@ -895,7 +896,7 @@ fn test_bad_justification() {
};
assert_eq!(
block_import.import_block(block(), HashMap::new()).unwrap(),
block_on(block_import.import_block(block(), HashMap::new())).unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: true,
clear_justification_requests: false,
@@ -906,7 +907,7 @@ fn test_bad_justification() {
);
assert_eq!(
block_import.import_block(block(), HashMap::new()).unwrap(),
block_on(block_import.import_block(block(), HashMap::new())).unwrap(),
ImportResult::AlreadyInChain
);
}
@@ -950,9 +951,7 @@ fn voter_persists_its_votes() {
let set_state = {
let bob_client = net.peer(1).client().clone();
let (_, _, link) = net
.make_block_import::<
TransactionFor<substrate_test_runtime_client::Backend, Block>
>(bob_client);
.make_block_import(bob_client);
let LinkHalf { persistent_data, .. } = link.lock().take().unwrap();
let PersistentData { set_state, .. } = persistent_data;
set_state
@@ -1019,9 +1018,7 @@ fn voter_persists_its_votes() {
let alice_client = net.peer(0).client().clone();
let (_block_import, _, link) = net
.make_block_import::<
TransactionFor<substrate_test_runtime_client::Backend, Block>
>(alice_client);
.make_block_import(alice_client);
let link = link.lock().take().unwrap();
let grandpa_params = GrandpaParams {
@@ -1422,7 +1419,7 @@ fn grandpa_environment_respects_voting_rules() {
// the unrestricted environment should just return the best block
assert_eq!(
futures::executor::block_on(unrestricted_env.best_chain_containing(
block_on(unrestricted_env.best_chain_containing(
peer.client().info().finalized_hash
)).unwrap().unwrap().1,
21,
@@ -1431,14 +1428,14 @@ fn grandpa_environment_respects_voting_rules() {
// both the other environments should return block 16, which is 3/4 of the
// way in the unfinalized chain
assert_eq!(
futures::executor::block_on(three_quarters_env.best_chain_containing(
block_on(three_quarters_env.best_chain_containing(
peer.client().info().finalized_hash
)).unwrap().unwrap().1,
16,
);
assert_eq!(
futures::executor::block_on(default_env.best_chain_containing(
block_on(default_env.best_chain_containing(
peer.client().info().finalized_hash
)).unwrap().unwrap().1,
16,
@@ -1449,7 +1446,7 @@ fn grandpa_environment_respects_voting_rules() {
// the 3/4 environment should propose block 21 for voting
assert_eq!(
futures::executor::block_on(three_quarters_env.best_chain_containing(
block_on(three_quarters_env.best_chain_containing(
peer.client().info().finalized_hash
)).unwrap().unwrap().1,
21,
@@ -1458,7 +1455,7 @@ fn grandpa_environment_respects_voting_rules() {
// while the default environment will always still make sure we don't vote
// on the best block (2 behind)
assert_eq!(
futures::executor::block_on(default_env.best_chain_containing(
block_on(default_env.best_chain_containing(
peer.client().info().finalized_hash
)).unwrap().unwrap().1,
19,
@@ -1471,7 +1468,7 @@ fn grandpa_environment_respects_voting_rules() {
// best block, there's a hard rule that we can't cast any votes lower than
// the given base (#21).
assert_eq!(
futures::executor::block_on(default_env.best_chain_containing(
block_on(default_env.best_chain_containing(
peer.client().info().finalized_hash
)).unwrap().unwrap().1,
21,
@@ -1557,9 +1554,7 @@ fn imports_justification_for_regular_blocks_on_import() {
let mut net = GrandpaTestNet::new(api.clone(), 1, 0);
let client = net.peer(0).client().clone();
let (mut block_import, ..) = net.make_block_import::<
TransactionFor<substrate_test_runtime_client::Backend, Block>
>(client.clone());
let (mut block_import, ..) = net.make_block_import(client.clone());
let full_client = client.as_full().expect("only full clients are used in test");
let builder = full_client.new_block_at(&BlockId::Number(0), Default::default(), false).unwrap();
@@ -1607,7 +1602,7 @@ fn imports_justification_for_regular_blocks_on_import() {
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
assert_eq!(
block_import.import_block(import, HashMap::new()).unwrap(),
block_on(block_import.import_block(import, HashMap::new())).unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: false,
clear_justification_requests: false,