mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 16:21:06 +00:00
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:
committed by
Benjamin Kampmann
parent
e26d1a0b3e
commit
2ecffa1cd0
@@ -60,7 +60,7 @@ use sr_primitives::{
|
||||
use timestamp::OnTimestampSet;
|
||||
#[cfg(feature = "std")]
|
||||
use timestamp::TimestampInherentData;
|
||||
use inherents::{RuntimeString, InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
||||
use inherents::{InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::{InherentDataProviders, ProvideInherentData};
|
||||
use substrate_consensus_aura_primitives::{AURA_ENGINE_ID, ConsensusLog, AuthorityIndex};
|
||||
@@ -77,13 +77,13 @@ pub type InherentType = u64;
|
||||
/// Auxiliary trait to extract Aura inherent data.
|
||||
pub trait AuraInherentData {
|
||||
/// Get aura inherent data.
|
||||
fn aura_inherent_data(&self) -> result::Result<InherentType, RuntimeString>;
|
||||
fn aura_inherent_data(&self) -> result::Result<InherentType, inherents::Error>;
|
||||
/// Replace aura inherent data.
|
||||
fn aura_replace_inherent_data(&mut self, new: InherentType);
|
||||
}
|
||||
|
||||
impl AuraInherentData for InherentData {
|
||||
fn aura_inherent_data(&self) -> result::Result<InherentType, RuntimeString> {
|
||||
fn aura_inherent_data(&self) -> result::Result<InherentType, inherents::Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Aura inherent data not found".into()))
|
||||
}
|
||||
@@ -113,7 +113,7 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
fn on_register(
|
||||
&self,
|
||||
providers: &InherentDataProviders,
|
||||
) -> result::Result<(), RuntimeString> {
|
||||
) -> result::Result<(), inherents::Error> {
|
||||
if !providers.has_provider(×tamp::INHERENT_IDENTIFIER) {
|
||||
// Add the timestamp inherent data provider, as we require it.
|
||||
providers.register_provider(timestamp::InherentDataProvider)
|
||||
@@ -129,14 +129,14 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> result::Result<(), RuntimeString> {
|
||||
) -> result::Result<(), inherents::Error> {
|
||||
let timestamp = inherent_data.timestamp_inherent_data()?;
|
||||
let slot_num = timestamp / self.slot_duration;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot_num)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
RuntimeString::decode(&mut &error[..]).map(Into::into).ok()
|
||||
inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ impl<T: Trait> OnTimestampSet<T::Moment> for Module<T> {
|
||||
|
||||
impl<T: Trait> ProvideInherent for Module<T> {
|
||||
type Call = timestamp::Call<T>;
|
||||
type Error = MakeFatalError<RuntimeString>;
|
||||
type Error = MakeFatalError<inherents::Error>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
|
||||
fn create_inherent(_: &InherentData) -> Option<Self::Call> {
|
||||
@@ -300,7 +300,7 @@ impl<T: Trait> ProvideInherent for Module<T> {
|
||||
if timestamp_based_slot == seal_slot {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(RuntimeString::from("timestamp set in block doesn't match slot in seal").into())
|
||||
Err(inherents::Error::from("timestamp set in block doesn't match slot in seal").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,10 +29,7 @@ use codec::{Encode, Decode};
|
||||
use system::ensure_none;
|
||||
use sr_primitives::traits::{Header as HeaderT, One, Zero};
|
||||
use sr_primitives::weights::SimpleDispatchInfo;
|
||||
use inherents::{
|
||||
RuntimeString, InherentIdentifier, ProvideInherent,
|
||||
InherentData, MakeFatalError,
|
||||
};
|
||||
use inherents::{InherentIdentifier, ProvideInherent, InherentData, MakeFatalError};
|
||||
|
||||
/// The identifier for the `uncles` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"uncles00";
|
||||
@@ -40,11 +37,11 @@ pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"uncles00";
|
||||
/// Auxiliary trait to extract uncles inherent data.
|
||||
pub trait UnclesInherentData<H: Decode> {
|
||||
/// Get uncles.
|
||||
fn uncles(&self) -> Result<Vec<H>, RuntimeString>;
|
||||
fn uncles(&self) -> Result<Vec<H>, inherents::Error>;
|
||||
}
|
||||
|
||||
impl<H: Decode> UnclesInherentData<H> for InherentData {
|
||||
fn uncles(&self) -> Result<Vec<H>, RuntimeString> {
|
||||
fn uncles(&self) -> Result<Vec<H>, inherents::Error> {
|
||||
Ok(self.get_data(&INHERENT_IDENTIFIER)?.unwrap_or_default())
|
||||
}
|
||||
}
|
||||
@@ -71,7 +68,7 @@ where F: Fn() -> Vec<H>
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), RuntimeString> {
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), inherents::Error> {
|
||||
let uncles = (self.inner)();
|
||||
if !uncles.is_empty() {
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &uncles)
|
||||
|
||||
@@ -34,7 +34,7 @@ use sr_staking_primitives::{
|
||||
#[cfg(feature = "std")]
|
||||
use timestamp::TimestampInherentData;
|
||||
use codec::{Encode, Decode};
|
||||
use inherents::{RuntimeString, InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
||||
use inherents::{InherentIdentifier, InherentData, ProvideInherent, MakeFatalError};
|
||||
#[cfg(feature = "std")]
|
||||
use inherents::{InherentDataProviders, ProvideInherentData};
|
||||
use babe_primitives::{
|
||||
@@ -57,13 +57,13 @@ pub type InherentType = u64;
|
||||
/// Auxiliary trait to extract BABE inherent data.
|
||||
pub trait BabeInherentData {
|
||||
/// Get BABE inherent data.
|
||||
fn babe_inherent_data(&self) -> result::Result<InherentType, RuntimeString>;
|
||||
fn babe_inherent_data(&self) -> result::Result<InherentType, inherents::Error>;
|
||||
/// Replace BABE inherent data.
|
||||
fn babe_replace_inherent_data(&mut self, new: InherentType);
|
||||
}
|
||||
|
||||
impl BabeInherentData for InherentData {
|
||||
fn babe_inherent_data(&self) -> result::Result<InherentType, RuntimeString> {
|
||||
fn babe_inherent_data(&self) -> result::Result<InherentType, inherents::Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "BABE inherent data not found".into()))
|
||||
}
|
||||
@@ -94,7 +94,7 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
fn on_register(
|
||||
&self,
|
||||
providers: &InherentDataProviders,
|
||||
) -> result::Result<(), RuntimeString> {
|
||||
) -> result::Result<(), inherents::Error> {
|
||||
if !providers.has_provider(×tamp::INHERENT_IDENTIFIER) {
|
||||
// Add the timestamp inherent data provider, as we require it.
|
||||
providers.register_provider(timestamp::InherentDataProvider)
|
||||
@@ -110,14 +110,14 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> result::Result<(), RuntimeString> {
|
||||
) -> result::Result<(), inherents::Error> {
|
||||
let timestamp = inherent_data.timestamp_inherent_data()?;
|
||||
let slot_number = timestamp / self.slot_duration;
|
||||
inherent_data.put_data(INHERENT_IDENTIFIER, &slot_number)
|
||||
}
|
||||
|
||||
fn error_to_string(&self, error: &[u8]) -> Option<String> {
|
||||
RuntimeString::decode(&mut &error[..]).map(Into::into).ok()
|
||||
inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -602,7 +602,7 @@ fn compute_randomness(
|
||||
|
||||
impl<T: Trait> ProvideInherent for Module<T> {
|
||||
type Call = timestamp::Call<T>;
|
||||
type Error = MakeFatalError<RuntimeString>;
|
||||
type Error = MakeFatalError<inherents::Error>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
|
||||
fn create_inherent(_: &InherentData) -> Option<Self::Call> {
|
||||
@@ -621,7 +621,7 @@ impl<T: Trait> ProvideInherent for Module<T> {
|
||||
if timestamp_based_slot == seal_slot {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(RuntimeString::from("timestamp set in block doesn't match slot in seal").into())
|
||||
Err(inherents::Error::from("timestamp set in block doesn't match slot in seal").into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
client = { package = "substrate-client", path = "../../../../core/client", default-features = false }
|
||||
sr-api = { path = "../../../../core/sr-api", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
|
||||
rstd = { package = "sr-std", path = "../../../../core/sr-std", default-features = false }
|
||||
serde = { version = "1.0.101", optional = true, features = ["derive"] }
|
||||
@@ -14,7 +14,7 @@ sr-primitives = { path = "../../../../core/sr-primitives", default-features = fa
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"client/std",
|
||||
"sr-api/std",
|
||||
"codec/std",
|
||||
"rstd/std",
|
||||
"serde",
|
||||
|
||||
@@ -59,7 +59,7 @@ pub enum GetStorageError {
|
||||
IsTombstone,
|
||||
}
|
||||
|
||||
client::decl_runtime_apis! {
|
||||
sr_api::decl_runtime_apis! {
|
||||
/// The API to interact with contracts without using executive.
|
||||
pub trait ContractsApi<AccountId, Balance> where
|
||||
AccountId: Codec,
|
||||
|
||||
@@ -18,10 +18,7 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use inherents::{
|
||||
RuntimeString, InherentIdentifier, ProvideInherent,
|
||||
InherentData, MakeFatalError,
|
||||
};
|
||||
use inherents::{InherentIdentifier, ProvideInherent, InherentData, MakeFatalError};
|
||||
use sr_primitives::traits::{One, Zero, SaturatedConversion};
|
||||
use rstd::{prelude::*, result, cmp, vec};
|
||||
use codec::Decode;
|
||||
@@ -38,11 +35,11 @@ pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"finalnum";
|
||||
/// Auxiliary trait to extract finalized inherent data.
|
||||
pub trait FinalizedInherentData<N: Decode> {
|
||||
/// Get finalized inherent data.
|
||||
fn finalized_number(&self) -> Result<N, RuntimeString>;
|
||||
fn finalized_number(&self) -> Result<N, inherents::Error>;
|
||||
}
|
||||
|
||||
impl<N: Decode> FinalizedInherentData<N> for InherentData {
|
||||
fn finalized_number(&self) -> Result<N, RuntimeString> {
|
||||
fn finalized_number(&self) -> Result<N, inherents::Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Finalized number inherent data not found".into()))
|
||||
}
|
||||
@@ -64,13 +61,16 @@ impl<F, N> InherentDataProvider<F, N> {
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<F, N: Encode> inherents::ProvideInherentData for InherentDataProvider<F, N>
|
||||
where F: Fn() -> Result<N, RuntimeString>
|
||||
where F: Fn() -> Result<N, inherents::Error>
|
||||
{
|
||||
fn inherent_identifier(&self) -> &'static InherentIdentifier {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), RuntimeString> {
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> Result<(), inherents::Error> {
|
||||
(self.inner)()
|
||||
.and_then(|n| inherent_data.put_data(INHERENT_IDENTIFIER, &n))
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ support = { package = "srml-support", path = "../support", default-features = fa
|
||||
system = { package = "srml-system", path = "../system", default-features = false }
|
||||
timestamp = { package = "srml-timestamp", path = "../timestamp", default-features = false }
|
||||
substrate-trie = { path = "../../core/trie", default-features = false, optional = true }
|
||||
runtime-io ={ package = "sr-io", path = "../../core/sr-io", default-features = false }
|
||||
runtime-io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
|
||||
impl-trait-for-tuples = "0.1.3"
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -125,10 +125,7 @@ use sr_primitives::{KeyTypeId, Perbill, RuntimeAppPublic, BoundToRuntimeAppPubli
|
||||
use sr_primitives::weights::SimpleDispatchInfo;
|
||||
use sr_primitives::traits::{Convert, Zero, Member, OpaqueKeys};
|
||||
use sr_staking_primitives::SessionIndex;
|
||||
use support::{
|
||||
dispatch::Result, ConsensusEngineId, decl_module, decl_event,
|
||||
decl_storage,
|
||||
};
|
||||
use support::{dispatch::Result, ConsensusEngineId, decl_module, decl_event, decl_storage};
|
||||
use support::{ensure, traits::{OnFreeBalanceZero, Get, FindAuthor}, Parameter};
|
||||
use system::{self, ensure_signed};
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
srml-support-procedural-tools = { package = "srml-support-procedural-tools", path = "./tools" }
|
||||
sr-api-macros = { path = "../../../core/sr-api-macros" }
|
||||
|
||||
proc-macro2 = "1.0.6"
|
||||
quote = "1.0.2"
|
||||
|
||||
@@ -25,9 +25,7 @@ use support::{
|
||||
},
|
||||
StorageValue, StorageMap, StorageLinkedMap, StorageDoubleMap,
|
||||
};
|
||||
use inherents::{
|
||||
ProvideInherent, InherentData, InherentIdentifier, RuntimeString, MakeFatalError
|
||||
};
|
||||
use inherents::{ProvideInherent, InherentData, InherentIdentifier, MakeFatalError};
|
||||
use primitives::{H256, sr25519};
|
||||
|
||||
mod system;
|
||||
@@ -100,7 +98,7 @@ mod module1 {
|
||||
T::BlockNumber: From<u32>
|
||||
{
|
||||
type Call = Call<T, I>;
|
||||
type Error = MakeFatalError<RuntimeString>;
|
||||
type Error = MakeFatalError<inherents::Error>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
|
||||
fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
|
||||
@@ -160,7 +158,7 @@ mod module2 {
|
||||
|
||||
impl<T: Trait<I>, I: Instance> ProvideInherent for Module<T, I> {
|
||||
type Call = Call<T, I>;
|
||||
type Error = MakeFatalError<RuntimeString>;
|
||||
type Error = MakeFatalError<inherents::Error>;
|
||||
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
|
||||
|
||||
fn create_inherent(_data: &InherentData) -> Option<Self::Call> {
|
||||
|
||||
@@ -5,12 +5,12 @@ authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
client = { package = "substrate-client", path = "../../../../core/client", default-features = false }
|
||||
sr-api = { path = "../../../../core/sr-api", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"client/std",
|
||||
"sr-api/std",
|
||||
"codec/std",
|
||||
]
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
client::decl_runtime_apis! {
|
||||
sr_api::decl_runtime_apis! {
|
||||
/// The API to query account nonce (aka transaction index).
|
||||
pub trait AccountNonceApi<AccountId, Index> where
|
||||
AccountId: codec::Codec,
|
||||
|
||||
@@ -98,12 +98,15 @@ use codec::Decode;
|
||||
use inherents::ProvideInherentData;
|
||||
use support::{Parameter, decl_storage, decl_module};
|
||||
use support::traits::{Time, Get};
|
||||
use sr_primitives::traits::{
|
||||
SimpleArithmetic, Zero, SaturatedConversion, Scale
|
||||
use sr_primitives::{
|
||||
RuntimeString,
|
||||
traits::{
|
||||
SimpleArithmetic, Zero, SaturatedConversion, Scale
|
||||
}
|
||||
};
|
||||
use sr_primitives::weights::SimpleDispatchInfo;
|
||||
use system::ensure_none;
|
||||
use inherents::{RuntimeString, InherentIdentifier, ProvideInherent, IsFatalError, InherentData};
|
||||
use inherents::{InherentIdentifier, ProvideInherent, IsFatalError, InherentData};
|
||||
|
||||
/// The identifier for the `timestamp` inherent.
|
||||
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"timstap0";
|
||||
@@ -145,11 +148,11 @@ impl InherentError {
|
||||
/// Auxiliary trait to extract timestamp inherent data.
|
||||
pub trait TimestampInherentData {
|
||||
/// Get timestamp inherent data.
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, RuntimeString>;
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, inherents::Error>;
|
||||
}
|
||||
|
||||
impl TimestampInherentData for InherentData {
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, RuntimeString> {
|
||||
fn timestamp_inherent_data(&self) -> Result<InherentType, inherents::Error> {
|
||||
self.get_data(&INHERENT_IDENTIFIER)
|
||||
.and_then(|r| r.ok_or_else(|| "Timestamp inherent data not found".into()))
|
||||
}
|
||||
@@ -164,7 +167,10 @@ impl ProvideInherentData for InherentDataProvider {
|
||||
&INHERENT_IDENTIFIER
|
||||
}
|
||||
|
||||
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), RuntimeString> {
|
||||
fn provide_inherent_data(
|
||||
&self,
|
||||
inherent_data: &mut InherentData,
|
||||
) -> Result<(), inherents::Error> {
|
||||
use std::time::SystemTime;
|
||||
|
||||
let now = SystemTime::now();
|
||||
|
||||
@@ -6,7 +6,7 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.101", optional = true, features = ["derive"] }
|
||||
client = { package = "substrate-client", path = "../../../../core/client", default-features = false }
|
||||
sr-api = { path = "../../../../core/sr-api", default-features = false }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.6", default-features = false, features = ["derive"] }
|
||||
rstd = { package = "sr-std", path = "../../../../core/sr-std", default-features = false }
|
||||
sr-primitives = { path = "../../../../core/sr-primitives", default-features = false }
|
||||
@@ -15,7 +15,7 @@ sr-primitives = { path = "../../../../core/sr-primitives", default-features = fa
|
||||
default = ["std"]
|
||||
std = [
|
||||
"serde",
|
||||
"client/std",
|
||||
"sr-api/std",
|
||||
"codec/std",
|
||||
"rstd/std",
|
||||
"sr-primitives/std",
|
||||
|
||||
@@ -37,7 +37,7 @@ pub struct RuntimeDispatchInfo<Balance> {
|
||||
pub partial_fee: Balance,
|
||||
}
|
||||
|
||||
client::decl_runtime_apis! {
|
||||
sr_api::decl_runtime_apis! {
|
||||
pub trait TransactionPaymentApi<Balance, Extrinsic> where
|
||||
Balance: Codec,
|
||||
Extrinsic: Codec,
|
||||
|
||||
Reference in New Issue
Block a user