From a92d95d60fd26a09e3a77908658995d9548a7638 Mon Sep 17 00:00:00 2001 From: gilescope Date: Sat, 10 Jul 2021 08:03:32 +0100 Subject: [PATCH] split consensus --- Cargo.lock | 56 +++- Cargo.toml | 1 + bin/node-template/node/Cargo.toml | 1 + bin/node-template/node/src/service.rs | 2 +- bin/node/cli/Cargo.toml | 1 + bin/node/cli/src/service.rs | 5 +- bin/node/executor/Cargo.toml | 1 + bin/node/executor/tests/basic.rs | 2 +- bin/node/testing/Cargo.toml | 1 + bin/node/testing/src/bench.rs | 2 +- client/api/Cargo.toml | 1 + client/api/src/backend.rs | 4 +- client/api/src/client.rs | 2 +- client/basic-authorship/Cargo.toml | 1 + .../basic-authorship/src/basic_authorship.rs | 3 +- client/consensus/api/Cargo.toml | 39 +++ client/consensus/api/README.md | 3 + .../consensus/api}/src/block_import.rs | 14 +- client/consensus/api/src/import_queue.rs | 304 ++++++++++++++++++ .../api}/src/import_queue/basic_queue.rs | 42 ++- .../api}/src/import_queue/buffered_link.rs | 12 +- client/consensus/api/src/lib.rs | 11 + .../consensus/api}/src/metrics.rs | 4 +- client/consensus/aura/Cargo.toml | 1 + client/consensus/aura/src/import_queue.rs | 9 +- client/consensus/aura/src/lib.rs | 20 +- client/consensus/babe/Cargo.toml | 1 + client/consensus/babe/src/lib.rs | 48 ++- client/consensus/babe/src/tests.rs | 2 +- client/consensus/manual-seal/Cargo.toml | 1 + client/consensus/manual-seal/src/consensus.rs | 2 +- .../manual-seal/src/consensus/babe.rs | 2 +- client/consensus/manual-seal/src/error.rs | 3 +- client/consensus/manual-seal/src/lib.rs | 11 +- client/consensus/manual-seal/src/rpc.rs | 2 +- .../consensus/manual-seal/src/seal_block.rs | 8 +- client/consensus/pow/Cargo.toml | 1 + client/consensus/pow/src/lib.rs | 9 +- client/consensus/pow/src/worker.rs | 8 +- client/consensus/slots/Cargo.toml | 1 + client/consensus/slots/src/lib.rs | 5 +- client/finality-grandpa-warp-sync/Cargo.toml | 1 + .../finality-grandpa-warp-sync/src/proof.rs | 2 +- client/finality-grandpa/Cargo.toml | 1 + client/finality-grandpa/src/finality_proof.rs | 2 +- client/finality-grandpa/src/import.rs | 7 +- client/finality-grandpa/src/lib.rs | 3 +- client/finality-grandpa/src/tests.rs | 34 +- client/finality-grandpa/src/until_imported.rs | 2 +- client/finality-grandpa/src/voting_rule.rs | 2 +- client/network/Cargo.toml | 1 + client/network/src/behaviour.rs | 2 +- client/network/src/config.rs | 3 +- client/network/src/gossip/tests.rs | 12 +- client/network/src/protocol.rs | 8 +- client/network/src/protocol/sync.rs | 12 +- client/network/src/service.rs | 6 +- client/network/src/service/tests.rs | 12 +- client/network/test/Cargo.toml | 1 + client/network/test/src/block_import.rs | 9 +- client/network/test/src/lib.rs | 75 +++-- client/network/test/src/sync.rs | 3 +- client/offchain/Cargo.toml | 1 + client/offchain/src/lib.rs | 2 +- client/rpc/Cargo.toml | 1 + client/rpc/src/chain/tests.rs | 2 +- client/rpc/src/state/tests.rs | 2 +- client/service/Cargo.toml | 1 + client/service/src/builder.rs | 2 +- client/service/src/chain_ops/check_block.rs | 2 +- client/service/src/chain_ops/import_blocks.rs | 8 +- client/service/src/client/client.rs | 100 ++++-- client/service/src/lib.rs | 2 +- client/service/test/Cargo.toml | 1 + client/service/test/src/client/light.rs | 2 +- client/service/test/src/client/mod.rs | 7 +- client/transaction-pool/Cargo.toml | 1 + client/transaction-pool/tests/pool.rs | 2 +- primitives/consensus/common/Cargo.toml | 26 +- .../consensus/common/src/import_queue.rs | 269 +--------------- primitives/consensus/common/src/lib.rs | 10 - test-utils/client/Cargo.toml | 1 + test-utils/client/src/client_ext.rs | 6 +- test-utils/runtime/Cargo.toml | 1 + test-utils/runtime/client/Cargo.toml | 1 + test-utils/runtime/client/src/trait_tests.rs | 2 +- test-utils/runtime/src/lib.rs | 4 +- test-utils/test-runner/Cargo.toml | 1 + test-utils/test-runner/src/lib.rs | 3 +- 89 files changed, 776 insertions(+), 528 deletions(-) create mode 100644 client/consensus/api/Cargo.toml create mode 100644 client/consensus/api/README.md rename {primitives/consensus/common => client/consensus/api}/src/block_import.rs (97%) create mode 100644 client/consensus/api/src/import_queue.rs rename {primitives/consensus/common => client/consensus/api}/src/import_queue/basic_queue.rs (94%) rename {primitives/consensus/common => client/consensus/api}/src/import_queue/buffered_link.rs (95%) create mode 100644 client/consensus/api/src/lib.rs rename {primitives/consensus/common => client/consensus/api}/src/metrics.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index c40c48a9b4446..57d9ff1ba149d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4185,6 +4185,7 @@ dependencies = [ "sc-client-api", "sc-client-db", "sc-consensus", + "sc-consensus-api", "sc-consensus-babe", "sc-consensus-epochs", "sc-consensus-slots", @@ -4249,6 +4250,7 @@ dependencies = [ "pallet-transaction-payment", "pallet-treasury", "parity-scale-codec", + "sc-consensus-api", "sc-executor", "sp-application-crypto", "sp-consensus-babe", @@ -4429,6 +4431,7 @@ dependencies = [ "sc-cli", "sc-client-api", "sc-consensus", + "sc-consensus-api", "sc-consensus-aura", "sc-executor", "sc-finality-grandpa", @@ -4517,6 +4520,7 @@ dependencies = [ "sc-cli", "sc-client-api", "sc-client-db", + "sc-consensus-api", "sc-executor", "sc-service", "sp-api", @@ -7034,6 +7038,7 @@ dependencies = [ "parking_lot 0.11.1", "sc-block-builder", "sc-client-api", + "sc-consensus-api", "sc-proposer-metrics", "sc-telemetry", "sc-transaction-pool", @@ -7146,6 +7151,7 @@ dependencies = [ "log", "parity-scale-codec", "parking_lot 0.11.1", + "sc-consensus-api", "sc-executor", "sc-transaction-pool-api", "sp-api", @@ -7215,6 +7221,29 @@ dependencies = [ "sp-runtime", ] +[[package]] +name = "sc-consensus-api" +version = "0.9.0" +dependencies = [ + "async-trait", + "futures 0.3.15", + "futures-timer 3.0.2", + "libp2p", + "log", + "parking_lot 0.11.1", + "serde", + "sp-api", + "sp-consensus", + "sp-core", + "sp-runtime", + "sp-state-machine", + "sp-test-primitives", + "sp-utils", + "substrate-prometheus-endpoint", + "thiserror", + "wasm-timer", +] + [[package]] name = "sc-consensus-aura" version = "0.9.0" @@ -7229,6 +7258,7 @@ dependencies = [ "parking_lot 0.11.1", "sc-block-builder", "sc-client-api", + "sc-consensus-api", "sc-consensus-slots", "sc-executor", "sc-keystore", @@ -7279,6 +7309,7 @@ dependencies = [ "retain_mut", "sc-block-builder", "sc-client-api", + "sc-consensus-api", "sc-consensus-epochs", "sc-consensus-slots", "sc-consensus-uncles", @@ -7370,6 +7401,7 @@ dependencies = [ "parking_lot 0.11.1", "sc-basic-authorship", "sc-client-api", + "sc-consensus-api", "sc-consensus-babe", "sc-consensus-epochs", "sc-transaction-pool", @@ -7405,6 +7437,7 @@ dependencies = [ "parity-scale-codec", "parking_lot 0.11.1", "sc-client-api", + "sc-consensus-api", "sp-api", "sp-block-builder", "sp-blockchain", @@ -7427,6 +7460,7 @@ dependencies = [ "log", "parity-scale-codec", "sc-client-api", + "sc-consensus-api", "sc-telemetry", "sp-api", "sp-application-crypto", @@ -7570,6 +7604,7 @@ dependencies = [ "sc-block-builder", "sc-client-api", "sc-consensus", + "sc-consensus-api", "sc-keystore", "sc-network", "sc-network-gossip", @@ -7643,6 +7678,7 @@ dependencies = [ "rand 0.8.3", "sc-block-builder", "sc-client-api", + "sc-consensus-api", "sc-finality-grandpa", "sc-network", "sc-service", @@ -7745,6 +7781,7 @@ dependencies = [ "rand 0.7.3", "sc-block-builder", "sc-client-api", + "sc-consensus-api", "sc-peerset", "serde", "serde_json", @@ -7804,6 +7841,7 @@ dependencies = [ "sc-block-builder", "sc-client-api", "sc-consensus", + "sc-consensus-api", "sc-network", "sc-service", "sp-blockchain", @@ -7837,6 +7875,7 @@ dependencies = [ "sc-block-builder", "sc-client-api", "sc-client-db", + "sc-consensus-api", "sc-keystore", "sc-network", "sc-transaction-pool", @@ -7892,6 +7931,7 @@ dependencies = [ "sc-chain-spec", "sc-cli", "sc-client-api", + "sc-consensus-api", "sc-executor", "sc-keystore", "sc-network", @@ -7996,6 +8036,7 @@ dependencies = [ "sc-chain-spec", "sc-client-api", "sc-client-db", + "sc-consensus-api", "sc-executor", "sc-finality-grandpa", "sc-informant", @@ -8058,6 +8099,7 @@ dependencies = [ "sc-block-builder", "sc-client-api", "sc-client-db", + "sc-consensus-api", "sc-executor", "sc-light", "sc-network", @@ -8196,6 +8238,7 @@ dependencies = [ "retain_mut", "sc-block-builder", "sc-client-api", + "sc-consensus-api", "sc-transaction-pool-api", "serde", "sp-api", @@ -8794,25 +8837,16 @@ version = "0.9.0" dependencies = [ "async-trait", "futures 0.3.15", - "futures-timer 3.0.2", - "libp2p", "log", "parity-scale-codec", - "parking_lot 0.11.1", - "serde", "sp-api", "sp-core", "sp-inherents", "sp-runtime", "sp-state-machine", "sp-std", - "sp-test-primitives", - "sp-trie", - "sp-utils", "sp-version", - "substrate-prometheus-endpoint", "thiserror", - "wasm-timer", ] [[package]] @@ -9679,6 +9713,7 @@ dependencies = [ "sc-client-api", "sc-client-db", "sc-consensus", + "sc-consensus-api", "sc-executor", "sc-light", "sc-offchain", @@ -9710,6 +9745,7 @@ dependencies = [ "parity-scale-codec", "parity-util-mem", "sc-block-builder", + "sc-consensus-api", "sc-executor", "sc-service", "serde", @@ -9747,6 +9783,7 @@ dependencies = [ "sc-block-builder", "sc-client-api", "sc-consensus", + "sc-consensus-api", "sc-light", "sc-service", "sp-api", @@ -9905,6 +9942,7 @@ dependencies = [ "sc-basic-authorship", "sc-cli", "sc-client-api", + "sc-consensus-api", "sc-consensus-manual-seal", "sc-executor", "sc-informant", diff --git a/Cargo.toml b/Cargo.toml index 03115fe5593f1..a988c181b20e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ members = [ "client/chain-spec", "client/chain-spec/derive", "client/cli", + "client/consensus/api", "client/consensus/aura", "client/consensus/babe", "client/consensus/babe/rpc", diff --git a/bin/node-template/node/Cargo.toml b/bin/node-template/node/Cargo.toml index 88657934b1d07..6a86420cd7933 100644 --- a/bin/node-template/node/Cargo.toml +++ b/bin/node-template/node/Cargo.toml @@ -31,6 +31,7 @@ sc-consensus-aura = { version = "0.9.0", path = "../../../client/consensus/aura" sp-consensus-aura = { version = "0.9.0", path = "../../../primitives/consensus/aura" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } sc-consensus = { version = "0.9.0", path = "../../../client/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../../client/consensus/api" } sc-finality-grandpa = { version = "0.9.0", path = "../../../client/finality-grandpa" } sp-finality-grandpa = { version = "3.0.0", path = "../../../primitives/finality-grandpa" } sc-client-api = { version = "3.0.0", path = "../../../client/api" } diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index c19824e9eaa38..9501e8d4d255a 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -27,7 +27,7 @@ type FullSelectChain = sc_consensus::LongestChain; pub fn new_partial(config: &Configuration) -> Result, + sc_consensus_api::DefaultImportQueue, sc_transaction_pool::FullPool, ( sc_finality_grandpa::GrandpaBlockImport, diff --git a/bin/node/cli/Cargo.toml b/bin/node/cli/Cargo.toml index c8d7d4728b03d..12f73573abe9f 100644 --- a/bin/node/cli/Cargo.toml +++ b/bin/node/cli/Cargo.toml @@ -64,6 +64,7 @@ sc-consensus = { version = "0.9.0", path = "../../../client/consensus/common" } sc-transaction-pool = { version = "3.0.0", path = "../../../client/transaction-pool" } sc-transaction-pool-api = { version = "3.0.0", path = "../../../client/transaction-pool/api" } sc-network = { version = "0.9.0", path = "../../../client/network" } +sc-consensus-api = { version = "0.9.0", path = "../../../client/consensus/api" } sc-consensus-slots = { version = "0.9.0", path = "../../../client/consensus/slots" } sc-consensus-babe = { version = "0.9.0", path = "../../../client/consensus/babe" } sc-consensus-uncles = { version = "0.9.0", path = "../../../client/consensus/uncles" } diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 4886b798b050c..10aab9a4b2707 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -46,7 +46,7 @@ pub fn new_partial( config: &Configuration, ) -> Result, + sc_consensus_api::DefaultImportQueue, sc_transaction_pool::FullPool, ( impl Fn( @@ -606,8 +606,9 @@ mod tests { use sc_consensus_babe::{CompatibleDigestItem, BabeIntermediate, INTERMEDIATE_KEY}; use sc_consensus_epochs::descendent_query; use sp_consensus::{ - Environment, Proposer, BlockImportParams, BlockOrigin, ForkChoiceStrategy, BlockImport, + Environment, Proposer, }; + use sc_consensus_api::{BlockImportParams, BlockOrigin, ForkChoiceStrategy, BlockImport,}; use node_primitives::{Block, DigestItem, Signature}; use node_runtime::{BalancesCall, Call, UncheckedExtrinsic, Address}; use node_runtime::constants::{currency::CENTS, time::SLOT_DURATION}; diff --git a/bin/node/executor/Cargo.toml b/bin/node/executor/Cargo.toml index b08d1d78b4aaa..c13cb5f91626a 100644 --- a/bin/node/executor/Cargo.toml +++ b/bin/node/executor/Cargo.toml @@ -40,6 +40,7 @@ pallet-transaction-payment = { version = "3.0.0", path = "../../../frame/transac pallet-treasury = { version = "3.0.0", path = "../../../frame/treasury" } sp-application-crypto = { version = "3.0.0", path = "../../../primitives/application-crypto" } sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } +sc-consensus-api = { version = "0.9.0", path = "../../../client/consensus/api" } sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } sp-externalities = { version = "0.9.0", path = "../../../primitives/externalities" } substrate-test-client = { version = "2.0.0", path = "../../../test-utils/client" } diff --git a/bin/node/executor/tests/basic.rs b/bin/node/executor/tests/basic.rs index af9843715f135..b64fa475169f4 100644 --- a/bin/node/executor/tests/basic.rs +++ b/bin/node/executor/tests/basic.rs @@ -836,8 +836,8 @@ fn full_wasm_block_import_works_with_changes_trie() { fn should_import_block_with_test_client() { use node_testing::client::{ ClientBlockImportExt, TestClientBuilderExt, TestClientBuilder, - sp_consensus::BlockOrigin, }; + use sc_consensus_api::BlockOrigin; let mut client = TestClientBuilder::new().build(); let block1 = changes_trie_block(); diff --git a/bin/node/testing/Cargo.toml b/bin/node/testing/Cargo.toml index 706816ddae671..df8d1eed21572 100644 --- a/bin/node/testing/Cargo.toml +++ b/bin/node/testing/Cargo.toml @@ -34,6 +34,7 @@ sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } pallet-staking = { version = "3.0.0", path = "../../../frame/staking" } sc-executor = { version = "0.9.0", path = "../../../client/executor", features = ["wasmtime"] } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../../client/consensus/api" } frame-system = { version = "3.0.0", path = "../../../frame/system" } substrate-test-client = { version = "2.0.0", path = "../../../test-utils/client" } pallet-timestamp = { version = "3.0.0", path = "../../../frame/timestamp" } diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index edb99c617771a..4fa686be90c3c 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -29,7 +29,7 @@ use crate::client::{Client, Backend}; use crate::keyring::*; use sc_client_db::PruningMode; use sc_executor::{NativeExecutor, WasmExecutionMethod}; -use sp_consensus::{ +use sc_consensus_api::{ BlockOrigin, BlockImport, BlockImportParams, ForkChoiceStrategy, ImportResult, ImportedAux }; diff --git a/client/api/Cargo.toml b/client/api/Cargo.toml index 044ef78a07fbe..3ce5a0020258d 100644 --- a/client/api/Cargo.toml +++ b/client/api/Cargo.toml @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../consensus/api" } derive_more = "0.99.2" sc-executor = { version = "0.9.0", path = "../executor" } sp-externalities = { version = "0.9.0", path = "../../primitives/externalities" } diff --git a/client/api/src/backend.rs b/client/api/src/backend.rs index 195fc49612ba9..79040db97d3d9 100644 --- a/client/api/src/backend.rs +++ b/client/api/src/backend.rs @@ -37,11 +37,11 @@ use crate::{ UsageInfo, }; use sp_blockchain; -use sp_consensus::BlockOrigin; +use sc_consensus_api::BlockOrigin; use parking_lot::RwLock; pub use sp_state_machine::Backend as StateBackend; -pub use sp_consensus::ImportedState; +pub use sc_consensus_api::ImportedState; use std::marker::PhantomData; /// Extracts the state backend type for the given backend. diff --git a/client/api/src/client.rs b/client/api/src/client.rs index 3f4dfc8f35be1..ac839cdf60055 100644 --- a/client/api/src/client.rs +++ b/client/api/src/client.rs @@ -25,7 +25,7 @@ use sp_runtime::{ generic::{BlockId, SignedBlock}, Justifications, }; -use sp_consensus::BlockOrigin; +use sc_consensus_api::BlockOrigin; use crate::blockchain::Info; use crate::notifications::StorageEventStream; diff --git a/client/basic-authorship/Cargo.toml b/client/basic-authorship/Cargo.toml index 47dc04a37868e..d046e3d687f01 100644 --- a/client/basic-authorship/Cargo.toml +++ b/client/basic-authorship/Cargo.toml @@ -32,5 +32,6 @@ sc-proposer-metrics = { version = "0.9.0", path = "../proposer-metrics" } [dev-dependencies] sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } +sc-consensus-api = { version = "0.9.0", path = "../consensus/api" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } parking_lot = "0.11.1" diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 590f4275bf760..7ff05b8d7b7cc 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -468,7 +468,8 @@ mod tests { use super::*; use parking_lot::Mutex; - use sp_consensus::{BlockOrigin, Proposer}; + use sp_consensus::Proposer; + use sc_consensus_api::BlockOrigin; use substrate_test_runtime_client::{ prelude::*, TestClientBuilder, runtime::{Extrinsic, Transfer}, TestClientBuilderExt, }; diff --git a/client/consensus/api/Cargo.toml b/client/consensus/api/Cargo.toml new file mode 100644 index 0000000000000..ed2a9ea0319d6 --- /dev/null +++ b/client/consensus/api/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "sc-consensus-api" +version = "0.9.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "Minimal interface for client's consensus api." +documentation = "https://docs.rs/sc-consensus-api/" +readme = "README.md" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + + +[dependencies] +thiserror = "1.0.21" +libp2p = { version = "0.37.1", default-features = false } +log = "0.4.8" +sp-core = { path = "../../../primitives/core", version = "3.0.0"} +sp-consensus = { path = "../../../primitives/consensus/common", version = "0.9.0"} +sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machine" } +futures = { version = "0.3.1", features = ["thread-pool"] } +futures-timer = "3.0.1" +sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } +sp-utils = { version = "3.0.0", path = "../../../primitives/utils" } +sp-api = { version = "3.0.0", path = "../../../primitives/api" } +parking_lot = "0.11.1" +serde = { version = "1.0", features = ["derive"] } +prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} +wasm-timer = "0.2.5" +async-trait = "0.1.42" + +[dev-dependencies] +sp-test-primitives = { version = "2.0.0", path = "../../../primitives/test-primitives" } + +[features] +default = [] diff --git a/client/consensus/api/README.md b/client/consensus/api/README.md new file mode 100644 index 0000000000000..a8c86ef5df457 --- /dev/null +++ b/client/consensus/api/README.md @@ -0,0 +1,3 @@ +Types and traits needed for consensus by the client. + +License: GPL-3.0-or-later WITH Classpath-exception-2.0 \ No newline at end of file diff --git a/primitives/consensus/common/src/block_import.rs b/client/consensus/api/src/block_import.rs similarity index 97% rename from primitives/consensus/common/src/block_import.rs rename to client/consensus/api/src/block_import.rs index 447ea5761f767..6b926fb2acfe3 100644 --- a/primitives/consensus/common/src/block_import.rs +++ b/client/consensus/api/src/block_import.rs @@ -25,8 +25,8 @@ use std::collections::HashMap; use std::sync::Arc; use std::any::Any; -use crate::Error; -use crate::import_queue::CacheKeyId; +use sp_consensus::Error; +use sp_consensus::import_queue::CacheKeyId; /// Block import result. #[derive(Debug, PartialEq, Eq)] @@ -268,9 +268,11 @@ impl BlockImportParams { /// Auxiliary function for "converting" the transaction type. /// - /// Actually this just sets `StorageChanges::Changes` to `None` and makes rustc think that `Self` now - /// uses a different transaction type. - pub fn clear_storage_changes_and_mutate(self) -> BlockImportParams { + /// Actually this just sets `StorageChanges::Changes` to `None` and makes rustc + /// think that `Self` now uses a different transaction type. + pub fn clear_storage_changes_and_mutate(self) + -> BlockImportParams + { // Preserve imported state. let state_action = match self.state_action { StateAction::ApplyChanges(StorageChanges::Import(state)) => @@ -352,7 +354,7 @@ impl BlockImport for crate::import_queue::BoxBlockImp where Transaction: Send + 'static, { - type Error = crate::error::Error; + type Error = sp_consensus::error::Error; type Transaction = Transaction; /// Check block preconditions. diff --git a/client/consensus/api/src/import_queue.rs b/client/consensus/api/src/import_queue.rs new file mode 100644 index 0000000000000..518f9d3708b30 --- /dev/null +++ b/client/consensus/api/src/import_queue.rs @@ -0,0 +1,304 @@ +// This file is part of Substrate. + +// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Import Queue primitive: something which can verify and import blocks. +//! +//! This serves as an intermediate and abstracted step between synchronization +//! and import. Each mode of consensus will have its own requirements for block +//! verification. Some algorithms can verify in parallel, while others only +//! sequentially. +//! +//! The `ImportQueue` trait allows such verification strategies to be +//! instantiated. The `BasicQueue` and `BasicVerifier` traits allow serial +//! queues to be instantiated simply. + +use std::collections::HashMap; + +use sp_runtime::{Justifications, traits::{Block as BlockT, Header as _, NumberFor}}; + +use sp_consensus::{error::Error as ConsensusError, import_queue::CacheKeyId}; +use crate::{ + block_import::{ + BlockImport, BlockOrigin, BlockImportParams, ImportedAux, JustificationImport, ImportResult, + BlockCheckParams, ImportedState, StateAction, + }, + metrics::Metrics, +}; +pub use basic_queue::BasicQueue; + +/// A commonly-used Import Queue type. +/// +/// This defines the transaction type of the `BasicQueue` to be the transaction type for a client. +pub type DefaultImportQueue = + BasicQueue>; + +mod basic_queue; +pub mod buffered_link; + +/// Shared block import struct used by the queue. +pub type BoxBlockImport = Box< + dyn BlockImport + Send + Sync +>; + +/// Shared justification import struct used by the queue. +pub type BoxJustificationImport = Box< + dyn JustificationImport + Send + Sync +>; + +/// Maps to the Origin used by the network. +pub type Origin = libp2p::PeerId; + +/// Block data used by the queue. +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct IncomingBlock { + /// Block header hash. + pub hash: ::Hash, + /// Block header if requested. + pub header: Option<::Header>, + /// Block body if requested. + pub body: Option::Extrinsic>>, + /// Justification(s) if requested. + pub justifications: Option, + /// The peer, we received this from + pub origin: Option, + /// Allow importing the block skipping state verification if parent state is missing. + pub allow_missing_state: bool, + /// Skip block execution and state verification. + pub skip_execution: bool, + /// Re-validate existing block. + pub import_existing: bool, + /// Do not compute new state, but rather set it to the given set. + pub state: Option>, +} + +/// Verify a justification of a block +#[async_trait::async_trait] +pub trait Verifier: Send + Sync { + /// Verify the given data and return the BlockImportParams and an optional + /// new set of validators to import. If not, err with an Error-Message + /// presented to the User in the logs. + async fn verify( + &mut self, + origin: BlockOrigin, + header: B::Header, + justifications: Option, + body: Option>, + ) -> Result<(BlockImportParams, Option)>>), String>; +} + +/// Blocks import queue API. +/// +/// The `import_*` methods can be called in order to send elements for the import queue to verify. +/// Afterwards, call `poll_actions` to determine how to respond to these elements. +pub trait ImportQueue: Send { + /// Import bunch of blocks. + fn import_blocks(&mut self, origin: BlockOrigin, blocks: Vec>); + /// Import block justifications. + fn import_justifications( + &mut self, + who: Origin, + hash: B::Hash, + number: NumberFor, + justifications: Justifications + ); + /// Polls for actions to perform on the network. + /// + /// This method should behave in a way similar to `Future::poll`. It can register the current + /// task and notify later when more actions are ready to be polled. To continue the comparison, + /// it is as if this method always returned `Poll::Pending`. + fn poll_actions(&mut self, cx: &mut futures::task::Context, link: &mut dyn Link); +} + +/// Hooks that the verification queue can use to influence the synchronization +/// algorithm. +pub trait Link: Send { + /// Batch of blocks imported, with or without error. + fn blocks_processed( + &mut self, + _imported: usize, + _count: usize, + _results: Vec<(BlockImportResult, B::Hash)> + ) {} + + /// Justification import result. + fn justification_imported( + &mut self, + _who: Origin, + _hash: &B::Hash, + _number: NumberFor, + _success: bool) {} + + /// Request a justification for the given block. + fn request_justification(&mut self, _hash: &B::Hash, _number: NumberFor) {} +} + +/// Block import successful result. +#[derive(Debug, PartialEq)] +pub enum BlockImportStatus { + /// Imported known block. + ImportedKnown(N, Option), + /// Imported unknown block. + ImportedUnknown(N, ImportedAux, Option), +} + +/// Block import error. +#[derive(Debug)] +pub enum BlockImportError { + /// Block missed header, can't be imported + IncompleteHeader(Option), + /// Block verification failed, can't be imported + VerificationFailed(Option, String), + /// Block is known to be Bad + BadBlock(Option), + /// Parent state is missing. + MissingState, + /// Block has an unknown parent + UnknownParent, + /// Block import has been cancelled. This can happen if the parent block fails to be imported. + Cancelled, + /// Other error. + Other(ConsensusError), +} + +type BlockImportResult = Result>, BlockImportError>; + +/// Single block import function. +pub async fn import_single_block, Transaction: Send + 'static>( + import_handle: &mut impl BlockImport, + block_origin: BlockOrigin, + block: IncomingBlock, + verifier: &mut V, +) -> BlockImportResult { + import_single_block_metered(import_handle, block_origin, block, verifier, None).await +} + +/// Single block import function with metering. +pub(crate) async fn import_single_block_metered< + B: BlockT, + V: Verifier, + Transaction: Send + 'static> +( + import_handle: &mut impl BlockImport, + block_origin: BlockOrigin, + block: IncomingBlock, + verifier: &mut V, + metrics: Option, +) -> BlockImportResult { + let peer = block.origin; + + let (header, justifications) = match (block.header, block.justifications) { + (Some(header), justifications) => (header, justifications), + (None, _) => { + if let Some(ref peer) = peer { + debug!(target: "sync", "Header {} was not provided by {} ", block.hash, peer); + } else { + debug!(target: "sync", "Header {} was not provided ", block.hash); + } + return Err(BlockImportError::IncompleteHeader(peer)) + }, + }; + + trace!(target: "sync", "Header {} has {:?} logs", block.hash, header.digest().logs().len()); + + let number = header.number().clone(); + let hash = header.hash(); + let parent_hash = header.parent_hash().clone(); + + let import_handler = |import| { + match import { + Ok(ImportResult::AlreadyInChain) => { + trace!(target: "sync", "Block already in chain {}: {:?}", number, hash); + Ok(BlockImportStatus::ImportedKnown(number, peer.clone())) + }, + Ok(ImportResult::Imported(aux)) => { + Ok(BlockImportStatus::ImportedUnknown(number, aux, peer.clone())) + }, + Ok(ImportResult::MissingState) => { + debug!(target: "sync", "Parent state is missing for {}: {:?}, parent: {:?}", + number, hash, parent_hash); + Err(BlockImportError::MissingState) + }, + Ok(ImportResult::UnknownParent) => { + debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", + number, hash, parent_hash); + Err(BlockImportError::UnknownParent) + }, + Ok(ImportResult::KnownBad) => { + debug!(target: "sync", "Peer gave us a bad block {}: {:?}", number, hash); + Err(BlockImportError::BadBlock(peer.clone())) + }, + Err(e) => { + debug!(target: "sync", "Error importing block {}: {:?}: {:?}", number, hash, e); + Err(BlockImportError::Other(e)) + } + } + }; + + match import_handler(import_handle.check_block(BlockCheckParams { + hash, + number, + parent_hash, + allow_missing_state: block.allow_missing_state, + import_existing: block.import_existing, + }).await)? { + BlockImportStatus::ImportedUnknown { .. } => (), + r => return Ok(r), // Any other successful result means that the block is already imported. + } + + let started = wasm_timer::Instant::now(); + let (mut import_block, maybe_keys) = verifier.verify( + block_origin, + header, + justifications, + block.body + ).await.map_err(|msg| { + if let Some(ref peer) = peer { + trace!(target: "sync", "Verifying {}({}) from {} failed: {}", number, hash, peer, msg); + } else { + trace!(target: "sync", "Verifying {}({}) failed: {}", number, hash, msg); + } + if let Some(metrics) = metrics.as_ref() { + metrics.report_verification(false, started.elapsed()); + } + BlockImportError::VerificationFailed(peer.clone(), msg) + })?; + + if let Some(metrics) = metrics.as_ref() { + metrics.report_verification(true, started.elapsed()); + } + + let mut cache = HashMap::new(); + if let Some(keys) = maybe_keys { + cache.extend(keys.into_iter()); + } + import_block.import_existing = block.import_existing; + let mut import_block = import_block.clear_storage_changes_and_mutate(); + if let Some(state) = block.state { + let changes = crate::block_import::StorageChanges::Import(state); + import_block.state_action = StateAction::ApplyChanges(changes); + } else if block.skip_execution { + import_block.state_action = StateAction::Skip; + } else if block.allow_missing_state { + import_block.state_action = StateAction::ExecuteIfPossible; + } + + let imported = import_handle.import_block(import_block, cache).await; + if let Some(metrics) = metrics.as_ref() { + metrics.report_verification_and_import(started.elapsed()); + } + import_handler(imported) +} diff --git a/primitives/consensus/common/src/import_queue/basic_queue.rs b/client/consensus/api/src/import_queue/basic_queue.rs similarity index 94% rename from primitives/consensus/common/src/import_queue/basic_queue.rs rename to client/consensus/api/src/import_queue/basic_queue.rs index 5767b72dd8084..ca94b9b4d26c5 100644 --- a/primitives/consensus/common/src/import_queue/basic_queue.rs +++ b/client/consensus/api/src/import_queue/basic_queue.rs @@ -18,14 +18,17 @@ use std::{pin::Pin, time::Duration, marker::PhantomData}; use futures::{prelude::*, task::Context, task::Poll}; use futures_timer::Delay; -use sp_runtime::{Justification, Justifications, traits::{Block as BlockT, Header as HeaderT, NumberFor}}; +use sp_runtime::{ + Justification, Justifications, + traits::{Block as BlockT, Header as HeaderT, NumberFor} +}; use sp_utils::mpsc::{TracingUnboundedSender, tracing_unbounded, TracingUnboundedReceiver}; use prometheus_endpoint::Registry; use crate::{ block_import::BlockOrigin, import_queue::{ - BlockImportResult, BlockImportError, Verifier, BoxBlockImport, + BlockImportStatus, BlockImportError, Verifier, BoxBlockImport, BoxJustificationImport, ImportQueue, Link, Origin, IncomingBlock, import_single_block_metered, buffered_link::{self, BufferedLinkSender, BufferedLinkReceiver}, @@ -145,14 +148,18 @@ mod worker_messages { use super::*; pub struct ImportBlocks(pub BlockOrigin, pub Vec>); - pub struct ImportJustification(pub Origin, pub B::Hash, pub NumberFor, pub Justification); + pub struct ImportJustification( + pub Origin, + pub B::Hash, + pub NumberFor, + pub Justification); } /// The process of importing blocks. /// -/// This polls the `block_import_receiver` for new blocks to import and than awaits on importing these blocks. -/// After each block is imported, this async function yields once to give other futures the possibility -/// to be run. +/// This polls the `block_import_receiver` for new blocks to import and than awaits on +/// importing these blocks. After each block is imported, this async function yields once +/// to give other futures the possibility to be run. /// /// Returns when `block_import` ended. async fn block_import_process( @@ -327,12 +334,13 @@ struct ImportManyBlocksResult { /// The total number of blocks processed. block_count: usize, /// The import results for each block. - results: Vec<(Result>, BlockImportError>, B::Hash)>, + results: Vec<(Result>, BlockImportError>, B::Hash)>, } /// Import several blocks at once, returning import result for each block. /// -/// This will yield after each imported block once, to ensure that other futures can be called as well. +/// This will yield after each imported block once, to ensure that other futures can +/// be called as well. async fn import_many_blocks, Transaction: Send + 'static>( import_handle: &mut BoxBlockImport, blocks_origin: BlockOrigin, @@ -411,11 +419,11 @@ async fn import_many_blocks, Transaction: Send + 'stat } } -/// A future that will always `yield` on the first call of `poll` but schedules the current task for -/// re-execution. +/// A future that will always `yield` on the first call of `poll` but schedules the +/// current task for re-execution. /// -/// This is done by getting the waker and calling `wake_by_ref` followed by returning `Pending`. -/// The next time the `poll` is called, it will return `Ready`. +/// This is done by getting the waker and calling `wake_by_ref` followed by returning +/// `Pending`. The next time the `poll` is called, it will return `Ready`. struct Yield(bool); impl Yield { @@ -443,7 +451,9 @@ mod tests { use super::*; use crate::{ import_queue::{CacheKeyId, Verifier}, - BlockCheckParams, BlockImport, BlockImportParams, ImportResult, JustificationImport, + }; + use crate::block_import::{ + BlockImportParams, ImportResult, BlockCheckParams, BlockImport, JustificationImport }; use futures::{executor::block_on, Future}; use sp_test_primitives::{Block, BlockNumber, Extrinsic, Hash, Header}; @@ -464,7 +474,7 @@ mod tests { #[async_trait::async_trait] impl BlockImport for () { - type Error = crate::Error; + type Error = sp_consensus::Error; type Transaction = Extrinsic; async fn check_block( @@ -485,7 +495,7 @@ mod tests { #[async_trait::async_trait] impl JustificationImport for () { - type Error = crate::Error; + type Error = sp_consensus::Error; async fn on_start(&mut self) -> Vec<(Hash, BlockNumber)> { Vec::new() @@ -517,7 +527,7 @@ mod tests { &mut self, _imported: usize, _count: usize, - results: Vec<(Result, BlockImportError>, Hash)>, + results: Vec<(Result, BlockImportError>, Hash)>, ) { if let Some(hash) = results.into_iter().find_map(|(r, h)| r.ok().map(|_| h)) { self.events.push(Event::BlockImported(hash)); diff --git a/primitives/consensus/common/src/import_queue/buffered_link.rs b/client/consensus/api/src/import_queue/buffered_link.rs similarity index 95% rename from primitives/consensus/common/src/import_queue/buffered_link.rs rename to client/consensus/api/src/import_queue/buffered_link.rs index 0295f704c4efc..a32b9a44e3211 100644 --- a/primitives/consensus/common/src/import_queue/buffered_link.rs +++ b/client/consensus/api/src/import_queue/buffered_link.rs @@ -42,7 +42,9 @@ use futures::prelude::*; use sp_runtime::traits::{Block as BlockT, NumberFor}; use sp_utils::mpsc::{TracingUnboundedSender, TracingUnboundedReceiver, tracing_unbounded}; use std::{pin::Pin, task::Context, task::Poll}; -use crate::import_queue::{Origin, Link, BlockImportResult, BlockImportError}; +use crate::import_queue::{Origin, Link, BlockImportStatus, BlockImportError}; + +use super::BlockImportResult; /// Wraps around an unbounded channel from the `futures` crate. The sender implements `Link` and /// can be used to buffer commands, and the receiver can be used to poll said commands and transfer @@ -78,7 +80,11 @@ impl Clone for BufferedLinkSender { /// Internal buffered message. enum BlockImportWorkerMsg { - BlocksProcessed(usize, usize, Vec<(Result>, BlockImportError>, B::Hash)>), + BlocksProcessed( + usize, + usize, + Vec<(BlockImportResult, B::Hash)> + ), JustificationImported(Origin, B::Hash, NumberFor, bool), RequestJustification(B::Hash, NumberFor), } @@ -88,7 +94,7 @@ impl Link for BufferedLinkSender { &mut self, imported: usize, count: usize, - results: Vec<(Result>, BlockImportError>, B::Hash)> + results: Vec<(BlockImportResult, B::Hash)> ) { let _ = self.tx.unbounded_send(BlockImportWorkerMsg::BlocksProcessed(imported, count, results)); } diff --git a/client/consensus/api/src/lib.rs b/client/consensus/api/src/lib.rs new file mode 100644 index 0000000000000..d0dde97145a49 --- /dev/null +++ b/client/consensus/api/src/lib.rs @@ -0,0 +1,11 @@ +// our error-chain could potentially blow up otherwise +#![recursion_limit="128"] + +#[macro_use] extern crate log; + +pub mod import_queue; +pub mod block_import; +pub mod metrics; + +pub use import_queue::*; +pub use block_import::*; diff --git a/primitives/consensus/common/src/metrics.rs b/client/consensus/api/src/metrics.rs similarity index 96% rename from primitives/consensus/common/src/metrics.rs rename to client/consensus/api/src/metrics.rs index 29d39436cbefc..884d89c1266cf 100644 --- a/primitives/consensus/common/src/metrics.rs +++ b/client/consensus/api/src/metrics.rs @@ -23,7 +23,7 @@ use prometheus_endpoint::{ use sp_runtime::traits::{Block as BlockT, NumberFor}; -use crate::import_queue::{BlockImportResult, BlockImportError}; +use crate::import_queue::{BlockImportStatus, BlockImportError}; /// Generic Prometheus metrics for common consensus functionality. #[derive(Clone)] @@ -77,7 +77,7 @@ impl Metrics { pub fn report_import( &self, - result: &Result>, BlockImportError>, + result: &Result>, BlockImportError>, ) { let label = match result { Ok(_) => "success", diff --git a/client/consensus/aura/Cargo.toml b/client/consensus/aura/Cargo.toml index 27c1534032f49..758a8b28d07e0 100644 --- a/client/consensus/aura/Cargo.toml +++ b/client/consensus/aura/Cargo.toml @@ -20,6 +20,7 @@ sc-block-builder = { version = "0.9.0", path = "../../block-builder" } sc-client-api = { version = "3.0.0", path = "../../api" } codec = { package = "parity-scale-codec", version = "2.0.0" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../consensus/api" } sp-consensus-slots = { version = "0.9.0", path = "../../../primitives/consensus/slots" } derive_more = "0.99.2" futures = "0.3.9" diff --git a/client/consensus/aura/src/import_queue.rs b/client/consensus/aura/src/import_queue.rs index c3faa5382686e..cf50c8f66d385 100644 --- a/client/consensus/aura/src/import_queue.rs +++ b/client/consensus/aura/src/import_queue.rs @@ -26,11 +26,14 @@ use log::{debug, info, trace}; use prometheus_endpoint::Registry; use codec::{Encode, Decode, Codec}; use sp_consensus::{ - BlockImport, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, - BlockOrigin, Error as ConsensusError, + CanAuthorWith, + Error as ConsensusError, +}; +use sc_consensus_api::{ + block_import::{BlockImport, ForkChoiceStrategy, BlockImportParams,BlockOrigin}, import_queue::{ Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport, - }, + } }; use sc_client_api::{BlockOf, UsageProvider, backend::AuxStore}; use sp_blockchain::{well_known_cache_keys::{self, Id as CacheKeyId}, ProvideCache, HeaderBackend}; diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 72545eda077ba..38b921c16bdc7 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -41,8 +41,11 @@ use log::{debug, trace}; use codec::{Encode, Decode, Codec}; use sp_consensus::{ - BlockImport, Environment, Proposer, CanAuthorWith, ForkChoiceStrategy, BlockImportParams, - BlockOrigin, Error as ConsensusError, SelectChain, StateAction, + Environment, Proposer, CanAuthorWith, + Error as ConsensusError, SelectChain, +}; +use sc_consensus_api::{ + BlockImport,BlockOrigin, StateAction,ForkChoiceStrategy, BlockImportParams }; use sc_client_api::{backend::AuxStore, BlockOf, UsageProvider}; use sp_blockchain::{Result as CResult, ProvideCache, HeaderBackend}; @@ -179,7 +182,7 @@ where PF: Environment + Send + Sync + 'static, PF::Proposer: Proposer>, SO: SyncOracle + Send + Sync + Clone, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, CIDP: CreateInherentDataProviders + Send, CIDP::InherentDataProviders: InherentDataProviderExt + Send, BS: BackoffAuthoringBlocksStrategy> + Send + 'static, @@ -271,7 +274,7 @@ where I: BlockImport> + Send + Sync + 'static, Error: std::error::Error + Send + From + 'static, SO: SyncOracle + Send + Sync + Clone, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, BS: BackoffAuthoringBlocksStrategy> + Send + 'static, { AuraWorker { @@ -318,7 +321,7 @@ where P::Public: AppPublic + Public + Member + Encode + Decode + Hash, P::Signature: TryFrom> + Member + Encode + Decode + Hash + Debug, SO: SyncOracle + Send + Clone, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, BS: BackoffAuthoringBlocksStrategy> + Send + 'static, Error: std::error::Error + Send + From + 'static, { @@ -389,7 +392,7 @@ where Self::Claim, Self::EpochData, ) -> Result< - sp_consensus::BlockImportParams>, + sc_consensus_api::BlockImportParams>, sp_consensus::Error> + Send + 'static> { let keystore = self.keystore.clone(); @@ -422,7 +425,7 @@ where import_block.post_digests.push(signature_digest_item); import_block.body = Some(body); import_block.state_action = StateAction::ApplyChanges( - sp_consensus::StorageChanges::Changes(storage_changes) + sc_consensus_api::StorageChanges::Changes(storage_changes) ); import_block.fork_choice = Some(ForkChoiceStrategy::LongestChain); @@ -547,8 +550,9 @@ mod tests { use super::*; use sp_consensus::{ NoNetwork as DummyOracle, Proposal, AlwaysCanAuthor, DisableProofRecording, - import_queue::BoxJustificationImport, SlotData, + SlotData, }; + use sc_consensus_api::BoxJustificationImport; use sc_network_test::{Block as TestBlock, *}; use sp_runtime::traits::{Block as BlockT, DigestFor}; use sc_network::config::ProtocolConfig; diff --git a/client/consensus/babe/Cargo.toml b/client/consensus/babe/Cargo.toml index 9ada9fda6216f..2ef007530e4fd 100644 --- a/client/consensus/babe/Cargo.toml +++ b/client/consensus/babe/Cargo.toml @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } sp-consensus-babe = { version = "0.9.0", path = "../../../primitives/consensus/babe" } +sc-consensus-api = { version = "0.9.0", path = "../../consensus/api" } sp-core = { version = "3.0.0", path = "../../../primitives/core" } sp-application-crypto = { version = "3.0.0", path = "../../../primitives/application-crypto" } sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" } diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 61b58bf1b5999..a69e8fecddacd 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -96,12 +96,16 @@ use sp_block_builder::BlockBuilder as BlockBuilderApi; use sp_blockchain::{ Error as ClientError, HeaderBackend, HeaderMetadata, ProvideCache, Result as ClientResult, }; -use sp_consensus::{import_queue::BoxJustificationImport, CanAuthorWith, ImportResult}; use sp_consensus::{ - import_queue::{BasicQueue, CacheKeyId, DefaultImportQueue, Verifier}, - BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, Environment, - Error as ConsensusError, ForkChoiceStrategy, Proposer, SelectChain, SlotData, - StateAction, + import_queue::CacheKeyId, Environment, CanAuthorWith, + Error as ConsensusError, Proposer, SelectChain, SlotData, +}; +use sc_consensus_api::{ + import_queue::{BoxJustificationImport, BasicQueue, DefaultImportQueue, Verifier}, + block_import::{ + BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, + ForkChoiceStrategy, ImportResult, StateAction + }, }; use sp_consensus_babe::inherents::BabeInherentData; use sp_consensus_slots::Slot; @@ -461,7 +465,7 @@ where + Sync + 'static, SO: SyncOracle + Send + Sync + Clone + 'static, - L: sp_consensus::JustificationSyncLink + 'static, + L: sc_consensus_api::JustificationSyncLink + 'static, CIDP: CreateInherentDataProviders + Send + Sync + 'static, CIDP::InherentDataProviders: InherentDataProviderExt + Send, BS: BackoffAuthoringBlocksStrategy> + Send + 'static, @@ -502,7 +506,12 @@ where let (worker_tx, worker_rx) = channel(HANDLE_BUFFER_SIZE); - let answer_requests = answer_requests(worker_rx, config.0, client, babe_link.epoch_changes.clone()); + let answer_requests = answer_requests( + worker_rx, + config.0, + client, + babe_link.epoch_changes.clone()); + Ok(BabeWorker { inner: Box::pin(future::join(inner, answer_requests).map(|_| ())), slot_notification_sinks, @@ -654,7 +663,7 @@ where E::Proposer: Proposer>, I: BlockImport> + Send + Sync + 'static, SO: SyncOracle + Send + Clone, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, BS: BackoffAuthoringBlocksStrategy>, Error: std::error::Error + Send + From + From + 'static, { @@ -761,7 +770,7 @@ where Self::Claim, Self::EpochData, ) -> Result< - sp_consensus::BlockImportParams, + sc_consensus_api::BlockImportParams, sp_consensus::Error> + Send + 'static> { let keystore = self.keystore.clone(); @@ -792,7 +801,7 @@ where import_block.post_digests.push(digest_item); import_block.body = Some(body); import_block.state_action = StateAction::ApplyChanges( - sp_consensus::StorageChanges::Changes(storage_changes) + sc_consensus_api::StorageChanges::Changes(storage_changes) ); import_block.intermediates.insert( Cow::from(INTERMEDIATE_KEY), @@ -890,8 +899,10 @@ fn find_next_epoch_digest(header: &B::Header) trace!(target: "babe", "Checking log {:?}, looking for epoch change digest.", log); let log = log.try_to::(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID)); match (log, epoch_digest.is_some()) { - (Some(ConsensusLog::NextEpochData(_)), true) => return Err(babe_err(Error::MultipleEpochChangeDigests)), - (Some(ConsensusLog::NextEpochData(epoch)), false) => epoch_digest = Some(epoch), + (Some(ConsensusLog::NextEpochData(_)), true) => + return Err(babe_err(Error::MultipleEpochChangeDigests)), + (Some(ConsensusLog::NextEpochData(epoch)), false) => + epoch_digest = Some(epoch), _ => trace!(target: "babe", "Ignoring digest not meant for us"), } } @@ -909,8 +920,10 @@ fn find_next_config_digest(header: &B::Header) trace!(target: "babe", "Checking log {:?}, looking for epoch change digest.", log); let log = log.try_to::(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID)); match (log, config_digest.is_some()) { - (Some(ConsensusLog::NextConfigData(_)), true) => return Err(babe_err(Error::MultipleConfigChangeDigests)), - (Some(ConsensusLog::NextConfigData(config)), false) => config_digest = Some(config), + (Some(ConsensusLog::NextConfigData(_)), true) => + return Err(babe_err(Error::MultipleConfigChangeDigests)), + (Some(ConsensusLog::NextConfigData(config)), false) => + config_digest = Some(config), _ => trace!(target: "babe", "Ignoring digest not meant for us"), } } @@ -1630,8 +1643,11 @@ pub fn import_queue( can_author_with: CAW, telemetry: Option, ) -> ClientResult> where - Inner: BlockImport> - + Send + Sync + 'static, + Inner: BlockImport< + Block, + Error = ConsensusError, + Transaction = sp_api::TransactionFor + > + Send + Sync + 'static, Client: ProvideRuntimeApi + ProvideCache + HeaderBackend + HeaderMetadata + AuxStore + Send + Sync + 'static, diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index 3392ffade98ee..cf2c6064ec280 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -36,8 +36,8 @@ use sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging; use sc_block_builder::{BlockBuilder, BlockBuilderProvider}; use sp_consensus::{ NoNetwork as DummyOracle, Proposal, DisableProofRecording, AlwaysCanAuthor, - import_queue::{BoxBlockImport, BoxJustificationImport}, }; +use sc_consensus_api::{BoxBlockImport, BoxJustificationImport}; use sc_network_test::{Block as TestBlock, *}; use sc_network::config::ProtocolConfig; use sp_runtime::{generic::DigestItem, traits::{Block as BlockT, DigestFor}}; diff --git a/client/consensus/manual-seal/Cargo.toml b/client/consensus/manual-seal/Cargo.toml index f7c2e98656c1b..5db512eab1b82 100644 --- a/client/consensus/manual-seal/Cargo.toml +++ b/client/consensus/manual-seal/Cargo.toml @@ -28,6 +28,7 @@ async-trait = "0.1.42" sc-client-api = { path = "../../api", version = "3.0.0"} sc-consensus-babe = { path = "../../consensus/babe", version = "0.9.0"} sc-consensus-epochs = { path = "../../consensus/epochs", version = "0.9.0"} +sc-consensus-api = { path = "../../consensus/api", version = "0.9.0"} sp-consensus-babe = { path = "../../../primitives/consensus/babe", version = "0.9.0"} sc-transaction-pool = { path = "../../transaction-pool", version = "3.0.0"} diff --git a/client/consensus/manual-seal/src/consensus.rs b/client/consensus/manual-seal/src/consensus.rs index 0cfd99cab5c99..63f38a329966b 100644 --- a/client/consensus/manual-seal/src/consensus.rs +++ b/client/consensus/manual-seal/src/consensus.rs @@ -21,7 +21,7 @@ use super::Error; use sp_runtime::traits::{Block as BlockT, DigestFor}; use sp_inherents::InherentData; -use sp_consensus::BlockImportParams; +use sc_consensus_api::BlockImportParams; pub mod babe; diff --git a/client/consensus/manual-seal/src/consensus/babe.rs b/client/consensus/manual-seal/src/consensus/babe.rs index 100fec912faa8..5eff145066e01 100644 --- a/client/consensus/manual-seal/src/consensus/babe.rs +++ b/client/consensus/manual-seal/src/consensus/babe.rs @@ -32,7 +32,7 @@ use sp_keystore::SyncCryptoStorePtr; use sp_api::{ProvideRuntimeApi, TransactionFor}; use sp_blockchain::{HeaderBackend, HeaderMetadata}; -use sp_consensus::BlockImportParams; +use sc_consensus_api::BlockImportParams; use sp_consensus_slots::Slot; use sp_consensus_babe::{ BabeApi, inherents::BabeInherentData, ConsensusLog, BABE_ENGINE_ID, AuthorityId, diff --git a/client/consensus/manual-seal/src/error.rs b/client/consensus/manual-seal/src/error.rs index 77140c835a3ee..046fd61fc6e0d 100644 --- a/client/consensus/manual-seal/src/error.rs +++ b/client/consensus/manual-seal/src/error.rs @@ -19,7 +19,8 @@ //! A manual sealing engine: the engine listens for rpc calls to seal blocks and create forks. //! This is suitable for a testing environment. -use sp_consensus::{Error as ConsensusError, ImportResult}; +use sp_consensus::{Error as ConsensusError}; +use sc_consensus_api::ImportResult; use sp_blockchain::Error as BlockchainError; use sp_inherents::Error as InherentsError; use futures::channel::{oneshot, mpsc::SendError}; diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 5d93f6724ee9f..e0b573cf7191c 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -21,9 +21,12 @@ use futures::prelude::*; use sp_consensus::{ - Environment, Proposer, SelectChain, BlockImport, - ForkChoiceStrategy, BlockImportParams, BlockOrigin, - import_queue::{Verifier, BasicQueue, CacheKeyId, BoxBlockImport}, + Environment, Proposer, SelectChain, + import_queue::CacheKeyId, +}; +use sc_consensus_api::{ + block_import::{BlockImport, ForkChoiceStrategy, BlockImportParams, BlockOrigin}, + import_queue::{Verifier, BasicQueue, BoxBlockImport}, }; use sp_blockchain::HeaderBackend; use sp_inherents::CreateInherentDataProviders; @@ -278,7 +281,7 @@ mod tests { use substrate_test_runtime_transaction_pool::{TestApi, uxt}; use sc_transaction_pool_api::{TransactionPool, MaintainedTransactionPool, TransactionSource}; use sp_runtime::generic::BlockId; - use sp_consensus::ImportedAux; + use sc_consensus_api::ImportedAux; use sc_basic_authorship::ProposerFactory; use sc_client_api::BlockBackend; diff --git a/client/consensus/manual-seal/src/rpc.rs b/client/consensus/manual-seal/src/rpc.rs index eb056f22fed8b..b6da7239f9c87 100644 --- a/client/consensus/manual-seal/src/rpc.rs +++ b/client/consensus/manual-seal/src/rpc.rs @@ -18,7 +18,7 @@ //! RPC interface for the `ManualSeal` Engine. -use sp_consensus::ImportedAux; +use sc_consensus_api::ImportedAux; use jsonrpc_core::Error; use jsonrpc_derive::rpc; use futures::{ diff --git a/client/consensus/manual-seal/src/seal_block.rs b/client/consensus/manual-seal/src/seal_block.rs index 450a7bff4cd40..030101a347de0 100644 --- a/client/consensus/manual-seal/src/seal_block.rs +++ b/client/consensus/manual-seal/src/seal_block.rs @@ -25,9 +25,9 @@ use sp_runtime::{ generic::BlockId, }; use futures::prelude::*; -use sp_consensus::{ - self, BlockImport, Environment, Proposer, ForkChoiceStrategy, - BlockImportParams, BlockOrigin, ImportResult, SelectChain, StateAction, +use sp_consensus::{self, Environment, Proposer, SelectChain}; +use sc_consensus_api::{ + BlockImportParams, BlockImport, BlockOrigin, ImportResult, StateAction, ForkChoiceStrategy }; use sp_blockchain::HeaderBackend; use std::collections::HashMap; @@ -146,7 +146,7 @@ pub async fn seal_block( params.finalized = finalize; params.fork_choice = Some(ForkChoiceStrategy::LongestChain); params.state_action = StateAction::ApplyChanges( - sp_consensus::StorageChanges::Changes(proposal.storage_changes) + sc_consensus_api::StorageChanges::Changes(proposal.storage_changes) ); if let Some(digest_provider) = digest_provider { diff --git a/client/consensus/pow/Cargo.toml b/client/consensus/pow/Cargo.toml index 443b852c41e5d..7cf732fa986a9 100644 --- a/client/consensus/pow/Cargo.toml +++ b/client/consensus/pow/Cargo.toml @@ -23,6 +23,7 @@ sp-block-builder = { version = "3.0.0", path = "../../../primitives/block-builde sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } sp-consensus-pow = { version = "0.9.0", path = "../../../primitives/consensus/pow" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../consensus/api" } log = "0.4.8" futures = { version = "0.3.1", features = ["compat"] } futures-timer = "3.0.1" diff --git a/client/consensus/pow/src/lib.rs b/client/consensus/pow/src/lib.rs index e71726564ebe5..f25e70d36823a 100644 --- a/client/consensus/pow/src/lib.rs +++ b/client/consensus/pow/src/lib.rs @@ -59,10 +59,11 @@ use sp_api::ProvideRuntimeApi; use sp_consensus_pow::{Seal, TotalDifficulty, POW_ENGINE_ID}; use sp_inherents::{CreateInherentDataProviders, InherentDataProvider}; use sp_consensus::{ - BlockImportParams, BlockOrigin, ForkChoiceStrategy, SyncOracle, Environment, Proposer, - SelectChain, Error as ConsensusError, CanAuthorWith, BlockImport, BlockCheckParams, ImportResult, + SyncOracle, Environment, Proposer, + SelectChain, Error as ConsensusError, CanAuthorWith }; -use sp_consensus::import_queue::{ +use sc_consensus_api::{ + BlockImportParams, BlockOrigin, ForkChoiceStrategy, BlockImport, BlockCheckParams, ImportResult, BoxBlockImport, BasicQueue, Verifier, BoxJustificationImport, }; use codec::{Encode, Decode}; @@ -557,7 +558,7 @@ where E::Error: std::fmt::Debug, E::Proposer: Proposer>, SO: SyncOracle + Clone + Send + Sync + 'static, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, CIDP: CreateInherentDataProviders, CAW: CanAuthorWith + Clone + Send + 'static, { diff --git a/client/consensus/pow/src/worker.rs b/client/consensus/pow/src/worker.rs index 74fbcce81341d..2a2af34473acf 100644 --- a/client/consensus/pow/src/worker.rs +++ b/client/consensus/pow/src/worker.rs @@ -18,8 +18,8 @@ use std::{pin::Pin, time::Duration, collections::HashMap, borrow::Cow}; use sc_client_api::ImportNotifications; -use sp_consensus::{Proposal, BlockOrigin, BlockImportParams, StorageChanges, - StateAction, import_queue::BoxBlockImport}; +use sp_consensus::Proposal; +use sc_consensus_api::{BlockOrigin, BlockImportParams, StorageChanges, StateAction, BoxBlockImport}; use sp_runtime::{ generic::BlockId, traits::{Block as BlockT, Header as HeaderT}, @@ -62,7 +62,7 @@ pub struct MiningWorker< Block: BlockT, Algorithm: PowAlgorithm, C: sp_api::ProvideRuntimeApi, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, Proof, > { pub(crate) build: Option>, @@ -77,7 +77,7 @@ where C: sp_api::ProvideRuntimeApi, Algorithm: PowAlgorithm, Algorithm::Difficulty: 'static + Send, - L: sp_consensus::JustificationSyncLink, + L: sc_consensus_api::JustificationSyncLink, sp_api::TransactionFor: Send + 'static, { /// Get the current best hash. `None` if the worker has just started or the client is doing diff --git a/client/consensus/slots/Cargo.toml b/client/consensus/slots/Cargo.toml index 51382198f5087..c4e140ad0764d 100644 --- a/client/consensus/slots/Cargo.toml +++ b/client/consensus/slots/Cargo.toml @@ -27,6 +27,7 @@ sp-state-machine = { version = "0.9.0", path = "../../../primitives/state-machin sp-api = { version = "3.0.0", path = "../../../primitives/api" } sc-telemetry = { version = "3.0.0", path = "../../telemetry" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../consensus/api" } sp-inherents = { version = "3.0.0", path = "../../../primitives/inherents" } sp-timestamp = { version = "3.0.0", path = "../../../primitives/timestamp" } futures = "0.3.9" diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 1ec89a6f519af..7963ce8d989af 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -40,8 +40,9 @@ use log::{debug, error, info, warn}; use sp_api::{ProvideRuntimeApi, ApiRef}; use sp_arithmetic::traits::BaseArithmetic; use sp_consensus::{ - BlockImport, CanAuthorWith, JustificationSyncLink, Proposer, SelectChain, SlotData, SyncOracle, + CanAuthorWith, Proposer, SelectChain, SlotData, SyncOracle, }; +use sc_consensus_api::{BlockImport, JustificationSyncLink}; use sp_consensus_slots::Slot; use sp_inherents::CreateInherentDataProviders; use sp_runtime::{ @@ -163,7 +164,7 @@ pub trait SimpleSlotWorker { Self::Claim, Self::EpochData, ) -> Result< - sp_consensus::BlockImportParams>::Transaction>, + sc_consensus_api::BlockImportParams>::Transaction>, sp_consensus::Error > + Send + 'static >; diff --git a/client/finality-grandpa-warp-sync/Cargo.toml b/client/finality-grandpa-warp-sync/Cargo.toml index 27728e159c762..a48720505739f 100644 --- a/client/finality-grandpa-warp-sync/Cargo.toml +++ b/client/finality-grandpa-warp-sync/Cargo.toml @@ -31,6 +31,7 @@ sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } finality-grandpa = { version = "0.14.1" } rand = "0.8" sc-block-builder = { version = "0.9.0", path = "../block-builder" } +sc-consensus-api = { version = "0.9.0", path = "../../client/consensus/api" } sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } sp-keyring = { version = "3.0.0", path = "../../primitives/keyring" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } diff --git a/client/finality-grandpa-warp-sync/src/proof.rs b/client/finality-grandpa-warp-sync/src/proof.rs index 87a6220267827..409332bbf43d3 100644 --- a/client/finality-grandpa-warp-sync/src/proof.rs +++ b/client/finality-grandpa-warp-sync/src/proof.rs @@ -224,7 +224,7 @@ mod tests { use sc_block_builder::BlockBuilderProvider; use sc_finality_grandpa::{AuthoritySetChanges, GrandpaJustification}; use sp_blockchain::HeaderBackend; - use sp_consensus::BlockOrigin; + use sc_consensus_api::BlockOrigin; use sp_finality_grandpa::GRANDPA_ENGINE_ID; use sp_keyring::Ed25519Keyring; use sp_runtime::{generic::BlockId, traits::Header as _}; diff --git a/client/finality-grandpa/Cargo.toml b/client/finality-grandpa/Cargo.toml index 3cb577aee5db8..28b24a0d68f5d 100644 --- a/client/finality-grandpa/Cargo.toml +++ b/client/finality-grandpa/Cargo.toml @@ -30,6 +30,7 @@ sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } sp-utils = { version = "3.0.0", path = "../../primitives/utils" } sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } sc-consensus = { version = "0.9.0", path = "../consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../consensus/api" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } sp-api = { version = "3.0.0", path = "../../primitives/api" } diff --git a/client/finality-grandpa/src/finality_proof.rs b/client/finality-grandpa/src/finality_proof.rs index ec33d48774ae5..2cebbb9c2e3df 100644 --- a/client/finality-grandpa/src/finality_proof.rs +++ b/client/finality-grandpa/src/finality_proof.rs @@ -251,7 +251,7 @@ pub(crate) mod tests { use futures::executor::block_on; use sc_block_builder::BlockBuilderProvider; use sc_client_api::{apply_aux, LockImportRun}; - use sp_consensus::BlockOrigin; + use sc_consensus_api::BlockOrigin; use sp_core::crypto::Public; use sp_finality_grandpa::{AuthorityId, GRANDPA_ENGINE_ID as ID}; use sp_keyring::Ed25519Keyring; diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index ebb26a28c3485..89edcb645c10d 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -27,8 +27,11 @@ use sc_telemetry::TelemetryHandle; use sp_api::TransactionFor; use sp_blockchain::{well_known_cache_keys, BlockStatus}; use sp_consensus::{ - BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, Error as ConsensusError, - ImportResult, JustificationImport, SelectChain, + Error as ConsensusError, SelectChain, +}; +use sc_consensus_api::{ + BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, + ImportResult, JustificationImport }; use sp_finality_grandpa::{ConsensusLog, ScheduledChange, SetId, GRANDPA_ENGINE_ID}; use sp_runtime::generic::{BlockId, OpaqueDigestItemId}; diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 6c3f0f6af37a8..5cf359ae33efe 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -72,7 +72,8 @@ use sp_api::ProvideRuntimeApi; use sp_blockchain::{HeaderBackend, Error as ClientError, HeaderMetadata}; use sp_runtime::generic::BlockId; use sp_runtime::traits::{NumberFor, Block as BlockT, DigestFor, Zero}; -use sp_consensus::{SelectChain, BlockImport}; +use sp_consensus::{SelectChain}; +use sc_consensus_api::BlockImport; use sp_core::{ crypto::Public, }; diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 725beec6a94b2..b5eb657db406d 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -34,9 +34,9 @@ use sp_keyring::Ed25519Keyring; use sp_blockchain::Result; use sp_api::{ApiRef, ProvideRuntimeApi}; use substrate_test_runtime_client::runtime::BlockNumber; -use sp_consensus::{ - BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, ImportResult, BlockImport, - import_queue::BoxJustificationImport, +use sc_consensus_api::{ + BlockOrigin, ForkChoiceStrategy, ImportedAux, BlockImportParams, + ImportResult, BlockImport, BoxJustificationImport }; use std::{collections::{HashMap, HashSet}, pin::Pin}; use sp_runtime::{Justifications, traits::{Block as BlockT, Header as HeaderT}}; @@ -243,7 +243,11 @@ fn create_keystore(authority: Ed25519Keyring) -> (SyncCryptoStorePtr, tempfile:: (keystore, keystore_path) } -fn block_until_complete(future: impl Future + Unpin, net: &Arc>, runtime: &mut Runtime) { +fn block_until_complete( + future: impl Future + Unpin, + net: &Arc>, + runtime: &mut Runtime) +{ let drive_to_completion = futures::future::poll_fn(|cx| { net.lock().poll(cx); Poll::<()>::Pending }); @@ -547,7 +551,8 @@ fn transition_3_voters_twice_1_full_observer() { assert_eq!(full_client.chain_info().best_number, 1, "Peer #{} failed to sync", i); - let set: AuthoritySet = crate::aux_schema::load_authorities(&*full_client).unwrap(); + let set: AuthoritySet = + crate::aux_schema::load_authorities(&*full_client).unwrap(); assert_eq!(set.current(), (0, make_ids(peers_a).as_slice())); assert_eq!(set.pending_changes().count(), 0); @@ -616,8 +621,10 @@ fn transition_3_voters_twice_1_full_observer() { .take_while(|n| future::ready(n.header.number() < &30)) .for_each(move |_| future::ready(())) .map(move |()| { - let full_client = client.as_full().expect("only full clients are used in test"); - let set: AuthoritySet = crate::aux_schema::load_authorities(&*full_client).unwrap(); + let full_client = client.as_full() + .expect("only full clients are used in test"); + let set: AuthoritySet = + crate::aux_schema::load_authorities(&*full_client).unwrap(); assert_eq!(set.current(), (2, make_ids(peers_c).as_slice())); assert_eq!(set.pending_changes().count(), 0); @@ -815,8 +822,10 @@ fn force_change_to_new_set() { assert_eq!(peer.client().info().best_number, 26, "Peer #{} failed to sync", i); - let full_client = peer.client().as_full().expect("only full clients are used in test"); - let set: AuthoritySet = crate::aux_schema::load_authorities(&*full_client).unwrap(); + let full_client = peer.client().as_full() + .expect("only full clients are used in test"); + let set: AuthoritySet = + crate::aux_schema::load_authorities(&*full_client).unwrap(); assert_eq!(set.current(), (1, voters.as_slice())); assert_eq!(set.pending_changes().count(), 0); @@ -1144,7 +1153,8 @@ fn voter_persists_its_votes() { // by `Sink::poll_complete` to make sure items are being flushed. Given that // we send in a loop including a delay until items are received, this can be // ignored for the sake of reduced complexity. - Pin::new(&mut *round_tx.lock()).start_send(finality_grandpa::Message::Prevote(prevote)).unwrap(); + Pin::new(&mut *round_tx.lock()) + .start_send(finality_grandpa::Message::Prevote(prevote)).unwrap(); } else if state.compare_exchange(1, 2, Ordering::SeqCst, Ordering::SeqCst).unwrap() == 1 { // the next message we receive should be our own prevote let prevote = match signed.message { @@ -1231,7 +1241,9 @@ fn voter_catches_up_to_latest_round_when_behind() { let net = Arc::new(Mutex::new(net)); let mut finality_notifications = Vec::new(); - let voter = |keystore, peer_id, link, net: Arc>| -> Pin + Send>> { + let voter = |keystore, peer_id, link, net: Arc>| + -> Pin + Send>> + { let grandpa_params = GrandpaParams { config: Config { gossip_duration: TEST_GOSSIP_DURATION, diff --git a/client/finality-grandpa/src/until_imported.rs b/client/finality-grandpa/src/until_imported.rs index 7cfd9e6074c47..650147fd32869 100644 --- a/client/finality-grandpa/src/until_imported.rs +++ b/client/finality-grandpa/src/until_imported.rs @@ -555,7 +555,7 @@ mod tests { use super::*; use crate::{CatchUp, CompactCommit}; use substrate_test_runtime_client::runtime::{Block, Hash, Header}; - use sp_consensus::BlockOrigin; + use sc_consensus_api::BlockOrigin; use sc_client_api::BlockImportNotification; use futures::future::Either; use futures_timer::Delay; diff --git a/client/finality-grandpa/src/voting_rule.rs b/client/finality-grandpa/src/voting_rule.rs index a5515c1be23ed..8183b4f2d0dfe 100644 --- a/client/finality-grandpa/src/voting_rule.rs +++ b/client/finality-grandpa/src/voting_rule.rs @@ -324,7 +324,7 @@ impl VotingRule for Box> where mod tests { use super::*; use sc_block_builder::BlockBuilderProvider; - use sp_consensus::BlockOrigin; + use sc_consensus_api::BlockOrigin; use sp_runtime::traits::Header as _; use substrate_test_runtime_client::{ diff --git a/client/network/Cargo.toml b/client/network/Cargo.toml index 3740ebceb6389..ace55cea28899 100644 --- a/client/network/Cargo.toml +++ b/client/network/Cargo.toml @@ -53,6 +53,7 @@ smallvec = "1.5.0" sp-arithmetic = { version = "3.0.0", path = "../../primitives/arithmetic" } sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" } sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../consensus/api" } sp-core = { version = "3.0.0", path = "../../primitives/core" } sp-runtime = { version = "3.0.0", path = "../../primitives/runtime" } sp-utils = { version = "3.0.0", path = "../../primitives/utils" } diff --git a/client/network/src/behaviour.rs b/client/network/src/behaviour.rs index 576c49d1da366..09516fb10366c 100644 --- a/client/network/src/behaviour.rs +++ b/client/network/src/behaviour.rs @@ -36,7 +36,7 @@ use libp2p::swarm::{ }; use log::debug; use prost::Message; -use sp_consensus::{BlockOrigin, import_queue::{IncomingBlock, Origin}}; +use sc_consensus_api::{import_queue::{IncomingBlock, Origin}, block_import::BlockOrigin}; use sp_runtime::{traits::{Block as BlockT, NumberFor}, Justifications}; use std::{ borrow::Cow, diff --git a/client/network/src/config.rs b/client/network/src/config.rs index 36ae1e831b8ce..fc17e323caafb 100644 --- a/client/network/src/config.rs +++ b/client/network/src/config.rs @@ -44,7 +44,8 @@ use libp2p::{ multiaddr, wasm_ext, Multiaddr, PeerId, }; use prometheus_endpoint::Registry; -use sp_consensus::{block_validation::BlockAnnounceValidator, import_queue::ImportQueue}; +use sp_consensus::{block_validation::BlockAnnounceValidator}; +use sc_consensus_api::ImportQueue; use sp_runtime::traits::Block as BlockT; use std::{borrow::Cow, convert::TryFrom, future::Future, pin::Pin, str::FromStr}; use std::{ diff --git a/client/network/src/gossip/tests.rs b/client/network/src/gossip/tests.rs index bdef28f9bebe5..053967ed90c76 100644 --- a/client/network/src/gossip/tests.rs +++ b/client/network/src/gossip/tests.rs @@ -50,16 +50,16 @@ fn build_test_full_node(network_config: config::NetworkConfiguration) struct PassThroughVerifier(bool); #[async_trait::async_trait] - impl sp_consensus::import_queue::Verifier for PassThroughVerifier { + impl sc_consensus_api::Verifier for PassThroughVerifier { async fn verify( &mut self, - origin: sp_consensus::BlockOrigin, + origin: sc_consensus_api::BlockOrigin, header: B::Header, justifications: Option, body: Option>, ) -> Result< ( - sp_consensus::BlockImportParams, + sc_consensus_api::BlockImportParams, Option)>>, ), String, @@ -79,16 +79,16 @@ fn build_test_full_node(network_config: config::NetworkConfiguration) )] }); - let mut import = sp_consensus::BlockImportParams::new(origin, header); + let mut import = sc_consensus_api::BlockImportParams::new(origin, header); import.body = body; import.finalized = self.0; import.justifications = justifications; - import.fork_choice = Some(sp_consensus::ForkChoiceStrategy::LongestChain); + import.fork_choice = Some(sc_consensus_api::ForkChoiceStrategy::LongestChain); Ok((import, maybe_keys)) } } - let import_queue = Box::new(sp_consensus::import_queue::BasicQueue::new( + let import_queue = Box::new(sc_consensus_api::BasicQueue::new( PassThroughVerifier(false), Box::new(client.clone()), None, diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index b9a189a0f384f..103f6fb6068a5 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -40,9 +40,11 @@ use message::generic::{Message as GenericMessage, Roles}; use prometheus_endpoint::{Registry, Gauge, GaugeVec, PrometheusError, Opts, register, U64}; use prost::Message as _; use sp_consensus::{ - BlockOrigin, block_validation::BlockAnnounceValidator, - import_queue::{BlockImportResult, BlockImportError, IncomingBlock, Origin} +}; +use sc_consensus_api::{ + import_queue::{BlockImportStatus, BlockImportError, IncomingBlock, Origin}, + block_import::BlockOrigin }; use sp_runtime::{ Justifications, @@ -1028,7 +1030,7 @@ impl Protocol { &mut self, imported: usize, count: usize, - results: Vec<(Result>, BlockImportError>, B::Hash)> + results: Vec<(Result>, BlockImportError>, B::Hash)> ) { let results = self.sync.on_blocks_processed( imported, diff --git a/client/network/src/protocol/sync.rs b/client/network/src/protocol/sync.rs index 44fbe64bfcff4..78d06acbac88c 100644 --- a/client/network/src/protocol/sync.rs +++ b/client/network/src/protocol/sync.rs @@ -33,10 +33,10 @@ use codec::Encode; use blocks::BlockCollection; use state::StateSync; use sp_blockchain::{Error as ClientError, HeaderMetadata}; -use sp_consensus::{BlockOrigin, BlockStatus, +use sp_consensus::{BlockStatus, block_validation::{BlockAnnounceValidator, Validation}, - import_queue::{IncomingBlock, BlockImportResult, BlockImportError} }; +use sc_consensus_api::{BlockOrigin, IncomingBlock, BlockImportStatus, BlockImportError}; use crate::protocol::message::{ self, BlockAnnounce, BlockAttributes, BlockRequest, BlockResponse, }; @@ -1230,7 +1230,7 @@ impl ChainSync { &'a mut self, imported: usize, count: usize, - results: Vec<(Result>, BlockImportError>, B::Hash)>, + results: Vec<(Result>, BlockImportError>, B::Hash)>, ) -> impl Iterator), BadPeer>> + 'a { trace!(target: "sync", "Imported {} of {}", imported, count); @@ -1250,12 +1250,12 @@ impl ChainSync { } match result { - Ok(BlockImportResult::ImportedKnown(number, who)) => { + Ok(BlockImportStatus::ImportedKnown(number, who)) => { if let Some(peer) = who.and_then(|p| self.peers.get_mut(&p)) { peer.update_common_number(number); } } - Ok(BlockImportResult::ImportedUnknown(number, aux, who)) => { + Ok(BlockImportStatus::ImportedUnknown(number, aux, who)) => { if aux.clear_justification_requests { trace!( target: "sync", @@ -2759,7 +2759,7 @@ mod test { .map(|b| ( Ok( - BlockImportResult::ImportedUnknown( + BlockImportStatus::ImportedUnknown( b.header().number().clone(), Default::default(), Some(peer_id1.clone()), diff --git a/client/network/src/service.rs b/client/network/src/service.rs index fb303312093cd..b553a03125203 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -83,7 +83,7 @@ use log::{error, info, trace, debug, warn}; use metrics::{Metrics, MetricSources, Histogram, HistogramVec}; use parking_lot::Mutex; use sc_peerset::PeersetHandle; -use sp_consensus::import_queue::{BlockImportError, BlockImportResult, ImportQueue, Link}; +use sc_consensus_api::{BlockImportError, BlockImportStatus, ImportQueue, Link}; use sp_runtime::traits::{Block as BlockT, NumberFor}; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use std::{ @@ -1230,7 +1230,7 @@ impl<'a, B: BlockT + 'static, H: ExHashT> sp_consensus::SyncOracle } } -impl sp_consensus::JustificationSyncLink for NetworkService { +impl sc_consensus_api::JustificationSyncLink for NetworkService { fn request_justification(&self, hash: &B::Hash, number: NumberFor) { NetworkService::request_justification(self, hash, number); } @@ -1927,7 +1927,7 @@ impl<'a, B: BlockT> Link for NetworkLink<'a, B> { &mut self, imported: usize, count: usize, - results: Vec<(Result>, BlockImportError>, B::Hash)> + results: Vec<(Result>, BlockImportError>, B::Hash)> ) { self.protocol.behaviour_mut().user_protocol_mut().on_blocks_processed(imported, count, results) } diff --git a/client/network/src/service/tests.rs b/client/network/src/service/tests.rs index 4a739e50628a5..519e2a096f679 100644 --- a/client/network/src/service/tests.rs +++ b/client/network/src/service/tests.rs @@ -50,16 +50,16 @@ fn build_test_full_node(config: config::NetworkConfiguration) struct PassThroughVerifier(bool); #[async_trait::async_trait] - impl sp_consensus::import_queue::Verifier for PassThroughVerifier { + impl sc_consensus_api::Verifier for PassThroughVerifier { async fn verify( &mut self, - origin: sp_consensus::BlockOrigin, + origin: sc_consensus_api::BlockOrigin, header: B::Header, justifications: Option, body: Option>, ) -> Result< ( - sp_consensus::BlockImportParams, + sc_consensus_api::BlockImportParams, Option)>>, ), String, @@ -79,16 +79,16 @@ fn build_test_full_node(config: config::NetworkConfiguration) )] }); - let mut import = sp_consensus::BlockImportParams::new(origin, header); + let mut import = sc_consensus_api::BlockImportParams::new(origin, header); import.body = body; import.finalized = self.0; import.justifications = justifications; - import.fork_choice = Some(sp_consensus::ForkChoiceStrategy::LongestChain); + import.fork_choice = Some(sc_consensus_api::ForkChoiceStrategy::LongestChain); Ok((import, maybe_keys)) } } - let import_queue = Box::new(sp_consensus::import_queue::BasicQueue::new( + let import_queue = Box::new(sc_consensus_api::BasicQueue::new( PassThroughVerifier(false), Box::new(client.clone()), None, diff --git a/client/network/test/Cargo.toml b/client/network/test/Cargo.toml index 18a8d5cf8ca0a..56b9d1af1f820 100644 --- a/client/network/test/Cargo.toml +++ b/client/network/test/Cargo.toml @@ -23,6 +23,7 @@ rand = "0.7.2" libp2p = { version = "0.37.1", default-features = false } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } sc-consensus = { version = "0.9.0", path = "../../consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../consensus/api" } sc-client-api = { version = "3.0.0", path = "../../api" } sp-blockchain = { version = "3.0.0", path = "../../../primitives/blockchain" } sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } diff --git a/client/network/test/src/block_import.rs b/client/network/test/src/block_import.rs index 05169aba8d730..1ec3c31dbfcbd 100644 --- a/client/network/test/src/block_import.rs +++ b/client/network/test/src/block_import.rs @@ -18,9 +18,8 @@ //! Testing block import logic. -use sp_consensus::ImportedAux; -use sp_consensus::import_queue::{ - import_single_block, BasicQueue, BlockImportError, BlockImportResult, IncomingBlock, +use sc_consensus_api::{ + ImportedAux, import_single_block, BasicQueue, BlockImportError, BlockImportStatus, IncomingBlock, }; use substrate_test_runtime_client::{self, prelude::*}; use substrate_test_runtime_client::runtime::{Block, Hash}; @@ -64,7 +63,7 @@ fn import_single_good_block_works() { block, &mut PassThroughVerifier::new(true) )) { - Ok(BlockImportResult::ImportedUnknown(ref num, ref aux, ref org)) + Ok(BlockImportStatus::ImportedUnknown(ref num, ref aux, ref org)) if *num == number && *aux == expected_aux && *org == Some(peer_id) => {} r @ _ => panic!("{:?}", r) } @@ -79,7 +78,7 @@ fn import_single_good_known_block_is_ignored() { block, &mut PassThroughVerifier::new(true) )) { - Ok(BlockImportResult::ImportedKnown(ref n, _)) if *n == number => {} + Ok(BlockImportStatus::ImportedKnown(ref n, _)) if *n == number => {} _ => panic!() } } diff --git a/client/network/test/src/lib.rs b/client/network/test/src/lib.rs index b6e8f897bb809..2a4e08421dfba 100644 --- a/client/network/test/src/lib.rs +++ b/client/network/test/src/lib.rs @@ -37,19 +37,22 @@ use sp_blockchain::{ Info as BlockchainInfo, }; use sc_client_api::{ - BlockchainEvents, BlockImportNotification, FinalityNotifications, ImportNotifications, FinalityNotification, + BlockchainEvents, BlockImportNotification, FinalityNotifications, + ImportNotifications, FinalityNotification, backend::{TransactionFor, AuxStore, Backend, Finalizer}, BlockBackend, }; use sc_consensus::LongestChain; use sc_block_builder::{BlockBuilder, BlockBuilderProvider}; use sc_network::config::Role; use sp_consensus::block_validation::{DefaultBlockAnnounceValidator, BlockAnnounceValidator}; -use sp_consensus::import_queue::{ +use sc_consensus_api::{ BasicQueue, BoxJustificationImport, Verifier, }; -use sp_consensus::block_import::{BlockImport, ImportResult}; use sp_consensus::Error as ConsensusError; -use sp_consensus::{BlockOrigin, ForkChoiceStrategy, BlockImportParams, BlockCheckParams, JustificationImport}; +use sc_consensus_api::{ + BlockImport, ImportResult, BlockOrigin, ForkChoiceStrategy, BlockImportParams, + BlockCheckParams, JustificationImport +}; use futures::prelude::*; use futures::future::BoxFuture; use sc_network::{ @@ -150,7 +153,7 @@ pub enum PeersClient { impl PeersClient { pub fn as_full(&self) -> Option> { match *self { - PeersClient::Full(ref client, ref _backend) => Some(client.clone()), + PeersClient::Full(ref client, _) => Some(client.clone()), _ => None, } } @@ -161,22 +164,22 @@ impl PeersClient { pub fn get_aux(&self, key: &[u8]) -> ClientResult>> { match *self { - PeersClient::Full(ref client, ref _backend) => client.get_aux(key), - PeersClient::Light(ref client, ref _backend) => client.get_aux(key), + PeersClient::Full(ref client, _) => client.get_aux(key), + PeersClient::Light(ref client, _) => client.get_aux(key), } } pub fn info(&self) -> BlockchainInfo { match *self { - PeersClient::Full(ref client, ref _backend) => client.chain_info(), - PeersClient::Light(ref client, ref _backend) => client.chain_info(), + PeersClient::Full(ref client, _) => client.chain_info(), + PeersClient::Light(ref client, _) => client.chain_info(), } } pub fn header(&self, block: &BlockId) -> ClientResult::Header>> { match *self { - PeersClient::Full(ref client, ref _backend) => client.header(block), - PeersClient::Light(ref client, ref _backend) => client.header(block), + PeersClient::Full(ref client, _) => client.header(block), + PeersClient::Light(ref client, _) => client.header(block), } } @@ -195,22 +198,22 @@ impl PeersClient { pub fn justifications(&self, block: &BlockId) -> ClientResult> { match *self { - PeersClient::Full(ref client, ref _backend) => client.justifications(block), - PeersClient::Light(ref client, ref _backend) => client.justifications(block), + PeersClient::Full(ref client, _) => client.justifications(block), + PeersClient::Light(ref client, _) => client.justifications(block), } } pub fn finality_notification_stream(&self) -> FinalityNotifications { match *self { - PeersClient::Full(ref client, ref _backend) => client.finality_notification_stream(), - PeersClient::Light(ref client, ref _backend) => client.finality_notification_stream(), + PeersClient::Full(ref client, _) => client.finality_notification_stream(), + PeersClient::Light(ref client, _) => client.finality_notification_stream(), } } pub fn import_notification_stream(&self) -> ImportNotifications{ match *self { - PeersClient::Full(ref client, ref _backend) => client.import_notification_stream(), - PeersClient::Light(ref client, ref _backend) => client.import_notification_stream(), + PeersClient::Full(ref client, _) => client.import_notification_stream(), + PeersClient::Light(ref client, _) => client.import_notification_stream(), } } @@ -221,8 +224,8 @@ impl PeersClient { notify: bool ) -> ClientResult<()> { match *self { - PeersClient::Full(ref client, ref _backend) => client.finalize_block(id, justification, notify), - PeersClient::Light(ref client, ref _backend) => client.finalize_block(id, justification, notify), + PeersClient::Full(ref client, _) => client.finalize_block(id, justification, notify), + PeersClient::Light(ref client, _) => client.finalize_block(id, justification, notify), } } } @@ -288,7 +291,8 @@ impl Peer where } // Returns a clone of the local SelectChain, only available on full nodes - pub fn select_chain(&self) -> Option> { + pub fn select_chain(&self) + -> Option> { self.select_chain.clone() } @@ -352,7 +356,11 @@ impl Peer where headers_only: bool, inform_sync_about_new_best_block: bool, announce_block: bool, - ) -> H256 where F: FnMut(BlockBuilder) -> Block { + ) -> H256 + where + F: FnMut(BlockBuilder) -> Block + { let full_client = self.client.as_full() .expect("blocks could only be generated by full clients"); let mut at = full_client.header(&at).unwrap().unwrap().hash(); @@ -618,9 +626,11 @@ impl Verifier for VerifierAdapter { body: Option> ) -> Result<(BlockImportParams, Option)>>), String> { let hash = header.hash(); - self.verifier.lock().await.verify(origin, header, justifications, body).await.map_err(|e| { - self.failed_verifications.lock().insert(hash, e.clone()); - e + self.verifier.lock().await + .verify(origin, header, justifications, body).await + .map_err(|e| { + self.failed_verifications.lock().insert(hash, e.clone()); + e }) } } @@ -662,7 +672,10 @@ pub struct FullPeerConfig { pub sync_mode: SyncMode, } -pub trait TestNetFactory: Sized where >::Transaction: Send { +pub trait TestNetFactory: Sized + where + >::Transaction: Send +{ type Verifier: 'static + Verifier; type BlockImport: BlockImport + Clone + Send + Sync + 'static; type PeerData: Default; @@ -1034,13 +1047,17 @@ pub trait TestNetFactory: Sized where >: trace!(target: "sync", "-- Polling complete {}: {}", i, peer.id()); // We poll `imported_blocks_stream`. - while let Poll::Ready(Some(notification)) = peer.imported_blocks_stream.as_mut().poll_next(cx) { + while let Poll::Ready(Some(notification)) = + peer.imported_blocks_stream.as_mut().poll_next(cx) + { peer.network.service().announce_block(notification.hash, None); } // We poll `finality_notification_stream`, but we only take the last event. let mut last = None; - while let Poll::Ready(Some(item)) = peer.finality_notification_stream.as_mut().poll_next(cx) { + while let Poll::Ready(Some(item)) = + peer.finality_notification_stream.as_mut().poll_next(cx) + { last = Some(item); } if let Some(notification) = last { @@ -1140,7 +1157,9 @@ impl TestNetFactory for JustificationTestNet { JustificationTestNet(TestNet::from_config(config)) } - fn make_verifier(&self, client: PeersClient, config: &ProtocolConfig, peer_data: &()) -> Self::Verifier { + fn make_verifier(&self, client: PeersClient, config: &ProtocolConfig, peer_data: &()) + -> Self::Verifier + { self.0.make_verifier(client, config, peer_data) } diff --git a/client/network/test/src/sync.rs b/client/network/test/src/sync.rs index 56cec7e4cdfd9..2bfb967bcb0df 100644 --- a/client/network/test/src/sync.rs +++ b/client/network/test/src/sync.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use sp_consensus::BlockOrigin; +use sc_consensus_api::BlockOrigin; use std::time::Duration; use futures::{Future, executor::block_on}; use super::*; @@ -1126,4 +1126,3 @@ fn syncs_state() { })); } } - diff --git a/client/offchain/Cargo.toml b/client/offchain/Cargo.toml index 9f0fbdb64dccf..ef7319c447ef2 100644 --- a/client/offchain/Cargo.toml +++ b/client/offchain/Cargo.toml @@ -44,6 +44,7 @@ sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } sc-transaction-pool-api = { version = "3.0.0", path = "../transaction-pool/api" } sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../client/consensus/api" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } tokio = "0.2" lazy_static = "1.4.0" diff --git a/client/offchain/src/lib.rs b/client/offchain/src/lib.rs index 21b1b7b7d21ca..a64b8148fbcfb 100644 --- a/client/offchain/src/lib.rs +++ b/client/offchain/src/lib.rs @@ -237,7 +237,7 @@ mod tests { }; use sc_transaction_pool::{BasicPool, FullChainApi}; use sc_transaction_pool_api::{TransactionPool, InPoolTransaction}; - use sp_consensus::BlockOrigin; + use sc_consensus_api::BlockOrigin; use sc_client_api::Backend as _; use sc_block_builder::BlockBuilderProvider as _; use futures::executor::block_on; diff --git a/client/rpc/Cargo.toml b/client/rpc/Cargo.toml index c4cfc40c34d63..27289e5d78d3f 100644 --- a/client/rpc/Cargo.toml +++ b/client/rpc/Cargo.toml @@ -52,6 +52,7 @@ sp-io = { version = "3.0.0", path = "../../primitives/io" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } tokio = "0.1.22" sc-transaction-pool = { version = "3.0.0", path = "../transaction-pool" } +sc-consensus-api = { version = "0.9.0", path = "../consensus/api" } sc-cli = { version = "0.9.0", path = "../cli" } [features] diff --git a/client/rpc/src/chain/tests.rs b/client/rpc/src/chain/tests.rs index bb673d65ea0f2..4c081f22626d9 100644 --- a/client/rpc/src/chain/tests.rs +++ b/client/rpc/src/chain/tests.rs @@ -20,9 +20,9 @@ use super::*; use assert_matches::assert_matches; use substrate_test_runtime_client::{ prelude::*, - sp_consensus::BlockOrigin, runtime::{H256, Block, Header}, }; +use sc_consensus_api::BlockOrigin; use sp_rpc::list::ListOrValue; use sc_block_builder::BlockBuilderProvider; use futures::{executor, compat::{Future01CompatExt, Stream01CompatExt}}; diff --git a/client/rpc/src/state/tests.rs b/client/rpc/src/state/tests.rs index c9cb0bde89c1a..9f001c6ee09db 100644 --- a/client/rpc/src/state/tests.rs +++ b/client/rpc/src/state/tests.rs @@ -29,9 +29,9 @@ use sc_block_builder::BlockBuilderProvider; use sp_io::hashing::blake2_256; use substrate_test_runtime_client::{ prelude::*, - sp_consensus::BlockOrigin, runtime, }; +use sc_consensus_api::BlockOrigin; use sc_rpc_api::DenyUnsafe; use sp_runtime::generic::BlockId; use crate::testing::TaskExecutor; diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index fa424a9d0b22c..8795440d7fe08 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -53,6 +53,7 @@ sp-keystore = { version = "0.9.0", path = "../../primitives/keystore" } sp-session = { version = "3.0.0", path = "../../primitives/session" } sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine" } sp-application-crypto = { version = "3.0.0", path = "../../primitives/application-crypto" } +sc-consensus-api = { version = "0.9.0", path = "../consensus/api" } sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } sp-inherents = { version = "3.0.0", path = "../../primitives/inherents" } sp-storage = { version = "3.0.0", path = "../../primitives/storage" } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 6318469a7f0bc..836b09f437f2a 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -30,8 +30,8 @@ use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedSender}; use sc_chain_spec::get_extension; use sp_consensus::{ block_validation::{BlockAnnounceValidator, DefaultBlockAnnounceValidator, Chain}, - import_queue::ImportQueue, }; +use sc_consensus_api::ImportQueue; use jsonrpc_pubsub::manager::SubscriptionManager; use futures::{ FutureExt, StreamExt, diff --git a/client/service/src/chain_ops/check_block.rs b/client/service/src/chain_ops/check_block.rs index 94f6d25c9eb8f..1f6957155ed1e 100644 --- a/client/service/src/chain_ops/check_block.rs +++ b/client/service/src/chain_ops/check_block.rs @@ -21,7 +21,7 @@ use futures::{future, prelude::*}; use sp_runtime::traits::Block as BlockT; use sp_runtime::generic::BlockId; use codec::Encode; -use sp_consensus::import_queue::ImportQueue; +use sc_consensus_api::ImportQueue; use sc_client_api::{BlockBackend, UsageProvider}; use std::pin::Pin; diff --git a/client/service/src/chain_ops/import_blocks.rs b/client/service/src/chain_ops/import_blocks.rs index 330aaea4f555b..6e6f261edea70 100644 --- a/client/service/src/chain_ops/import_blocks.rs +++ b/client/service/src/chain_ops/import_blocks.rs @@ -26,9 +26,9 @@ use sp_runtime::traits::{ }; use sp_runtime::generic::SignedBlock; use codec::{Decode, IoReader as CodecIoReader}; -use sp_consensus::{ - BlockOrigin, - import_queue::{IncomingBlock, Link, BlockImportError, BlockImportResult, ImportQueue}, +use sc_consensus_api::{ + block_import::BlockOrigin, + import_queue::{IncomingBlock, Link, BlockImportError, BlockImportStatus, ImportQueue}, }; use std::{io::{Read, Seek}, pin::Pin}; @@ -317,7 +317,7 @@ where &mut self, imported: usize, _num_expected_blocks: usize, - results: Vec<(Result>, BlockImportError>, B::Hash)> + results: Vec<(Result>, BlockImportError>, B::Hash)> ) { self.imported_blocks += imported as u64; diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index 25957560f4dbd..ec415e9cbe26c 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -56,7 +56,10 @@ use sp_state_machine::{ }; use sc_executor::RuntimeVersion; use sp_consensus::{ - Error as ConsensusError, BlockStatus, BlockImportParams, BlockCheckParams, + Error as ConsensusError, BlockStatus +}; +use sc_consensus_api::{ + BlockImportParams, BlockCheckParams, ImportResult, BlockOrigin, ForkChoiceStrategy, StateAction, }; use sp_blockchain::{ @@ -124,17 +127,18 @@ pub struct Client where Block: BlockT { _phantom: PhantomData, } -// used in importing a block, where additional changes are made after the runtime -// executed. +/// Used in importing a block, where additional changes are made after the runtime +/// executed. enum PrePostHeader { - // they are the same: no post-runtime digest items. + /// they are the same: no post-runtime digest items. Same(H), - // different headers (pre, post). + /// different headers (pre, post). Different(H, H), } impl PrePostHeader { - // get a reference to the "post-header" -- the header as it should be after all changes are applied. + /// get a reference to the "post-header" -- the header as it should be + /// after all changes are applied. fn post(&self) -> &H { match *self { PrePostHeader::Same(ref h) => h, @@ -142,7 +146,8 @@ impl PrePostHeader { } } - // convert to the "post-header" -- the header as it should be after all changes are applied. + /// convert to the "post-header" -- the header as it should be after + /// all changes are applied. fn into_post(self) -> H { match self { PrePostHeader::Same(h) => h, @@ -153,7 +158,7 @@ impl PrePostHeader { enum PrepareStorageChangesResult, Block: BlockT> { Discard(ImportResult), - Import(Option>>), + Import(Option>>), } /// Create an instance of in-memory client. @@ -235,7 +240,9 @@ pub fn new_with_backend( Block: BlockT, B: backend::LocalBackend + 'static, { - let call_executor = LocalCallExecutor::new(backend.clone(), executor, spawn_handle, config.clone())?; + let call_executor = LocalCallExecutor::new( + backend.clone(), executor, spawn_handle, config.clone())?; + let extensions = ExecutionExtensions::new( Default::default(), keystore, @@ -410,7 +417,9 @@ impl Client where id: &BlockId, cht_size: NumberFor, ) -> sp_blockchain::Result<(Block::Header, StorageProof)> { - let proof_error = || sp_blockchain::Error::Backend(format!("Failed to generate header proof for {:?}", id)); + let proof_error = || sp_blockchain::Error::Backend( + format!("Failed to generate header proof for {:?}", id)); + let header = self.backend.blockchain().expect_header(*id)?; let block_num = *header.number(); let cht_num = cht::block_to_cht_number(cht_size, block_num).ok_or_else(proof_error)?; @@ -571,7 +580,8 @@ impl Client where Ok(StorageProof::merge(proofs)) } - /// Generates CHT-based proof for roots of changes tries at given blocks (that are part of single CHT). + /// Generates CHT-based proof for roots of changes tries at given blocks + /// (that are part of single CHT). fn changes_trie_roots_proof_at_cht( &self, cht_size: NumberFor, @@ -599,11 +609,12 @@ impl Client where Ok(proof) } - /// Returns changes trie storage and all configurations that have been active in the range [first; last]. + /// Returns changes trie storage and all configurations that have been active + /// in the range [first; last]. /// /// Configurations are returned in descending order (and obviously never overlap). - /// If fail_if_disabled is false, returns maximal consequent configurations ranges, starting from last and - /// stopping on either first, or when CT have been disabled. + /// If fail_if_disabled is false, returns maximal consequent configurations ranges, + /// starting from last and stopping on either first, or when CT have been disabled. /// If fail_if_disabled is true, fails when there's a subrange where CT have been disabled /// inside first..last blocks range. fn require_changes_trie( @@ -632,7 +643,8 @@ impl Client where break; } - current = *self.backend.blockchain().expect_header(BlockId::Hash(config_range.zero.1))?.parent_hash(); + current = *self.backend.blockchain() + .expect_header(BlockId::Hash(config_range.zero.1))?.parent_hash(); } Ok((storage, configs)) @@ -645,7 +657,9 @@ impl Client where operation: &mut ClientImportOperation, import_block: BlockImportParams>, new_cache: HashMap>, - storage_changes: Option>>, + storage_changes: Option< + sc_consensus_api::StorageChanges> + >, ) -> sp_blockchain::Result where Self: ProvideRuntimeApi, >::Api: CoreApi + @@ -734,7 +748,9 @@ impl Client where import_headers: PrePostHeader, justifications: Option, body: Option>, - storage_changes: Option>>, + storage_changes: Option< + sc_consensus_api::StorageChanges> + >, new_cache: HashMap>, finalized: bool, aux: Vec<(Vec, Option>)>, @@ -775,7 +791,7 @@ impl Client where let storage_changes = match storage_changes { Some(storage_changes) => { let storage_changes = match storage_changes { - sp_consensus::StorageChanges::Changes(storage_changes) => { + sc_consensus_api::StorageChanges::Changes(storage_changes) => { self.backend.begin_state_operation(&mut operation.op, BlockId::Hash(parent_hash))?; let ( main_sc, @@ -800,7 +816,7 @@ impl Client where Some((main_sc, child_sc)) } - sp_consensus::StorageChanges::Import(changes) => { + sc_consensus_api::StorageChanges::Import(changes) => { let storage = sp_storage::Storage { top: changes.state.into_iter().collect(), children_default: Default::default(), @@ -877,7 +893,8 @@ impl Client where operation.op.insert_aux(aux)?; - // we only notify when we are already synced to the tip of the chain or if this import triggers a re-org + // we only notify when we are already synced to the tip of the chain + // or if this import triggers a re-org if make_notifications || tree_route.is_some() { if finalized { operation.notify_finalized.push(hash); @@ -914,10 +931,12 @@ impl Client where let at = BlockId::Hash(*parent_hash); let state_action = std::mem::replace(&mut import_block.state_action, StateAction::Skip); let (enact_state, storage_changes) = match (self.block_status(&at)?, state_action) { - (BlockStatus::Unknown, _) => return Ok(PrepareStorageChangesResult::Discard(ImportResult::UnknownParent)), - (BlockStatus::KnownBad, _) => return Ok(PrepareStorageChangesResult::Discard(ImportResult::KnownBad)), + (BlockStatus::Unknown, _) => + return Ok(PrepareStorageChangesResult::Discard(ImportResult::UnknownParent)), + (BlockStatus::KnownBad, _) => + return Ok(PrepareStorageChangesResult::Discard(ImportResult::KnownBad)), (_, StateAction::Skip) => (false, None), - (BlockStatus::InChainPruned, StateAction::ApplyChanges(sp_consensus::StorageChanges::Changes(_))) => + (BlockStatus::InChainPruned, StateAction::ApplyChanges(sc_consensus_api::StorageChanges::Changes(_))) => return Ok(PrepareStorageChangesResult::Discard(ImportResult::MissingState)), (BlockStatus::InChainPruned, StateAction::Execute) => return Ok(PrepareStorageChangesResult::Discard(ImportResult::MissingState)), @@ -964,7 +983,7 @@ impl Client where { return Err(Error::InvalidStateRoot) } - Some(sp_consensus::StorageChanges::Changes(gen_storage_changes)) + Some(sc_consensus_api::StorageChanges::Changes(gen_storage_changes)) }, // No block body, no storage changes (true, None, None) => None, @@ -987,11 +1006,13 @@ impl Client where let last_finalized = self.backend.blockchain().last_finalized()?; if block == last_finalized { - warn!("Possible safety violation: attempted to re-finalize last finalized block {:?} ", last_finalized); + warn!("Possible safety violation: attempted to re-finalize last finalized block {:?} ", + last_finalized); return Ok(()); } - let route_from_finalized = sp_blockchain::tree_route(self.backend.blockchain(), last_finalized, block)?; + let route_from_finalized = sp_blockchain::tree_route( + self.backend.blockchain(), last_finalized, block)?; if let Some(retracted) = route_from_finalized.retracted().get(0) { warn!("Safety violation: attempted to revert finalized block {:?} which is not in the \ @@ -1194,12 +1215,16 @@ impl Client where } /// Get block header by id. - pub fn header(&self, id: &BlockId) -> sp_blockchain::Result::Header>> { + pub fn header(&self, id: &BlockId) + -> sp_blockchain::Result::Header>> + { self.backend.blockchain().header(*id) } /// Get block body by id. - pub fn body(&self, id: &BlockId) -> sp_blockchain::Result::Extrinsic>>> { + pub fn body(&self, id: &BlockId) + -> sp_blockchain::Result::Extrinsic>>> + { self.backend.blockchain().body(*id) } @@ -1311,7 +1336,9 @@ impl ProofProvider for Client where }) } - fn header_proof(&self, id: &BlockId) -> sp_blockchain::Result<(Block::Header, StorageProof)> { + fn header_proof(&self, id: &BlockId) + -> sp_blockchain::Result<(Block::Header, StorageProof)> + { self.header_proof_with_cht_size(id, cht::size()) } @@ -1405,7 +1432,8 @@ impl BlockBuilderProvider for Client + Send + Sync + 'static, Block: BlockT, Self: ChainHeaderBackend + ProvideRuntimeApi, - >::Api: ApiExt> + >::Api: + ApiExt> + BlockBuilderApi, { fn new_block_at>( @@ -1461,7 +1489,9 @@ impl StorageProvider for Client wher E: CallExecutor, Block: BlockT, { - fn storage_keys(&self, id: &BlockId, key_prefix: &StorageKey) -> sp_blockchain::Result> { + fn storage_keys(&self, id: &BlockId, key_prefix: &StorageKey) + -> sp_blockchain::Result> + { let keys = self.state_at(id)?.keys(&key_prefix.0).into_iter().map(StorageKey).collect(); Ok(keys) } @@ -1668,7 +1698,9 @@ impl ProvideUncles for Client where E: CallExecutor, Block: BlockT, { - fn uncles(&self, target_hash: Block::Hash, max_generation: NumberFor) -> sp_blockchain::Result> { + fn uncles(&self, target_hash: Block::Hash, max_generation: NumberFor) + -> sp_blockchain::Result> + { Ok(Client::uncles(self, target_hash, max_generation)? .into_iter() .filter_map(|hash| Client::header(self, &BlockId::Hash(hash)).unwrap_or(None)) @@ -1817,7 +1849,7 @@ impl CallApiAt for Client where /// objects. Otherwise, importing blocks directly into the client would be bypassing /// important verification work. #[async_trait::async_trait] -impl sp_consensus::BlockImport for &Client where +impl sc_consensus_api::BlockImport for &Client where B: backend::Backend, E: CallExecutor + Send + Sync, Block: BlockT, @@ -1926,7 +1958,7 @@ impl sp_consensus::BlockImport for &Client sp_consensus::BlockImport for Client where +impl sc_consensus_api::BlockImport for Client where B: backend::Backend, E: CallExecutor + Send + Sync, Block: BlockT, diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 40cb1aeea6a9e..fa7a47738b7d7 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -78,7 +78,7 @@ pub use sc_network::config::{ pub use sc_tracing::TracingReceiver; pub use task_manager::SpawnTaskHandle; pub use task_manager::TaskManager; -pub use sp_consensus::import_queue::ImportQueue; +pub use sc_consensus_api::ImportQueue; pub use self::client::{LocalCallExecutor, ClientConfig}; use sc_client_api::{blockchain::HeaderBackend, BlockchainEvents}; diff --git a/client/service/test/Cargo.toml b/client/service/test/Cargo.toml index 27aa14b0d2bc1..b6ae254cb3d69 100644 --- a/client/service/test/Cargo.toml +++ b/client/service/test/Cargo.toml @@ -30,6 +30,7 @@ sc-client-db = { version = "0.9.0", default-features = false, path = "../../db" futures = { version = "0.3.1", features = ["compat"] } sc-service = { version = "0.9.0", features = ["test-helpers"], path = "../../service" } sc-network = { version = "0.9.0", path = "../../network" } +sc-consensus-api = { version = "0.9.0", path = "../../../client/consensus/api" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } sp-runtime = { version = "3.0.0", path = "../../../primitives/runtime" } sp-core = { version = "3.0.0", path = "../../../primitives/core" } diff --git a/client/service/test/src/client/light.rs b/client/service/test/src/client/light.rs index 440e0b4dd0dc3..42e60ff63ede0 100644 --- a/client/service/test/src/client/light.rs +++ b/client/service/test/src/client/light.rs @@ -37,7 +37,7 @@ use substrate_test_runtime_client::{ runtime::{Hash, Block, Header}, TestClient, ClientBlockImportExt, }; use sp_api::{StorageTransactionCache, ProofRecorder}; -use sp_consensus::BlockOrigin; +use sc_consensus_api::BlockOrigin; use sc_executor::{NativeExecutor, WasmExecutionMethod, RuntimeVersion, NativeVersion}; use sp_core::{H256, NativeOrEncoded, testing::TaskExecutor}; use sc_client_api::{ diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index bdd693f57b2d0..0d31f9511791b 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -46,9 +46,10 @@ use sp_api::ProvideRuntimeApi; use sp_core::{H256, ChangesTrieConfiguration, blake2_256, testing::TaskExecutor}; use std::collections::{HashMap, HashSet}; use std::sync::Arc; -use sp_consensus::{ - BlockOrigin, SelectChain, BlockImport, Error as ConsensusError, BlockCheckParams, ImportResult, - BlockStatus, BlockImportParams, ForkChoiceStrategy, +use sp_consensus::{SelectChain, Error as ConsensusError, BlockStatus}; +use sc_consensus_api::{ + BlockOrigin, BlockImport, BlockCheckParams, ImportResult, + BlockImportParams, ForkChoiceStrategy, }; use sp_storage::{StorageKey, ChildInfo}; use sp_trie::{TrieConfiguration, trie_types::Layout}; diff --git a/client/transaction-pool/Cargo.toml b/client/transaction-pool/Cargo.toml index 988f252a63430..baff4939f069d 100644 --- a/client/transaction-pool/Cargo.toml +++ b/client/transaction-pool/Cargo.toml @@ -40,6 +40,7 @@ retain_mut = "0.1.3" assert_matches = "1.3.0" hex = "0.4" sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../client/consensus/api" } substrate-test-runtime-transaction-pool = { version = "2.0.0", path = "../../test-utils/runtime/transaction-pool" } substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" } sc-block-builder = { version = "0.9.0", path = "../block-builder" } diff --git a/client/transaction-pool/tests/pool.rs b/client/transaction-pool/tests/pool.rs index 9a9d59214d0b9..6588cb85e0767 100644 --- a/client/transaction-pool/tests/pool.rs +++ b/client/transaction-pool/tests/pool.rs @@ -33,7 +33,7 @@ use codec::Encode; use std::{collections::BTreeSet, sync::Arc, convert::TryInto}; use sc_client_api::client::BlockchainEvents; use sc_block_builder::BlockBuilderProvider; -use sp_consensus::BlockOrigin; +use sc_consensus_api::BlockOrigin; use sc_transaction_pool::{*, test_helpers::*}; fn pool() -> Pool { diff --git a/primitives/consensus/common/Cargo.toml b/primitives/consensus/common/Cargo.toml index 038a28ddab351..4563ef4caeac8 100644 --- a/primitives/consensus/common/Cargo.toml +++ b/primitives/consensus/common/Cargo.toml @@ -13,32 +13,20 @@ readme = "README.md" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] - [dependencies] -thiserror = "1.0.21" -libp2p = { version = "0.37.1", default-features = false } +async-trait = "0.1.42" +codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } +futures = { version = "0.3.1", features = ["thread-pool"] } log = "0.4.8" -sp-core = { path= "../../core", version = "3.0.0"} +thiserror = "1.0.21" + +sp-api = { version = "3.0.0", path = "../../api" } +sp-core = { version = "3.0.0", path = "../../core"} sp-inherents = { version = "3.0.0", path = "../../inherents" } sp-state-machine = { version = "0.9.0", path = "../../state-machine" } -futures = { version = "0.3.1", features = ["thread-pool"] } -futures-timer = "3.0.1" sp-std = { version = "3.0.0", path = "../../std" } sp-version = { version = "3.0.0", path = "../../version" } sp-runtime = { version = "3.0.0", path = "../../runtime" } -sp-utils = { version = "3.0.0", path = "../../utils" } -sp-trie = { version = "3.0.0", path = "../../trie" } -sp-api = { version = "3.0.0", path = "../../api" } -codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] } -parking_lot = "0.11.1" -serde = { version = "1.0", features = ["derive"] } -prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.9.0"} -wasm-timer = "0.2.5" -async-trait = "0.1.42" - -[dev-dependencies] -futures = "0.3.9" -sp-test-primitives = { version = "2.0.0", path = "../../test-primitives" } [features] default = [] diff --git a/primitives/consensus/common/src/import_queue.rs b/primitives/consensus/common/src/import_queue.rs index fba5b51e921ca..12bd46f5e4ad6 100644 --- a/primitives/consensus/common/src/import_queue.rs +++ b/primitives/consensus/common/src/import_queue.rs @@ -15,272 +15,5 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! Import Queue primitive: something which can verify and import blocks. -//! -//! This serves as an intermediate and abstracted step between synchronization -//! and import. Each mode of consensus will have its own requirements for block -//! verification. Some algorithms can verify in parallel, while others only -//! sequentially. -//! -//! The `ImportQueue` trait allows such verification strategies to be -//! instantiated. The `BasicQueue` and `BasicVerifier` traits allow serial -//! queues to be instantiated simply. - -use std::collections::HashMap; - -use sp_runtime::{Justifications, traits::{Block as BlockT, Header as _, NumberFor}}; - -use crate::{ - error::Error as ConsensusError, - block_import::{ - BlockImport, BlockOrigin, BlockImportParams, ImportedAux, JustificationImport, ImportResult, - BlockCheckParams, ImportedState, StateAction, - }, - metrics::Metrics, -}; -pub use basic_queue::BasicQueue; - -/// A commonly-used Import Queue type. -/// -/// This defines the transaction type of the `BasicQueue` to be the transaction type for a client. -pub type DefaultImportQueue = BasicQueue>; - -mod basic_queue; -pub mod buffered_link; - -/// Shared block import struct used by the queue. -pub type BoxBlockImport = Box< - dyn BlockImport + Send + Sync ->; - -/// Shared justification import struct used by the queue. -pub type BoxJustificationImport = Box + Send + Sync>; - -/// Maps to the Origin used by the network. -pub type Origin = libp2p::PeerId; - -/// Block data used by the queue. -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct IncomingBlock { - /// Block header hash. - pub hash: ::Hash, - /// Block header if requested. - pub header: Option<::Header>, - /// Block body if requested. - pub body: Option::Extrinsic>>, - /// Justification(s) if requested. - pub justifications: Option, - /// The peer, we received this from - pub origin: Option, - /// Allow importing the block skipping state verification if parent state is missing. - pub allow_missing_state: bool, - /// Skip block exection and state verification. - pub skip_execution: bool, - /// Re-validate existing block. - pub import_existing: bool, - /// Do not compute new state, but rather set it to the given set. - pub state: Option>, -} - /// Type of keys in the blockchain cache that consensus module could use for its needs. -pub type CacheKeyId = [u8; 4]; - -/// Verify a justification of a block -#[async_trait::async_trait] -pub trait Verifier: Send + Sync { - /// Verify the given data and return the BlockImportParams and an optional - /// new set of validators to import. If not, err with an Error-Message - /// presented to the User in the logs. - async fn verify( - &mut self, - origin: BlockOrigin, - header: B::Header, - justifications: Option, - body: Option>, - ) -> Result<(BlockImportParams, Option)>>), String>; -} - -/// Blocks import queue API. -/// -/// The `import_*` methods can be called in order to send elements for the import queue to verify. -/// Afterwards, call `poll_actions` to determine how to respond to these elements. -pub trait ImportQueue: Send { - /// Import bunch of blocks. - fn import_blocks(&mut self, origin: BlockOrigin, blocks: Vec>); - /// Import block justifications. - fn import_justifications( - &mut self, - who: Origin, - hash: B::Hash, - number: NumberFor, - justifications: Justifications - ); - /// Polls for actions to perform on the network. - /// - /// This method should behave in a way similar to `Future::poll`. It can register the current - /// task and notify later when more actions are ready to be polled. To continue the comparison, - /// it is as if this method always returned `Poll::Pending`. - fn poll_actions(&mut self, cx: &mut futures::task::Context, link: &mut dyn Link); -} - -/// Hooks that the verification queue can use to influence the synchronization -/// algorithm. -pub trait Link: Send { - /// Batch of blocks imported, with or without error. - fn blocks_processed( - &mut self, - _imported: usize, - _count: usize, - _results: Vec<(Result>, BlockImportError>, B::Hash)> - ) {} - /// Justification import result. - fn justification_imported(&mut self, _who: Origin, _hash: &B::Hash, _number: NumberFor, _success: bool) {} - /// Request a justification for the given block. - fn request_justification(&mut self, _hash: &B::Hash, _number: NumberFor) {} -} - -/// Block import successful result. -#[derive(Debug, PartialEq)] -pub enum BlockImportResult { - /// Imported known block. - ImportedKnown(N, Option), - /// Imported unknown block. - ImportedUnknown(N, ImportedAux, Option), -} - -/// Block import error. -#[derive(Debug)] -pub enum BlockImportError { - /// Block missed header, can't be imported - IncompleteHeader(Option), - /// Block verification failed, can't be imported - VerificationFailed(Option, String), - /// Block is known to be Bad - BadBlock(Option), - /// Parent state is missing. - MissingState, - /// Block has an unknown parent - UnknownParent, - /// Block import has been cancelled. This can happen if the parent block fails to be imported. - Cancelled, - /// Other error. - Other(ConsensusError), -} - -/// Single block import function. -pub async fn import_single_block, Transaction: Send + 'static>( - import_handle: &mut impl BlockImport, - block_origin: BlockOrigin, - block: IncomingBlock, - verifier: &mut V, -) -> Result>, BlockImportError> { - import_single_block_metered(import_handle, block_origin, block, verifier, None).await -} - -/// Single block import function with metering. -pub(crate) async fn import_single_block_metered, Transaction: Send + 'static>( - import_handle: &mut impl BlockImport, - block_origin: BlockOrigin, - block: IncomingBlock, - verifier: &mut V, - metrics: Option, -) -> Result>, BlockImportError> { - let peer = block.origin; - - let (header, justifications) = match (block.header, block.justifications) { - (Some(header), justifications) => (header, justifications), - (None, _) => { - if let Some(ref peer) = peer { - debug!(target: "sync", "Header {} was not provided by {} ", block.hash, peer); - } else { - debug!(target: "sync", "Header {} was not provided ", block.hash); - } - return Err(BlockImportError::IncompleteHeader(peer)) - }, - }; - - trace!(target: "sync", "Header {} has {:?} logs", block.hash, header.digest().logs().len()); - - let number = header.number().clone(); - let hash = header.hash(); - let parent_hash = header.parent_hash().clone(); - - let import_handler = |import| { - match import { - Ok(ImportResult::AlreadyInChain) => { - trace!(target: "sync", "Block already in chain {}: {:?}", number, hash); - Ok(BlockImportResult::ImportedKnown(number, peer.clone())) - }, - Ok(ImportResult::Imported(aux)) => Ok(BlockImportResult::ImportedUnknown(number, aux, peer.clone())), - Ok(ImportResult::MissingState) => { - debug!(target: "sync", "Parent state is missing for {}: {:?}, parent: {:?}", number, hash, parent_hash); - Err(BlockImportError::MissingState) - }, - Ok(ImportResult::UnknownParent) => { - debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", number, hash, parent_hash); - Err(BlockImportError::UnknownParent) - }, - Ok(ImportResult::KnownBad) => { - debug!(target: "sync", "Peer gave us a bad block {}: {:?}", number, hash); - Err(BlockImportError::BadBlock(peer.clone())) - }, - Err(e) => { - debug!(target: "sync", "Error importing block {}: {:?}: {:?}", number, hash, e); - Err(BlockImportError::Other(e)) - } - } - }; - - match import_handler(import_handle.check_block(BlockCheckParams { - hash, - number, - parent_hash, - allow_missing_state: block.allow_missing_state, - import_existing: block.import_existing, - }).await)? { - BlockImportResult::ImportedUnknown { .. } => (), - r => return Ok(r), // Any other successful result means that the block is already imported. - } - - let started = wasm_timer::Instant::now(); - let (mut import_block, maybe_keys) = verifier.verify( - block_origin, - header, - justifications, - block.body - ).await.map_err(|msg| { - if let Some(ref peer) = peer { - trace!(target: "sync", "Verifying {}({}) from {} failed: {}", number, hash, peer, msg); - } else { - trace!(target: "sync", "Verifying {}({}) failed: {}", number, hash, msg); - } - if let Some(metrics) = metrics.as_ref() { - metrics.report_verification(false, started.elapsed()); - } - BlockImportError::VerificationFailed(peer.clone(), msg) - })?; - - if let Some(metrics) = metrics.as_ref() { - metrics.report_verification(true, started.elapsed()); - } - - let mut cache = HashMap::new(); - if let Some(keys) = maybe_keys { - cache.extend(keys.into_iter()); - } - import_block.import_existing = block.import_existing; - let mut import_block = import_block.clear_storage_changes_and_mutate(); - if let Some(state) = block.state { - import_block.state_action = StateAction::ApplyChanges(crate::StorageChanges::Import(state)); - } else if block.skip_execution { - import_block.state_action = StateAction::Skip; - } else if block.allow_missing_state { - import_block.state_action = StateAction::ExecuteIfPossible; - } - - let imported = import_handle.import_block(import_block, cache).await; - if let Some(metrics) = metrics.as_ref() { - metrics.report_verification_and_import(started.elapsed()); - } - import_handler(imported) -} +pub type CacheKeyId = [u8; 4]; \ No newline at end of file diff --git a/primitives/consensus/common/src/lib.rs b/primitives/consensus/common/src/lib.rs index 51b2a96e17758..73b7ae1cac852 100644 --- a/primitives/consensus/common/src/lib.rs +++ b/primitives/consensus/common/src/lib.rs @@ -27,8 +27,6 @@ // our error-chain could potentially blow up otherwise #![recursion_limit="128"] -#[macro_use] extern crate log; - use std::sync::Arc; use std::time::Duration; @@ -40,21 +38,13 @@ use sp_state_machine::StorageProof; pub mod block_validation; pub mod error; -pub mod block_import; mod select_chain; pub mod import_queue; pub mod evaluation; -mod metrics; pub use self::error::Error; -pub use block_import::{ - BlockCheckParams, BlockImport, BlockImportParams, BlockOrigin, ForkChoiceStrategy, - ImportResult, ImportedAux, ImportedState, JustificationImport, JustificationSyncLink, - StateAction, StorageChanges, -}; pub use select_chain::SelectChain; pub use sp_state_machine::Backend as StateBackend; -pub use import_queue::DefaultImportQueue; pub use sp_inherents::InherentData; /// Block status. diff --git a/test-utils/client/Cargo.toml b/test-utils/client/Cargo.toml index 925a69e41bb48..00a82350605f3 100644 --- a/test-utils/client/Cargo.toml +++ b/test-utils/client/Cargo.toml @@ -22,6 +22,7 @@ serde_json = "1.0.55" sc-client-api = { version = "3.0.0", path = "../../client/api" } sc-client-db = { version = "0.9.0", features = ["test-helpers"], path = "../../client/db" } sc-consensus = { version = "0.9.0", path = "../../client/consensus/common" } +sc-consensus-api = { version = "0.9.0", path = "../../client/consensus/api" } sc-executor = { version = "0.9.0", path = "../../client/executor" } sc-light = { version = "3.0.0", path = "../../client/light" } sc-offchain = { version = "3.0.0", path = "../../client/offchain" } diff --git a/test-utils/client/src/client_ext.rs b/test-utils/client/src/client_ext.rs index edba96d760fc2..fb884f5d84145 100644 --- a/test-utils/client/src/client_ext.rs +++ b/test-utils/client/src/client_ext.rs @@ -20,10 +20,8 @@ use sc_service::client::Client; use sc_client_api::backend::Finalizer; use sc_client_api::client::BlockBackend; -use sp_consensus::{ - BlockImportParams, BlockImport, BlockOrigin, Error as ConsensusError, - ForkChoiceStrategy, -}; +use sc_consensus_api::{BlockImportParams, BlockImport, BlockOrigin, ForkChoiceStrategy}; +use sp_consensus::Error as ConsensusError; use sp_runtime::{Justification, Justifications}; use sp_runtime::traits::{Block as BlockT}; use sp_runtime::generic::BlockId; diff --git a/test-utils/runtime/Cargo.toml b/test-utils/runtime/Cargo.toml index 60283bb97d183..e13be580a35c3 100644 --- a/test-utils/runtime/Cargo.toml +++ b/test-utils/runtime/Cargo.toml @@ -52,6 +52,7 @@ serde = { version = "1.0.101", optional = true, features = ["derive"] } [dev-dependencies] sc-block-builder = { version = "0.9.0", path = "../../client/block-builder" } sc-executor = { version = "0.9.0", path = "../../client/executor" } +sc-consensus-api = { version = "0.9.0", default-features = false, path = "../../client/consensus/api" } substrate-test-runtime-client = { version = "2.0.0", path = "./client" } futures = "0.3.9" diff --git a/test-utils/runtime/client/Cargo.toml b/test-utils/runtime/client/Cargo.toml index 0c822f0cdff84..a69a4caaeed4a 100644 --- a/test-utils/runtime/client/Cargo.toml +++ b/test-utils/runtime/client/Cargo.toml @@ -13,6 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] sc-light = { version = "3.0.0", path = "../../../client/light" } +sc-consensus-api = { version = "0.9.0", path = "../../../client/consensus/api" } sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" } sc-block-builder = { version = "0.9.0", path = "../../../client/block-builder" } substrate-test-client = { version = "2.0.0", path = "../../client" } diff --git a/test-utils/runtime/client/src/trait_tests.rs b/test-utils/runtime/client/src/trait_tests.rs index 797c7ec089bd6..b596f54db47af 100644 --- a/test-utils/runtime/client/src/trait_tests.rs +++ b/test-utils/runtime/client/src/trait_tests.rs @@ -27,7 +27,7 @@ use crate::{ }; use sc_client_api::backend; use sc_client_api::blockchain::{Backend as BlockChainBackendT, HeaderBackend}; -use sp_consensus::BlockOrigin; +use sc_consensus_api::BlockOrigin; use substrate_test_runtime::{self, Transfer}; use sp_runtime::generic::BlockId; use sp_runtime::traits::Block as BlockT; diff --git a/test-utils/runtime/src/lib.rs b/test-utils/runtime/src/lib.rs index 084f1338cd261..afd168c08a4d6 100644 --- a/test-utils/runtime/src/lib.rs +++ b/test-utils/runtime/src/lib.rs @@ -1205,11 +1205,11 @@ fn test_witness(proof: StorageProof, root: crate::Hash) { #[cfg(test)] mod tests { use substrate_test_runtime_client::{ - prelude::*, - sp_consensus::BlockOrigin, + prelude::*, DefaultTestClientBuilderExt, TestClientBuilder, runtime::TestAPI, }; + use sc_consensus_api::BlockOrigin; use sp_api::ProvideRuntimeApi; use sp_runtime::generic::BlockId; use sp_core::storage::well_known_keys::HEAP_PAGES; diff --git a/test-utils/test-runner/Cargo.toml b/test-utils/test-runner/Cargo.toml index cf171b0ea5ef3..72268400195ac 100644 --- a/test-utils/test-runner/Cargo.toml +++ b/test-utils/test-runner/Cargo.toml @@ -18,6 +18,7 @@ sc-transaction-pool = { version = "3.0.0", path = "../../client/transaction-pool sc-transaction-pool-api = { version = "3.0.0", path = "../../client/transaction-pool/api" } sc-client-api = { version = "3.0.0", path = "../../client/api" } sc-rpc-server = { version = "3.0.0", path = "../../client/rpc-servers" } +sc-consensus-api = { version = "0.9.0", path = "../../client/consensus/api" } manual-seal = { package = "sc-consensus-manual-seal", version = "0.9.0", path = "../../client/consensus/manual-seal" } # primitive deps diff --git a/test-utils/test-runner/src/lib.rs b/test-utils/test-runner/src/lib.rs index 000d3efc3e96f..7c210bcdb88fc 100644 --- a/test-utils/test-runner/src/lib.rs +++ b/test-utils/test-runner/src/lib.rs @@ -230,7 +230,8 @@ use manual_seal::consensus::ConsensusDataProvider; use sc_executor::NativeExecutionDispatch; use sc_service::{Configuration, TFullBackend, TFullClient, TaskManager, TaskExecutor}; use sp_api::{ConstructRuntimeApi, TransactionFor}; -use sp_consensus::{BlockImport, SelectChain}; +use sp_consensus::SelectChain; +use sc_consensus_api::{BlockImport}; use sp_inherents::{CreateInherentDataProviders, InherentDataProvider}; use sp_keystore::SyncCryptoStorePtr; use sp_runtime::traits::{Block as BlockT, SignedExtension};