Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Refactor sr-api to not depend on client anymore (paritytech#4086)
Browse files Browse the repository at this point in the history
* Refactor sr-api to not depend on client anymore

* Fix benches

* Apply suggestions from code review

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Apply suggestions from code review
  • Loading branch information
bkchr authored and gnunicorn committed Nov 11, 2019
1 parent a3c5b77 commit d2c4b0d
Show file tree
Hide file tree
Showing 140 changed files with 1,510 additions and 980 deletions.
178 changes: 130 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ members = [
"core/consensus/slots",
"core/consensus/uncles",
"core/consensus/pow",
"core/block-builder",
"core/block-builder/runtime-api",
"core/executor",
"core/executor/runtime-test",
"core/externalities",
Expand All @@ -36,7 +38,9 @@ members = [
"core/service",
"core/service/test",
"core/session",
"core/sr-api-macros",
"core/sr-api",
"core/sr-api/proc-macro",
"core/sr-api/test",
"core/sr-arithmetic",
"core/sr-io",
"core/sr-primitives",
Expand All @@ -51,6 +55,7 @@ members = [
"core/test-runtime/client",
"core/transaction-pool",
"core/transaction-pool/graph",
"core/transaction-pool/runtime-api",
"core/trie",
"core/utils/fork-tree",
"core/utils/wasm-builder",
Expand Down
1 change: 1 addition & 0 deletions core/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ futures-timer = "0.4"
parking_lot = "0.9.0"
peerset = { package = "substrate-peerset", path = "../../core/peerset" }
test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" }
sr-api = { path = "../sr-api" }
10 changes: 5 additions & 5 deletions core/authority-discovery/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ edition = "2018"

[dependencies]
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
client = { package = "substrate-client", path = "../../client", default-features = false }
sr-api = { path = "../../sr-api", default-features = false }
sr-primitives = { path = "../../sr-primitives", default-features = false }
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }

[features]
default = ["std"]
std = [
"rstd/std",
"client/std",
"codec/std",
"sr-primitives/std"
"rstd/std",
"sr-api/std",
"codec/std",
"sr-primitives/std"
]
3 changes: 1 addition & 2 deletions core/authority-discovery/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#![cfg_attr(not(feature = "std"), no_std)]

use client::decl_runtime_apis;
use rstd::vec::Vec;
use sr_primitives::RuntimeDebug;

Expand All @@ -29,7 +28,7 @@ pub struct Signature(pub Vec<u8>);
#[cfg_attr(feature = "std", derive(Hash))]
pub struct AuthorityId(pub Vec<u8>);

decl_runtime_apis! {
sr_api::decl_runtime_apis! {
/// The authority discovery api.
///
/// This api is used by the `core/authority-discovery` module to retrieve our
Expand Down
8 changes: 5 additions & 3 deletions core/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi>::Api: AuthorityDiscoveryApi<Block>,
<Client as ProvideRuntimeApi>::Api: AuthorityDiscoveryApi<Block, Error = client::error::Error>,
Self: Future<Output = ()>,
{
/// Return a new authority discovery.
Expand Down Expand Up @@ -303,7 +303,7 @@ where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi>::Api: AuthorityDiscoveryApi<Block>,
<Client as ProvideRuntimeApi>::Api: AuthorityDiscoveryApi<Block, Error = client::error::Error>,
{
type Output = ();

Expand Down Expand Up @@ -404,7 +404,7 @@ fn hash_authority_id(id: &[u8]) -> Result<libp2p::kad::record::Key> {
#[cfg(test)]
mod tests {
use super::*;
use client::runtime_api::{ApiExt, Core, RuntimeVersion, StorageProof};
use sr_api::{ApiExt, Core, RuntimeVersion, StorageProof};
use futures::channel::mpsc::channel;
use futures::executor::block_on;
use futures::future::poll_fn;
Expand Down Expand Up @@ -501,6 +501,8 @@ mod tests {
}

impl ApiExt<Block> for RuntimeApi {
type Error = client::error::Error;

fn map_api_result<F: FnOnce(&Self) -> std::result::Result<R, E>, R, E>(
&self,
_: F,
Expand Down
3 changes: 2 additions & 1 deletion core/basic-authorship/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ client = { package = "substrate-client", path = "../../core/client" }
consensus_common = { package = "substrate-consensus-common", path = "../../core/consensus/common" }
inherents = { package = "substrate-inherents", path = "../inherents" }
substrate-telemetry = { path = "../telemetry" }
transaction_pool = { package = "substrate-transaction-pool", path = "../../core/transaction-pool" }
transaction_pool = { package = "substrate-transaction-pool", path = "../transaction-pool" }
block-builder = { package = "substrate-block-builder", path = "../block-builder" }

[dev-dependencies]
test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" }
17 changes: 9 additions & 8 deletions core/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
//

use std::{time, sync::Arc};
use client::{
error, Client as SubstrateClient, CallExecutor,
block_builder::api::BlockBuilder as BlockBuilderApi,
};
use client::{error, Client as SubstrateClient, CallExecutor};
use codec::Decode;
use consensus_common::{evaluation};
use inherents::InherentData;
Expand All @@ -37,6 +34,7 @@ use sr_primitives::{
};
use transaction_pool::txpool::{self, Pool as TransactionPool};
use substrate_telemetry::{telemetry, CONSENSUS_INFO};
use block_builder::BlockBuilderApi;

/// Proposer factory.
pub struct ProposerFactory<C, A> where A: txpool::ChainApi {
Expand All @@ -55,7 +53,8 @@ where
Block: BlockT<Hash=H256>,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api:
BlockBuilderApi<Block, Error = client::error::Error>,
{
type Proposer = Proposer<Block, SubstrateClient<B, E, Block, RA>, A>;
type Error = error::Error;
Expand Down Expand Up @@ -102,7 +101,8 @@ where
Block: BlockT<Hash=H256>,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api:
BlockBuilderApi<Block, Error = client::error::Error>,
{
type Create = futures::future::Ready<Result<Block, error::Error>>;
type Error = error::Error;
Expand All @@ -126,7 +126,8 @@ impl<Block, B, E, RA, A> Proposer<Block, SubstrateClient<B, E, Block, RA>, A> wh
Block: BlockT<Hash=H256>,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api:
BlockBuilderApi<Block, Error = client::error::Error>,
{
fn propose_with(
&self,
Expand Down Expand Up @@ -167,7 +168,7 @@ impl<Block, B, E, RA, A> Proposer<Block, SubstrateClient<B, E, Block, RA>, A> wh
}

trace!("[{:?}] Pushing to the block.", pending.hash);
match client::block_builder::BlockBuilder::push(&mut block_builder, pending.data.clone()) {
match block_builder::BlockBuilder::push(&mut block_builder, pending.data.clone()) {
Ok(()) => {
debug!("[{:?}] Pushed to the block.", pending.hash);
}
Expand Down
14 changes: 14 additions & 0 deletions core/block-builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "substrate-block-builder"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
state-machine = { package = "substrate-state-machine", path = "../state-machine" }
sr-primitives = { path = "../sr-primitives" }
primitives = { package = "substrate-primitives", path = "../primitives" }
codec = { package = "parity-scale-codec", version = "1.0.6", features = ["derive"] }
runtime_api = { package = "substrate-block-builder-runtime-api", path = "runtime-api" }
sr-api = { path = "../sr-api" }

20 changes: 20 additions & 0 deletions core/block-builder/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "substrate-block-builder-runtime-api"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
sr-primitives = { path = "../../sr-primitives", default-features = false }
sr-api = { path = "../../sr-api", default-features = false }
rstd = { package = "sr-std", path = "../../sr-std", default-features = false }
inherents = { package = "substrate-inherents", path = "../../inherents", default-features = false }

[features]
default = [ "std" ]
std = [
"sr-primitives/std",
"inherents/std",
"sr-api/std",
"rstd/std",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
Expand All @@ -14,15 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! The runtime api for building blocks.
//! The block builder runtime api.

#![cfg_attr(not(feature = "std"), no_std)]

use sr_primitives::{traits::Block as BlockT, ApplyResult};
use rstd::vec::Vec;
use sr_api_macros::decl_runtime_apis;
pub use inherents::{InherentData, CheckInherentsResult};

decl_runtime_apis! {
/// The `BlockBuilder` api trait that provides required functions for building a block for a runtime.
use inherents::{InherentData, CheckInherentsResult};

sr_api::decl_runtime_apis! {
/// The `BlockBuilder` api trait that provides the required functionality for building a block.
#[api_version(3)]
pub trait BlockBuilder {
/// Apply the given extrinsics.
Expand All @@ -31,7 +32,9 @@ decl_runtime_apis! {
#[renamed("finalise_block", 3)]
fn finalize_block() -> <Block as BlockT>::Header;
/// Generate inherent extrinsics. The inherent data will vary from chain to chain.
fn inherent_extrinsics(inherent: InherentData) -> Vec<<Block as BlockT>::Extrinsic>;
fn inherent_extrinsics(
inherent: InherentData,
) -> rstd::vec::Vec<<Block as BlockT>::Extrinsic>;
/// Check that the inherents are valid. The inherent data will vary from chain to chain.
fn check_inherents(block: Block, data: InherentData) -> CheckInherentsResult;
/// Generate a random seed.
Expand Down
Loading

0 comments on commit d2c4b0d

Please sign in to comment.