Simplify runtime api error handling (#8114)

* Ahh

* Work work work

* Fix all the compilation errors

* Fix test

* More fixes...
This commit is contained in:
Bastian Köcher
2021-02-15 12:55:40 +01:00
committed by GitHub
parent b5e692104c
commit 33f9becf41
48 changed files with 270 additions and 415 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ pub trait CallExecutor<B: BlockT> {
Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
NC: FnOnce() -> result::Result<R, sp_api::ApiError> + UnwindSafe,
>(
&self,
initialize_block_fn: IB,
@@ -93,7 +93,7 @@ where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block, Error = sp_blockchain::Error>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block>,
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
{
new_worker_and_service_with_config(
@@ -121,7 +121,7 @@ where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block, Error = sp_blockchain::Error>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block>,
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
{
let (to_worker, from_service) = mpsc::channel(0);
@@ -132,7 +132,7 @@ where
Network: NetworkProvider,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi<Block>>::Api:
AuthorityDiscoveryApi<Block, Error = sp_blockchain::Error>,
AuthorityDiscoveryApi<Block>,
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
{
/// Construct a [`Worker`].
@@ -332,7 +332,7 @@ where
.client
.runtime_api()
.authorities(&id)
.map_err(Error::CallingRuntime)?
.map_err(|e| Error::CallingRuntime(e.into()))?
.into_iter()
.filter(|id| !local_keys.contains(id.as_ref()))
.collect();
@@ -546,7 +546,7 @@ where
let id = BlockId::hash(client.info().best_hash);
let authorities = client.runtime_api()
.authorities(&id)
.map_err(Error::CallingRuntime)?
.map_err(|e| Error::CallingRuntime(e.into()))?
.into_iter()
.map(std::convert::Into::into)
.collect::<HashSet<_>>();
@@ -100,8 +100,6 @@ pub(crate) struct RuntimeApi {
sp_api::mock_impl_runtime_apis! {
impl AuthorityDiscoveryApi<Block> for RuntimeApi {
type Error = sp_blockchain::Error;
fn authorities(&self) -> Vec<AuthorityId> {
self.authorities.clone()
}
@@ -99,7 +99,7 @@ impl<B, Block, C, A> ProposerFactory<A, B, C>
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
+ BlockBuilderApi<Block>,
{
pub fn init_with_now(
&mut self,
@@ -138,7 +138,7 @@ impl<A, B, Block, C> sp_consensus::Environment<Block> for
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
+ BlockBuilderApi<Block>,
{
type CreateProposer = future::Ready<Result<Self::Proposer, Self::Error>>;
type Proposer = Proposer<B, Block, C, A>;
@@ -175,7 +175,7 @@ impl<A, B, Block, C> sp_consensus::Proposer<Block> for
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
+ BlockBuilderApi<Block>,
{
type Transaction = backend::TransactionFor<B, Block>;
type Proposal = Pin<Box<dyn Future<
@@ -221,7 +221,7 @@ impl<A, B, Block, C> Proposer<B, Block, C, A>
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
+ BlockBuilderApi<Block>,
{
async fn propose_with(
self,
+8 -13
View File
@@ -35,8 +35,7 @@ use sp_runtime::{
use sp_blockchain::{ApplyExtrinsicFailed, Error};
use sp_core::ExecutionContext;
use sp_api::{
Core, ApiExt, ApiErrorFor, ApiRef, ProvideRuntimeApi, StorageChanges, StorageProof,
TransactionOutcome,
Core, ApiExt, ApiRef, ProvideRuntimeApi, StorageChanges, StorageProof, TransactionOutcome,
};
use sp_consensus::RecordProof;
@@ -106,8 +105,7 @@ impl<'a, Block, A, B> BlockBuilder<'a, Block, A, B>
where
Block: BlockT,
A: ProvideRuntimeApi<Block> + 'a,
A::Api: BlockBuilderApi<Block, Error = Error> +
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
A::Api: BlockBuilderApi<Block> + ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
B: backend::Backend<Block>,
{
/// Create a new instance of builder based on the given `parent_hash` and `parent_number`.
@@ -122,7 +120,7 @@ where
record_proof: RecordProof,
inherent_digests: DigestFor<Block>,
backend: &'a B,
) -> Result<Self, ApiErrorFor<A, Block>> {
) -> Result<Self, Error> {
let header = <<Block as BlockT>::Header as HeaderT>::new(
parent_number + One::one(),
Default::default(),
@@ -155,7 +153,7 @@ where
/// Push onto the block's list of extrinsics.
///
/// This will ensure the extrinsic can be validly executed (by executing it).
pub fn push(&mut self, xt: <Block as BlockT>::Extrinsic) -> Result<(), ApiErrorFor<A, Block>> {
pub fn push(&mut self, xt: <Block as BlockT>::Extrinsic) -> Result<(), Error> {
let block_id = &self.block_id;
let extrinsics = &mut self.extrinsics;
@@ -174,7 +172,7 @@ where
Err(ApplyExtrinsicFailed::Validity(tx_validity).into()),
)
},
Err(e) => TransactionOutcome::Rollback(Err(e)),
Err(e) => TransactionOutcome::Rollback(Err(Error::from(e))),
}
})
}
@@ -184,10 +182,7 @@ where
/// Returns the build `Block`, the changes to the storage and an optional `StorageProof`
/// supplied by `self.api`, combined as [`BuiltBlock`].
/// The storage proof will be `Some(_)` when proof recording was enabled.
pub fn build(mut self) -> Result<
BuiltBlock<Block, backend::StateBackendFor<B, Block>>,
ApiErrorFor<A, Block>
> {
pub fn build(mut self) -> Result<BuiltBlock<Block, backend::StateBackendFor<B, Block>>, Error> {
let header = self.api.finalize_block_with_context(
&self.block_id, ExecutionContext::BlockConstruction
)?;
@@ -227,7 +222,7 @@ where
pub fn create_inherents(
&mut self,
inherent_data: sp_inherents::InherentData,
) -> Result<Vec<Block::Extrinsic>, ApiErrorFor<A, Block>> {
) -> Result<Vec<Block::Extrinsic>, Error> {
let block_id = self.block_id;
self.api.execute_in_transaction(move |api| {
// `create_inherents` should not change any state, to ensure this we always rollback
@@ -237,7 +232,7 @@ where
ExecutionContext::BlockConstruction,
inherent_data
))
})
}).map_err(|e| Error::Application(Box::new(e)))
}
}
+7 -7
View File
@@ -98,9 +98,9 @@ pub fn slot_duration<A, B, C>(client: &C) -> CResult<SlotDuration> where
A: Codec,
B: BlockT,
C: AuxStore + ProvideRuntimeApi<B>,
C::Api: AuraApi<B, A, Error = sp_blockchain::Error>,
C::Api: AuraApi<B, A>,
{
SlotDuration::get_or_compute(client, |a, b| a.slot_duration(b))
SlotDuration::get_or_compute(client, |a, b| a.slot_duration(b).map_err(Into::into))
}
/// Get slot author for given block along with authorities.
@@ -515,7 +515,7 @@ impl<C, P, CAW> AuraVerifier<C, P, CAW> where
inherent_data: InherentData,
timestamp_now: u64,
) -> Result<(), Error<B>> where
C: ProvideRuntimeApi<B>, C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
C: ProvideRuntimeApi<B>, C::Api: BlockBuilderApi<B>,
CAW: CanAuthorWith<B>,
{
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
@@ -534,7 +534,7 @@ impl<C, P, CAW> AuraVerifier<C, P, CAW> where
&block_id,
block,
inherent_data,
).map_err(Error::Client)?;
).map_err(|e| Error::Client(e.into()))?;
if !inherent_res.ok() {
inherent_res
@@ -578,7 +578,7 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
sc_client_api::backend::AuxStore +
ProvideCache<B> +
BlockOf,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B, Error = sp_blockchain::Error>,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B>,
DigestItemFor<B>: CompatibleDigestItem<P>,
P: Pair + Send + Sync + 'static,
P::Public: Send + Sync + Hash + Eq + Clone + Decode + Encode + Debug + 'static,
@@ -624,7 +624,7 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
// skip the inherents verification if the runtime API is old.
if self.client
.runtime_api()
.has_api_with::<dyn BlockBuilderApi<B, Error = ()>, _>(
.has_api_with::<dyn BlockBuilderApi<B>, _>(
&BlockId::Hash(parent_hash),
|v| v >= 2,
)
@@ -842,7 +842,7 @@ pub fn import_queue<B, I, C, P, S, CAW>(
can_author_with: CAW,
) -> Result<DefaultImportQueue<B, C>, sp_consensus::Error> where
B: BlockT,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B, Error = sp_blockchain::Error>,
C::Api: BlockBuilderApi<B> + AuraApi<B, AuthorityId<P>> + ApiExt<B>,
C: 'static + ProvideRuntimeApi<B> + BlockOf + ProvideCache<B> + Send + Sync + AuxStore + HeaderBackend<B>,
I: BlockImport<B, Error=ConsensusError, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
DigestItemFor<B>: CompatibleDigestItem<P>,
+12 -11
View File
@@ -273,6 +273,8 @@ pub enum Error<B: BlockT> {
CheckInherents(String),
/// Client error
Client(sp_blockchain::Error),
/// Runtime Api error.
RuntimeApi(sp_api::ApiError),
/// Runtime error
Runtime(sp_inherents::Error),
/// Fork tree error
@@ -310,14 +312,14 @@ impl Config {
/// Either fetch the slot duration from disk or compute it from the genesis
/// state.
pub fn get_or_compute<B: BlockT, C>(client: &C) -> ClientResult<Self> where
C: AuxStore + ProvideRuntimeApi<B>, C::Api: BabeApi<B, Error = sp_blockchain::Error>,
C: AuxStore + ProvideRuntimeApi<B>, C::Api: BabeApi<B>,
{
trace!(target: "babe", "Getting slot duration");
match sc_consensus_slots::SlotDuration::get_or_compute(client, |a, b| {
let has_api_v1 = a.has_api_with::<dyn BabeApi<B, Error = sp_blockchain::Error>, _>(
let has_api_v1 = a.has_api_with::<dyn BabeApi<B>, _>(
&b, |v| v == 1,
)?;
let has_api_v2 = a.has_api_with::<dyn BabeApi<B, Error = sp_blockchain::Error>, _>(
let has_api_v2 = a.has_api_with::<dyn BabeApi<B>, _>(
&b, |v| v == 2,
)?;
@@ -326,7 +328,7 @@ impl Config {
Ok(a.configuration_before_version_2(b)?.into())
}
} else if has_api_v2 {
a.configuration(b)
a.configuration(b).map_err(Into::into)
} else {
Err(sp_blockchain::Error::VersionInvalid(
"Unsupported or invalid BabeApi version".to_string()
@@ -846,8 +848,7 @@ impl<Block, Client, SelectChain, CAW> BabeVerifier<Block, Client, SelectChain, C
where
Block: BlockT,
Client: AuxStore + HeaderBackend<Block> + HeaderMetadata<Block> + ProvideRuntimeApi<Block>,
Client::Api: BlockBuilderApi<Block, Error = sp_blockchain::Error>
+ BabeApi<Block, Error = sp_blockchain::Error>,
Client::Api: BlockBuilderApi<Block> + BabeApi<Block>,
SelectChain: sp_consensus::SelectChain<Block>,
CAW: CanAuthorWith<Block>,
{
@@ -871,7 +872,7 @@ where
&block_id,
block,
inherent_data,
).map_err(Error::Client)?;
).map_err(Error::RuntimeApi)?;
if !inherent_res.ok() {
inherent_res
@@ -934,7 +935,7 @@ where
self.client
.runtime_api()
.generate_key_ownership_proof(block_id, slot, equivocation_proof.offender.clone())
.map_err(Error::Client)
.map_err(Error::RuntimeApi)
};
let parent_id = BlockId::Hash(*header.parent_hash());
@@ -957,7 +958,7 @@ where
equivocation_proof,
key_owner_proof,
)
.map_err(Error::Client)?;
.map_err(Error::RuntimeApi)?;
info!(target: "babe", "Submitted equivocation report for author {:?}", author);
@@ -971,7 +972,7 @@ where
Block: BlockT,
Client: HeaderMetadata<Block, Error = sp_blockchain::Error> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + AuxStore + ProvideCache<Block>,
Client::Api: BlockBuilderApi<Block, Error = sp_blockchain::Error> + BabeApi<Block, Error = sp_blockchain::Error>,
Client::Api: BlockBuilderApi<Block> + BabeApi<Block>,
SelectChain: sp_consensus::SelectChain<Block>,
CAW: CanAuthorWith<Block> + Send + Sync,
{
@@ -1498,7 +1499,7 @@ pub fn import_queue<Block: BlockT, Client, SelectChain, Inner, CAW>(
+ Send + Sync + 'static,
Client: ProvideRuntimeApi<Block> + ProvideCache<Block> + Send + Sync + AuxStore + 'static,
Client: HeaderBackend<Block> + HeaderMetadata<Block, Error = sp_blockchain::Error>,
Client::Api: BlockBuilderApi<Block> + BabeApi<Block> + ApiExt<Block, Error = sp_blockchain::Error>,
Client::Api: BlockBuilderApi<Block> + BabeApi<Block> + ApiExt<Block>,
SelectChain: sp_consensus::SelectChain<Block> + 'static,
CAW: CanAuthorWith<Block> + Send + Sync + 'static,
{
@@ -73,7 +73,7 @@ impl<B, C> BabeConsensusDataProvider<B, C>
where
B: BlockT,
C: AuxStore + HeaderBackend<B> + ProvideRuntimeApi<B> + HeaderMetadata<B, Error = sp_blockchain::Error>,
C::Api: BabeApi<B, Error = sp_blockchain::Error>,
C::Api: BabeApi<B>,
{
pub fn new(
client: Arc<C>,
@@ -131,7 +131,7 @@ impl<B, C> ConsensusDataProvider<B> for BabeConsensusDataProvider<B, C>
where
B: BlockT,
C: AuxStore + HeaderBackend<B> + HeaderMetadata<B, Error = sp_blockchain::Error> + ProvideRuntimeApi<B>,
C::Api: BabeApi<B, Error = sp_blockchain::Error>,
C::Api: BabeApi<B>,
{
type Transaction = TransactionFor<C, B>;
@@ -259,7 +259,7 @@ impl SlotTimestampProvider {
where
B: BlockT,
C: AuxStore + HeaderBackend<B> + ProvideRuntimeApi<B>,
C::Api: BabeApi<B, Error = sp_blockchain::Error>,
C::Api: BabeApi<B>,
{
let slot_duration = Config::get_or_compute(&*client)?.slot_duration;
let info = client.info();
+3 -3
View File
@@ -232,7 +232,7 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync,
I::Error: Into<ConsensusError>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
C::Api: BlockBuilderApi<B>,
Algorithm: PowAlgorithm<B>,
CAW: CanAuthorWith<B>,
{
@@ -284,7 +284,7 @@ impl<B, I, C, S, Algorithm, CAW> PowBlockImport<B, I, C, S, Algorithm, CAW> wher
&block_id,
block,
inherent_data,
).map_err(Error::Client)?;
).map_err(|e| Error::Client(e.into()))?;
if !inherent_res.ok() {
inherent_res
@@ -314,7 +314,7 @@ impl<B, I, C, S, Algorithm, CAW> BlockImport<B> for PowBlockImport<B, I, C, S, A
I::Error: Into<ConsensusError>,
S: SelectChain<B>,
C: ProvideRuntimeApi<B> + Send + Sync + HeaderBackend<B> + AuxStore + ProvideCache<B> + BlockOf,
C::Api: BlockBuilderApi<B, Error = sp_blockchain::Error>,
C::Api: BlockBuilderApi<B>,
Algorithm: PowAlgorithm<B>,
Algorithm::Difficulty: 'static,
CAW: CanAuthorWith<B>,
@@ -37,8 +37,8 @@ pub enum Error {
#[error(transparent)]
Wasmi(#[from] wasmi::Error),
#[error("API Error: {0}")]
ApiError(String),
#[error("Error calling api function: {0}")]
ApiError(Box<dyn std::error::Error + Send + Sync>),
#[error("Method not found: '{0}'")]
MethodNotFound(String),
@@ -96,16 +96,16 @@ pub enum Error {
#[error(transparent)]
RuntimeConstruction(#[from] WasmError),
#[error("Shared memory is not supported")]
SharedMemUnsupported,
#[error("Imported globals are not supported yet")]
ImportedGlobalsUnsupported,
#[error("initializer expression can have only up to 2 expressions in wasm 1.0")]
InitializerHasTooManyExpressions,
#[error("Invalid initializer expression provided {0}")]
InvalidInitializerExpression(String),
}
@@ -456,7 +456,7 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeExecutor<D> {
fn call<
R: Decode + Encode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
NC: FnOnce() -> result::Result<R, Box<dyn std::error::Error + Send + Sync>> + UnwindSafe,
>(
&self,
ext: &mut dyn Externalities,
@@ -514,7 +514,7 @@ impl<D: NativeExecutionDispatch + 'static> CodeExecutor for NativeExecutor<D> {
let res = with_externalities_safe(&mut **ext, move || (call)())
.and_then(|r| r
.map(NativeOrEncoded::Native)
.map_err(|s| Error::ApiError(s))
.map_err(Error::ApiError)
);
Ok(res)
@@ -414,7 +414,7 @@ mod tests {
authoring_version: 1,
spec_version: 1,
impl_version: 1,
apis: sp_api::create_apis_vec!([(Core::<Block, Error = ()>::ID, 1)]),
apis: sp_api::create_apis_vec!([(Core::<Block>::ID, 1)]),
};
let version = decode_version(&old_runtime_version.encode()).unwrap();
@@ -429,7 +429,7 @@ mod tests {
authoring_version: 1,
spec_version: 1,
impl_version: 1,
apis: sp_api::create_apis_vec!([(Core::<Block, Error = ()>::ID, 3)]),
apis: sp_api::create_apis_vec!([(Core::<Block>::ID, 3)]),
};
decode_version(&old_runtime_version.encode()).unwrap_err();
@@ -443,7 +443,7 @@ mod tests {
authoring_version: 1,
spec_version: 1,
impl_version: 1,
apis: sp_api::create_apis_vec!([(Core::<Block, Error = ()>::ID, 3)]),
apis: sp_api::create_apis_vec!([(Core::<Block>::ID, 3)]),
transaction_version: 3,
};
@@ -59,4 +59,3 @@ sp-state-machine = { version = "0.9.0", path = "../../primitives/state-machine"
sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" }
tokio = { version = "0.2", features = ["rt-core"] }
tempfile = "3.1.0"
sp-api = { version = "3.0.0", path = "../../primitives/api" }
@@ -480,7 +480,7 @@ where
Block: BlockT,
BE: Backend<Block>,
C: crate::ClientForGrandpa<Block, BE>,
C::Api: GrandpaApi<Block, Error = sp_blockchain::Error>,
C::Api: GrandpaApi<Block>,
N: NetworkT<Block>,
SC: SelectChain<Block> + 'static,
{
@@ -549,7 +549,7 @@ where
authority_set.set_id,
equivocation.offender().clone(),
)
.map_err(Error::Client)?
.map_err(Error::RuntimeApi)?
{
Some(proof) => proof,
None => {
@@ -571,7 +571,7 @@ where
equivocation_proof,
key_owner_proof,
)
.map_err(Error::Client)?;
.map_err(Error::RuntimeApi)?;
Ok(())
}
@@ -726,7 +726,7 @@ where
Block: 'static,
B: Backend<Block>,
C: crate::ClientForGrandpa<Block, B> + 'static,
C::Api: GrandpaApi<Block, Error = sp_blockchain::Error>,
C::Api: GrandpaApi<Block>,
N: NetworkT<Block> + 'static + Send + Sync,
SC: SelectChain<Block> + 'static,
VR: VotingRule<Block, C>,
@@ -663,6 +663,7 @@ where
Error::Safety(error) => ConsensusError::ClientImport(error),
Error::Signing(error) => ConsensusError::ClientImport(error),
Error::Timer(error) => ConsensusError::ClientImport(error.to_string()),
Error::RuntimeApi(error) => ConsensusError::ClientImport(error.to_string()),
});
},
Ok(_) => {
+5 -3
View File
@@ -295,6 +295,8 @@ pub enum Error {
Safety(String),
/// A timer failed to fire.
Timer(io::Error),
/// A runtime api request failed.
RuntimeApi(sp_api::ApiError),
}
impl From<GrandpaError> for Error {
@@ -698,7 +700,7 @@ where
NumberFor<Block>: BlockNumberOps,
DigestFor<Block>: Encode,
C: ClientForGrandpa<Block, BE> + 'static,
C::Api: GrandpaApi<Block, Error = sp_blockchain::Error>,
C::Api: GrandpaApi<Block>,
{
let GrandpaParams {
mut config,
@@ -824,7 +826,7 @@ where
Block: BlockT,
B: Backend<Block> + 'static,
C: ClientForGrandpa<Block, B> + 'static,
C::Api: GrandpaApi<Block, Error = sp_blockchain::Error>,
C::Api: GrandpaApi<Block>,
N: NetworkT<Block> + Sync,
NumberFor<Block>: BlockNumberOps,
SC: SelectChain<Block> + 'static,
@@ -1042,7 +1044,7 @@ where
NumberFor<Block>: BlockNumberOps,
SC: SelectChain<Block> + 'static,
C: ClientForGrandpa<Block, B> + 'static,
C::Api: GrandpaApi<Block, Error = sp_blockchain::Error>,
C::Api: GrandpaApi<Block>,
VR: VotingRule<Block, C> + Clone + 'static,
{
type Output = Result<(), Error>;
@@ -174,8 +174,6 @@ impl ProvideRuntimeApi<Block> for TestApi {
sp_api::mock_impl_runtime_apis! {
impl GrandpaApi<Block> for RuntimeApi {
type Error = sp_blockchain::Error;
fn grandpa_authorities(&self) -> AuthorityList {
self.inner.genesis_authorities.clone()
}
+1 -1
View File
@@ -104,7 +104,7 @@ impl<Block, B, Local> CallExecutor<Block> for
Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
NC: FnOnce() -> result::Result<R, sp_api::ApiError> + UnwindSafe,
>(
&self,
initialize_block_fn: IB,
+2 -2
View File
@@ -132,10 +132,10 @@ impl<Client, Storage, Block> OffchainWorkers<
) -> impl Future<Output = ()> {
let runtime = self.client.runtime_api();
let at = BlockId::hash(header.hash());
let has_api_v1 = runtime.has_api_with::<dyn OffchainWorkerApi<Block, Error = ()>, _>(
let has_api_v1 = runtime.has_api_with::<dyn OffchainWorkerApi<Block>, _>(
&at, |v| v == 1
);
let has_api_v2 = runtime.has_api_with::<dyn OffchainWorkerApi<Block, Error = ()>, _>(
let has_api_v2 = runtime.has_api_with::<dyn OffchainWorkerApi<Block>, _>(
&at, |v| v == 2
);
let version = match (has_api_v1, has_api_v2) {
+3 -6
View File
@@ -24,12 +24,9 @@ mod tests;
use std::{sync::Arc, convert::TryInto};
use log::warn;
use sp_blockchain::{Error as ClientError, HeaderBackend};
use sp_blockchain::HeaderBackend;
use rpc::futures::{
Sink, Future,
future::result,
};
use rpc::futures::{Sink, Future, future::result};
use futures::{StreamExt as _, compat::Compat};
use futures::future::{ready, FutureExt, TryFutureExt};
use sc_rpc_api::DenyUnsafe;
@@ -93,7 +90,7 @@ impl<P, Client> AuthorApi<TxHash<P>, BlockHash<P>> for Author<P, Client>
where
P: TransactionPool + Sync + Send + 'static,
Client: HeaderBackend<P::Block> + ProvideRuntimeApi<P::Block> + Send + Sync + 'static,
Client::Api: SessionKeys<P::Block, Error = ClientError>,
Client::Api: SessionKeys<P::Block>,
{
type Metadata = crate::Metadata;
+2 -3
View File
@@ -178,9 +178,8 @@ pub fn new_full<BE, Block: BlockT, Client>(
BE: Backend<Block> + 'static,
Client: ExecutorProvider<Block> + StorageProvider<Block, BE> + ProofProvider<Block> + HeaderBackend<Block>
+ HeaderMetadata<Block, Error = sp_blockchain::Error> + BlockchainEvents<Block>
+ CallApiAt<Block, Error = sp_blockchain::Error>
+ ProvideRuntimeApi<Block> + Send + Sync + 'static,
Client::Api: Metadata<Block, Error = sp_blockchain::Error>,
+ CallApiAt<Block> + ProvideRuntimeApi<Block> + Send + Sync + 'static,
Client::Api: Metadata<Block>,
{
let child_backend = Box::new(
self::state_full::FullState::new(client.clone(), subscriptions.clone())
+16 -10
View File
@@ -223,9 +223,9 @@ impl<BE, Block, Client> StateBackend<Block, Client> for FullState<BE, Block, Cli
BE: Backend<Block> + 'static,
Client: ExecutorProvider<Block> + StorageProvider<Block, BE> + ProofProvider<Block> + HeaderBackend<Block>
+ HeaderMetadata<Block, Error = sp_blockchain::Error> + BlockchainEvents<Block>
+ CallApiAt<Block, Error = sp_blockchain::Error> + ProvideRuntimeApi<Block>
+ CallApiAt<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
Client::Api: Metadata<Block, Error = sp_blockchain::Error>,
Client::Api: Metadata<Block>,
{
fn call(
&self,
@@ -344,17 +344,23 @@ impl<BE, Block, Client> StateBackend<Block, Client> for FullState<BE, Block, Cli
fn metadata(&self, block: Option<Block::Hash>) -> FutureResult<Bytes> {
Box::new(result(
self.block_or_best(block)
.map_err(client_err)
.and_then(|block|
self.client.runtime_api().metadata(&BlockId::Hash(block)).map(Into::into)
)
.map_err(client_err)))
self.client.runtime_api().metadata(&BlockId::Hash(block))
.map(Into::into)
.map_err(|e| Error::Client(Box::new(e))))
))
}
fn runtime_version(&self, block: Option<Block::Hash>) -> FutureResult<RuntimeVersion> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.runtime_version_at(&BlockId::Hash(block)))
.map_err(client_err)))
.map_err(client_err)
.and_then(|block|
self.client.runtime_version_at(&BlockId::Hash(block))
.map_err(|e| Error::Client(Box::new(e)))
)
))
}
fn query_storage(
@@ -432,7 +438,7 @@ impl<BE, Block, Client> StateBackend<Block, Client> for FullState<BE, Block, Cli
let info = client.info();
let version = client
.runtime_version_at(&BlockId::hash(info.best_hash))
.map_err(client_err)
.map_err(|e| Error::Client(Box::new(e)))
.map_err(Into::into);
if previous_version != version {
previous_version = version.clone();
@@ -528,9 +534,9 @@ impl<BE, Block, Client> ChildStateBackend<Block, Client> for FullState<BE, Block
BE: Backend<Block> + 'static,
Client: ExecutorProvider<Block> + StorageProvider<Block, BE> + HeaderBackend<Block>
+ HeaderMetadata<Block, Error = sp_blockchain::Error> + BlockchainEvents<Block>
+ CallApiAt<Block, Error = sp_blockchain::Error> + ProvideRuntimeApi<Block>
+ CallApiAt<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
Client::Api: Metadata<Block, Error = sp_blockchain::Error>,
Client::Api: Metadata<Block>,
{
fn storage_keys(
&self,
+4 -5
View File
@@ -542,14 +542,13 @@ pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
TCl: ProvideRuntimeApi<TBl> + HeaderMetadata<TBl, Error=sp_blockchain::Error> + Chain<TBl> +
BlockBackend<TBl> + BlockIdTo<TBl, Error=sp_blockchain::Error> + ProofProvider<TBl> +
HeaderBackend<TBl> + BlockchainEvents<TBl> + ExecutorProvider<TBl> + UsageProvider<TBl> +
StorageProvider<TBl, TBackend> + CallApiAt<TBl, Error=sp_blockchain::Error> +
StorageProvider<TBl, TBackend> + CallApiAt<TBl> +
Send + 'static,
<TCl as ProvideRuntimeApi<TBl>>::Api:
sp_api::Metadata<TBl> +
sc_offchain::OffchainWorkerApi<TBl> +
sp_transaction_pool::runtime_api::TaggedTransactionQueue<TBl> +
sp_session::SessionKeys<TBl> +
sp_api::ApiErrorExt<Error = sp_blockchain::Error> +
sp_api::ApiExt<TBl, StateBackend = TBackend::State>,
TBl: BlockT,
TBackend: 'static + sc_client_api::backend::Backend<TBl> + Send,
@@ -578,7 +577,7 @@ pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
client.clone(),
&BlockId::Hash(chain_info.best_hash),
config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(),
)?;
).map_err(|e| Error::Application(Box::new(e)))?;
let telemetry_connection_notifier = init_telemetry(
&mut config,
@@ -729,14 +728,14 @@ fn gen_handler<TBl, TBackend, TExPool, TRpc, TCl>(
TBl: BlockT,
TCl: ProvideRuntimeApi<TBl> + BlockchainEvents<TBl> + HeaderBackend<TBl> +
HeaderMetadata<TBl, Error=sp_blockchain::Error> + ExecutorProvider<TBl> +
CallApiAt<TBl, Error=sp_blockchain::Error> + ProofProvider<TBl> +
CallApiAt<TBl> + ProofProvider<TBl> +
StorageProvider<TBl, TBackend> + BlockBackend<TBl> + Send + Sync + 'static,
TExPool: MaintainedTransactionPool<Block=TBl, Hash = <TBl as BlockT>::Hash> + 'static,
TBackend: sc_client_api::backend::Backend<TBl> + 'static,
TRpc: sc_rpc::RpcExtension<sc_rpc::Metadata>,
<TCl as ProvideRuntimeApi<TBl>>::Api:
sp_session::SessionKeys<TBl> +
sp_api::Metadata<TBl, Error = sp_blockchain::Error>,
sp_api::Metadata<TBl>,
{
use sc_rpc::{chain, state, author, system, offchain};
@@ -161,7 +161,7 @@ where
Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
NC: FnOnce() -> result::Result<R, sp_api::ApiError> + UnwindSafe,
>(
&self,
initialize_block_fn: IB,
@@ -226,7 +226,10 @@ where
);
// TODO: https://github.com/paritytech/substrate/issues/4455
// .with_storage_transaction_cache(storage_transaction_cache.as_mut().map(|c| &mut **c))
state_machine.execute_using_consensus_failure_handler(execution_manager, native_call)
state_machine.execute_using_consensus_failure_handler(
execution_manager,
native_call.map(|n| || (n)().map_err(|e| Box::new(e) as Box<_>)),
)
},
None => {
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state);
@@ -245,7 +248,10 @@ where
&runtime_code,
self.spawn_handle.clone(),
).with_storage_transaction_cache(storage_transaction_cache.as_mut().map(|c| &mut **c));
state_machine.execute_using_consensus_failure_handler(execution_manager, native_call)
state_machine.execute_using_consensus_failure_handler(
execution_manager,
native_call.map(|n| || (n)().map_err(|e| Box::new(e) as Box<_>)),
)
}
}.map_err(Into::into)
}
+20 -16
View File
@@ -604,7 +604,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> sp_blockchain::Result<ImportResult> where
Self: ProvideRuntimeApi<Block>,
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error> +
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block> +
ApiExt<Block, StateBackend = B::State>,
{
let BlockImportParams {
@@ -696,7 +696,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
import_existing: bool,
) -> sp_blockchain::Result<ImportResult> where
Self: ProvideRuntimeApi<Block>,
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error> +
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block> +
ApiExt<Block, StateBackend = B::State>,
{
let parent_hash = import_headers.post().parent_hash().clone();
@@ -838,7 +838,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
) -> sp_blockchain::Result<Option<ImportResult>>
where
Self: ProvideRuntimeApi<Block>,
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error> +
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block> +
ApiExt<Block, StateBackend = B::State>,
{
let parent_hash = import_block.header.parent_hash();
@@ -1272,7 +1272,7 @@ impl<B, E, Block, RA> BlockBuilderProvider<B, Block, Self> for Client<B, E, Bloc
Block: BlockT,
Self: ChainHeaderBackend<Block> + ProvideRuntimeApi<Block>,
<Self as ProvideRuntimeApi<Block>>::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = Error>,
+ BlockBuilderApi<Block>,
{
fn new_block_at<R: Into<RecordProof>>(
&self,
@@ -1628,18 +1628,17 @@ impl<B, E, Block, RA> CallApiAt<Block> for Client<B, E, Block, RA> where
E: CallExecutor<Block, Backend = B> + Send + Sync,
Block: BlockT,
{
type Error = Error;
type StateBackend = B::State;
fn call_api_at<
'a,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> result::Result<R, String> + UnwindSafe,
C: CoreApi<Block, Error = Error>,
NC: FnOnce() -> result::Result<R, sp_api::ApiError> + UnwindSafe,
C: CoreApi<Block>,
>(
&self,
params: CallApiAtParams<'a, Block, C, NC, B::State>,
) -> sp_blockchain::Result<NativeOrEncoded<R>> {
) -> Result<NativeOrEncoded<R>, sp_api::ApiError> {
let core_api = params.core_api;
let at = params.at;
@@ -1649,7 +1648,9 @@ impl<B, E, Block, RA> CallApiAt<Block> for Client<B, E, Block, RA> where
);
self.executor.contextual_call::<_, fn(_,_) -> _,_,_>(
|| core_api.initialize_block(at, &self.prepare_environment_block(at)?),
|| core_api
.initialize_block(at, &self.prepare_environment_block(at)?)
.map_err(Error::RuntimeApiError),
at,
params.function,
&params.arguments,
@@ -1660,11 +1661,14 @@ impl<B, E, Block, RA> CallApiAt<Block> for Client<B, E, Block, RA> where
params.native_call,
params.recorder,
Some(extensions),
)
).map_err(Into::into)
}
fn runtime_version_at(&self, at: &BlockId<Block>) -> sp_blockchain::Result<RuntimeVersion> {
self.runtime_version_at(at)
fn runtime_version_at(
&self,
at: &BlockId<Block>,
) -> Result<RuntimeVersion, sp_api::ApiError> {
self.runtime_version_at(at).map_err(Into::into)
}
}
@@ -1676,7 +1680,7 @@ impl<B, E, Block, RA> sp_consensus::BlockImport<Block> for &Client<B, E, Block,
E: CallExecutor<Block> + Send + Sync,
Block: BlockT,
Client<B, E, Block, RA>: ProvideRuntimeApi<Block>,
<Client<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error> +
<Client<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api: CoreApi<Block> +
ApiExt<Block, StateBackend = B::State>,
{
type Error = ConsensusError;
@@ -1776,7 +1780,7 @@ impl<B, E, Block, RA> sp_consensus::BlockImport<Block> for Client<B, E, Block, R
E: CallExecutor<Block> + Send + Sync,
Block: BlockT,
Self: ProvideRuntimeApi<Block>,
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error> +
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block> +
ApiExt<Block, StateBackend = B::State>,
{
type Error = ConsensusError;
@@ -1935,7 +1939,7 @@ impl<B, E, Block, RA> backend::AuxStore for Client<B, E, Block, RA>
E: CallExecutor<Block>,
Block: BlockT,
Self: ProvideRuntimeApi<Block>,
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error>,
<Self as ProvideRuntimeApi<Block>>::Api: CoreApi<Block>,
{
/// Insert auxiliary data into key-value store.
fn insert_aux<
@@ -1965,7 +1969,7 @@ impl<B, E, Block, RA> backend::AuxStore for &Client<B, E, Block, RA>
E: CallExecutor<Block>,
Block: BlockT,
Client<B, E, Block, RA>: ProvideRuntimeApi<Block>,
<Client<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api: CoreApi<Block, Error = Error>,
<Client<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api: CoreApi<Block>,
{
fn insert_aux<
'a,
+3 -3
View File
@@ -33,13 +33,13 @@ pub type Result<T> = std::result::Result<T, Error>;
pub enum Error {
#[error(transparent)]
Client(#[from] sp_blockchain::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Consensus(#[from] sp_consensus::Error),
#[error(transparent)]
Network(#[from] sc_network::error::Error),
@@ -215,7 +215,7 @@ impl CallExecutor<Block> for DummyCallExecutor {
Result<NativeOrEncoded<R>, Self::Error>
) -> Result<NativeOrEncoded<R>, Self::Error>,
R: Encode + Decode + PartialEq,
NC: FnOnce() -> Result<R, String> + UnwindSafe,
NC: FnOnce() -> Result<R, sp_api::ApiError> + UnwindSafe,
>(
&self,
_initialize_block_fn: IB,
@@ -1327,7 +1327,9 @@ fn doesnt_import_blocks_that_revert_finality() {
let import_err = client.import(BlockOrigin::Own, b3).err().unwrap();
let expected_err = ConsensusError::ClientImport(
sp_blockchain::Error::NotInFinalizedChain.to_string()
sp_blockchain::Error::RuntimeApiError(
sp_api::ApiError::Application(Box::new(sp_blockchain::Error::NotInFinalizedChain))
).to_string()
);
assert_eq!(
+1 -4
View File
@@ -84,7 +84,6 @@ where
Client: ProvideRuntimeApi<Block> + BlockBackend<Block> + BlockIdTo<Block>,
Client: Send + Sync + 'static,
Client::Api: TaggedTransactionQueue<Block>,
sp_api::ApiErrorFor<Client, Block>: Send + std::fmt::Display,
{
type Block = Block;
type Error = error::Error;
@@ -166,14 +165,13 @@ where
Client: ProvideRuntimeApi<Block> + BlockBackend<Block> + BlockIdTo<Block>,
Client: Send + Sync + 'static,
Client::Api: TaggedTransactionQueue<Block>,
sp_api::ApiErrorFor<Client, Block>: Send + std::fmt::Display,
{
sp_tracing::within_span!(sp_tracing::Level::TRACE, "validate_transaction";
{
let runtime_api = client.runtime_api();
let has_v2 = sp_tracing::within_span! { sp_tracing::Level::TRACE, "check_version";
runtime_api
.has_api_with::<dyn TaggedTransactionQueue<Block, Error=()>, _>(&at, |v| v >= 2)
.has_api_with::<dyn TaggedTransactionQueue<Block>, _>(&at, |v| v >= 2)
.unwrap_or_default()
};
@@ -198,7 +196,6 @@ where
Client: ProvideRuntimeApi<Block> + BlockBackend<Block> + BlockIdTo<Block>,
Client: Send + Sync + 'static,
Client::Api: TaggedTransactionQueue<Block>,
sp_api::ApiErrorFor<Client, Block>: Send + std::fmt::Display,
{
/// Validates a transaction by calling into the runtime, same as
/// `validate_transaction` but blocks the current thread when performing
@@ -360,7 +360,6 @@ where
+ sp_runtime::traits::BlockIdTo<Block>,
Client: sc_client_api::ExecutorProvider<Block> + Send + Sync + 'static,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
sp_api::ApiErrorFor<Client, Block>: Send + std::fmt::Display,
{
/// Create new basic transaction pool for a full node with the provided api.
pub fn new_full(
@@ -391,7 +390,6 @@ where
+ sp_runtime::traits::BlockIdTo<Block>,
Client: Send + Sync + 'static,
Client::Api: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>,
sp_api::ApiErrorFor<Client, Block>: Send + std::fmt::Display,
{
type Block = Block;
type Hash = sc_transaction_graph::ExtrinsicHash<FullChainApi<Client, Block>>;