Refactor sr-api to not depend on client anymore (#4086)

* 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
This commit is contained in:
Bastian Köcher
2019-11-11 16:26:49 +01:00
committed by Benjamin Kampmann
parent e26d1a0b3e
commit 2ecffa1cd0
140 changed files with 1514 additions and 984 deletions
+3 -1
View File
@@ -20,13 +20,15 @@ client = { package = "substrate-client", path = "../../client" }
substrate-telemetry = { path = "../../telemetry" }
keystore = { package = "substrate-keystore", path = "../../keystore" }
consensus_common = { package = "substrate-consensus-common", path = "../common" }
sr-primitives = { path = "../../sr-primitives" }
sr-primitives = { path = "../../sr-primitives" }
sr-api = { path = "../../sr-api" }
futures-preview = { version = "0.3.0-alpha.19", features = ["compat"] }
futures01 = { package = "futures", version = "0.1" }
futures-timer = "0.4.0"
parking_lot = "0.9.0"
log = "0.4.8"
derive_more = "0.15.0"
block-builder-api = { package = "substrate-block-builder-runtime-api", path = "../../block-builder/runtime-api" }
[dev-dependencies]
keyring = { package = "substrate-keyring", path = "../../keyring" }
@@ -7,7 +7,7 @@ edition = "2018"
[dependencies]
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
substrate-client = { path = "../../../client", default-features = false }
sr-api = { path = "../../../sr-api", default-features = false }
app-crypto = { package = "substrate-application-crypto", path = "../../../application-crypto", default-features = false }
rstd = { package = "sr-std", path = "../../../sr-std", default-features = false }
sr-primitives = { path = "../../../sr-primitives", default-features = false }
@@ -18,6 +18,6 @@ std = [
"rstd/std",
"codec/std",
"sr-primitives/std",
"substrate-client/std",
"sr-api/std",
"app-crypto/std",
]
@@ -19,7 +19,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Encode, Decode, Codec};
use substrate_client::decl_runtime_apis;
use rstd::vec::Vec;
use sr_primitives::ConsensusEngineId;
@@ -74,7 +73,7 @@ pub enum ConsensusLog<AuthorityId: Codec> {
OnDisabled(AuthorityIndex),
}
decl_runtime_apis! {
sr_api::decl_runtime_apis! {
/// API necessary for block authorship with aura.
pub trait AuraApi<AuthorityId: Codec> {
/// Return the slot duration in seconds for Aura.
+19 -11
View File
@@ -39,16 +39,17 @@ use consensus_common::import_queue::{
Verifier, BasicQueue, BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport,
};
use client::{
block_builder::api::BlockBuilder as BlockBuilderApi, blockchain::ProvideCache,
runtime_api::ApiExt, error::Result as CResult, backend::AuxStore, BlockOf,
blockchain::ProvideCache, error::Result as CResult, backend::AuxStore, BlockOf,
well_known_cache_keys::{self, Id as CacheKeyId},
};
use block_builder_api::BlockBuilder as BlockBuilderApi;
use sr_primitives::{generic::{BlockId, OpaqueDigestItemId}, Justification};
use sr_primitives::traits::{Block as BlockT, Header, DigestItemFor, ProvideRuntimeApi, Zero, Member};
use primitives::crypto::Pair;
use inherents::{InherentDataProviders, InherentData, RuntimeString};
use inherents::{InherentDataProviders, InherentData};
use futures::prelude::*;
use parking_lot::Mutex;
@@ -65,6 +66,8 @@ use slots::check_equivocation;
use keystore::KeyStorePtr;
use sr_api::ApiExt;
pub use aura_primitives::*;
pub use consensus_common::SyncOracle;
pub use digest::CompatibleDigestItem;
@@ -85,7 +88,7 @@ impl SlotDuration {
A: Codec,
B: BlockT,
C: AuxStore + ProvideRuntimeApi,
C::Api: AuraApi<B, A>,
C::Api: AuraApi<B, A, Error = client::error::Error>,
{
slots::SlotDuration::get_or_compute(client, |a, b| a.slot_duration(b)).map(Self)
}
@@ -332,7 +335,7 @@ enum Error<B: BlockT> {
TooFarInFuture,
Client(client::error::Error),
DataProvider(String),
Runtime(RuntimeString)
Runtime(String),
}
fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<u64, Error<B>>
@@ -438,7 +441,7 @@ impl<C, P, T> AuraVerifier<C, P, T>
inherent_data: InherentData,
timestamp_now: u64,
) -> Result<(), Error<B>>
where C: ProvideRuntimeApi, C::Api: BlockBuilderApi<B>
where C: ProvideRuntimeApi, C::Api: BlockBuilderApi<B, Error = client::error::Error>
{
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
@@ -471,7 +474,7 @@ impl<C, P, T> AuraVerifier<C, P, T>
thread::sleep(Duration::from_secs(diff));
Ok(())
},
Some(TIError::Other(e)) => Err(Error::Runtime(e)),
Some(TIError::Other(e)) => Err(Error::Runtime(e.into())),
None => Err(Error::DataProvider(
self.inherent_data_providers.error_to_string(&i, &e)
)),
@@ -485,7 +488,7 @@ impl<C, P, T> AuraVerifier<C, P, T>
#[forbid(deprecated)]
impl<B: BlockT, C, P, T> Verifier<B> for AuraVerifier<C, P, T> where
C: ProvideRuntimeApi + Send + Sync + client::backend::AuxStore + ProvideCache<B> + BlockOf,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>>,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B, Error = client::error::Error>,
DigestItemFor<B>: CompatibleDigestItem<P>,
P: Pair + Send + Sync + 'static,
P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + 'static,
@@ -499,7 +502,9 @@ impl<B: BlockT, C, P, T> Verifier<B> for AuraVerifier<C, P, T> where
justification: Option<Justification>,
mut body: Option<Vec<B::Extrinsic>>,
) -> Result<(BlockImportParams<B>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
let mut inherent_data = self.inherent_data_providers.create_inherent_data().map_err(String::from)?;
let mut inherent_data = self.inherent_data_providers
.create_inherent_data()
.map_err(|e| e.into_string())?;
let (timestamp_now, slot_now, _) = AuraSlotCompatible.extract_timestamp_and_slot(&inherent_data)
.map_err(|e| format!("Could not extract timestamp and slot: {:?}", e))?;
let hash = header.hash();
@@ -530,7 +535,10 @@ impl<B: BlockT, C, P, T> Verifier<B> for AuraVerifier<C, P, T> where
// skip the inherents verification if the runtime API is old.
if self.client
.runtime_api()
.has_api_with::<dyn BlockBuilderApi<B>, _>(&BlockId::Hash(parent_hash), |v| v >= 2)
.has_api_with::<dyn BlockBuilderApi<B, Error = ()>, _>(
&BlockId::Hash(parent_hash),
|v| v >= 2,
)
.map_err(|e| format!("{:?}", e))?
{
self.check_inherents(
@@ -667,7 +675,7 @@ pub fn import_queue<B, C, P, T>(
) -> Result<AuraImportQueue<B>, consensus_common::Error> where
B: BlockT,
C: 'static + ProvideRuntimeApi + BlockOf + ProvideCache<B> + Send + Sync + AuxStore,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>>,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B, Error = client::error::Error>,
DigestItemFor<B>: CompatibleDigestItem<P>,
P: Pair + Send + Sync + 'static,
P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode,