Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/master' into bkchr-rust-1.54
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr committed Aug 27, 2021
2 parents 5418f44 + ae70e6e commit d416dbb
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0" }
derive_more = "0.99.2"
futures = "0.3.16"
jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
jsonrpc-derive = "18.0.0"
jsonrpc-pubsub = "18.0.0"
log = "0.4.8"
parking_lot = "0.11.1"
thiserror = "1.0"

sp-core = { version = "4.0.0-dev", path = "../../primitives/core" }
sp-version = { version = "4.0.0-dev", path = "../../primitives/version" }
sp-runtime = { path = "../../primitives/runtime", version = "4.0.0-dev" }
Expand Down
41 changes: 14 additions & 27 deletions client/rpc-api/src/author/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,38 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T>>;

/// Author RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Client error.
#[display(fmt = "Client error: {}", _0)]
#[from(ignore)]
#[error("Client error: {}", .0)]
Client(Box<dyn std::error::Error + Send>),
/// Transaction pool error,
#[display(fmt = "Transaction pool error: {}", _0)]
Pool(sc_transaction_pool_api::error::Error),
#[error("Transaction pool error: {}", .0)]
Pool(#[from] sc_transaction_pool_api::error::Error),
/// Verification error
#[display(fmt = "Extrinsic verification error: {}", _0)]
#[from(ignore)]
#[error("Extrinsic verification error: {}", .0)]
Verification(Box<dyn std::error::Error + Send>),
/// Incorrect extrinsic format.
#[display(fmt = "Invalid extrinsic format: {}", _0)]
BadFormat(codec::Error),
#[error("Invalid extrinsic format: {}", .0)]
BadFormat(#[from] codec::Error),
/// Incorrect seed phrase.
#[display(fmt = "Invalid seed phrase/SURI")]
#[error("Invalid seed phrase/SURI")]
BadSeedPhrase,
/// Key type ID has an unknown format.
#[display(fmt = "Invalid key type ID format (should be of length four)")]
#[error("Invalid key type ID format (should be of length four)")]
BadKeyType,
/// Key type ID has some unsupported crypto.
#[display(fmt = "The crypto of key type ID is unknown")]
#[error("The crypto of key type ID is unknown")]
UnsupportedKeyType,
/// Some random issue with the key store. Shouldn't happen.
#[display(fmt = "The key store is unavailable")]
#[error("The key store is unavailable")]
KeyStoreUnavailable,
/// Invalid session keys encoding.
#[display(fmt = "Session keys are not encoded correctly")]
#[error("Session keys are not encoded correctly")]
InvalidSessionKeys,
/// Call to an unsafe RPC was denied.
UnsafeRpcCalled(crate::policy::UnsafeRpcError),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
Error::Pool(ref err) => Some(err),
Error::Verification(ref err) => Some(&**err),
Error::UnsafeRpcCalled(ref err) => Some(err),
_ => None,
}
}
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
}

/// Base code for all authorship errors.
Expand Down
16 changes: 4 additions & 12 deletions client/rpc-api/src/chain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,16 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T>>;

/// Chain RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Client error.
#[display(fmt = "Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
#[error("Client error: {}", .0)]
Client(#[from] Box<dyn std::error::Error + Send>),
/// Other error type.
#[error("{0}")]
Other(String),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
}

/// Base error code for all chain errors.
const BASE_ERROR: i64 = 3000;

Expand Down
16 changes: 4 additions & 12 deletions client/rpc-api/src/offchain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@ use jsonrpc_core as rpc;
pub type Result<T> = std::result::Result<T, Error>;

/// Offchain RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Unavailable storage kind error.
#[display(fmt = "This storage kind is not available yet.")]
#[error("This storage kind is not available yet.")]
UnavailableStorageKind,
/// Call to an unsafe RPC was denied.
UnsafeRpcCalled(crate::policy::UnsafeRpcError),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::UnsafeRpcCalled(err) => Some(err),
_ => None,
}
}
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
}

/// Base error code for all offchain errors.
Expand Down
22 changes: 7 additions & 15 deletions client/rpc-api/src/state/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T>>;

/// State RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Client error.
#[display(fmt = "Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
#[error("Client error: {}", .0)]
Client(#[from] Box<dyn std::error::Error + Send>),
/// Provided block range couldn't be resolved to a list of blocks.
#[display(fmt = "Cannot resolve a block range ['{:?}' ... '{:?}]. {}", from, to, details)]
#[error("Cannot resolve a block range ['{:?}' ... '{:?}]. {}", .from, .to, .details)]
InvalidBlockRange {
/// Beginning of the block range.
from: String,
Expand All @@ -44,24 +44,16 @@ pub enum Error {
details: String,
},
/// Provided count exceeds maximum value.
#[display(fmt = "count exceeds maximum value. value: {}, max: {}", value, max)]
#[error("count exceeds maximum value. value: {}, max: {}", .value, .max)]
InvalidCount {
/// Provided value
value: u32,
/// Maximum allowed value
max: u32,
},
/// Call to an unsafe RPC was denied.
UnsafeRpcCalled(crate::policy::UnsafeRpcError),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
}

/// Base code for all state errors.
Expand Down
7 changes: 3 additions & 4 deletions client/rpc-api/src/system/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ use jsonrpc_core as rpc;
pub type Result<T> = std::result::Result<T, Error>;

/// System RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Provided block range couldn't be resolved to a list of blocks.
#[display(fmt = "Node is not fully functional: {}", _0)]
#[error("Node is not fully functional: {}", .0)]
NotHealthy(Health),
/// Peer argument is malformatted.
#[error("{0}")]
MalformattedPeerArg(String),
}

impl std::error::Error for Error {}

/// Base code for all system errors.
const BASE_ERROR: i64 = 2000;

Expand Down
6 changes: 4 additions & 2 deletions client/rpc/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ where

// FIXME <2329>: Database seems to limit the block number to u32 for no reason
let block_num: u32 = num_or_hex.try_into().map_err(|_| {
Error::from(format!(
Error::Other(format!(
"`{:?}` > u32::MAX, the max block number is u32.",
num_or_hex
))
Expand Down Expand Up @@ -332,7 +332,9 @@ fn subscribe_headers<Block, Client, F, G, S>(
let header = client
.header(BlockId::Hash(best_block_hash()))
.map_err(client_err)
.and_then(|header| header.ok_or_else(|| "Best header missing.".to_string().into()))
.and_then(|header| {
header.ok_or_else(|| Error::Other("Best header missing.".to_string()))
})
.map_err(Into::into);

// send further subscriptions
Expand Down
20 changes: 10 additions & 10 deletions frame/democracy/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ use sp_runtime::{
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Tally<Balance> {
/// The number of aye votes, expressed in terms of post-conviction lock-vote.
pub(crate) ayes: Balance,
pub ayes: Balance,
/// The number of nay votes, expressed in terms of post-conviction lock-vote.
pub(crate) nays: Balance,
pub nays: Balance,
/// The amount of funds currently expressing its opinion. Pre-conviction.
pub(crate) turnout: Balance,
pub turnout: Balance,
}

/// Amount of votes and capital placed in delegation for an account.
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct Delegations<Balance> {
/// The number of votes (this is post-conviction).
pub(crate) votes: Balance,
pub votes: Balance,
/// The amount of raw capital, used for the turnout.
pub(crate) capital: Balance,
pub capital: Balance,
}

impl<Balance: Saturating> Saturating for Delegations<Balance> {
Expand Down Expand Up @@ -162,15 +162,15 @@ impl<
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)]
pub struct ReferendumStatus<BlockNumber, Hash, Balance> {
/// When voting on this referendum will end.
pub(crate) end: BlockNumber,
pub end: BlockNumber,
/// The hash of the proposal being voted on.
pub(crate) proposal_hash: Hash,
pub proposal_hash: Hash,
/// The thresholding mechanism to determine whether it passed.
pub(crate) threshold: VoteThreshold,
pub threshold: VoteThreshold,
/// The delay (in blocks) to wait after a successful referendum before deploying.
pub(crate) delay: BlockNumber,
pub delay: BlockNumber,
/// The current tally of votes in this referendum.
pub(crate) tally: Tally<Balance>,
pub tally: Tally<Balance>,
}

/// Info regarding a referendum, present or past.
Expand Down
4 changes: 3 additions & 1 deletion frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,9 @@ impl<T: Config> Pallet<T> {
}

// After election finalization, clear OCW solution storage.
if <frame_system::Pallet<T>>::events()
//
// We can read the events here because offchain worker doesn't affect PoV.
if <frame_system::Pallet<T>>::read_events_no_consensus()
.into_iter()
.filter_map(|event_record| {
let local_event = <T as Config>::Event::from(event_record.event);
Expand Down
16 changes: 15 additions & 1 deletion frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,19 @@ pub mod pallet_prelude {
/// E.g. if runtime names the pallet "MyExample" then the storage `type Foo<T> = ...` use the
/// prefix: `Twox128(b"MyExample") ++ Twox128(b"Foo")`.
///
/// The optional attribute `#[pallet::getter(fn $my_getter_fn_name)]` allow to define a
/// The optional attribute `#[pallet::storage_prefix = "$custom_name"]` allows to define a
/// specific name to use for the prefix.
///
/// E.g:
/// ```ignore
/// #[pallet::storage]
/// #[pallet::storage_prefix = "OtherName"]
/// pub(super) type MyStorage<T> = StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
/// ```
/// In this case the final prefix used by the map is
/// `Twox128(b"MyExample") ++ Twox128(b"OtherName")`.
///
/// The optional attribute `#[pallet::getter(fn $my_getter_fn_name)]` allows to define a
/// getter function on `Pallet`.
///
/// E.g:
Expand Down Expand Up @@ -2023,6 +2035,7 @@ pub mod pallet_prelude {
/// // Another storage declaration
/// #[pallet::storage]
/// #[pallet::getter(fn my_storage)]
/// #[pallet::storage_prefix = "SomeOtherName"]
/// pub(super) type MyStorage<T> =
/// StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
///
Expand Down Expand Up @@ -2165,6 +2178,7 @@ pub mod pallet_prelude {
///
/// #[pallet::storage]
/// #[pallet::getter(fn my_storage)]
/// #[pallet::storage_prefix = "SomeOtherName"]
/// pub(super) type MyStorage<T, I = ()> =
/// StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
///
Expand Down
25 changes: 23 additions & 2 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,12 @@ pub mod pallet {
pub(super) type Digest<T: Config> = StorageValue<_, DigestOf<T>, ValueQuery>;

/// Events deposited for the current block.
///
/// NOTE: This storage item is explicitly unbounded since it is never intended to be read
/// from within the runtime.
#[pallet::storage]
#[pallet::getter(fn events)]
pub type Events<T: Config> = StorageValue<_, Vec<EventRecord<T::Event, T::Hash>>, ValueQuery>;
pub(super) type Events<T: Config> =
StorageValue<_, Vec<EventRecord<T::Event, T::Hash>>, ValueQuery>;

/// The number of events in the `Events<T>` list.
#[pallet::storage]
Expand Down Expand Up @@ -1448,6 +1451,24 @@ impl<T: Config> Pallet<T> {
})
}

/// Get the current events deposited by the runtime.
///
/// NOTE: This should only be used in tests. Reading events from the runtime can have a large
/// impact on the PoV size of a block. Users should use alternative and well bounded storage
/// items for any behavior like this.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
pub fn events() -> Vec<EventRecord<T::Event, T::Hash>> {
Self::read_events_no_consensus()
}

/// Get the current events deposited by the runtime.
///
/// Should only be called if you know what you are doing and outside of the runtime block
/// execution else it can have a large impact on the PoV size of a block.
pub fn read_events_no_consensus() -> Vec<EventRecord<T::Event, T::Hash>> {
Events::<T>::get()
}

/// Set the block number to something in particular. Can be used as an alternative to
/// `initialize` for tests that don't need to bother with the other environment entries.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
Expand Down
Loading

0 comments on commit d416dbb

Please sign in to comment.