Rename PAINT to PALETTE (#4161)

* /paint to /palette

* rename paint to palette

* rename the modules in palette to be pallets

* update Structure.adoc

* bump impl

* fix CI directory

* Update docs/Structure.adoc

Co-Authored-By: Benjamin Kampmann <ben@gnunicorn.org>
This commit is contained in:
joe petrowski
2019-11-21 01:08:25 +01:00
committed by Benjamin Kampmann
parent 512c86a72f
commit 2783b44207
206 changed files with 898 additions and 888 deletions
+215
View File
@@ -0,0 +1,215 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Runtime debugging and logging utilities.
//!
//! This module contains macros and functions that will allow
//! you to print logs out of the runtime code.
//!
//! First and foremost be aware that adding regular logging code to
//! your runtime will have a negative effect on the performance
//! and size of the blob. Luckily there are some ways to mitigate
//! this that are described below.
//!
//! First component to utilize debug-printing and loggin is actually
//! located in `primitives` crate: `primitives::RuntimeDebug`.
//! This custom-derive generates `core::fmt::Debug` implementation,
//! just like regular `derive(Debug)`, however it does not generate
//! any code when the code is compiled to WASM. This means that
//! you can safely sprinkle `RuntimeDebug` in your runtime codebase,
//! without affecting the size. This also allows you to print/log
//! both when the code is running natively or in WASM, but note
//! that WASM debug formatting of structs will be empty.
//!
//! ```rust,no_run
//! use palette_support::debug;
//!
//! #[derive(primitives::RuntimeDebug)]
//! struct MyStruct {
//! a: u64,
//! }
//!
//! // First initialize the logger.
//! //
//! // This is only required when you want the logs to be printed
//! // also during non-native run.
//! // Note that enabling the logger has performance impact on
//! // WASM runtime execution and should be used sparingly.
//! debug::RuntimeLogger::init();
//!
//! let x = MyStruct { a: 5 };
//! // will log an info line `"My struct: MyStruct{a:5}"` when running
//! // natively, but will only print `"My struct: "` when running WASM.
//! debug::info!("My struct: {:?}", x);
//!
//! // same output here, although this will print to stdout
//! // (and without log format)
//! debug::print!("My struct: {:?}", x);
//! ```
//!
//! If you want to avoid extra overhead in WASM, but still be able
//! to print / log when the code is executed natively you can use
//! macros coming from `native` sub-module. This module enables
//! logs conditionally and strips out logs in WASM.
//!
//! ```rust,no_run
//! use palette_support::debug::native;
//!
//! #[derive(primitives::RuntimeDebug)]
//! struct MyStruct {
//! a: u64,
//! }
//!
//! // We don't initialize the logger, since
//! // we are not printing anything out in WASM.
//! // debug::RuntimeLogger::init();
//!
//! let x = MyStruct { a: 5 };
//!
//! // Displays an info log when running natively, nothing when WASM.
//! native::info!("My struct: {:?}", x);
//!
//! // same output to stdout, no overhead on WASM.
//! native::print!("My struct: {:?}", x);
//! ```
use rstd::vec::Vec;
use rstd::fmt::{self, Debug};
pub use log::{info, debug, error, trace, warn};
pub use crate::runtime_print as print;
/// Native-only logging.
///
/// Using any functions from this module will have any effect
/// only if the runtime is running natively (i.e. not via WASM)
#[cfg(feature = "std")]
pub mod native {
pub use super::{info, debug, error, trace, warn, print};
}
/// Native-only logging.
///
/// Using any functions from this module will have any effect
/// only if the runtime is running natively (i.e. not via WASM)
#[cfg(not(feature = "std"))]
pub mod native {
#[macro_export]
macro_rules! noop {
($($arg:tt)+) => {}
}
pub use noop as info;
pub use noop as debug;
pub use noop as error;
pub use noop as trace;
pub use noop as warn;
pub use noop as print;
}
/// Print out a formatted message.
///
/// # Example
///
/// ```
/// palette_support::runtime_print!("my value is {}", 3);
/// ```
#[macro_export]
macro_rules! runtime_print {
($($arg:tt)+) => {
use core::fmt::Write;
let mut w = $crate::debug::Writer::default();
let _ = core::write!(&mut w, $($arg)+);
w.print();
}
}
/// Print out the debuggable type.
pub fn debug(data: &impl Debug) {
runtime_print!("{:?}", data);
}
/// A target for `core::write!` macro - constructs a string in memory.
#[derive(Default)]
pub struct Writer(Vec<u8>);
impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.extend(s.as_bytes());
Ok(())
}
}
impl Writer {
/// Print the content of this `Writer` out.
pub fn print(&self) {
runtime_io::misc::print_utf8(&self.0)
}
}
/// Runtime logger implementation - `log` crate backend.
///
/// The logger should be initialized if you want to display
/// logs inside the runtime that is not necessarily running natively.
///
/// When runtime is executed natively any log statements are displayed
/// even if this logger is NOT initialized.
///
/// Note that even though the logs are not displayed in WASM, they
/// may still affect the size and performance of the generated runtime.
/// To lower the footprint make sure to only use macros from `native`
/// sub-module.
pub struct RuntimeLogger;
impl RuntimeLogger {
/// Initialize the logger.
///
/// This is a no-op when running natively (`std`).
#[cfg(feature = "std")]
pub fn init() {}
/// Initialize the logger.
///
/// This is a no-op when running natively (`std`).
#[cfg(not(feature = "std"))]
pub fn init() {
static LOGGER: RuntimeLogger = RuntimeLogger;;
let _ = log::set_logger(&LOGGER);
}
}
impl log::Log for RuntimeLogger {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
// to avoid calling to host twice, we pass everything
// and let the host decide what to print.
// If someone is initializing the logger they should
// know what they are doing.
true
}
fn log(&self, record: &log::Record) {
use fmt::Write;
let mut w = Writer::default();
let _ = core::write!(&mut w, "{}", record.args());
runtime_io::logging::log(
record.level().into(),
record.target(),
&w.0,
);
}
fn flush(&self) {}
}
File diff suppressed because it is too large Load Diff
+166
View File
@@ -0,0 +1,166 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Macro for declaring a module error.
#[doc(hidden)]
pub use sr_primitives::traits::LookupError;
pub use palette_metadata::{ModuleErrorMetadata, ErrorMetadata, DecodeDifferent};
/// Declare an error type for a runtime module.
///
/// The generated error type inherently has the variants `Other` and `CannotLookup`. `Other` can
/// hold any `&'static str` error message and is present for convenience/backward compatibility.
/// The `CannotLookup` variant indicates that some lookup could not be done. For both variants the
/// error type implements `From<&'static str>` and `From<LookupError>` to make them usable with the
/// try operator.
///
/// # Usage
///
/// ```
/// # use palette_support::decl_error;
/// decl_error! {
/// /// Errors that can occur in my module.
/// pub enum MyError {
/// /// Hey this is an error message that indicates bla.
/// MyCoolErrorMessage,
/// /// You are just not cool enough for my module!
/// YouAreNotCoolEnough,
/// }
/// }
/// ```
///
/// `decl_error!` supports only variants that do not hold any data.
#[macro_export]
macro_rules! decl_error {
(
$(#[$attr:meta])*
pub enum $error:ident {
$(
$( #[doc = $doc_attr:tt] )*
$name:ident
),*
$(,)?
}
) => {
#[derive(Clone, PartialEq, Eq, $crate::RuntimeDebug)]
$(#[$attr])*
pub enum $error {
Other(&'static str),
CannotLookup,
$(
$( #[doc = $doc_attr] )*
$name
),*
}
impl $crate::dispatch::ModuleDispatchError for $error {
fn as_u8(&self) -> u8 {
$crate::decl_error! {
@GENERATE_AS_U8
self
$error
{}
2,
$( $name ),*
}
}
fn as_str(&self) -> &'static str {
match self {
$error::Other(err) => err,
$error::CannotLookup => "Can not lookup",
$(
$error::$name => stringify!($name),
)*
}
}
}
impl From<&'static str> for $error {
fn from(val: &'static str) -> $error {
$error::Other(val)
}
}
impl From<$crate::error::LookupError> for $error {
fn from(_: $crate::error::LookupError) -> $error {
$error::CannotLookup
}
}
impl From<$error> for &'static str {
fn from(err: $error) -> &'static str {
use $crate::dispatch::ModuleDispatchError;
err.as_str()
}
}
impl Into<$crate::dispatch::DispatchError> for $error {
fn into(self) -> $crate::dispatch::DispatchError {
use $crate::dispatch::ModuleDispatchError;
$crate::dispatch::DispatchError::new(None, self.as_u8(), Some(self.as_str()))
}
}
impl $crate::error::ModuleErrorMetadata for $error {
fn metadata() -> &'static [$crate::error::ErrorMetadata] {
&[
$(
$crate::error::ErrorMetadata {
name: $crate::error::DecodeDifferent::Encode(stringify!($name)),
documentation: $crate::error::DecodeDifferent::Encode(&[
$( $doc_attr ),*
]),
}
),*
]
}
}
};
(@GENERATE_AS_U8
$self:ident
$error:ident
{ $( $generated:tt )* }
$index:expr,
$name:ident
$( , $rest:ident )*
) => {
$crate::decl_error! {
@GENERATE_AS_U8
$self
$error
{
$( $generated )*
$error::$name => $index,
}
$index + 1,
$( $rest ),*
}
};
(@GENERATE_AS_U8
$self:ident
$error:ident
{ $( $generated:tt )* }
$index:expr,
) => {
match $self {
$error::Other(_) => 0,
$error::CannotLookup => 1,
$( $generated )*
}
}
}
+819
View File
@@ -0,0 +1,819 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//! Macros that define an Event types. Events can be used to easily report changes or conditions
//! in your runtime to external entities like users, chain explorers, or dApps.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
pub use palette_metadata::{EventMetadata, DecodeDifferent, OuterEventMetadata, FnEncode};
/// Implement the `Event` for a module.
///
/// # Simple Event Example:
///
/// ```rust
/// palette_support::decl_event!(
/// pub enum Event {
/// Success,
/// Failure(String),
/// }
/// );
///
///# fn main() {}
/// ```
///
/// # Generic Event Example:
///
/// ```rust
/// trait Trait {
/// type Balance;
/// type Token;
/// }
///
/// mod event1 {
/// // Event that specifies the generic parameter explicitly (`Balance`).
/// palette_support::decl_event!(
/// pub enum Event<T> where Balance = <T as super::Trait>::Balance {
/// Message(Balance),
/// }
/// );
/// }
///
/// mod event2 {
/// // Event that uses the generic parameter `Balance`.
/// // If no name for the generic parameter is specified explicitly,
/// // the name will be taken from the type name of the trait.
/// palette_support::decl_event!(
/// pub enum Event<T> where <T as super::Trait>::Balance {
/// Message(Balance),
/// }
/// );
/// }
///
/// mod event3 {
/// // And we even support declaring multiple generic parameters!
/// palette_support::decl_event!(
/// pub enum Event<T> where <T as super::Trait>::Balance, <T as super::Trait>::Token {
/// Message(Balance, Token),
/// }
/// );
/// }
///
///# fn main() {}
/// ```
///
/// The syntax for generic events requires the `where`.
///
/// # Generic Event with Instance Example:
///
/// ```rust
///# struct DefaultInstance;
///# trait Instance {}
///# impl Instance for DefaultInstance {}
/// trait Trait<I: Instance=DefaultInstance> {
/// type Balance;
/// type Token;
/// }
///
/// // For module with instances, DefaultInstance is optional
/// palette_support::decl_event!(
/// pub enum Event<T, I: Instance = DefaultInstance> where
/// <T as Trait>::Balance,
/// <T as Trait>::Token
/// {
/// Message(Balance, Token),
/// }
/// );
///# fn main() {}
/// ```
#[macro_export]
macro_rules! decl_event {
(
$(#[$attr:meta])*
pub enum Event<$evt_generic_param:ident $(, $instance:ident $(: $instantiable:ident)? $( = $event_default_instance:path)? )?> where
$( $tt:tt )*
) => {
$crate::__decl_generic_event!(
$( #[ $attr ] )*;
$evt_generic_param;
$($instance $( = $event_default_instance)? )?;
{ $( $tt )* };
);
};
(
$(#[$attr:meta])*
pub enum Event {
$(
$events:tt
)*
}
) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(
Clone, PartialEq, Eq,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
/// Events for this module.
///
$(#[$attr])*
pub enum Event {
$(
$events
)*
}
impl From<Event> for () {
fn from(_: Event) -> () { () }
}
impl Event {
#[allow(dead_code)]
#[doc(hidden)]
pub fn metadata() -> &'static [ $crate::event::EventMetadata ] {
$crate::__events_to_metadata!(; $( $events )* )
}
}
}
}
#[macro_export]
#[doc(hidden)]
// This parsing to retrieve last ident on unnamed generic could be improved.
// but user can still name it if the parsing fails. And improving parsing seems difficult.
macro_rules! __decl_generic_event {
(
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ $( $tt:tt )* };
) => {
$crate::__decl_generic_event!(@format_generic
$( #[ $attr ] )*;
$event_generic_param;
$($instance $( = $event_default_instance)? )?;
{ $( $tt )* };
{};
);
};
// Finish formatting on an unnamed one
(@format_generic
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ <$generic:ident as $trait:path>::$trait_type:ident $(,)? { $( $events:tt )* } };
{$( $parsed:tt)*};
) => {
$crate::__decl_generic_event!(@generate
$( #[ $attr ] )*;
$event_generic_param;
$($instance $( = $event_default_instance)? )?;
{ $($events)* };
{ $($parsed)*, $trait_type = <$generic as $trait>::$trait_type };
);
};
// Finish formatting on a named one
(@format_generic
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ $generic_rename:ident = $generic_type:ty $(,)? { $( $events:tt )* } };
{ $($parsed:tt)* };
) => {
$crate::__decl_generic_event!(@generate
$(#[$attr])*;
$event_generic_param;
$($instance $( = $event_default_instance)? )?;
{ $($events)* };
{ $($parsed)*, $generic_rename = $generic_type };
);
};
// Parse named
(@format_generic
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ $generic_rename:ident = $generic_type:ty, $($rest:tt)* };
{$( $parsed:tt)*};
) => {
$crate::__decl_generic_event!(@format_generic
$( #[ $attr ] )*;
$event_generic_param;
$( $instance $( = $event_default_instance)? )?;
{ $($rest)* };
{ $($parsed)*, $generic_rename = $generic_type };
);
};
// Parse unnamed
(@format_generic
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ <$generic:ident as $trait:path>::$trait_type:ident, $($rest:tt)* };
{$($parsed:tt)*};
) => {
$crate::__decl_generic_event!(@format_generic
$( #[ $attr ] )*;
$event_generic_param;
$($instance $( = $event_default_instance)? )?;
{ $($rest)* };
{ $($parsed)*, $trait_type = <$generic as $trait>::$trait_type };
);
};
// Unnamed type can't be parsed
(@format_generic
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ $generic_type:ty, $($rest:tt)* };
{ $($parsed:tt)* };
) => {
$crate::__decl_generic_event!(@cannot_parse $generic_type);
};
// Final unnamed type can't be parsed
(@format_generic
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ $generic_type:ty { $( $events:tt )* } };
{$( $parsed:tt)*};
) => {
$crate::__decl_generic_event!(@cannot_parse $generic_type);
};
(@generate
$(#[$attr:meta])*;
$event_generic_param:ident;
$($instance:ident $( = $event_default_instance:path)? )?;
{ $( $events:tt )* };
{ ,$( $generic_param:ident = $generic_type:ty ),* };
) => {
/// [`RawEvent`] specialized for the configuration [`Trait`]
///
/// [`RawEvent`]: enum.RawEvent.html
/// [`Trait`]: trait.Trait.html
pub type Event<$event_generic_param $(, $instance $( = $event_default_instance)? )?> = RawEvent<$( $generic_type ),* $(, $instance)? >;
#[derive(
Clone, PartialEq, Eq,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
/// Events for this module.
///
$(#[$attr])*
pub enum RawEvent<$( $generic_param ),* $(, $instance)? > {
$(
$events
)*
$(
#[doc(hidden)]
#[codec(skip)]
PhantomData($crate::rstd::marker::PhantomData<$instance>),
)?
}
impl<$( $generic_param ),* $(, $instance)? > From<RawEvent<$( $generic_param ),* $(, $instance)?>> for () {
fn from(_: RawEvent<$( $generic_param ),* $(, $instance)?>) -> () { () }
}
impl<$( $generic_param ),* $(, $instance)?> RawEvent<$( $generic_param ),* $(, $instance)?> {
#[allow(dead_code)]
pub fn metadata() -> &'static [$crate::event::EventMetadata] {
$crate::__events_to_metadata!(; $( $events )* )
}
}
};
(@cannot_parse $ty:ty) => {
compile_error!(concat!("The type `", stringify!($ty), "` can't be parsed as an unnamed one, please name it `Name = ", stringify!($ty), "`"));
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! __events_to_metadata {
(
$( $metadata:expr ),*;
$( #[doc = $doc_attr:tt] )*
$event:ident $( ( $( $param:path ),* ) )*,
$( $rest:tt )*
) => {
$crate::__events_to_metadata!(
$( $metadata, )*
$crate::event::EventMetadata {
name: $crate::event::DecodeDifferent::Encode(stringify!($event)),
arguments: $crate::event::DecodeDifferent::Encode(&[
$( $( stringify!($param) ),* )*
]),
documentation: $crate::event::DecodeDifferent::Encode(&[
$( $doc_attr ),*
]),
};
$( $rest )*
)
};
(
$( $metadata:expr ),*;
) => {
&[ $( $metadata ),* ]
}
}
/// Constructs an Event type for a runtime. This is usually called automatically by the
/// construct_runtime macro.
#[macro_export]
macro_rules! impl_outer_event {
// Macro transformations (to convert invocations with incomplete parameters to the canonical
// form)
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident {
$( $rest_event_without_system:tt )*
}
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
system;
Modules { $( $rest_event_without_system )* };
;
);
};
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident where system = $system:ident {
$( $rest_event_with_system:tt )*
}
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_event_with_system )* };
;
);
};
// Generic + Instance
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident $instance:ident<T>,
$( $rest_event_generic_instance:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_event_generic_instance )* };
$( $module_name::Event $( <$generic_param> )? $( { $generic_instance } )?, )* $module::Event<$runtime>{ $instance },;
);
};
// Instance
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident $instance:ident,
$( $rest_event_instance:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_event_instance )* };
$( $module_name::Event $( <$generic_param> )* $( { $generic_instance } )?, )* $module::Event { $instance },;
);
};
// Generic
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident<T>,
$( $rest_event_generic:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_event_generic )* };
$( $module_name::Event $( <$generic_param> )? $( { $generic_instance } )?, )* $module::Event<$runtime>,;
);
};
// No Generic and no Instance
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident,
$( $rest_event_no_generic_no_instance:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_event_no_generic_no_instance )* };
$( $module_name::Event $( <$generic_param> )? $( { $generic_instance } )?, )* $module::Event,;
);
};
// The main macro expansion that actually renders the Event enum code.
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
) => {
$crate::paste::item! {
#[derive(
Clone, PartialEq, Eq,
$crate::codec::Encode,
$crate::codec::Decode,
$crate::RuntimeDebug,
)]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $name {
system($system::Event),
$(
[< $module_name $(_ $generic_instance )? >](
$module_name::Event < $( $generic_param )? $(, $module_name::$generic_instance )? >
),
)*
}
impl From<$system::Event> for $name {
fn from(x: $system::Event) -> Self {
$name::system(x)
}
}
$(
impl From<$module_name::Event < $( $generic_param, )? $( $module_name::$generic_instance )? >> for $name {
fn from(x: $module_name::Event < $( $generic_param, )? $( $module_name::$generic_instance )? >) -> Self {
$name::[< $module_name $(_ $generic_instance )? >](x)
}
}
impl $crate::rstd::convert::TryInto<
$module_name::Event < $( $generic_param, )? $( $module_name::$generic_instance )? >
> for $name {
type Error = ();
fn try_into(self) -> $crate::rstd::result::Result<
$module_name::Event < $( $generic_param, )? $( $module_name::$generic_instance )? >, Self::Error
> {
match self {
Self::[< $module_name $(_ $generic_instance )? >](evt) => Ok(evt),
_ => Err(()),
}
}
}
)*
}
$crate::__impl_outer_event_json_metadata!(
$runtime;
$name;
$system;
$(
$module_name::Event
< $( $generic_param )? $(, $module_name::$generic_instance )? >
$( $generic_instance )?,
)*;
);
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_outer_event_json_metadata {
(
$runtime:ident;
$event_name:ident;
$system:ident;
$( $module_name:ident::Event < $( $generic_params:path ),* > $( $instance:ident )?, )*;
) => {
impl $runtime {
#[allow(dead_code)]
pub fn outer_event_metadata() -> $crate::event::OuterEventMetadata {
$crate::event::OuterEventMetadata {
name: $crate::event::DecodeDifferent::Encode(stringify!($event_name)),
events: $crate::event::DecodeDifferent::Encode(&[
("system", $crate::event::FnEncode(system::Event::metadata))
$(
, (
stringify!($module_name),
$crate::event::FnEncode(
$module_name::Event ::< $( $generic_params ),* > ::metadata
)
)
)*
])
}
}
#[allow(dead_code)]
pub fn __module_events_system() -> &'static [$crate::event::EventMetadata] {
system::Event::metadata()
}
$crate::paste::item! {
$(
#[allow(dead_code)]
pub fn [< __module_events_ $module_name $( _ $instance )? >] () ->
&'static [$crate::event::EventMetadata]
{
$module_name::Event ::< $( $generic_params ),* > ::metadata()
}
)*
}
}
}
}
#[cfg(test)]
#[allow(dead_code)]
mod tests {
use super::*;
use serde::Serialize;
use codec::{Encode, Decode};
mod system {
pub trait Trait {
type Origin;
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_event!(
pub enum Event {
SystemEvent,
}
);
}
mod system_renamed {
pub trait Trait {
type Origin;
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_event!(
pub enum Event {
SystemEvent,
}
);
}
mod event_module {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_event!(
/// Event without renaming the generic parameter `Balance` and `Origin`.
pub enum Event<T> where <T as Trait>::Balance, <T as Trait>::Origin
{
/// Hi, I am a comment.
TestEvent(Balance, Origin),
/// Dog
EventWithoutParams,
}
);
}
mod event_module2 {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_event!(
/// Event with renamed generic parameter
pub enum Event<T> where
BalanceRenamed = <T as Trait>::Balance,
OriginRenamed = <T as Trait>::Origin
{
TestEvent(BalanceRenamed),
TestOrigin(OriginRenamed),
}
);
}
mod event_module3 {
decl_event!(
pub enum Event {
HiEvent,
}
);
}
mod event_module4 {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_event!(
/// Event finish formatting on an unnamed one with trailling comma
pub enum Event<T> where
<T as Trait>::Balance,
<T as Trait>::Origin,
{
TestEvent(Balance, Origin),
}
);
}
mod event_module5 {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
decl_event!(
/// Event finish formatting on an named one with trailing comma
pub enum Event<T> where
BalanceRenamed = <T as Trait>::Balance,
OriginRenamed = <T as Trait>::Origin,
{
TestEvent(BalanceRenamed, OriginRenamed),
}
);
}
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize)]
pub struct TestRuntime;
impl_outer_event! {
pub enum TestEvent for TestRuntime {
event_module<T>,
event_module2<T>,
event_module3,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Serialize)]
pub struct TestRuntime2;
impl_outer_event! {
pub enum TestEventSystemRenamed for TestRuntime2 where system = system_renamed {
event_module<T>,
event_module2<T>,
event_module3,
}
}
impl event_module::Trait for TestRuntime {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl system::Trait for TestRuntime {
type Origin = u32;
type BlockNumber = u32;
}
impl event_module::Trait for TestRuntime2 {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime2 {
type Origin = u32;
type Balance = u32;
type BlockNumber = u32;
}
impl system_renamed::Trait for TestRuntime2 {
type Origin = u32;
type BlockNumber = u32;
}
const EXPECTED_METADATA: OuterEventMetadata = OuterEventMetadata {
name: DecodeDifferent::Encode("TestEvent"),
events: DecodeDifferent::Encode(&[
(
"system",
FnEncode(|| &[
EventMetadata {
name: DecodeDifferent::Encode("SystemEvent"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[]),
}
])
),
(
"event_module",
FnEncode(|| &[
EventMetadata {
name: DecodeDifferent::Encode("TestEvent"),
arguments: DecodeDifferent::Encode(&[ "Balance", "Origin" ]),
documentation: DecodeDifferent::Encode(&[ " Hi, I am a comment." ])
},
EventMetadata {
name: DecodeDifferent::Encode("EventWithoutParams"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[ " Dog" ]),
},
])
),
(
"event_module2",
FnEncode(|| &[
EventMetadata {
name: DecodeDifferent::Encode("TestEvent"),
arguments: DecodeDifferent::Encode(&[ "BalanceRenamed" ]),
documentation: DecodeDifferent::Encode(&[])
},
EventMetadata {
name: DecodeDifferent::Encode("TestOrigin"),
arguments: DecodeDifferent::Encode(&[ "OriginRenamed" ]),
documentation: DecodeDifferent::Encode(&[]),
},
])
),
(
"event_module3",
FnEncode(|| &[
EventMetadata {
name: DecodeDifferent::Encode("HiEvent"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[])
}
])
)
])
};
#[test]
fn outer_event_metadata() {
assert_eq!(EXPECTED_METADATA, TestRuntime::outer_event_metadata());
}
}
+114
View File
@@ -0,0 +1,114 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Hash utilities.
use codec::Codec;
use rstd::prelude::Vec;
use runtime_io::hashing::{blake2_128, blake2_256, twox_64, twox_128, twox_256};
// This trait must be kept coherent with palette-support-procedural HasherKind usage
pub trait Hashable: Sized {
fn blake2_128(&self) -> [u8; 16];
fn blake2_256(&self) -> [u8; 32];
fn twox_128(&self) -> [u8; 16];
fn twox_256(&self) -> [u8; 32];
fn twox_64_concat(&self) -> Vec<u8>;
}
impl<T: Codec> Hashable for T {
fn blake2_128(&self) -> [u8; 16] {
self.using_encoded(blake2_128)
}
fn blake2_256(&self) -> [u8; 32] {
self.using_encoded(blake2_256)
}
fn twox_128(&self) -> [u8; 16] {
self.using_encoded(twox_128)
}
fn twox_256(&self) -> [u8; 32] {
self.using_encoded(twox_256)
}
fn twox_64_concat(&self) -> Vec<u8> {
self.using_encoded(Twox64Concat::hash)
}
}
/// Hasher to use to hash keys to insert to storage.
pub trait StorageHasher: 'static {
type Output: AsRef<[u8]>;
fn hash(x: &[u8]) -> Self::Output;
}
/// Hash storage keys with `concat(twox64(key), key)`
pub struct Twox64Concat;
impl StorageHasher for Twox64Concat {
type Output = Vec<u8>;
fn hash(x: &[u8]) -> Vec<u8> {
twox_64(x)
.iter()
.chain(x.into_iter())
.cloned()
.collect::<Vec<_>>()
}
}
/// Hash storage keys with blake2 128
pub struct Blake2_128;
impl StorageHasher for Blake2_128 {
type Output = [u8; 16];
fn hash(x: &[u8]) -> [u8; 16] {
blake2_128(x)
}
}
/// Hash storage keys with blake2 256
pub struct Blake2_256;
impl StorageHasher for Blake2_256 {
type Output = [u8; 32];
fn hash(x: &[u8]) -> [u8; 32] {
blake2_256(x)
}
}
/// Hash storage keys with twox 128
pub struct Twox128;
impl StorageHasher for Twox128 {
type Output = [u8; 16];
fn hash(x: &[u8]) -> [u8; 16] {
twox_128(x)
}
}
/// Hash storage keys with twox 256
pub struct Twox256;
impl StorageHasher for Twox256 {
type Output = [u8; 32];
fn hash(x: &[u8]) -> [u8; 32] {
twox_256(x)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_twox_64_concat() {
let r = Twox64Concat::hash(b"foo");
assert_eq!(r.split_at(8), (&twox_128(b"foo")[..8], &b"foo"[..]))
}
}
+104
View File
@@ -0,0 +1,104 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#[doc(hidden)]
pub use crate::rstd::vec::Vec;
#[doc(hidden)]
pub use crate::sr_primitives::traits::{Block as BlockT, Extrinsic};
#[doc(hidden)]
pub use inherents::{InherentData, ProvideInherent, CheckInherentsResult, IsFatalError};
/// Implement the outer inherent.
/// All given modules need to implement `ProvideInherent`.
///
/// # Example
///
/// ```nocompile
/// impl_outer_inherent! {
/// impl Inherents where Block = Block, UncheckedExtrinsic = UncheckedExtrinsic {
/// timestamp: Timestamp,
/// consensus: Consensus,
/// /// Aura module using the `Timestamp` call.
/// aura: Timestamp,
/// }
/// }
/// ```
#[macro_export]
macro_rules! impl_outer_inherent {
(
impl Inherents where Block = $block:ident, UncheckedExtrinsic = $uncheckedextrinsic:ident
{
$( $module:ident: $call:ident, )*
}
) => {
trait InherentDataExt {
fn create_extrinsics(&self) ->
$crate::inherent::Vec<<$block as $crate::inherent::BlockT>::Extrinsic>;
fn check_extrinsics(&self, block: &$block) -> $crate::inherent::CheckInherentsResult;
}
impl InherentDataExt for $crate::inherent::InherentData {
fn create_extrinsics(&self) ->
$crate::inherent::Vec<<$block as $crate::inherent::BlockT>::Extrinsic> {
use $crate::inherent::ProvideInherent;
use $crate::inherent::Extrinsic;
let mut inherents = Vec::new();
$(
if let Some(inherent) = $module::create_inherent(self) {
inherents.push($uncheckedextrinsic::new(
Call::$call(inherent),
None,
).expect("Runtime UncheckedExtrinsic is not Opaque, so it has to return `Some`; qed"));
}
)*
inherents
}
fn check_extrinsics(&self, block: &$block) -> $crate::inherent::CheckInherentsResult {
use $crate::inherent::{ProvideInherent, IsFatalError};
let mut result = $crate::inherent::CheckInherentsResult::new();
for xt in block.extrinsics() {
if $crate::inherent::Extrinsic::is_signed(xt).unwrap_or(false) {
break;
}
$(
match xt.function {
Call::$call(ref call) => {
if let Err(e) = $module::check_inherent(call, self) {
result.put_error(
$module::INHERENT_IDENTIFIER, &e
).expect("There is only one fatal error; qed");
if e.is_fatal_error() {
return result;
}
}
}
_ => {},
}
)*
}
result
}
}
};
}
+605
View File
@@ -0,0 +1,605 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Support code for the runtime.
#![cfg_attr(not(feature = "std"), no_std)]
/// Export ourself as `palette_support` to make tests happy.
extern crate self as palette_support;
#[macro_use]
extern crate bitmask;
#[cfg(feature = "std")]
pub use serde;
#[doc(hidden)]
pub use rstd;
#[doc(hidden)]
pub use codec;
#[cfg(feature = "std")]
#[doc(hidden)]
pub use once_cell;
#[doc(hidden)]
pub use paste;
#[cfg(feature = "std")]
#[doc(hidden)]
pub use state_machine::BasicExternalities;
#[doc(hidden)]
pub use runtime_io::storage::root as storage_root;
#[doc(hidden)]
pub use sr_primitives::RuntimeDebug;
#[macro_use]
pub mod debug;
#[macro_use]
pub mod dispatch;
pub mod storage;
mod hash;
#[macro_use]
pub mod event;
#[macro_use]
mod origin;
#[macro_use]
pub mod metadata;
#[macro_use]
mod runtime;
#[macro_use]
pub mod inherent;
#[macro_use]
pub mod unsigned;
#[macro_use]
pub mod error;
pub mod traits;
pub use self::hash::{Twox256, Twox128, Blake2_256, Blake2_128, Twox64Concat, Hashable};
pub use self::storage::{StorageValue, StorageMap, StorageLinkedMap, StorageDoubleMap};
pub use self::dispatch::{Parameter, Callable, IsSubType};
pub use sr_primitives::{self, ConsensusEngineId, print, traits::Printable};
/// Macro for easily creating a new implementation of the `Get` trait. Use similarly to
/// how you would declare a `const`:
///
/// ```no_compile
/// parameter_types! {
/// pub const Argument: u64 = 42;
/// }
/// trait Config {
/// type Parameter: Get<u64>;
/// }
/// struct Runtime;
/// impl Config for Runtime {
/// type Parameter = Argument;
/// }
/// ```
#[macro_export]
macro_rules! parameter_types {
(
$( #[ $attr:meta ] )*
$vis:vis const $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
$crate::parameter_types!{IMPL $name , $type , $value}
$crate::parameter_types!{ $( $rest )* }
);
() => ();
(IMPL $name:ident , $type:ty , $value:expr) => {
impl $name {
pub fn get() -> $type {
$value
}
}
impl<I: From<$type>> $crate::traits::Get<I> for $name {
fn get() -> I {
I::from($value)
}
}
}
}
#[doc(inline)]
pub use palette_support_procedural::decl_storage;
/// Return Err of the expression: `return Err($expression);`.
///
/// Used as `fail!(expression)`.
#[macro_export]
macro_rules! fail {
( $y:expr ) => {{
return Err($y);
}}
}
/// Evaluate `$x:expr` and if not true return `Err($y:expr)`.
///
/// Used as `ensure!(expression_to_ensure, expression_to_return_on_false)`.
#[macro_export]
macro_rules! ensure {
( $x:expr, $y:expr $(,)? ) => {{
if !$x {
$crate::fail!($y);
}
}}
}
/// Evaluate an expression, assert it returns an expected `Err` value and that
/// runtime storage has not been mutated (i.e. expression is a no-operation).
///
/// Used as `assert_noop(expression_to_assert, expected_error_expression)`.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_noop {
(
$x:expr,
$y:expr $(,)?
) => {
let h = $crate::storage_root();
$crate::assert_err!($x, $y);
assert_eq!(h, $crate::storage_root());
}
}
/// Panic if an expression doesn't evaluate to an `Err`.
///
/// Used as `assert_err!(expression_to_assert, expected_err_expression)`.
/// Assert an expression returns an error specified.
///
/// Used as `assert_err!(expression_to_assert, expected_error_expression)`
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_err {
( $x:expr , $y:expr $(,)? ) => {
assert_eq!($x, Err($y));
}
}
/// Panic if an expression doesn't evaluate to `Ok`.
///
/// Used as `assert_ok!(expression_to_assert, expected_ok_expression)`,
/// or `assert_ok!(expression_to_assert)` which would assert against `Ok(())`.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_ok {
( $x:expr $(,)? ) => {
assert_eq!($x, Ok(()));
};
( $x:expr, $y:expr $(,)? ) => {
assert_eq!($x, Ok($y));
}
}
/// Panic when the vectors are different, without taking the order into account.
///
/// # Examples
///
/// ```rust
/// #[macro_use]
/// # extern crate palette_support;
/// # use palette_support::{assert_eq_uvec};
/// # fn main() {
/// assert_eq_uvec!(vec![1,2], vec![2,1]);
/// # }
/// ```
///
/// ```rust,should_panic
/// #[macro_use]
/// # extern crate palette_support;
/// # use palette_support::{assert_eq_uvec};
/// # fn main() {
/// assert_eq_uvec!(vec![1,2,3], vec![2,1]);
/// # }
/// ```
#[macro_export]
#[cfg(feature = "std")]
macro_rules! assert_eq_uvec {
( $x:expr, $y:expr ) => {
$crate::__assert_eq_uvec!($x, $y);
$crate::__assert_eq_uvec!($y, $x);
}
}
#[macro_export]
#[doc(hidden)]
#[cfg(feature = "std")]
macro_rules! __assert_eq_uvec {
( $x:expr, $y:expr ) => {
$x.iter().for_each(|e| {
if !$y.contains(e) { panic!(format!("vectors not equal: {:?} != {:?}", $x, $y)); }
});
}
}
/// The void type - it cannot exist.
// Oh rust, you crack me up...
#[derive(Clone, Eq, PartialEq, RuntimeDebug)]
pub enum Void {}
#[cfg(feature = "std")]
#[doc(hidden)]
pub use serde::{Serialize, Deserialize};
#[cfg(test)]
mod tests {
use super::*;
use codec::{Codec, EncodeLike};
use palette_metadata::{
DecodeDifferent, StorageEntryMetadata, StorageMetadata, StorageEntryType,
StorageEntryModifier, DefaultByteGetter, StorageHasher,
};
use rstd::marker::PhantomData;
pub trait Trait {
type BlockNumber: Codec + EncodeLike + Default;
type Origin;
}
mod module {
#![allow(dead_code)]
use super::Trait;
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
}
use self::module::Module;
decl_storage! {
trait Store for Module<T: Trait> as Example {
pub Data get(fn data) build(|_| vec![(15u32, 42u64)]):
linked_map hasher(twox_64_concat) u32 => u64;
pub OptionLinkedMap: linked_map u32 => Option<u32>;
pub GenericData get(fn generic_data):
linked_map hasher(twox_128) T::BlockNumber => T::BlockNumber;
pub GenericData2 get(fn generic_data2):
linked_map T::BlockNumber => Option<T::BlockNumber>;
pub GetterNoFnKeyword get(no_fn): Option<u32>;
pub DataDM config(test_config) build(|_| vec![(15u32, 16u32, 42u64)]):
double_map hasher(twox_64_concat) u32, blake2_256(u32) => u64;
pub GenericDataDM:
double_map T::BlockNumber, twox_128(T::BlockNumber) => T::BlockNumber;
pub GenericData2DM:
double_map T::BlockNumber, twox_256(T::BlockNumber) => Option<T::BlockNumber>;
pub AppendableDM: double_map u32, blake2_256(T::BlockNumber) => Vec<u32>;
}
}
struct Test;
impl Trait for Test {
type BlockNumber = u32;
type Origin = u32;
}
fn new_test_ext() -> runtime_io::TestExternalities {
GenesisConfig::default().build_storage().unwrap().into()
}
type Map = Data;
#[test]
fn linked_map_issue_3318() {
new_test_ext().execute_with(|| {
OptionLinkedMap::insert(1, 1);
assert_eq!(OptionLinkedMap::get(1), Some(1));
OptionLinkedMap::insert(1, 2);
assert_eq!(OptionLinkedMap::get(1), Some(2));
});
}
#[test]
fn linked_map_swap_works() {
new_test_ext().execute_with(|| {
OptionLinkedMap::insert(0, 0);
OptionLinkedMap::insert(1, 1);
OptionLinkedMap::insert(2, 2);
OptionLinkedMap::insert(3, 3);
let collect = || OptionLinkedMap::enumerate().collect::<Vec<_>>();
assert_eq!(collect(), vec![(3, 3), (2, 2), (1, 1), (0, 0)]);
// Two existing
OptionLinkedMap::swap(1, 2);
assert_eq!(collect(), vec![(3, 3), (2, 1), (1, 2), (0, 0)]);
// Back to normal
OptionLinkedMap::swap(2, 1);
assert_eq!(collect(), vec![(3, 3), (2, 2), (1, 1), (0, 0)]);
// Left existing
OptionLinkedMap::swap(2, 5);
assert_eq!(collect(), vec![(5, 2), (3, 3), (1, 1), (0, 0)]);
// Right existing
OptionLinkedMap::swap(5, 2);
assert_eq!(collect(), vec![(2, 2), (3, 3), (1, 1), (0, 0)]);
});
}
#[test]
fn linked_map_basic_insert_remove_should_work() {
new_test_ext().execute_with(|| {
// initialized during genesis
assert_eq!(Map::get(&15u32), 42u64);
// get / insert / take
let key = 17u32;
assert_eq!(Map::get(&key), 0u64);
Map::insert(key, 4u64);
assert_eq!(Map::get(&key), 4u64);
assert_eq!(Map::take(&key), 4u64);
assert_eq!(Map::get(&key), 0u64);
// mutate
Map::mutate(&key, |val| {
*val = 15;
});
assert_eq!(Map::get(&key), 15u64);
// remove
Map::remove(&key);
assert_eq!(Map::get(&key), 0u64);
});
}
#[test]
fn linked_map_enumeration_and_head_should_work() {
new_test_ext().execute_with(|| {
assert_eq!(Map::head(), Some(15));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(15, 42)]);
// insert / remove
let key = 17u32;
Map::insert(key, 4u64);
assert_eq!(Map::head(), Some(key));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key, 4), (15, 42)]);
assert_eq!(Map::take(&15), 42u64);
assert_eq!(Map::take(&key), 4u64);
assert_eq!(Map::head(), None);
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![]);
// Add couple of more elements
Map::insert(key, 42u64);
assert_eq!(Map::head(), Some(key));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key, 42)]);
Map::insert(key + 1, 43u64);
assert_eq!(Map::head(), Some(key + 1));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key + 1, 43), (key, 42)]);
// mutate
let key = key + 2;
Map::mutate(&key, |val| {
*val = 15;
});
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key, 15), (key - 1, 43), (key - 2, 42)]);
assert_eq!(Map::head(), Some(key));
Map::mutate(&key, |val| {
*val = 17;
});
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key, 17), (key - 1, 43), (key - 2, 42)]);
// remove first
Map::remove(&key);
assert_eq!(Map::head(), Some(key - 1));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key - 1, 43), (key - 2, 42)]);
// remove last from the list
Map::remove(&(key - 2));
assert_eq!(Map::head(), Some(key - 1));
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![(key - 1, 43)]);
// remove the last element
Map::remove(&(key - 1));
assert_eq!(Map::head(), None);
assert_eq!(Map::enumerate().collect::<Vec<_>>(), vec![]);
});
}
#[test]
fn double_map_basic_insert_remove_remove_prefix_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = DataDM;
// initialized during genesis
assert_eq!(DoubleMap::get(&15u32, &16u32), 42u64);
// get / insert / take
let key1 = 17u32;
let key2 = 18u32;
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
DoubleMap::insert(&key1, &key2, &4u64);
assert_eq!(DoubleMap::get(&key1, &key2), 4u64);
assert_eq!(DoubleMap::take(&key1, &key2), 4u64);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
// mutate
DoubleMap::mutate(&key1, &key2, |val| {
*val = 15;
});
assert_eq!(DoubleMap::get(&key1, &key2), 15u64);
// remove
DoubleMap::remove(&key1, &key2);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
// remove prefix
DoubleMap::insert(&key1, &key2, &4u64);
DoubleMap::insert(&key1, &(key2 + 1), &4u64);
DoubleMap::insert(&(key1 + 1), &key2, &4u64);
DoubleMap::insert(&(key1 + 1), &(key2 + 1), &4u64);
DoubleMap::remove_prefix(&key1);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
assert_eq!(DoubleMap::get(&key1, &(key2 + 1)), 0u64);
assert_eq!(DoubleMap::get(&(key1 + 1), &key2), 4u64);
assert_eq!(DoubleMap::get(&(key1 + 1), &(key2 + 1)), 4u64);
});
}
#[test]
fn double_map_append_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = AppendableDM<Test>;
let key1 = 17u32;
let key2 = 18u32;
DoubleMap::insert(&key1, &key2, &vec![1]);
DoubleMap::append(&key1, &key2, &[2, 3]).unwrap();
assert_eq!(DoubleMap::get(&key1, &key2), &[1, 2, 3]);
});
}
const EXPECTED_METADATA: StorageMetadata = StorageMetadata {
prefix: DecodeDifferent::Encode("Example"),
entries: DecodeDifferent::Encode(
&[
StorageEntryMetadata {
name: DecodeDifferent::Encode("Data"),
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map{
hasher: StorageHasher::Twox64Concat,
key: DecodeDifferent::Encode("u32"),
value: DecodeDifferent::Encode("u64"),
is_linked: true,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructData(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("OptionLinkedMap"),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
hasher: StorageHasher::Blake2_256,
key: DecodeDifferent::Encode("u32"),
value: DecodeDifferent::Encode("u32"),
is_linked: true,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructOptionLinkedMap(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("GenericData"),
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map{
hasher: StorageHasher::Twox128,
key: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("T::BlockNumber"),
is_linked: true
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("GenericData2"),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map{
hasher: StorageHasher::Blake2_256,
key: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("T::BlockNumber"),
is_linked: true
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData2(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("GetterNoFnKeyword"),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Plain(DecodeDifferent::Encode("u32")),
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGetterNoFnKeyword(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("DataDM"),
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::DoubleMap{
hasher: StorageHasher::Twox64Concat,
key1: DecodeDifferent::Encode("u32"),
key2: DecodeDifferent::Encode("u32"),
value: DecodeDifferent::Encode("u64"),
key2_hasher: StorageHasher::Blake2_256,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructDataDM(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("GenericDataDM"),
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::DoubleMap{
hasher: StorageHasher::Blake2_256,
key1: DecodeDifferent::Encode("T::BlockNumber"),
key2: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("T::BlockNumber"),
key2_hasher: StorageHasher::Twox128,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericDataDM(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("GenericData2DM"),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::DoubleMap{
hasher: StorageHasher::Blake2_256,
key1: DecodeDifferent::Encode("T::BlockNumber"),
key2: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("T::BlockNumber"),
key2_hasher: StorageHasher::Twox256,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData2DM(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageEntryMetadata {
name: DecodeDifferent::Encode("AppendableDM"),
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::DoubleMap{
hasher: StorageHasher::Blake2_256,
key1: DecodeDifferent::Encode("u32"),
key2: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("Vec<u32>"),
key2_hasher: StorageHasher::Blake2_256,
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData2DM(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
]
),
};
#[test]
fn store_metadata() {
let metadata = Module::<Test>::storage_metadata();
pretty_assertions::assert_eq!(EXPECTED_METADATA, metadata);
}
}
+553
View File
@@ -0,0 +1,553 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
pub use palette_metadata::{
DecodeDifferent, FnEncode, RuntimeMetadata, ModuleMetadata, RuntimeMetadataLastVersion,
DefaultByteGetter, RuntimeMetadataPrefixed, StorageEntryMetadata, StorageMetadata,
StorageEntryType, StorageEntryModifier, DefaultByte, StorageHasher, ModuleErrorMetadata
};
/// Implements the metadata support for the given runtime and all its modules.
///
/// Example:
/// ```
///# mod module0 {
///# pub trait Trait {
///# type Origin;
///# type BlockNumber;
///# }
///# palette_support::decl_module! {
///# pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
///# }
///#
///# palette_support::decl_storage! {
///# trait Store for Module<T: Trait> as TestStorage {}
///# }
///# }
///# use module0 as module1;
///# use module0 as module2;
///# impl module0::Trait for Runtime {
///# type Origin = u32;
///# type BlockNumber = u32;
///# }
///
/// struct Runtime;
/// palette_support::impl_runtime_metadata! {
/// for Runtime with modules
/// module0::Module as Module0 with,
/// module1::Module as Module1 with,
/// module2::Module as Module2 with Storage,
/// };
/// ```
///
/// In this example, just `MODULE3` implements the `Storage` trait.
#[macro_export]
macro_rules! impl_runtime_metadata {
(
for $runtime:ident with modules
$( $rest:tt )*
) => {
impl $runtime {
pub fn metadata() -> $crate::metadata::RuntimeMetadataPrefixed {
$crate::metadata::RuntimeMetadataLastVersion {
modules: $crate::__runtime_modules_to_metadata!($runtime;; $( $rest )*),
}.into()
}
}
}
}
#[macro_export]
#[doc(hidden)]
macro_rules! __runtime_modules_to_metadata {
(
$runtime: ident;
$( $metadata:expr ),*;
$mod:ident::$module:ident $( < $instance:ident > )? as $name:ident $(with)+ $($kw:ident)*,
$( $rest:tt )*
) => {
$crate::__runtime_modules_to_metadata!(
$runtime;
$( $metadata, )* $crate::metadata::ModuleMetadata {
name: $crate::metadata::DecodeDifferent::Encode(stringify!($name)),
storage: $crate::__runtime_modules_to_metadata_calls_storage!(
$mod, $module $( <$instance> )?, $runtime, $(with $kw)*
),
calls: $crate::__runtime_modules_to_metadata_calls_call!(
$mod, $module $( <$instance> )?, $runtime, $(with $kw)*
),
event: $crate::__runtime_modules_to_metadata_calls_event!(
$mod, $module $( <$instance> )?, $runtime, $(with $kw)*
),
constants: $crate::metadata::DecodeDifferent::Encode(
$crate::metadata::FnEncode(
$mod::$module::<$runtime $(, $mod::$instance )?>::module_constants_metadata
)
),
errors: $crate::metadata::DecodeDifferent::Encode(
$crate::metadata::FnEncode(
<$mod::$module::<$runtime $(, $mod::$instance )?> as $crate::metadata::ModuleErrorMetadata>::metadata
)
)
};
$( $rest )*
)
};
(
$runtime:ident;
$( $metadata:expr ),*;
) => {
$crate::metadata::DecodeDifferent::Encode(&[ $( $metadata ),* ])
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __runtime_modules_to_metadata_calls_call {
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
with Call
$(with $kws:ident)*
) => {
Some($crate::metadata::DecodeDifferent::Encode(
$crate::metadata::FnEncode(
$mod::$module::<$runtime $(, $mod::$instance )?>::call_functions
)
))
};
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
with $_:ident
$(with $kws:ident)*
) => {
$crate::__runtime_modules_to_metadata_calls_call! {
$mod, $module $( <$instance> )?, $runtime, $(with $kws)*
};
};
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
) => {
None
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __runtime_modules_to_metadata_calls_event {
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
with Event
$(with $kws:ident)*
) => {
Some($crate::metadata::DecodeDifferent::Encode(
$crate::metadata::FnEncode(
$crate::paste::expr!{
$runtime:: [< __module_events_ $mod $(_ $instance)?>]
}
)
))
};
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
with $_:ident
$(with $kws:ident)*
) => {
$crate::__runtime_modules_to_metadata_calls_event!( $mod, $module $( <$instance> )?, $runtime, $(with $kws)* );
};
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
) => {
None
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! __runtime_modules_to_metadata_calls_storage {
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
with Storage
$(with $kws:ident)*
) => {
Some($crate::metadata::DecodeDifferent::Encode(
$crate::metadata::FnEncode(
$mod::$module::<$runtime $(, $mod::$instance )?>::storage_metadata
)
))
};
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
with $_:ident
$(with $kws:ident)*
) => {
$crate::__runtime_modules_to_metadata_calls_storage! {
$mod, $module $( <$instance> )?, $runtime, $(with $kws)*
};
};
(
$mod: ident,
$module: ident $( <$instance:ident> )?,
$runtime: ident,
) => {
None
};
}
#[cfg(test)]
// Do not complain about unused `dispatch` and `dispatch_aux`.
#[allow(dead_code)]
mod tests {
use super::*;
use palette_metadata::{
EventMetadata, StorageEntryModifier, StorageEntryType, FunctionMetadata, StorageEntryMetadata,
ModuleMetadata, RuntimeMetadataPrefixed, DefaultByte, ModuleConstantMetadata, DefaultByteGetter,
ErrorMetadata,
};
use codec::{Encode, Decode};
use crate::traits::Get;
mod system {
use super::*;
pub trait Trait {
const ASSOCIATED_CONST: u64 = 500;
type Origin: Into<Result<RawOrigin<Self::AccountId>, Self::Origin>>
+ From<RawOrigin<Self::AccountId>>;
type AccountId: From<u32> + Encode;
type BlockNumber: From<u32> + Encode;
type SomeValue: Get<u32>;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Hi, I am a comment.
const BlockNumber: T::BlockNumber = 100.into();
const GetType: T::AccountId = T::SomeValue::get().into();
const ASSOCIATED_CONST: u64 = T::ASSOCIATED_CONST.into();
}
}
decl_event!(
pub enum Event {
SystemEvent,
}
);
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum RawOrigin<AccountId> {
Root,
Signed(AccountId),
None,
}
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
match s {
Some(who) => RawOrigin::Signed(who),
None => RawOrigin::None,
}
}
}
pub type Origin<T> = RawOrigin<<T as Trait>::AccountId>;
}
mod event_module {
use crate::dispatch::DispatchResult;
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_event!(
pub enum Event<T> where <T as Trait>::Balance
{
/// Hi, I am a comment.
TestEvent(Balance),
}
);
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error;
fn aux_0(_origin) -> DispatchResult<Error> { unreachable!() }
}
}
crate::decl_error! {
pub enum Error {
/// Some user input error
UserInputError,
/// Something bad happened
/// this could be due to many reasons
BadThingHappened,
}
}
}
mod event_module2 {
pub trait Trait {
type Origin;
type Balance;
type BlockNumber;
}
decl_event!(
pub enum Event<T> where <T as Trait>::Balance
{
TestEvent(Balance),
}
);
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
crate::decl_storage! {
trait Store for Module<T: Trait> as TestStorage {
StorageMethod : Option<u32>;
}
add_extra_genesis {
build(|_| {});
}
}
}
type EventModule = event_module::Module<TestRuntime>;
type EventModule2 = event_module2::Module<TestRuntime>;
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub struct TestRuntime;
impl_outer_event! {
pub enum TestEvent for TestRuntime {
event_module<T>,
event_module2<T>,
}
}
impl_outer_origin! {
pub enum Origin for TestRuntime {}
}
impl_outer_dispatch! {
pub enum Call for TestRuntime where origin: Origin {
event_module::EventModule,
event_module2::EventModule2,
}
}
impl event_module::Trait for TestRuntime {
type Origin = Origin;
type Balance = u32;
type BlockNumber = u32;
}
impl event_module2::Trait for TestRuntime {
type Origin = Origin;
type Balance = u32;
type BlockNumber = u32;
}
crate::parameter_types! {
pub const SystemValue: u32 = 600;
}
impl system::Trait for TestRuntime {
type Origin = Origin;
type AccountId = u32;
type BlockNumber = u32;
type SomeValue = SystemValue;
}
impl_runtime_metadata!(
for TestRuntime with modules
system::Module as System with Event,
event_module::Module as Module with Event Call,
event_module2::Module as Module2 with Event Storage Call,
);
struct ConstantBlockNumberByteGetter;
impl DefaultByte for ConstantBlockNumberByteGetter {
fn default_byte(&self) -> Vec<u8> {
100u32.encode()
}
}
struct ConstantGetTypeByteGetter;
impl DefaultByte for ConstantGetTypeByteGetter {
fn default_byte(&self) -> Vec<u8> {
SystemValue::get().encode()
}
}
struct ConstantAssociatedConstByteGetter;
impl DefaultByte for ConstantAssociatedConstByteGetter {
fn default_byte(&self) -> Vec<u8> {
<TestRuntime as system::Trait>::ASSOCIATED_CONST.encode()
}
}
const EXPECTED_METADATA: RuntimeMetadataLastVersion = RuntimeMetadataLastVersion {
modules: DecodeDifferent::Encode(&[
ModuleMetadata {
name: DecodeDifferent::Encode("System"),
storage: None,
calls: None,
event: Some(DecodeDifferent::Encode(
FnEncode(||&[
EventMetadata {
name: DecodeDifferent::Encode("SystemEvent"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[])
}
])
)),
constants: DecodeDifferent::Encode(
FnEncode(|| &[
ModuleConstantMetadata {
name: DecodeDifferent::Encode("BlockNumber"),
ty: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode(
DefaultByteGetter(&ConstantBlockNumberByteGetter)
),
documentation: DecodeDifferent::Encode(&[" Hi, I am a comment."]),
},
ModuleConstantMetadata {
name: DecodeDifferent::Encode("GetType"),
ty: DecodeDifferent::Encode("T::AccountId"),
value: DecodeDifferent::Encode(
DefaultByteGetter(&ConstantGetTypeByteGetter)
),
documentation: DecodeDifferent::Encode(&[]),
},
ModuleConstantMetadata {
name: DecodeDifferent::Encode("ASSOCIATED_CONST"),
ty: DecodeDifferent::Encode("u64"),
value: DecodeDifferent::Encode(
DefaultByteGetter(&ConstantAssociatedConstByteGetter)
),
documentation: DecodeDifferent::Encode(&[]),
}
])
),
errors: DecodeDifferent::Encode(FnEncode(|| &[])),
},
ModuleMetadata {
name: DecodeDifferent::Encode("Module"),
storage: None,
calls: Some(
DecodeDifferent::Encode(FnEncode(|| &[
FunctionMetadata {
name: DecodeDifferent::Encode("aux_0"),
arguments: DecodeDifferent::Encode(&[]),
documentation: DecodeDifferent::Encode(&[]),
}
]))),
event: Some(DecodeDifferent::Encode(
FnEncode(||&[
EventMetadata {
name: DecodeDifferent::Encode("TestEvent"),
arguments: DecodeDifferent::Encode(&["Balance"]),
documentation: DecodeDifferent::Encode(&[" Hi, I am a comment."])
}
])
)),
constants: DecodeDifferent::Encode(FnEncode(|| &[])),
errors: DecodeDifferent::Encode(FnEncode(|| &[
ErrorMetadata {
name: DecodeDifferent::Encode("UserInputError"),
documentation: DecodeDifferent::Encode(&[" Some user input error"]),
},
ErrorMetadata {
name: DecodeDifferent::Encode("BadThingHappened"),
documentation: DecodeDifferent::Encode(&[
" Something bad happened",
" this could be due to many reasons",
]),
},
])),
},
ModuleMetadata {
name: DecodeDifferent::Encode("Module2"),
storage: Some(DecodeDifferent::Encode(
FnEncode(|| StorageMetadata {
prefix: DecodeDifferent::Encode("TestStorage"),
entries: DecodeDifferent::Encode(
&[
StorageEntryMetadata {
name: DecodeDifferent::Encode("StorageMethod"),
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Plain(DecodeDifferent::Encode("u32")),
default: DecodeDifferent::Encode(
DefaultByteGetter(
&event_module2::__GetByteStructStorageMethod(
std::marker::PhantomData::<TestRuntime>
)
)
),
documentation: DecodeDifferent::Encode(&[]),
}
]
)
}),
)),
calls: Some(DecodeDifferent::Encode(FnEncode(|| &[]))),
event: Some(DecodeDifferent::Encode(
FnEncode(||&[
EventMetadata {
name: DecodeDifferent::Encode("TestEvent"),
arguments: DecodeDifferent::Encode(&["Balance"]),
documentation: DecodeDifferent::Encode(&[])
}
])
)),
constants: DecodeDifferent::Encode(FnEncode(|| &[])),
errors: DecodeDifferent::Encode(FnEncode(|| &[])),
},
])
};
#[test]
fn runtime_metadata() {
let metadata_encoded = TestRuntime::metadata().encode();
let metadata_decoded = RuntimeMetadataPrefixed::decode(&mut &metadata_encoded[..]);
let expected_metadata: RuntimeMetadataPrefixed = EXPECTED_METADATA.into();
pretty_assertions::assert_eq!(expected_metadata, metadata_decoded.unwrap());
}
}
+300
View File
@@ -0,0 +1,300 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Macros that define an Origin type. Every function call to your runtime has an origin which
//! specifies where the extrinsic was generated from.
/// Constructs an Origin type for a runtime. This is usually called automatically by the
/// construct_runtime macro. See also __create_decl_macro.
#[macro_export]
macro_rules! impl_outer_origin {
// Macro transformations (to convert invocations with incomplete parameters to the canonical
// form)
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident {
$( $rest_without_system:tt )*
}
) => {
$crate::impl_outer_origin! {
$(#[$attr])*
pub enum $name for $runtime where system = system {
$( $rest_without_system )*
}
}
};
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident where system = $system:ident {
$( $rest_with_system:tt )*
}
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_with_system )* };
);
};
// Generic + Instance
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident $instance:ident <T>
$(, $( $rest_module:tt )* )?
};
$( $parsed:tt )*
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $( $rest_module )* )? };
$( $parsed )* $module <$runtime> { $instance },
);
};
// Instance
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident $instance:ident
$(, $rest_module:tt )*
};
$( $parsed:tt )*
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $rest_module )* };
$( $parsed )* $module { $instance },
);
};
// Generic
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident <T>
$(, $( $rest_module:tt )* )?
};
$( $parsed:tt )*
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $( $rest_module )* )? };
$( $parsed )* $module <$runtime>,
);
};
// No Generic and no Instance
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules {
$module:ident
$(, $( $rest_module:tt )* )?
};
$( $parsed:tt )*
) => {
$crate::impl_outer_origin!(
$( #[$attr] )*;
$name;
$runtime;
$system;
Modules { $( $( $rest_module )* )? };
$( $parsed )* $module,
);
};
// The main macro expansion that actually renders the Origin enum code.
(
$(#[$attr:meta])*;
$name:ident;
$runtime:ident;
$system:ident;
Modules { };
$( $module:ident $( < $generic:ident > )? $( { $generic_instance:ident } )? ,)*
) => {
$crate::paste::item! {
#[derive(Clone, PartialEq, Eq, $crate::RuntimeDebug)]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $name {
system($system::Origin<$runtime>),
$(
[< $module $( _ $generic_instance )? >]
($module::Origin < $( $generic, )? $( $module::$generic_instance )? > ),
)*
#[allow(dead_code)]
Void($crate::Void)
}
}
#[allow(dead_code)]
impl $name {
pub const NONE: Self = $name::system($system::RawOrigin::None);
pub const ROOT: Self = $name::system($system::RawOrigin::Root);
pub fn signed(by: <$runtime as $system::Trait>::AccountId) -> Self {
$name::system($system::RawOrigin::Signed(by))
}
}
impl From<$system::Origin<$runtime>> for $name {
fn from(x: $system::Origin<$runtime>) -> Self {
$name::system(x)
}
}
impl Into<$crate::rstd::result::Result<$system::Origin<$runtime>, $name>> for $name {
fn into(self) -> $crate::rstd::result::Result<$system::Origin<$runtime>, Self> {
if let $name::system(l) = self {
Ok(l)
} else {
Err(self)
}
}
}
impl From<Option<<$runtime as $system::Trait>::AccountId>> for $name {
fn from(x: Option<<$runtime as $system::Trait>::AccountId>) -> Self {
<$system::Origin<$runtime>>::from(x).into()
}
}
$(
$crate::paste::item! {
impl From<$module::Origin < $( $generic )? $(, $module::$generic_instance )? > > for $name {
fn from(x: $module::Origin < $( $generic )? $(, $module::$generic_instance )? >) -> Self {
$name::[< $module $( _ $generic_instance )? >](x)
}
}
impl Into<
$crate::rstd::result::Result<
$module::Origin < $( $generic )? $(, $module::$generic_instance )? >,
$name,
>>
for $name {
fn into(self) -> $crate::rstd::result::Result<
$module::Origin < $( $generic )? $(, $module::$generic_instance )? >,
Self,
> {
if let $name::[< $module $( _ $generic_instance )? >](l) = self {
Ok(l)
} else {
Err(self)
}
}
}
}
)*
}
}
#[cfg(test)]
mod tests {
mod system {
pub trait Trait {
type AccountId;
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum RawOrigin<AccountId> {
Root,
Signed(AccountId),
None,
}
impl<AccountId> From<Option<AccountId>> for RawOrigin<AccountId> {
fn from(s: Option<AccountId>) -> RawOrigin<AccountId> {
match s {
Some(who) => RawOrigin::Signed(who),
None => RawOrigin::None,
}
}
}
pub type Origin<T> = RawOrigin<<T as Trait>::AccountId>;
}
mod origin_without_generic {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Origin;
}
mod origin_with_generic {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Origin<T> {
t: T
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TestRuntime;
impl system::Trait for TestRuntime {
type AccountId = u32;
}
impl_outer_origin!(
pub enum OriginWithoutSystem for TestRuntime {
origin_without_generic,
origin_with_generic<T>,
}
);
impl_outer_origin!(
pub enum OriginWithoutSystem2 for TestRuntime {
origin_with_generic<T>,
origin_without_generic
}
);
impl_outer_origin!(
pub enum OriginWithSystem for TestRuntime where system = system {
origin_without_generic,
origin_with_generic<T>
}
);
impl_outer_origin!(
pub enum OriginWithSystem2 for TestRuntime where system = system {
origin_with_generic<T>,
origin_without_generic,
}
);
impl_outer_origin!(
pub enum OriginEmpty for TestRuntime where system = system {}
);
}
+948
View File
@@ -0,0 +1,948 @@
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Macros to define a runtime. A runtime is basically all your logic running in Substrate,
//! consisting of selected SRML modules and maybe some of your own modules.
//! A lot of supporting logic is automatically generated for a runtime,
//! mostly to combine data types and metadata of the included modules.
/// Construct a runtime, with the given name and the given modules.
///
/// The parameters here are specific types for `Block`, `NodeBlock`, and `InherentData`
/// and the modules that are used by the runtime.
/// `Block` is the block type that is used in the runtime and `NodeBlock` is the block type
/// that is used in the node. For instance they can differ in the extrinsics type.
///
/// # Example:
///
/// ```nocompile
/// construct_runtime!(
/// pub enum Runtime where
/// Block = Block,
/// NodeBlock = runtime::Block,
/// UncheckedExtrinsic = UncheckedExtrinsic
/// {
/// System: system,
/// Test: test::{default},
/// Test2: test_with_long_module::{Module},
///
/// // Module with instances
/// Test3_Instance1: test3::<Instance1>::{Module, Call, Storage, Event<T, I>, Config<T, I>, Origin<T, I>},
/// Test3_DefaultInstance: test3::{Module, Call, Storage, Event<T>, Config<T>, Origin<T>},
/// }
/// )
/// ```
///
/// The module `System: system` will expand to `System: system::{Module, Call, Storage, Event<T>, Config<T>}`.
/// The identifier `System` is the name of the module and the lower case identifier `system` is the
/// name of the Rust module/crate for this Substrate module.
///
/// The module `Test: test::{default}` will expand to
/// `Test: test::{Module, Call, Storage, Event<T>, Config<T>}`.
///
/// The module `Test2: test_with_long_module::{Module}` will expand to
/// `Test2: test_with_long_module::{Module}`.
///
/// We provide support for the following types in a module:
///
/// - `Module`
/// - `Call`
/// - `Storage`
/// - `Event` or `Event<T>` (if the event is generic)
/// - `Origin` or `Origin<T>` (if the origin is generic)
/// - `Config` or `Config<T>` (if the config is generic)
/// - `Inherent $( (CALL) )*` - If the module provides/can check inherents. The optional parameter
/// is for modules that use a `Call` from a different module as
/// inherent.
/// - `ValidateUnsigned` - If the module validates unsigned extrinsics.
///
/// # Note
///
/// The population of the genesis storage depends on the order of modules. So, if one of your
/// modules depends on another module, the module that is depended upon needs to come before
/// the module depending on it.
#[macro_export]
macro_rules! construct_runtime {
// Macro transformations (to convert invocations with incomplete parameters to the canonical
// form)
(
pub enum $runtime:ident
where
Block = $block:ident,
NodeBlock = $node_block:ty,
UncheckedExtrinsic = $uncheckedextrinsic:ident
{
$( $rest:tt )*
}
) => {
$crate::construct_runtime!(
{
$runtime;
$block;
$node_block;
$uncheckedextrinsic;
};
{};
$( $rest )*
);
};
// No modules given, expand to the default module set.
(
{ $( $preset:tt )* };
{ $( $expanded:tt )* };
$name:ident: $module:ident,
$( $rest:tt )*
) => {
$crate::construct_runtime!(
{ $( $preset )* };
{ $( $expanded )* };
$name: $module::{default},
$( $rest )*
);
};
// `default` identifier given, expand to default + given extra modules
(
{ $( $preset:tt )* };
{ $( $expanded:tt )* };
$name:ident: $module:ident::{
default
$(,
$modules:ident
$( <$modules_generic:ident> )*
$( ( $( $modules_args:ident ),* ) )*
)*
},
$( $rest:tt )*
) => {
$crate::construct_runtime!(
{ $( $preset )* };
{
$( $expanded )*
$name: $module::{
Module, Call, Storage, Event<T>, Config<T>
$(,
$modules $( <$modules_generic> )*
$( ( $( $modules_args ),* ) )*
)*
},
};
$( $rest )*
);
};
// Take all modules as given by the user.
(
{ $( $preset:tt )* };
{ $( $expanded:tt )* };
$name:ident: $module:ident :: $( < $module_instance:ident >:: )? {
$(
$modules:ident
$( <$modules_generic:ident> )*
$( ( $( $modules_args:ident ),* ) )*
),*
},
$( $rest:tt )*
) => {
$crate::construct_runtime!(
{ $( $preset )* };
{
$( $expanded )*
$name: $module:: $( < $module_instance >:: )? {
$(
$modules $( <$modules_generic> )*
$( ( $( $modules_args ),* ) )*
),*
},
};
$( $rest )*
);
};
// The main macro expansion that actually renders the Runtime code.
(
{
$runtime:ident;
$block:ident;
$node_block:ty;
$uncheckedextrinsic:ident;
};
{
$(
$name:ident: $module:ident :: $( < $module_instance:ident >:: )? {
$(
$modules:ident
$( <$modules_generic:ident> )*
$( ( $( $modules_args:ident ),* ) )*
),*
},
)*
};
) => {
#[derive(Clone, Copy, PartialEq, Eq, $crate::RuntimeDebug)]
pub struct $runtime;
impl $crate::sr_primitives::traits::GetNodeBlockType for $runtime {
type NodeBlock = $node_block;
}
impl $crate::sr_primitives::traits::GetRuntimeBlockType for $runtime {
type RuntimeBlock = $block;
}
$crate::__decl_outer_event!(
$runtime;
$(
$name: $module:: $( < $module_instance >:: )? {
$( $modules $( <$modules_generic> )* ),*
}
),*
);
$crate::__decl_outer_origin!(
$runtime;
$(
$name: $module:: $( < $module_instance >:: )? {
$( $modules $( <$modules_generic> )* ),*
}
),*
);
$crate::__decl_all_modules!(
$runtime;
;
{};
{};
$(
$name: $module:: $( < $module_instance >:: )? { $( $modules ),* },
)*
);
$crate::__decl_outer_dispatch!(
$runtime;
;
$(
$name: $module::{ $( $modules ),* }
),*;
);
$crate::__decl_runtime_metadata!(
$runtime;
{};
$(
$name: $module:: $( < $module_instance >:: )? { $( $modules )* }
)*
);
$crate::__decl_outer_config!(
$runtime;
{};
$(
$name: $module:: $( < $module_instance >:: )? {
$( $modules $( <$modules_generic> )* ),*
},
)*
);
$crate::__decl_outer_inherent!(
$runtime;
$block;
$uncheckedextrinsic;
;
$(
$name: $module::{ $( $modules $( ( $( $modules_args )* ) )* ),* }
),*;
);
$crate::__impl_outer_validate_unsigned!(
$runtime;
{};
$(
$name: $module::{ $( $modules $( ( $( $modules_args )* ) )* )* }
)*
);
}
}
/// A macro that generates a "__decl" private macro that transforms parts of the runtime definition
/// to feed them into a public "impl" macro which accepts the format
/// "pub enum $name for $runtime where system = $system".
///
/// Used to define Event and Origin associated types.
#[macro_export]
#[doc(hidden)]
macro_rules! __create_decl_macro {
(
// Parameter $d is a hack for the following issue:
// https://github.com/rust-lang/rust/issues/35853
$macro_name:ident, $macro_outer_name:ident, $macro_enum_name:ident, $d:tt
) => {
#[macro_export]
#[doc(hidden)]
macro_rules! $macro_name {
(
$runtime:ident;
$d( $name:ident : $module:ident:: $d( < $module_instance:ident >:: )? {
$d( $modules:ident $d( <$modules_generic:ident> ),* ),*
}),*
) => {
$d crate::$macro_name!(@inner
$runtime;
;
{};
$d(
$name: $module:: $d( < $module_instance >:: )? {
$d( $modules $d( <$modules_generic> )* ),*
},
)*
);
};
// Parse system module
(@inner
$runtime:ident;
; // there can not be multiple `System`s
{ $d( $parsed:tt )* };
System: $module:ident::{
$d( $modules:ident $d( <$modules_generic:ident> )* ),*
},
$d( $rest:tt )*
) => {
$d crate::$macro_name!(@inner
$runtime;
$module;
{ $d( $parsed )* };
$d( $rest )*
);
};
// Parse instantiable module with generic
(@inner
$runtime:ident;
$d( $system:ident )?;
{ $d( $parsed:tt )* };
$name:ident : $module:ident:: < $module_instance:ident >:: {
$macro_enum_name <$event_generic:ident> $d(, $ingore:ident $d( <$ignor:ident> )* )*
},
$d( $rest:tt )*
) => {
$d crate::$macro_name!(@inner
$runtime;
$d( $system )?;
{
$d( $parsed )*
$module $module_instance <$event_generic>,
};
$d( $rest )*
);
};
// Parse instantiable module with no generic
(@inner
$runtime:ident;
$d( $system:ident )?;
{ $d( $parsed:tt )* };
$name:ident : $module:ident:: < $module_instance:ident >:: {
$macro_enum_name $d(, $ingore:ident $d( <$ignor:ident> )* )*
},
$d( $rest:tt )*
) => {
compile_error!(concat!(
"Instantiable module with not generic ", stringify!($macro_enum_name),
" cannot be constructed: module `", stringify!($name), "` must have generic ",
stringify!($macro_enum_name), "."
));
};
// Parse instantiable module with no generic
(@inner
$runtime:ident;
$d( $system:ident )?;
{ $d( $parsed:tt )* };
$name:ident : $module:ident:: {
$macro_enum_name $d( <$event_generic:ident> )* $d(, $ignore:ident $d( <$ignor:ident> )* )*
},
$d( $rest:tt )*
) => {
$d crate::$macro_name!(@inner
$runtime;
$d( $system )?;
{
$d( $parsed )*
$module $d( <$event_generic> )*,
};
$d( $rest )*
);
};
// Ignore keyword
(@inner
$runtime:ident;
$d( $system:ident )?;
{ $d( $parsed:tt )* };
$name:ident : $module:ident:: $d( < $module_instance:ident >:: )? {
$ingore:ident $d( <$ignor:ident> )* $d(, $modules:ident $d( <$modules_generic:ident> )* )*
},
$d( $rest:tt )*
) => {
$d crate::$macro_name!(@inner
$runtime;
$d( $system )?;
{ $d( $parsed )* };
$name: $module:: $d( < $module_instance >:: )? { $d( $modules $d( <$modules_generic> )* ),* },
$d( $rest )*
);
};
// Ignore module
(@inner
$runtime:ident;
$d( $system:ident )?;
{ $d( $parsed:tt )* };
$name:ident: $module:ident:: $d( < $module_instance:ident >:: )? {},
$d( $rest:tt )*
) => {
$d crate::$macro_name!(@inner
$runtime;
$d( $system )?;
{ $d( $parsed )* };
$d( $rest )*
);
};
// Expand
(@inner
$runtime:ident;
$system:ident;
{ $d( $parsed_modules:ident $d( $instance:ident )? $d( <$parsed_generic:ident> )? ,)* };
) => {
$d crate::$macro_outer_name! {
pub enum $macro_enum_name for $runtime where system = $system {
$d(
$parsed_modules $d( $instance )? $d( <$parsed_generic> )?,
)*
}
}
}
}
}
}
__create_decl_macro!(__decl_outer_event, impl_outer_event, Event, $);
__create_decl_macro!(__decl_outer_origin, impl_outer_origin, Origin, $);
/// A macro that defines all modules as an associated types of the Runtime type.
#[macro_export]
#[doc(hidden)]
macro_rules! __decl_all_modules {
(
$runtime:ident;
;
{ $( $parsed:tt )* };
{ $( $parsed_nested:tt )* };
System: $module:ident::{ Module $(, $modules:ident )* },
$( $rest:tt )*
) => {
$crate::__decl_all_modules!(
$runtime;
$module;
{ $( $parsed )* };
{ $( $parsed_nested )* };
$( $rest )*
);
};
(
$runtime:ident;
$( $system:ident )?;
{ $( $parsed:tt )* };
{};
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? { Module $(, $modules:ident )* },
$( $rest:tt )*
) => {
$crate::__decl_all_modules!(
$runtime;
$( $system )?;
{
$( $parsed )*
$module::$name $(<$module_instance>)?,
};
{ $name };
$( $rest )*
);
};
(
$runtime:ident;
$( $system:ident )?;
{ $( $parsed:tt )* };
{ $( $parsed_nested:tt )* };
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? { Module $(, $modules:ident )* },
$( $rest:tt )*
) => {
$crate::__decl_all_modules!(
$runtime;
$( $system )?;
{
$( $parsed )*
$module::$name $(<$module_instance>)?,
};
{ ( $( $parsed_nested )*, $name, ) };
$( $rest )*
);
};
(
$runtime:ident;
$( $system:ident )?;
{ $( $parsed:tt )* };
{ $( $parsed_nested:tt )* };
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? { $ignore:ident $(, $modules:ident )* },
$( $rest:tt )*
) => {
$crate::__decl_all_modules!(
$runtime;
$( $system )?;
{ $( $parsed )* };
{ $( $parsed_nested )* };
$name: $module::{ $( $modules ),* },
$( $rest )*
);
};
(
$runtime:ident;
$( $system:ident )?;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {},
$( $rest:tt )*
) => {
$crate::__decl_all_modules!(
$runtime;
$( $system )?;
{ $( $parsed )* };
{ $( $parsed_nested )* };
$( $rest )*
);
};
(
$runtime:ident;
$system:ident;
{ $( $parsed_module:ident :: $parsed_name:ident $(<$instance:ident>)? ,)*};
{ $( $parsed_nested:tt )* };
) => {
pub type System = system::Module<$runtime>;
$(
pub type $parsed_name = $parsed_module::Module<$runtime $(, $parsed_module::$instance )?>;
)*
type AllModules = ( $( $parsed_nested )* );
}
}
/// A macro that defines the Call enum to represent calls to functions in the modules included
/// in the runtime (by wrapping the values of all FooModule::Call enums).
#[macro_export]
#[doc(hidden)]
macro_rules! __decl_outer_dispatch {
(
$runtime:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$name:ident: $module:ident::{
Call $(, $modules:ident $( <$modules_generic:ident> )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( <$rest_modules_generic:ident> )* ),*
})*;
) => {
$crate::__decl_outer_dispatch!(
$runtime;
$( $parsed_modules :: $parsed_name, )* $module::$name;
$(
$rest_name: $rest_module::{
$( $rest_modules $( <$rest_modules_generic> )* ),*
}
),*;
);
};
(
$runtime:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$name:ident: $module:ident::{
$ignore:ident $( <$ignor:ident> )* $(, $modules:ident $( <$modules_generic:ident> )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( <$rest_modules_generic:ident> )* ),*
})*;
) => {
$crate::__decl_outer_dispatch!(
$runtime;
$( $parsed_modules :: $parsed_name ),*;
$name: $module::{ $( $modules $( <$modules_generic> )* ),* }
$(
, $rest_name: $rest_module::{
$( $rest_modules $( <$rest_modules_generic> )* ),*
}
)*;
);
};
(
$runtime:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
$name:ident: $module:ident::{}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( <$rest_modules_generic:ident> )* ),*
})*;
) => {
$crate::__decl_outer_dispatch!(
$runtime;
$( $parsed_modules :: $parsed_name ),*;
$(
$rest_name: $rest_module::{
$( $rest_modules $( <$rest_modules_generic> )* ),*
}
),*;
);
};
(
$runtime:ident;
$( $parsed_modules:ident :: $parsed_name:ident ),*;
;
) => {
$crate::impl_outer_dispatch!(
pub enum Call for $runtime where origin: Origin {
$( $parsed_modules::$parsed_name, )*
}
);
};
}
/// A private macro that generates metadata() method for the runtime. See impl_runtime_metadata macro.
#[macro_export]
#[doc(hidden)]
macro_rules! __decl_runtime_metadata {
// leading is Module : parse
(
$runtime:ident;
{ $( $parsed:tt )* };
$( { leading_module: $( $leading_module:ident )* } )?
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {
Module $( $modules:ident )*
}
$( $rest:tt )*
) => {
$crate::__decl_runtime_metadata!(
$runtime;
{
$( $parsed )*
$module $( < $module_instance > )? as $name {
$( $( $leading_module )* )? $( $modules )*
}
};
$( $rest )*
);
};
// leading isn't Module : put it in leadings
(
$runtime:ident;
{ $( $parsed:tt )* };
$( { leading_module: $( $leading_module:ident )* } )?
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {
$other_module:ident $( $modules:ident )*
}
$( $rest:tt )*
) => {
$crate::__decl_runtime_metadata!(
$runtime;
{ $( $parsed )* };
{ leading_module: $( $( $leading_module )* )? $other_module }
$name: $module:: $( < $module_instance >:: )? {
$( $modules )*
}
$( $rest )*
);
};
// does not contain Module : skip
(
$runtime:ident;
{ $( $parsed:tt )* };
$( { leading_module: $( $leading_module:ident )* } )?
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {}
$( $rest:tt )*
) => {
$crate::__decl_runtime_metadata!(
$runtime;
{ $( $parsed )* };
$( $rest )*
);
};
// end of decl
(
$runtime:ident;
{
$(
$parsed_modules:ident $( < $module_instance:ident > )? as $parsed_name:ident {
$( $withs:ident )*
}
)*
};
) => {
$crate::impl_runtime_metadata!(
for $runtime with modules
$( $parsed_modules::Module $( < $module_instance > )? as $parsed_name
with $( $withs )* , )*
);
}
}
/// A private macro that generates GenesisConfig for the runtime. See `impl_outer_config!` macro.
#[macro_export]
#[doc(hidden)]
macro_rules! __decl_outer_config {
(
$runtime:ident;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {
Config $( <$config_generic:ident> )?
$(, $modules:ident $( <$modules_generic:ident> )* )*
},
$( $rest:tt )*
) => {
$crate::__decl_outer_config!(
$runtime;
{
$( $parsed )*
$module::$name $( $module_instance )? $( <$config_generic> )?,
};
$( $rest )*
);
};
(
$runtime:ident;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {
$ingore:ident $( <$ignore_gen:ident> )*
$(, $modules:ident $( <$modules_generic:ident> )* )*
},
$( $rest:tt )*
) => {
$crate::__decl_outer_config!(
$runtime;
{ $( $parsed )* };
$name: $module:: $( < $module_instance >:: )? { $( $modules $( <$modules_generic> )* ),* },
$( $rest )*
);
};
(
$runtime:ident;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $( < $module_instance:ident >:: )? {},
$( $rest:tt )*
) => {
$crate::__decl_outer_config!(
$runtime;
{ $( $parsed )* };
$( $rest )*
);
};
(
$runtime:ident;
{
$(
$parsed_modules:ident :: $parsed_name:ident $( $parsed_instance:ident )?
$(
<$parsed_generic:ident>
)*
,)*
};
) => {
$crate::paste::item! {
$crate::sr_primitives::impl_outer_config!(
pub struct GenesisConfig for $runtime {
$(
[< $parsed_name Config >] =>
$parsed_modules $( $parsed_instance )? $( <$parsed_generic> )*,
)*
}
);
}
};
}
/// A private macro that generates check_inherents() implementation for the runtime.
#[macro_export]
#[doc(hidden)]
macro_rules! __decl_outer_inherent {
(
$runtime:ident;
$block:ident;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{
Inherent $(, $modules:ident $( ( $( $modules_call:ident )* ) )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
$crate::__decl_outer_inherent!(
$runtime;
$block;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call, )* $name::$name;
$(
$rest_name: $rest_module::{
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
),*;
);
};
(
$runtime:ident;
$block:ident;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{
Inherent ( $call:ident ) $(, $modules:ident $( ( $( $modules_call:ident )* ) )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
$crate::__decl_outer_inherent!(
$runtime;
$block;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call, )* $name::$call;
$(
$rest_name: $rest_module::{
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
),*;
);
};
(
$runtime:ident;
$block:ident;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{
$ignore:ident $( ( $( $ignor:ident )* ) )*
$(, $modules:ident $( ( $( $modules_call:ident )* ) )* )*
}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
$crate::__decl_outer_inherent!(
$runtime;
$block;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call ),*;
$name: $module::{ $( $modules $( ( $( $modules_call )* ) )* ),* }
$(
, $rest_name: $rest_module::{
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
)*;
);
};
(
$runtime:ident;
$block:ident;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
$name:ident: $module:ident::{}
$(, $rest_name:ident : $rest_module:ident::{
$( $rest_modules:ident $( ( $( $rest_call:ident )* ) )* ),*
})*;
) => {
$crate::__decl_outer_inherent!(
$runtime;
$block;
$uncheckedextrinsic;
$( $parsed_name :: $parsed_call ),*;
$(
$rest_name: $rest_module::{
$( $rest_modules $( ( $( $rest_call )* ) )* ),*
}
),*;
);
};
(
$runtime:ident;
$block:ident;
$uncheckedextrinsic:ident;
$( $parsed_name:ident :: $parsed_call:ident ),*;
;
) => {
$crate::impl_outer_inherent!(
impl Inherents where Block = $block, UncheckedExtrinsic = $uncheckedextrinsic {
$( $parsed_name : $parsed_call, )*
}
);
};
}
#[macro_export]
#[doc(hidden)]
// Those imports are used by event, config, origin and log macros to get access to its inner type
macro_rules! __decl_instance_import {
( $( $module:ident <$instance:ident> )* ) => {
$crate::paste::item! {
$(use $module as [< $module _ $instance >];)*
}
};
}
/// A private macro that calls impl_outer_validate_unsigned for Call.
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_outer_validate_unsigned {
(
$runtime:ident;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $(<$module_instance:ident>::)? {
ValidateUnsigned $( $modules:ident $( ( $( $modules_args:ident )* ) )* )*
}
$( $rest:tt )*
) => {
$crate::__impl_outer_validate_unsigned!(
$runtime;
{ $( $parsed )* $name };
$( $rest )*
);
};
(
$runtime:ident;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $(<$module_instance:ident>::)? {
$ignore:ident $( ( $( $args_ignore:ident )* ) )*
$( $modules:ident $( ( $( $modules_args:ident )* ) )* )*
}
$( $rest:tt )*
) => {
$crate::__impl_outer_validate_unsigned!(
$runtime;
{ $( $parsed )* };
$name: $module:: $(<$module_instance>::)? {
$( $modules $( ( $( $modules_args )* ) )* )*
}
$( $rest )*
);
};
(
$runtime:ident;
{ $( $parsed:tt )* };
$name:ident: $module:ident:: $(<$module_instance:ident>::)? {}
$( $rest:tt )*
) => {
$crate::__impl_outer_validate_unsigned!(
$runtime;
{ $( $parsed )* };
$( $rest )*
);
};
(
$runtime:ident;
{ $(
$parsed_modules:ident
)* };
) => {
$crate::impl_outer_validate_unsigned!(
impl ValidateUnsigned for $runtime {
$( $parsed_modules )*
}
);
};
}
@@ -0,0 +1,115 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Operation on runtime child storages.
//!
//! This module is a currently only a variant of unhashed with additional `storage_key`.
//! Note that `storage_key` must be unique and strong (strong in the sense of being long enough to
//! avoid collision from a resistant hash function (which unique implies)).
// NOTE: could replace unhashed by having only one kind of storage (root being null storage key (storage_key can become Option<&[u8]>).
use crate::rstd::prelude::*;
use codec::{Codec, Encode, Decode};
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
pub fn get<T: Decode + Sized>(storage_key: &[u8], key: &[u8]) -> Option<T> {
runtime_io::storage::child_get(storage_key, key).map(|v| {
Decode::decode(&mut &v[..]).expect("storage is not null, therefore must be a valid type")
})
}
/// Return the value of the item in storage under `key`, or the type's default if there is no
/// explicit entry.
pub fn get_or_default<T: Decode + Sized + Default>(storage_key: &[u8], key: &[u8]) -> T {
get(storage_key, key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry.
pub fn get_or<T: Decode + Sized>(storage_key: &[u8], key: &[u8], default_value: T) -> T {
get(storage_key, key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry.
pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(
storage_key: &[u8],
key: &[u8],
default_value: F,
) -> T {
get(storage_key, key).unwrap_or_else(default_value)
}
/// Put `value` in storage under `key`.
pub fn put<T: Encode>(storage_key: &[u8], key: &[u8], value: &T) {
value.using_encoded(|slice| runtime_io::storage::child_set(storage_key, key, slice));
}
/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
pub fn take<T: Decode + Sized>(storage_key: &[u8], key: &[u8]) -> Option<T> {
let r = get(storage_key, key);
if r.is_some() {
kill(storage_key, key);
}
r
}
/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
/// the default for its type.
pub fn take_or_default<T: Codec + Sized + Default>(storage_key: &[u8], key: &[u8]) -> T {
take(storage_key, key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or<T: Codec + Sized>(storage_key: &[u8],key: &[u8], default_value: T) -> T {
take(storage_key, key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or_else<T: Codec + Sized, F: FnOnce() -> T>(
storage_key: &[u8],
key: &[u8],
default_value: F,
) -> T {
take(storage_key, key).unwrap_or_else(default_value)
}
/// Check to see if `key` has an explicit entry in storage.
pub fn exists(storage_key: &[u8], key: &[u8]) -> bool {
runtime_io::storage::child_read(storage_key, key, &mut [0;0][..], 0).is_some()
}
/// Remove all `storage_key` key/values
pub fn kill_storage(storage_key: &[u8]) {
runtime_io::storage::child_storage_kill(storage_key)
}
/// Ensure `key` has no explicit entry in storage.
pub fn kill(storage_key: &[u8], key: &[u8]) {
runtime_io::storage::child_clear(storage_key, key);
}
/// Get a Vec of bytes from storage.
pub fn get_raw(storage_key: &[u8], key: &[u8]) -> Option<Vec<u8>> {
runtime_io::storage::child_get(storage_key, key)
}
/// Put a raw byte slice into storage.
pub fn put_raw(storage_key: &[u8], key: &[u8], value: &[u8]) {
runtime_io::storage::child_set(storage_key, key, value)
}
@@ -0,0 +1,215 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use rstd::prelude::*;
use rstd::borrow::Borrow;
use codec::{Ref, FullCodec, FullEncode, Encode, EncodeLike, EncodeAppend};
use crate::{storage::{self, unhashed}, hash::StorageHasher};
/// Generator for `StorageDoubleMap` used by `decl_storage`.
///
/// # Mapping of keys to a storage path
///
/// The storage key (i.e. the key under which the `Value` will be stored) is created from two parts.
/// The first part is a hash of a concatenation of the `key1_prefix` and `Key1`. And the second part
/// is a hash of a `Key2`.
///
/// Thus value for (key1, key2) is stored at:
/// ```nocompile
/// Hasher1(key1_prefix ++ key1) ++ Hasher2(key2)
/// ```
///
/// # Warning
///
/// If the key1s are not trusted (e.g. can be set by a user), a cryptographic `hasher` such as
/// `blake2_256` must be used for Hasher1. Otherwise, other values in storage can be compromised.
/// If the key2s are not trusted (e.g. can be set by a user), a cryptographic `hasher` such as
/// `blake2_256` must be used for Hasher2. Otherwise, other items in storage with the same first
/// key can be compromised.
pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
/// The type that get/take returns.
type Query;
/// Hasher for the first key.
type Hasher1: StorageHasher;
/// Hasher for the second key.
type Hasher2: StorageHasher;
/// Get the prefix for first key.
fn key1_prefix() -> &'static [u8];
/// Convert an optional value retrieved from storage to the type queried.
fn from_optional_value_to_query(v: Option<V>) -> Self::Query;
/// Convert a query to an optional value into storage.
fn from_query_to_optional_value(v: Self::Query) -> Option<V>;
/// Generate the first part of the key used in top storage.
fn storage_double_map_final_key1<KArg1>(k1: KArg1) -> <Self::Hasher1 as StorageHasher>::Output
where
KArg1: EncodeLike<K1>,
{
let mut final_key1 = Self::key1_prefix().to_vec();
k1.encode_to(&mut final_key1);
Self::Hasher1::hash(&final_key1)
}
/// Generate the full key used in top storage.
fn storage_double_map_final_key<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Vec<u8>
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
let mut final_key = Self::storage_double_map_final_key1(k1).as_ref().to_vec();
final_key.extend_from_slice(k2.using_encoded(Self::Hasher2::hash).as_ref());
final_key
}
}
impl<K1, K2, V, G> storage::StorageDoubleMap<K1, K2, V> for G
where
K1: FullEncode,
K2: FullEncode,
V: FullCodec,
G: StorageDoubleMap<K1, K2, V>,
{
type Query = G::Query;
fn hashed_key_for<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Vec<u8>
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
Self::storage_double_map_final_key(k1, k2)
}
fn exists<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> bool
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
unhashed::exists(&Self::storage_double_map_final_key(k1, k2))
}
fn get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
G::from_optional_value_to_query(unhashed::get(&Self::storage_double_map_final_key(k1, k2)))
}
fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
let final_key = Self::storage_double_map_final_key(k1, k2);
let value = unhashed::take(&final_key);
G::from_optional_value_to_query(value)
}
fn insert<KArg1, KArg2, VArg>(k1: KArg1, k2: KArg2, val: VArg)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
VArg: EncodeLike<V>,
{
unhashed::put(&Self::storage_double_map_final_key(k1, k2), &val.borrow())
}
fn remove<KArg1, KArg2>(k1: KArg1, k2: KArg2)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
{
unhashed::kill(&Self::storage_double_map_final_key(k1, k2))
}
fn remove_prefix<KArg1>(k1: KArg1) where KArg1: EncodeLike<K1> {
unhashed::kill_prefix(Self::storage_double_map_final_key1(k1).as_ref())
}
fn mutate<KArg1, KArg2, R, F>(k1: KArg1, k2: KArg2, f: F) -> R
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
F: FnOnce(&mut Self::Query) -> R,
{
let final_key = Self::storage_double_map_final_key(k1, k2);
let mut val = G::from_optional_value_to_query(unhashed::get(final_key.as_ref()));
let ret = f(&mut val);
match G::from_query_to_optional_value(val) {
Some(ref val) => unhashed::put(final_key.as_ref(), val),
None => unhashed::kill(final_key.as_ref()),
}
ret
}
fn append<Items, Item, EncodeLikeItem, KArg1, KArg2>(
k1: KArg1,
k2: KArg2,
items: Items,
) -> Result<(), &'static str>
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem>,
Items::IntoIter: ExactSizeIterator
{
let final_key = Self::storage_double_map_final_key(k1, k2);
let encoded_value = unhashed::get_raw(&final_key)
.unwrap_or_else(|| {
match G::from_query_to_optional_value(G::from_optional_value_to_query(None)) {
Some(value) => value.encode(),
None => vec![],
}
});
let new_val = V::append_or_new(
encoded_value,
items,
).map_err(|_| "Could not append given item")?;
unhashed::put_raw(&final_key, &new_val);
Ok(())
}
fn append_or_insert<Items, Item, EncodeLikeItem, KArg1, KArg2>(
k1: KArg1,
k2: KArg2,
items: Items,
)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
Items::IntoIter: ExactSizeIterator
{
Self::append(Ref::from(&k1), Ref::from(&k2), items.clone())
.unwrap_or_else(|_| Self::insert(k1, k2, items));
}
}
@@ -0,0 +1,467 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use codec::{FullCodec, Encode, Decode, EncodeLike, Ref};
use crate::{storage::{self, unhashed}, hash::StorageHasher, traits::Len};
use rstd::marker::PhantomData;
/// Generator for `StorageLinkedMap` used by `decl_storage`.
///
/// # Mapping of keys to a storage path
///
/// The key for the head of the map is stored at one fixed path:
/// ```nocompile
/// Hasher(head_key)
/// ```
///
/// For each key, the value stored under that key is appended with a
/// [`Linkage`](struct.Linkage.html) (which hold previous and next key) at the path:
/// ```nocompile
/// Hasher(prefix ++ key)
/// ```
///
/// Enumeration is done by getting the head of the linked map and then iterating getting the
/// value and linkage stored at the key until the found linkage has no next key.
///
/// # Warning
///
/// If the keys are not trusted (e.g. can be set by a user), a cryptographic `hasher` such as
/// `blake2_256` must be used. Otherwise, other values in storage can be compromised.
pub trait StorageLinkedMap<K: FullCodec, V: FullCodec> {
/// The type that get/take returns.
type Query;
/// Hasher used to insert into storage.
type Hasher: StorageHasher;
/// The family of key formats used for this map.
type KeyFormat: KeyFormat<Hasher=Self::Hasher>;
/// Prefix used to prepend each key.
fn prefix() -> &'static [u8];
/// The head key of the linked-map.
fn head_key() -> &'static [u8] {
<Self::KeyFormat as KeyFormat>::head_key()
}
/// Convert an optional value retrieved from storage to the type queried.
fn from_optional_value_to_query(v: Option<V>) -> Self::Query;
/// Convert a query to an optional value into storage.
fn from_query_to_optional_value(v: Self::Query) -> Option<V>;
/// Generate the full key used in top storage.
fn storage_linked_map_final_key<KeyArg>(key: KeyArg) -> <Self::Hasher as StorageHasher>::Output
where
KeyArg: EncodeLike<K>,
{
<Self::KeyFormat as KeyFormat>::storage_linked_map_final_key::<KeyArg>(Self::prefix(), &key)
}
/// Generate the hashed key for head
fn storage_linked_map_final_head_key() -> <Self::Hasher as StorageHasher>::Output {
<Self::KeyFormat as KeyFormat>::storage_linked_map_final_head_key()
}
}
/// A type-abstracted key format used for a family of linked-map types.
pub trait KeyFormat {
type Hasher: StorageHasher;
/// Key used to store linked map head.
fn head_key() -> &'static [u8];
/// Generate the full key used in top storage.
fn storage_linked_map_final_key<K>(prefix: &[u8], key: &K)
-> <Self::Hasher as StorageHasher>::Output
where
K: Encode,
{
let mut final_key = prefix.to_vec();
key.encode_to(&mut final_key);
<Self::Hasher as StorageHasher>::hash(&final_key)
}
fn storage_linked_map_final_head_key()
-> <Self::Hasher as StorageHasher>::Output
{
<Self::Hasher as StorageHasher>::hash(Self::head_key())
}
}
/// Linkage data of an element (it's successor and predecessor)
#[derive(Encode, Decode)]
pub struct Linkage<Key> {
/// Previous element key in storage (None for the first element)
pub previous: Option<Key>,
/// Next element key in storage (None for the last element)
pub next: Option<Key>,
}
impl<Key> Default for Linkage<Key> {
fn default() -> Self {
Self {
previous: None,
next: None,
}
}
}
// Encode like a linkage.
#[derive(Encode)]
struct EncodeLikeLinkage<PKey: EncodeLike<Key>, NKey: EncodeLike<Key>, Key: Encode> {
// Previous element key in storage (None for the first element)
previous: Option<PKey>,
// Next element key in storage (None for the last element)
next: Option<NKey>,
// The key of the linkage this type encode to
phantom: core::marker::PhantomData<Key>,
}
/// A key-value pair iterator for enumerable map.
pub struct Enumerator<K, V, F> {
next: Option<K>,
prefix: &'static [u8],
_phantom: PhantomData<(V, F)>,
}
impl<K, V, F> Enumerator<K, V, F> {
/// Create an explicit enumerator for testing.
#[cfg(test)]
pub fn from_head(head: K, prefix: &'static [u8]) -> Self {
Enumerator {
next: Some(head),
prefix,
_phantom: Default::default(),
}
}
}
impl<K, V, F> Iterator for Enumerator<K, V, F>
where
K: FullCodec,
V: FullCodec,
F: KeyFormat,
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
let next = self.next.take()?;
let (val, linkage): (V, Linkage<K>) = {
let next_full_key = F::storage_linked_map_final_key(self.prefix, &next);
read_with_linkage::<K, V>(next_full_key.as_ref())
.expect("previous/next only contains existing entries;
we enumerate using next; entry exists; qed")
};
self.next = linkage.next;
Some((next, val))
}
}
/// Update linkage when this element is removed.
///
/// Takes care of updating previous and next elements points
/// as well as updates head if the element is first or last.
fn remove_linkage<K, V, F>(linkage: Linkage<K>, prefix: &[u8])
where
K: FullCodec,
V: FullCodec,
F: KeyFormat,
{
let next_key = linkage.next.as_ref()
.map(|k| F::storage_linked_map_final_key(prefix, k))
.map(|x| x.as_ref().to_vec());
let prev_key = linkage.previous.as_ref()
.map(|k| F::storage_linked_map_final_key(prefix, k))
.map(|x| x.as_ref().to_vec());
if let Some(prev_key) = prev_key {
// Retrieve previous element and update `next`
let mut res = read_with_linkage::<K, V>(prev_key.as_ref())
.expect("Linkage is updated in case entry is removed;
it always points to existing keys; qed");
res.1.next = linkage.next;
unhashed::put(prev_key.as_ref(), &res);
} else {
// we were first so let's update the head
write_head::<&K, K, F>(linkage.next.as_ref());
}
if let Some(next_key) = next_key {
// Update previous of next element
let mut res = read_with_linkage::<K, V>(next_key.as_ref())
.expect("Linkage is updated in case entry is removed;
it always points to existing keys; qed");
res.1.previous = linkage.previous;
unhashed::put(next_key.as_ref(), &res);
}
}
/// Read the contained data and its linkage.
pub(super) fn read_with_linkage<K, V>(key: &[u8]) -> Option<(V, Linkage<K>)>
where
K: Decode,
V: Decode,
{
unhashed::get(key)
}
/// Generate linkage for newly inserted element.
///
/// Takes care of updating head and previous head's pointer.
pub(super) fn new_head_linkage<KeyArg, K, V, F>(key: KeyArg, prefix: &[u8]) -> Linkage<K>
where
KeyArg: EncodeLike<K>,
K: FullCodec,
V: FullCodec,
F: KeyFormat,
{
if let Some(head) = read_head::<K, F>() {
// update previous head predecessor
{
let head_key = F::storage_linked_map_final_key(prefix, &head);
let (data, linkage) = read_with_linkage::<K, V>(head_key.as_ref())
.expect("head is set when first element is inserted
and unset when last element is removed;
if head is Some then it points to existing key; qed");
let new_linkage = EncodeLikeLinkage::<_, _, K> {
previous: Some(Ref::from(&key)),
next: linkage.next.as_ref(),
phantom: Default::default(),
};
unhashed::put(head_key.as_ref(), &(data, new_linkage));
}
// update to current head
write_head::<_, _, F>(Some(key));
// return linkage with pointer to previous head
let mut linkage = Linkage::default();
linkage.next = Some(head);
linkage
} else {
// we are first - update the head and produce empty linkage
write_head::<_, _, F>(Some(key));
Linkage::default()
}
}
/// Read current head pointer.
pub(crate) fn read_head<K, F>() -> Option<K>
where
K: Decode,
F: KeyFormat,
{
unhashed::get(F::storage_linked_map_final_head_key().as_ref())
}
/// Overwrite current head pointer.
///
/// If `None` is given head is removed from storage.
pub(super) fn write_head<KeyArg, K, F>(head: Option<KeyArg>)
where
KeyArg: EncodeLike<K>,
K: FullCodec,
F: KeyFormat,
{
match head.as_ref() {
Some(head) => unhashed::put(F::storage_linked_map_final_head_key().as_ref(), head),
None => unhashed::kill(F::storage_linked_map_final_head_key().as_ref()),
}
}
impl<K, V, G> storage::StorageLinkedMap<K, V> for G
where
K: FullCodec,
V: FullCodec,
G: StorageLinkedMap<K, V>,
{
type Query = G::Query;
type Enumerator = Enumerator<K, V, G::KeyFormat>;
fn exists<KeyArg: EncodeLike<K>>(key: KeyArg) -> bool {
unhashed::exists(Self::storage_linked_map_final_key(key).as_ref())
}
fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query {
let val = unhashed::get(Self::storage_linked_map_final_key(key).as_ref());
G::from_optional_value_to_query(val)
}
fn swap<KeyArg1: EncodeLike<K>, KeyArg2: EncodeLike<K>>(key1: KeyArg1, key2: KeyArg2) {
let prefix = Self::prefix();
let final_key1 = Self::storage_linked_map_final_key(Ref::from(&key1));
let final_key2 = Self::storage_linked_map_final_key(Ref::from(&key2));
let full_value_1 = read_with_linkage::<K, V>(final_key1.as_ref());
let full_value_2 = read_with_linkage::<K, V>(final_key2.as_ref());
match (full_value_1, full_value_2) {
// Just keep linkage in order and only swap values.
(Some((value1, linkage1)), Some((value2, linkage2))) => {
unhashed::put(final_key1.as_ref(), &(value2, linkage1));
unhashed::put(final_key2.as_ref(), &(value1, linkage2));
}
// Remove key and insert the new one.
(Some((value, _linkage)), None) => {
Self::remove(key1);
let linkage = new_head_linkage::<_, _, V, G::KeyFormat>(key2, prefix);
unhashed::put(final_key2.as_ref(), &(value, linkage));
}
// Remove key and insert the new one.
(None, Some((value, _linkage))) => {
Self::remove(key2);
let linkage = new_head_linkage::<_, _, V, G::KeyFormat>(key1, prefix);
unhashed::put(final_key1.as_ref(), &(value, linkage));
}
// No-op.
(None, None) => (),
}
}
fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg) {
let final_key = Self::storage_linked_map_final_key(Ref::from(&key));
let linkage = match read_with_linkage::<_, V>(final_key.as_ref()) {
// overwrite but reuse existing linkage
Some((_data, linkage)) => linkage,
// create new linkage
None => new_head_linkage::<_, _, V, G::KeyFormat>(key, Self::prefix()),
};
unhashed::put(final_key.as_ref(), &(val, linkage))
}
fn remove<KeyArg: EncodeLike<K>>(key: KeyArg) {
G::take(key);
}
fn mutate<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R {
let final_key = Self::storage_linked_map_final_key(Ref::from(&key));
let (mut val, _linkage) = read_with_linkage::<K, V>(final_key.as_ref())
.map(|(data, linkage)| (G::from_optional_value_to_query(Some(data)), Some(linkage)))
.unwrap_or_else(|| (G::from_optional_value_to_query(None), None));
let ret = f(&mut val);
match G::from_query_to_optional_value(val) {
Some(ref val) => G::insert(key, val),
None => G::remove(key),
}
ret
}
fn take<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query {
let final_key = Self::storage_linked_map_final_key(key);
let full_value: Option<(V, Linkage<K>)> = unhashed::take(final_key.as_ref());
let value = full_value.map(|(data, linkage)| {
remove_linkage::<K, V, G::KeyFormat>(linkage, Self::prefix());
data
});
G::from_optional_value_to_query(value)
}
fn enumerate() -> Self::Enumerator {
Enumerator::<_, _, G::KeyFormat> {
next: read_head::<_, G::KeyFormat>(),
prefix: Self::prefix(),
_phantom: Default::default(),
}
}
fn head() -> Option<K> {
read_head::<_, G::KeyFormat>()
}
fn decode_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<usize, &'static str>
where V: codec::DecodeLength + Len
{
let key = Self::storage_linked_map_final_key(key);
if let Some(v) = unhashed::get_raw(key.as_ref()) {
<V as codec::DecodeLength>::len(&v).map_err(|e| e.what())
} else {
let len = G::from_query_to_optional_value(G::from_optional_value_to_query(None))
.map(|v| v.len())
.unwrap_or(0);
Ok(len)
}
}
fn translate<K2, V2, TK, TV>(translate_key: TK, translate_val: TV) -> Result<(), Option<K2>>
where K2: FullCodec + Clone, V2: Decode, TK: Fn(K2) -> K, TV: Fn(V2) -> V
{
let head_key = read_head::<K2, G::KeyFormat>().ok_or(None)?;
let prefix = G::prefix();
let mut last_key = None;
let mut current_key = head_key.clone();
write_head::<&K, K, G::KeyFormat>(Some(&translate_key(head_key)));
let translate_linkage = |old: Linkage<K2>| -> Linkage<K> {
Linkage {
previous: old.previous.map(&translate_key),
next: old.next.map(&translate_key),
}
};
loop {
let old_raw_key = G::KeyFormat::storage_linked_map_final_key(prefix, &current_key);
let x = unhashed::take(old_raw_key.as_ref());
let (val, linkage): (V2, Linkage<K2>) = match x {
Some(v) => v,
None => {
// we failed to read value and linkage. Update the last key's linkage
// to end the map early, since it's impossible to iterate further.
if let Some(last_key) = last_key {
let last_raw_key = G::storage_linked_map_final_key(&last_key);
if let Some((val, mut linkage))
= read_with_linkage::<K, V>(last_raw_key.as_ref())
{
// defensive: should always happen, since it was just written
// in the last iteration of the loop.
linkage.next = None;
unhashed::put(last_raw_key.as_ref(), &(&val, &linkage));
}
}
return Err(Some(current_key));
}
};
let next = linkage.next.clone();
let val = translate_val(val);
let linkage = translate_linkage(linkage);
// and write in the value and linkage under the new key.
let new_key = translate_key(current_key.clone());
let new_raw_key = G::storage_linked_map_final_key(&new_key);
unhashed::put(new_raw_key.as_ref(), &(&val, &linkage));
match next {
None => break,
Some(next) => {
last_key = Some(new_key);
current_key = next
},
}
}
Ok(())
}
}
@@ -0,0 +1,172 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#[cfg(not(feature = "std"))]
use rstd::prelude::*;
use rstd::borrow::Borrow;
use codec::{FullCodec, FullEncode, Encode, EncodeLike, Ref, EncodeAppend};
use crate::{storage::{self, unhashed}, hash::StorageHasher, traits::Len};
/// Generator for `StorageMap` used by `decl_storage`.
///
/// For each key value is stored at:
/// ```nocompile
/// Hasher(prefix ++ key)
/// ```
///
/// # Warning
///
/// If the keys are not trusted (e.g. can be set by a user), a cryptographic `hasher` such as
/// `blake2_256` must be used. Otherwise, other values in storage can be compromised.
pub trait StorageMap<K: FullEncode, V: FullCodec> {
/// The type that get/take returns.
type Query;
/// Hasher used to insert into storage.
type Hasher: StorageHasher;
/// Prefix used to prepend each key.
fn prefix() -> &'static [u8];
/// Convert an optional value retrieved from storage to the type queried.
fn from_optional_value_to_query(v: Option<V>) -> Self::Query;
/// Convert a query to an optional value into storage.
fn from_query_to_optional_value(v: Self::Query) -> Option<V>;
/// Generate the full key used in top storage.
fn storage_map_final_key<KeyArg>(key: KeyArg) -> <Self::Hasher as StorageHasher>::Output
where
KeyArg: EncodeLike<K>,
{
let mut final_key = Self::prefix().to_vec();
key.borrow().encode_to(&mut final_key);
Self::Hasher::hash(&final_key)
}
}
impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V> for G {
type Query = G::Query;
fn hashed_key_for<KeyArg: EncodeLike<K>>(key: KeyArg) -> Vec<u8> {
Self::storage_map_final_key(key).as_ref().to_vec()
}
fn swap<KeyArg1: EncodeLike<K>, KeyArg2: EncodeLike<K>>(key1: KeyArg1, key2: KeyArg2) {
let k1 = Self::storage_map_final_key(key1);
let k2 = Self::storage_map_final_key(key2);
let v1 = unhashed::get_raw(k1.as_ref());
if let Some(val) = unhashed::get_raw(k2.as_ref()) {
unhashed::put_raw(k1.as_ref(), &val);
} else {
unhashed::kill(k1.as_ref())
}
if let Some(val) = v1 {
unhashed::put_raw(k2.as_ref(), &val);
} else {
unhashed::kill(k2.as_ref())
}
}
fn exists<KeyArg: EncodeLike<K>>(key: KeyArg) -> bool {
unhashed::exists(Self::storage_map_final_key(key).as_ref())
}
fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query {
G::from_optional_value_to_query(unhashed::get(Self::storage_map_final_key(key).as_ref()))
}
fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg) {
unhashed::put(Self::storage_map_final_key(key).as_ref(), &val.borrow())
}
fn remove<KeyArg: EncodeLike<K>>(key: KeyArg) {
unhashed::kill(Self::storage_map_final_key(key).as_ref())
}
fn mutate<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R {
let final_key = Self::storage_map_final_key(key);
let mut val = G::from_optional_value_to_query(unhashed::get(final_key.as_ref()));
let ret = f(&mut val);
match G::from_query_to_optional_value(val) {
Some(ref val) => unhashed::put(final_key.as_ref(), &val.borrow()),
None => unhashed::kill(final_key.as_ref()),
}
ret
}
fn take<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query {
let key = Self::storage_map_final_key(key);
let value = unhashed::take(key.as_ref());
G::from_optional_value_to_query(value)
}
fn append<Items, Item, EncodeLikeItem, KeyArg>(key: KeyArg, items: Items) -> Result<(), &'static str>
where
KeyArg: EncodeLike<K>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem>,
Items::IntoIter: ExactSizeIterator,
{
let key = Self::storage_map_final_key(key);
let encoded_value = unhashed::get_raw(key.as_ref())
.unwrap_or_else(|| {
match G::from_query_to_optional_value(G::from_optional_value_to_query(None)) {
Some(value) => value.encode(),
None => vec![],
}
});
let new_val = V::append_or_new(
encoded_value,
items,
).map_err(|_| "Could not append given item")?;
unhashed::put_raw(key.as_ref(), &new_val);
Ok(())
}
fn append_or_insert<Items, Item, EncodeLikeItem, KeyArg>(key: KeyArg, items: Items)
where
KeyArg: EncodeLike<K>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
Items::IntoIter: ExactSizeIterator,
{
Self::append(Ref::from(&key), items.clone())
.unwrap_or_else(|_| Self::insert(key, items));
}
fn decode_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<usize, &'static str>
where V: codec::DecodeLength + Len
{
let key = Self::storage_map_final_key(key);
if let Some(v) = unhashed::get_raw(key.as_ref()) {
<V as codec::DecodeLength>::len(&v).map_err(|e| e.what())
} else {
let len = G::from_query_to_optional_value(G::from_optional_value_to_query(None))
.map(|v| v.len())
.unwrap_or(0);
Ok(len)
}
}
}
@@ -0,0 +1,132 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Generators are a set of trait on which storage traits are implemented.
//!
//! (i.e. implementing the generator for StorageValue on a type will automatically derive the
//! implementation of StorageValue for this type).
//!
//! They are used by `decl_storage`.
mod linked_map;
mod map;
mod double_map;
mod value;
pub use linked_map::{StorageLinkedMap, Enumerator, Linkage, KeyFormat as LinkedMapKeyFormat};
pub use map::StorageMap;
pub use double_map::StorageDoubleMap;
pub use value::StorageValue;
#[cfg(test)]
#[allow(dead_code)]
mod tests {
use runtime_io::TestExternalities;
use codec::{Encode, Decode};
use crate::storage::{unhashed, generator::{StorageValue, StorageLinkedMap}};
struct Runtime {}
pub trait Trait {
type Origin;
type BlockNumber;
}
impl Trait for Runtime {
type Origin = u32;
type BlockNumber = u32;
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}
#[derive(Encode, Decode, Clone, Debug, Eq, PartialEq)]
struct NumberNumber {
a: u32,
b: u32,
}
crate::decl_storage! {
trait Store for Module<T: Trait> as Runtime {
Value get(fn value) config(): (u64, u64);
NumberMap: linked_map NumberNumber => u64;
}
}
#[test]
fn value_translate_works() {
let t = GenesisConfig::default().build_storage().unwrap();
TestExternalities::new(t).execute_with(|| {
// put the old value `1111u32` in the storage.
let key = Value::storage_value_final_key();
unhashed::put_raw(&key, &1111u32.encode());
// translate
let translate_fn = |old: Option<u32>| -> Option<(u64, u64)> {
old.map(|o| (o.into(), (o*2).into()))
};
let _ = Value::translate(translate_fn);
// new storage should be `(1111, 1111 * 2)`
assert_eq!(Value::get(), (1111, 2222));
})
}
#[test]
fn linked_map_translate_works() {
use super::linked_map::{self, Enumerator, KeyFormat};
type Format = <NumberMap as StorageLinkedMap<NumberNumber, u64>>::KeyFormat;
let t = GenesisConfig::default().build_storage().unwrap();
TestExternalities::new(t).execute_with(|| {
let prefix = NumberMap::prefix();
// start with a map of u32 -> u32.
for i in 0u32..100u32 {
let final_key = <Format as KeyFormat>::storage_linked_map_final_key(
prefix, &i,
);
let linkage = linked_map::new_head_linkage::<_, u32, u32, Format>(&i, prefix);
unhashed::put(final_key.as_ref(), &(&i, linkage));
}
let head = linked_map::read_head::<u32, Format>().unwrap();
assert_eq!(
Enumerator::<u32, u32, Format>::from_head(head, prefix).collect::<Vec<_>>(),
(0..100).rev().map(|x| (x, x)).collect::<Vec<_>>(),
);
// do translation.
NumberMap::translate(
|k: u32| NumberNumber { a: k, b: k },
|v: u32| (v as u64) << 32 | v as u64,
).unwrap();
assert!(linked_map::read_head::<NumberNumber, Format>().is_some());
assert_eq!(
NumberMap::enumerate().collect::<Vec<_>>(),
(0..100u32).rev().map(|x| (
NumberNumber { a: x, b: x },
(x as u64) << 32 | x as u64,
)).collect::<Vec<_>>(),
);
})
}
}
@@ -0,0 +1,171 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#[cfg(not(feature = "std"))]
use rstd::prelude::*;
use codec::{FullCodec, Encode, EncodeAppend, EncodeLike, Decode};
use crate::{storage::{self, unhashed}, hash::{Twox128, StorageHasher}, traits::Len};
/// Generator for `StorageValue` used by `decl_storage`.
///
/// Value is stored at:
/// ```nocompile
/// Twox128(unhashed_key)
/// ```
pub trait StorageValue<T: FullCodec> {
/// The type that get/take returns.
type Query;
/// Unhashed key used in storage
fn unhashed_key() -> &'static [u8];
/// Convert an optional value retrieved from storage to the type queried.
fn from_optional_value_to_query(v: Option<T>) -> Self::Query;
/// Convert a query to an optional value into storage.
fn from_query_to_optional_value(v: Self::Query) -> Option<T>;
/// Generate the full key used in top storage.
fn storage_value_final_key() -> [u8; 16] {
Twox128::hash(Self::unhashed_key())
}
}
impl<T: FullCodec, G: StorageValue<T>> storage::StorageValue<T> for G {
type Query = G::Query;
fn hashed_key() -> [u8; 16] {
Self::storage_value_final_key()
}
fn exists() -> bool {
unhashed::exists(&Self::storage_value_final_key())
}
fn get() -> Self::Query {
let value = unhashed::get(&Self::storage_value_final_key());
G::from_optional_value_to_query(value)
}
fn translate<O: Decode, F: FnOnce(Option<O>) -> Option<T>>(f: F) -> Result<Option<T>, ()> {
let key = Self::storage_value_final_key();
// attempt to get the length directly.
let maybe_old = match unhashed::get_raw(&key) {
Some(old_data) => Some(O::decode(&mut &old_data[..]).map_err(|_| ())?),
None => None,
};
let maybe_new = f(maybe_old);
if let Some(new) = maybe_new.as_ref() {
new.using_encoded(|d| unhashed::put_raw(&key, d));
} else {
unhashed::kill(&key);
}
Ok(maybe_new)
}
fn put<Arg: EncodeLike<T>>(val: Arg) {
unhashed::put(&Self::storage_value_final_key(), &val)
}
fn kill() {
unhashed::kill(&Self::storage_value_final_key())
}
fn mutate<R, F: FnOnce(&mut G::Query) -> R>(f: F) -> R {
let mut val = G::get();
let ret = f(&mut val);
match G::from_query_to_optional_value(val) {
Some(ref val) => G::put(val),
None => G::kill(),
}
ret
}
fn take() -> G::Query {
let key = Self::storage_value_final_key();
let value = unhashed::get(&key);
if value.is_some() {
unhashed::kill(&key)
}
G::from_optional_value_to_query(value)
}
/// Append the given items to the value in the storage.
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append<Items, Item, EncodeLikeItem>(items: Items) -> Result<(), &'static str>
where
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
T: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem>,
Items::IntoIter: ExactSizeIterator,
{
let key = Self::storage_value_final_key();
let encoded_value = unhashed::get_raw(&key)
.unwrap_or_else(|| {
match G::from_query_to_optional_value(G::from_optional_value_to_query(None)) {
Some(value) => value.encode(),
None => vec![],
}
});
let new_val = T::append_or_new(
encoded_value,
items,
).map_err(|_| "Could not append given item")?;
unhashed::put_raw(&key, &new_val);
Ok(())
}
/// Safely append the given items to the value in the storage. If a codec error occurs, then the
/// old (presumably corrupt) value is replaced with the given `items`.
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append_or_put<Items, Item, EncodeLikeItem>(items: Items) where
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
T: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<T>,
Items::IntoIter: ExactSizeIterator
{
Self::append(items.clone()).unwrap_or_else(|_| Self::put(items));
}
/// Read the length of the value in a fast way, without decoding the entire value.
///
/// `T` is required to implement `Codec::DecodeLength`.
///
/// Note that `0` is returned as the default value if no encoded value exists at the given key.
/// Therefore, this function cannot be used as a sign of _existence_. use the `::exists()`
/// function for this purpose.
fn decode_len() -> Result<usize, &'static str> where T: codec::DecodeLength, T: Len {
let key = Self::storage_value_final_key();
// attempt to get the length directly.
if let Some(k) = unhashed::get_raw(&key) {
<T as codec::DecodeLength>::len(&k).map_err(|e| e.what())
} else {
let len = G::from_query_to_optional_value(G::from_optional_value_to_query(None))
.map(|v| v.len())
.unwrap_or(0);
Ok(len)
}
}
}
@@ -0,0 +1,155 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Operation on runtime storage using hashed keys.
use super::unhashed;
use rstd::prelude::*;
use codec::{Encode, Decode};
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
pub fn get<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> Option<T>
where
T: Decode + Sized,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::get(&hash(key).as_ref())
}
/// Return the value of the item in storage under `key`, or the type's default if there is no
/// explicit entry.
pub fn get_or_default<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> T
where
T: Decode + Sized + Default,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::get_or_default(&hash(key).as_ref())
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry.
pub fn get_or<T, HashFn, R>(hash: &HashFn, key: &[u8], default_value: T) -> T
where
T: Decode + Sized,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::get_or(&hash(key).as_ref(), default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry.
pub fn get_or_else<T, F, HashFn, R>(hash: &HashFn, key: &[u8], default_value: F) -> T
where
T: Decode + Sized,
F: FnOnce() -> T,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::get_or_else(&hash(key).as_ref(), default_value)
}
/// Put `value` in storage under `key`.
pub fn put<T, HashFn, R>(hash: &HashFn, key: &[u8], value: &T)
where
T: Encode,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::put(&hash(key).as_ref(), value)
}
/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
pub fn take<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> Option<T>
where
T: Decode + Sized,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::take(&hash(key).as_ref())
}
/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
/// the default for its type.
pub fn take_or_default<T, HashFn, R>(hash: &HashFn, key: &[u8]) -> T
where
T: Decode + Sized + Default,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::take_or_default(&hash(key).as_ref())
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or<T, HashFn, R>(hash: &HashFn, key: &[u8], default_value: T) -> T
where
T: Decode + Sized,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::take_or(&hash(key).as_ref(), default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or_else<T, F, HashFn, R>(hash: &HashFn, key: &[u8], default_value: F) -> T
where
T: Decode + Sized,
F: FnOnce() -> T,
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::take_or_else(&hash(key).as_ref(), default_value)
}
/// Check to see if `key` has an explicit entry in storage.
pub fn exists<HashFn, R>(hash: &HashFn, key: &[u8]) -> bool
where
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::exists(&hash(key).as_ref())
}
/// Ensure `key` has no explicit entry in storage.
pub fn kill<HashFn, R>(hash: &HashFn, key: &[u8])
where
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::kill(&hash(key).as_ref())
}
/// Get a Vec of bytes from storage.
pub fn get_raw<HashFn, R>(hash: &HashFn, key: &[u8]) -> Option<Vec<u8>>
where
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::get_raw(&hash(key).as_ref())
}
/// Put a raw byte slice into storage.
pub fn put_raw<HashFn, R>(hash: &HashFn, key: &[u8], value: &[u8])
where
HashFn: Fn(&[u8]) -> R,
R: AsRef<[u8]>,
{
unhashed::put_raw(&hash(key).as_ref(), value)
}
@@ -0,0 +1,333 @@
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Stuff to do with the runtime's storage.
use rstd::prelude::*;
use codec::{FullCodec, FullEncode, Encode, EncodeAppend, EncodeLike, Decode};
use crate::traits::Len;
pub mod unhashed;
pub mod hashed;
pub mod child;
pub mod generator;
/// A trait for working with macro-generated storage values under the substrate storage API.
///
/// Details on implementation can be found at
/// [`generator::StorageValue`]
pub trait StorageValue<T: FullCodec> {
/// The type that get/take return.
type Query;
/// Get the storage key.
fn hashed_key() -> [u8; 16];
/// Does the value (explicitly) exist in storage?
fn exists() -> bool;
/// Load the value from the provided storage instance.
fn get() -> Self::Query;
/// Translate a value from some previous type (`O`) to the current type.
///
/// `f: F` is the translation function.
///
/// Returns `Err` if the storage item could not be interpreted as the old type, and Ok, along
/// with the new value if it could.
///
/// NOTE: This operates from and to `Option<_>` types; no effort is made to respect the default
/// value of the original type.
///
/// # Warning
///
/// This function must be used with care, before being updated the storage still contains the
/// old type, thus other calls (such as `get`) will fail at decoding it.
///
/// # Usage
///
/// This would typically be called inside the module implementation of on_initialize, while
/// ensuring **no usage of this storage are made before the call to `on_initialize`**. (More
/// precisely prior initialized modules doesn't make use of this storage).
fn translate<O: Decode, F: FnOnce(Option<O>) -> Option<T>>(f: F) -> Result<Option<T>, ()>;
/// Store a value under this key into the provided storage instance.
fn put<Arg: EncodeLike<T>>(val: Arg);
/// Mutate the value
fn mutate<R, F: FnOnce(&mut Self::Query) -> R>(f: F) -> R;
/// Clear the storage value.
fn kill();
/// Take a value from storage, removing it afterwards.
fn take() -> Self::Query;
/// Append the given item to the value in the storage.
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append<Items, Item, EncodeLikeItem>(items: Items) -> Result<(), &'static str>
where
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
T: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem>,
Items::IntoIter: ExactSizeIterator;
/// Append the given items to the value in the storage.
///
/// `T` is required to implement `Codec::EncodeAppend`.
///
/// Upon any failure, it replaces `items` as the new value (assuming that the previous stored
/// data is simply corrupt and no longer usable).
///
/// ### WARNING
///
/// use with care; if your use-case is not _exactly_ as what this function is doing,
/// you should use append and sensibly handle failure within the runtime code if it happens.
fn append_or_put<Items, Item, EncodeLikeItem>(items: Items) where
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
T: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<T>,
Items::IntoIter: ExactSizeIterator;
/// Read the length of the value in a fast way, without decoding the entire value.
///
/// `T` is required to implement `Codec::DecodeLength`.
fn decode_len() -> Result<usize, &'static str>
where T: codec::DecodeLength + Len;
}
/// A strongly-typed map in storage.
///
/// Details on implementation can be found at
/// [`generator::StorageMap`]
pub trait StorageMap<K: FullEncode, V: FullCodec> {
/// The type that get/take return.
type Query;
/// Get the storage key used to fetch a value corresponding to a specific key.
fn hashed_key_for<KeyArg: EncodeLike<K>>(key: KeyArg) -> Vec<u8>;
/// Does the value (explicitly) exist in storage?
fn exists<KeyArg: EncodeLike<K>>(key: KeyArg) -> bool;
/// Load the value associated with the given key from the map.
fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
/// Swap the values of two keys.
fn swap<KeyArg1: EncodeLike<K>, KeyArg2: EncodeLike<K>>(key1: KeyArg1, key2: KeyArg2);
/// Store a value to be associated with the given key from the map.
fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg);
/// Remove the value under a key.
fn remove<KeyArg: EncodeLike<K>>(key: KeyArg);
/// Mutate the value under a key.
fn mutate<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R;
/// Take the value under a key.
fn take<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
/// Append the given items to the value in the storage.
///
/// `V` is required to implement `codec::EncodeAppend`.
fn append<Items, Item, EncodeLikeItem, KeyArg>(key: KeyArg, items: Items) -> Result<(), &'static str>
where
KeyArg: EncodeLike<K>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem>,
Items::IntoIter: ExactSizeIterator;
/// Safely append the given items to the value in the storage. If a codec error occurs, then the
/// old (presumably corrupt) value is replaced with the given `items`.
///
/// `V` is required to implement `codec::EncodeAppend`.
fn append_or_insert<Items, Item, EncodeLikeItem, KeyArg>(key: KeyArg, items: Items)
where
KeyArg: EncodeLike<K>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
Items::IntoIter: ExactSizeIterator;
/// Read the length of the value in a fast way, without decoding the entire value.
///
/// `T` is required to implement `Codec::DecodeLength`.
///
/// Note that `0` is returned as the default value if no encoded value exists at the given key.
/// Therefore, this function cannot be used as a sign of _existence_. use the `::exists()`
/// function for this purpose.
fn decode_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<usize, &'static str>
where V: codec::DecodeLength + Len;
}
/// A strongly-typed linked map in storage.
///
/// Similar to `StorageMap` but allows to enumerate other elements and doesn't implement append.
///
/// Details on implementation can be found at
/// [`generator::StorageLinkedMap`]
pub trait StorageLinkedMap<K: FullCodec, V: FullCodec> {
/// The type that get/take return.
type Query;
/// The type that iterates over all `(key, value)`.
type Enumerator: Iterator<Item = (K, V)>;
/// Does the value (explicitly) exist in storage?
fn exists<KeyArg: EncodeLike<K>>(key: KeyArg) -> bool;
/// Load the value associated with the given key from the map.
fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
/// Swap the values of two keys.
fn swap<KeyArg1: EncodeLike<K>, KeyArg2: EncodeLike<K>>(key1: KeyArg1, key2: KeyArg2);
/// Store a value to be associated with the given key from the map.
fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg);
/// Remove the value under a key.
fn remove<KeyArg: EncodeLike<K>>(key: KeyArg);
/// Mutate the value under a key.
fn mutate<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R;
/// Take the value under a key.
fn take<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
/// Return current head element.
fn head() -> Option<K>;
/// Enumerate all elements in the map.
fn enumerate() -> Self::Enumerator;
/// Read the length of the value in a fast way, without decoding the entire value.
///
/// `T` is required to implement `Codec::DecodeLength`.
///
/// Note that `0` is returned as the default value if no encoded value exists at the given key.
/// Therefore, this function cannot be used as a sign of _existence_. use the `::exists()`
/// function for this purpose.
fn decode_len<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<usize, &'static str>
where V: codec::DecodeLength + Len;
/// Translate the keys and values from some previous `(K2, V2)` to the current type.
///
/// `TK` translates keys from the old type, and `TV` translates values.
///
/// Returns `Err` if the map could not be interpreted as the old type, and Ok if it could.
/// The `Err` contains the first key which could not be migrated, or `None` if the
/// head of the list could not be read.
///
/// # Warning
///
/// This function must be used with care, before being updated the storage still contains the
/// old type, thus other calls (such as `get`) will fail at decoding it.
///
/// # Usage
///
/// This would typically be called inside the module implementation of on_initialize, while
/// ensuring **no usage of this storage are made before the call to `on_initialize`**. (More
/// precisely prior initialized modules doesn't make use of this storage).
fn translate<K2, V2, TK, TV>(translate_key: TK, translate_val: TV) -> Result<(), Option<K2>>
where K2: FullCodec + Clone, V2: Decode, TK: Fn(K2) -> K, TV: Fn(V2) -> V;
}
/// An implementation of a map with a two keys.
///
/// It provides an important ability to efficiently remove all entries
/// that have a common first key.
///
/// Details on implementation can be found at
/// [`generator::StorageDoubleMap`]
pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
/// The type that get/take returns.
type Query;
fn hashed_key_for<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Vec<u8>
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;
fn exists<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> bool
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;
fn get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;
fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;
fn insert<KArg1, KArg2, VArg>(k1: KArg1, k2: KArg2, val: VArg)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
VArg: EncodeLike<V>;
fn remove<KArg1, KArg2>(k1: KArg1, k2: KArg2)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>;
fn remove_prefix<KArg1>(k1: KArg1) where KArg1: ?Sized + EncodeLike<K1>;
fn mutate<KArg1, KArg2, R, F>(k1: KArg1, k2: KArg2, f: F) -> R
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
F: FnOnce(&mut Self::Query) -> R;
fn append<Items, Item, EncodeLikeItem, KArg1, KArg2>(
k1: KArg1,
k2: KArg2,
items: Items,
) -> Result<(), &'static str>
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem>,
Items::IntoIter: ExactSizeIterator;
fn append_or_insert<Items, Item, EncodeLikeItem, KArg1, KArg2>(
k1: KArg1,
k2: KArg2,
items: Items,
)
where
KArg1: EncodeLike<K1>,
KArg2: EncodeLike<K2>,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: EncodeAppend<Item=Item>,
Items: IntoIterator<Item=EncodeLikeItem> + Clone + EncodeLike<V>,
Items::IntoIter: ExactSizeIterator;
}
@@ -0,0 +1,102 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Operation on unhashed runtime storage.
use rstd::prelude::*;
use codec::{Encode, Decode};
/// Return the value of the item in storage under `key`, or `None` if there is no explicit entry.
pub fn get<T: Decode + Sized>(key: &[u8]) -> Option<T> {
runtime_io::storage::get(key).map(|val| {
Decode::decode(&mut &val[..]).expect("storage is not null, therefore must be a valid type")
})
}
/// Return the value of the item in storage under `key`, or the type's default if there is no
/// explicit entry.
pub fn get_or_default<T: Decode + Sized + Default>(key: &[u8]) -> T {
get(key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry.
pub fn get_or<T: Decode + Sized>(key: &[u8], default_value: T) -> T {
get(key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry.
pub fn get_or_else<T: Decode + Sized, F: FnOnce() -> T>(key: &[u8], default_value: F) -> T {
get(key).unwrap_or_else(default_value)
}
/// Put `value` in storage under `key`.
pub fn put<T: Encode + ?Sized>(key: &[u8], value: &T) {
value.using_encoded(|slice| runtime_io::storage::set(key, slice));
}
/// Remove `key` from storage, returning its value if it had an explicit entry or `None` otherwise.
pub fn take<T: Decode + Sized>(key: &[u8]) -> Option<T> {
let r = get(key);
if r.is_some() {
kill(key);
}
r
}
/// Remove `key` from storage, returning its value, or, if there was no explicit entry in storage,
/// the default for its type.
pub fn take_or_default<T: Decode + Sized + Default>(key: &[u8]) -> T {
take(key).unwrap_or_else(Default::default)
}
/// Return the value of the item in storage under `key`, or `default_value` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or<T: Decode + Sized>(key: &[u8], default_value: T) -> T {
take(key).unwrap_or(default_value)
}
/// Return the value of the item in storage under `key`, or `default_value()` if there is no
/// explicit entry. Ensure there is no explicit entry on return.
pub fn take_or_else<T: Decode + Sized, F: FnOnce() -> T>(key: &[u8], default_value: F) -> T {
take(key).unwrap_or_else(default_value)
}
/// Check to see if `key` has an explicit entry in storage.
pub fn exists(key: &[u8]) -> bool {
runtime_io::storage::read(key, &mut [0;0][..], 0).is_some()
}
/// Ensure `key` has no explicit entry in storage.
pub fn kill(key: &[u8]) {
runtime_io::storage::clear(key);
}
/// Ensure keys with the given `prefix` have no entries in storage.
pub fn kill_prefix(prefix: &[u8]) {
runtime_io::storage::clear_prefix(prefix);
}
/// Get a Vec of bytes from storage.
pub fn get_raw(key: &[u8]) -> Option<Vec<u8>> {
runtime_io::storage::get(key)
}
/// Put a raw byte slice into storage.
pub fn put_raw(key: &[u8], value: &[u8]) {
runtime_io::storage::set(key, value)
}
+746
View File
@@ -0,0 +1,746 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Traits for SRML.
//!
//! NOTE: If you're looking for `parameter_types`, it has moved in to the top-level module.
use rstd::{prelude::*, result, marker::PhantomData, ops::Div, fmt::Debug};
use codec::{FullCodec, Codec, Encode, Decode};
use primitives::u32_trait::Value as U32;
use sr_primitives::{
ConsensusEngineId,
traits::{MaybeSerializeDeserialize, SimpleArithmetic, Saturating},
};
/// Anything that can have a `::len()` method.
pub trait Len {
/// Return the length of data type.
fn len(&self) -> usize;
}
impl<T: IntoIterator + Clone,> Len for T where <T as IntoIterator>::IntoIter: ExactSizeIterator {
fn len(&self) -> usize {
self.clone().into_iter().len()
}
}
/// A trait for querying a single fixed value from a type.
pub trait Get<T> {
/// Return a constant value.
fn get() -> T;
}
impl<T: Default> Get<T> for () {
fn get() -> T { T::default() }
}
/// A trait for querying whether a type can be said to statically "contain" a value. Similar
/// in nature to `Get`, except it is designed to be lazy rather than active (you can't ask it to
/// enumerate all values that it contains) and work for multiple values rather than just one.
pub trait Contains<T> {
/// Return `true` if this "contains" the given value `t`.
fn contains(t: &T) -> bool;
}
impl<V: PartialEq, T: Get<V>> Contains<V> for T {
fn contains(t: &V) -> bool {
&Self::get() == t
}
}
/// The account with the given id was killed.
#[impl_trait_for_tuples::impl_for_tuples(30)]
pub trait OnFreeBalanceZero<AccountId> {
/// The account was the given id was killed.
fn on_free_balance_zero(who: &AccountId);
}
/// Outcome of a balance update.
pub enum UpdateBalanceOutcome {
/// Account balance was simply updated.
Updated,
/// The update led to killing the account.
AccountKilled,
}
/// A trait for finding the author of a block header based on the `PreRuntime` digests contained
/// within it.
pub trait FindAuthor<Author> {
/// Find the author of a block based on the pre-runtime digests.
fn find_author<'a, I>(digests: I) -> Option<Author>
where I: 'a + IntoIterator<Item=(ConsensusEngineId, &'a [u8])>;
}
impl<A> FindAuthor<A> for () {
fn find_author<'a, I>(_: I) -> Option<A>
where I: 'a + IntoIterator<Item=(ConsensusEngineId, &'a [u8])>
{
None
}
}
/// A trait for verifying the seal of a header and returning the author.
pub trait VerifySeal<Header, Author> {
/// Verify a header and return the author, if any.
fn verify_seal(header: &Header) -> Result<Option<Author>, &'static str>;
}
/// Something which can compute and check proofs of
/// a historical key owner and return full identification data of that
/// key owner.
pub trait KeyOwnerProofSystem<Key> {
/// The proof of membership itself.
type Proof: Codec;
/// The full identification of a key owner and the stash account.
type IdentificationTuple: Codec;
/// Prove membership of a key owner in the current block-state.
///
/// This should typically only be called off-chain, since it may be
/// computationally heavy.
///
/// Returns `Some` iff the key owner referred to by the given `key` is a
/// member of the current set.
fn prove(key: Key) -> Option<Self::Proof>;
/// Check a proof of membership on-chain. Return `Some` iff the proof is
/// valid and recent enough to check.
fn check_proof(key: Key, proof: Self::Proof) -> Option<Self::IdentificationTuple>;
}
/// Handler for when some currency "account" decreased in balance for
/// some reason.
///
/// The only reason at present for an increase would be for validator rewards, but
/// there may be other reasons in the future or for other chains.
///
/// Reasons for decreases include:
///
/// - Someone got slashed.
/// - Someone paid for a transaction to be included.
pub trait OnUnbalanced<Imbalance: TryDrop> {
/// Handler for some imbalance. Infallible.
fn on_unbalanced(amount: Imbalance) {
amount.try_drop().unwrap_or_else(Self::on_nonzero_unbalanced)
}
/// Actually handle a non-zero imbalance. You probably want to implement this rather than
/// `on_unbalanced`.
fn on_nonzero_unbalanced(amount: Imbalance);
}
impl<Imbalance: TryDrop> OnUnbalanced<Imbalance> for () {
fn on_nonzero_unbalanced(amount: Imbalance) { drop(amount); }
}
/// Simple boolean for whether an account needs to be kept in existence.
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ExistenceRequirement {
/// Operation must not result in the account going out of existence.
///
/// Note this implies that if the account never existed in the first place, then the operation
/// may legitimately leave the account unchanged and still non-existent.
KeepAlive,
/// Operation may result in account going out of existence.
AllowDeath,
}
/// A type for which some values make sense to be able to drop without further consideration.
pub trait TryDrop: Sized {
/// Drop an instance cleanly. Only works if its value represents "no-operation".
fn try_drop(self) -> Result<(), Self>;
}
/// A trait for a not-quite Linear Type that tracks an imbalance.
///
/// Functions that alter account balances return an object of this trait to
/// express how much account balances have been altered in aggregate. If
/// dropped, the currency system will take some default steps to deal with
/// the imbalance (`balances` module simply reduces or increases its
/// total issuance). Your module should generally handle it in some way,
/// good practice is to do so in a configurable manner using an
/// `OnUnbalanced` type for each situation in which your module needs to
/// handle an imbalance.
///
/// Imbalances can either be Positive (funds were added somewhere without
/// being subtracted elsewhere - e.g. a reward) or Negative (funds deducted
/// somewhere without an equal and opposite addition - e.g. a slash or
/// system fee payment).
///
/// Since they are unsigned, the actual type is always Positive or Negative.
/// The trait makes no distinction except to define the `Opposite` type.
///
/// New instances of zero value can be created (`zero`) and destroyed
/// (`drop_zero`).
///
/// Existing instances can be `split` and merged either consuming `self` with
/// `merge` or mutating `self` with `subsume`. If the target is an `Option`,
/// then `maybe_merge` and `maybe_subsume` might work better. Instances can
/// also be `offset` with an `Opposite` that is less than or equal to in value.
///
/// You can always retrieve the raw balance value using `peek`.
#[must_use]
pub trait Imbalance<Balance>: Sized + TryDrop {
/// The oppositely imbalanced type. They come in pairs.
type Opposite: Imbalance<Balance>;
/// The zero imbalance. Can be destroyed with `drop_zero`.
fn zero() -> Self;
/// Drop an instance cleanly. Only works if its `self.value()` is zero.
fn drop_zero(self) -> Result<(), Self>;
/// Consume `self` and return two independent instances; the first
/// is guaranteed to be at most `amount` and the second will be the remainder.
fn split(self, amount: Balance) -> (Self, Self);
/// Consume `self` and an `other` to return a new instance that combines
/// both.
fn merge(self, other: Self) -> Self;
/// Consume `self` and maybe an `other` to return a new instance that combines
/// both.
fn maybe_merge(self, other: Option<Self>) -> Self {
if let Some(o) = other {
self.merge(o)
} else {
self
}
}
/// Consume an `other` to mutate `self` into a new instance that combines
/// both.
fn subsume(&mut self, other: Self);
/// Maybe consume an `other` to mutate `self` into a new instance that combines
/// both.
fn maybe_subsume(&mut self, other: Option<Self>) {
if let Some(o) = other {
self.subsume(o)
}
}
/// Consume self and along with an opposite counterpart to return
/// a combined result.
///
/// Returns `Ok` along with a new instance of `Self` if this instance has a
/// greater value than the `other`. Otherwise returns `Err` with an instance of
/// the `Opposite`. In both cases the value represents the combination of `self`
/// and `other`.
fn offset(self, other: Self::Opposite) -> Result<Self, Self::Opposite>;
/// The raw value of self.
fn peek(&self) -> Balance;
}
/// Either a positive or a negative imbalance.
pub enum SignedImbalance<B, P: Imbalance<B>>{
/// A positive imbalance (funds have been created but none destroyed).
Positive(P),
/// A negative imbalance (funds have been destroyed but none created).
Negative(P::Opposite),
}
impl<
P: Imbalance<B, Opposite=N>,
N: Imbalance<B, Opposite=P>,
B: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
> SignedImbalance<B, P> {
pub fn zero() -> Self {
SignedImbalance::Positive(P::zero())
}
pub fn drop_zero(self) -> Result<(), Self> {
match self {
SignedImbalance::Positive(x) => x.drop_zero().map_err(SignedImbalance::Positive),
SignedImbalance::Negative(x) => x.drop_zero().map_err(SignedImbalance::Negative),
}
}
/// Consume `self` and an `other` to return a new instance that combines
/// both.
pub fn merge(self, other: Self) -> Self {
match (self, other) {
(SignedImbalance::Positive(one), SignedImbalance::Positive(other)) =>
SignedImbalance::Positive(one.merge(other)),
(SignedImbalance::Negative(one), SignedImbalance::Negative(other)) =>
SignedImbalance::Negative(one.merge(other)),
(SignedImbalance::Positive(one), SignedImbalance::Negative(other)) =>
if one.peek() > other.peek() {
SignedImbalance::Positive(one.offset(other).ok().unwrap_or_else(P::zero))
} else {
SignedImbalance::Negative(other.offset(one).ok().unwrap_or_else(N::zero))
},
(one, other) => other.merge(one),
}
}
}
/// Split an unbalanced amount two ways between a common divisor.
pub struct SplitTwoWays<
Balance,
Imbalance,
Part1,
Target1,
Part2,
Target2,
>(PhantomData<(Balance, Imbalance, Part1, Target1, Part2, Target2)>);
impl<
Balance: From<u32> + Saturating + Div<Output=Balance>,
I: Imbalance<Balance>,
Part1: U32,
Target1: OnUnbalanced<I>,
Part2: U32,
Target2: OnUnbalanced<I>,
> OnUnbalanced<I> for SplitTwoWays<Balance, I, Part1, Target1, Part2, Target2>
{
fn on_nonzero_unbalanced(amount: I) {
let total: u32 = Part1::VALUE + Part2::VALUE;
let amount1 = amount.peek().saturating_mul(Part1::VALUE.into()) / total.into();
let (imb1, imb2) = amount.split(amount1);
Target1::on_unbalanced(imb1);
Target2::on_unbalanced(imb2);
}
}
/// Abstraction over a fungible assets system.
pub trait Currency<AccountId> {
/// The balance of an account.
type Balance: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default;
/// The opaque token type for an imbalance. This is returned by unbalanced operations
/// and must be dealt with. It may be dropped but cannot be cloned.
type PositiveImbalance: Imbalance<Self::Balance, Opposite=Self::NegativeImbalance>;
/// The opaque token type for an imbalance. This is returned by unbalanced operations
/// and must be dealt with. It may be dropped but cannot be cloned.
type NegativeImbalance: Imbalance<Self::Balance, Opposite=Self::PositiveImbalance>;
// PUBLIC IMMUTABLES
/// The combined balance of `who`.
fn total_balance(who: &AccountId) -> Self::Balance;
/// Same result as `slash(who, value)` (but without the side-effects) assuming there are no
/// balance changes in the meantime and only the reserved balance is not taken into account.
fn can_slash(who: &AccountId, value: Self::Balance) -> bool;
/// The total amount of issuance in the system.
fn total_issuance() -> Self::Balance;
/// The minimum balance any single account may have. This is equivalent to the `Balances` module's
/// `ExistentialDeposit`.
fn minimum_balance() -> Self::Balance;
/// Reduce the total issuance by `amount` and return the according imbalance. The imbalance will
/// typically be used to reduce an account by the same amount with e.g. `settle`.
///
/// This is infallible, but doesn't guarantee that the entire `amount` is burnt, for example
/// in the case of underflow.
fn burn(amount: Self::Balance) -> Self::PositiveImbalance;
/// Increase the total issuance by `amount` and return the according imbalance. The imbalance
/// will typically be used to increase an account by the same amount with e.g.
/// `resolve_into_existing` or `resolve_creating`.
///
/// This is infallible, but doesn't guarantee that the entire `amount` is issued, for example
/// in the case of overflow.
fn issue(amount: Self::Balance) -> Self::NegativeImbalance;
/// The 'free' balance of a given account.
///
/// This is the only balance that matters in terms of most operations on tokens. It alone
/// is used to determine the balance when in the contract execution environment. When this
/// balance falls below the value of `ExistentialDeposit`, then the 'current account' is
/// deleted: specifically `FreeBalance`. Further, the `OnFreeBalanceZero` callback
/// is invoked, giving a chance to external modules to clean up data associated with
/// the deleted account.
///
/// `system::AccountNonce` is also deleted if `ReservedBalance` is also zero (it also gets
/// collapsed to zero if it ever becomes less than `ExistentialDeposit`.
fn free_balance(who: &AccountId) -> Self::Balance;
/// Returns `Ok` iff the account is able to make a withdrawal of the given amount
/// for the given reason. Basically, it's just a dry-run of `withdraw`.
///
/// `Err(...)` with the reason why not otherwise.
fn ensure_can_withdraw(
who: &AccountId,
_amount: Self::Balance,
reasons: WithdrawReasons,
new_balance: Self::Balance,
) -> result::Result<(), &'static str>;
// PUBLIC MUTABLES (DANGEROUS)
/// Transfer some liquid free balance to another staker.
///
/// This is a very high-level function. It will ensure all appropriate fees are paid
/// and no imbalance in the system remains.
fn transfer(
source: &AccountId,
dest: &AccountId,
value: Self::Balance,
existence_requirement: ExistenceRequirement,
) -> result::Result<(), &'static str>;
/// Deducts up to `value` from the combined balance of `who`, preferring to deduct from the
/// free balance. This function cannot fail.
///
/// The resulting imbalance is the first item of the tuple returned.
///
/// As much funds up to `value` will be deducted as possible. If this is less than `value`,
/// then a non-zero second item will be returned.
fn slash(
who: &AccountId,
value: Self::Balance
) -> (Self::NegativeImbalance, Self::Balance);
/// Mints `value` to the free balance of `who`.
///
/// If `who` doesn't exist, nothing is done and an Err returned.
fn deposit_into_existing(
who: &AccountId,
value: Self::Balance
) -> result::Result<Self::PositiveImbalance, &'static str>;
/// Similar to deposit_creating, only accepts a `NegativeImbalance` and returns nothing on
/// success.
fn resolve_into_existing(
who: &AccountId,
value: Self::NegativeImbalance,
) -> result::Result<(), Self::NegativeImbalance> {
let v = value.peek();
match Self::deposit_into_existing(who, v) {
Ok(opposite) => Ok(drop(value.offset(opposite))),
_ => Err(value),
}
}
/// Adds up to `value` to the free balance of `who`. If `who` doesn't exist, it is created.
///
/// Infallible.
fn deposit_creating(
who: &AccountId,
value: Self::Balance,
) -> Self::PositiveImbalance;
/// Similar to deposit_creating, only accepts a `NegativeImbalance` and returns nothing on
/// success.
fn resolve_creating(
who: &AccountId,
value: Self::NegativeImbalance,
) {
let v = value.peek();
drop(value.offset(Self::deposit_creating(who, v)));
}
/// Removes some free balance from `who` account for `reason` if possible. If `liveness` is
/// `KeepAlive`, then no less than `ExistentialDeposit` must be left remaining.
///
/// This checks any locks, vesting, and liquidity requirements. If the removal is not possible,
/// then it returns `Err`.
///
/// If the operation is successful, this will return `Ok` with a `NegativeImbalance` whose value
/// is `value`.
fn withdraw(
who: &AccountId,
value: Self::Balance,
reasons: WithdrawReasons,
liveness: ExistenceRequirement,
) -> result::Result<Self::NegativeImbalance, &'static str>;
/// Similar to withdraw, only accepts a `PositiveImbalance` and returns nothing on success.
fn settle(
who: &AccountId,
value: Self::PositiveImbalance,
reasons: WithdrawReasons,
liveness: ExistenceRequirement,
) -> result::Result<(), Self::PositiveImbalance> {
let v = value.peek();
match Self::withdraw(who, v, reasons, liveness) {
Ok(opposite) => Ok(drop(value.offset(opposite))),
_ => Err(value),
}
}
/// Ensure an account's free balance equals some value; this will create the account
/// if needed.
///
/// Returns a signed imbalance and status to indicate if the account was successfully updated or update
/// has led to killing of the account.
fn make_free_balance_be(
who: &AccountId,
balance: Self::Balance,
) -> (
SignedImbalance<Self::Balance, Self::PositiveImbalance>,
UpdateBalanceOutcome,
);
}
/// A currency where funds can be reserved from the user.
pub trait ReservableCurrency<AccountId>: Currency<AccountId> {
/// Same result as `reserve(who, value)` (but without the side-effects) assuming there
/// are no balance changes in the meantime.
fn can_reserve(who: &AccountId, value: Self::Balance) -> bool;
/// Deducts up to `value` from reserved balance of `who`. This function cannot fail.
///
/// As much funds up to `value` will be deducted as possible. If the reserve balance of `who`
/// is less than `value`, then a non-zero second item will be returned.
fn slash_reserved(
who: &AccountId,
value: Self::Balance
) -> (Self::NegativeImbalance, Self::Balance);
/// The amount of the balance of a given account that is externally reserved; this can still get
/// slashed, but gets slashed last of all.
///
/// This balance is a 'reserve' balance that other subsystems use in order to set aside tokens
/// that are still 'owned' by the account holder, but which are suspendable.
///
/// When this balance falls below the value of `ExistentialDeposit`, then this 'reserve account'
/// is deleted: specifically, `ReservedBalance`.
///
/// `system::AccountNonce` is also deleted if `FreeBalance` is also zero (it also gets
/// collapsed to zero if it ever becomes less than `ExistentialDeposit`.
fn reserved_balance(who: &AccountId) -> Self::Balance;
/// Moves `value` from balance to reserved balance.
///
/// If the free balance is lower than `value`, then no funds will be moved and an `Err` will
/// be returned to notify of this. This is different behavior than `unreserve`.
fn reserve(who: &AccountId, value: Self::Balance) -> result::Result<(), &'static str>;
/// Moves up to `value` from reserved balance to free balance. This function cannot fail.
///
/// As much funds up to `value` will be moved as possible. If the reserve balance of `who`
/// is less than `value`, then the remaining amount will be returned.
///
/// # NOTES
///
/// - This is different from `reserve`.
/// - If the remaining reserved balance is less than `ExistentialDeposit`, it will
/// invoke `on_reserved_too_low` and could reap the account.
fn unreserve(who: &AccountId, value: Self::Balance) -> Self::Balance;
/// Moves up to `value` from reserved balance of account `slashed` to free balance of account
/// `beneficiary`. `beneficiary` must exist for this to succeed. If it does not, `Err` will be
/// returned.
///
/// As much funds up to `value` will be deducted as possible. If this is less than `value`,
/// then `Ok(non_zero)` will be returned.
fn repatriate_reserved(
slashed: &AccountId,
beneficiary: &AccountId,
value: Self::Balance
) -> result::Result<Self::Balance, &'static str>;
}
/// An identifier for a lock. Used for disambiguating different locks so that
/// they can be individually replaced or removed.
pub type LockIdentifier = [u8; 8];
/// A currency whose accounts can have liquidity restrictions.
pub trait LockableCurrency<AccountId>: Currency<AccountId> {
/// The quantity used to denote time; usually just a `BlockNumber`.
type Moment;
/// Create a new balance lock on account `who`.
///
/// If the new lock is valid (i.e. not already expired), it will push the struct to
/// the `Locks` vec in storage. Note that you can lock more funds than a user has.
///
/// If the lock `id` already exists, this will update it.
fn set_lock(
id: LockIdentifier,
who: &AccountId,
amount: Self::Balance,
until: Self::Moment,
reasons: WithdrawReasons,
);
/// Changes a balance lock (selected by `id`) so that it becomes less liquid in all
/// parameters or creates a new one if it does not exist.
///
/// Calling `extend_lock` on an existing lock `id` differs from `set_lock` in that it
/// applies the most severe constraints of the two, while `set_lock` replaces the lock
/// with the new parameters. As in, `extend_lock` will set:
/// - maximum `amount`
/// - farthest duration (`until`)
/// - bitwise mask of all `reasons`
fn extend_lock(
id: LockIdentifier,
who: &AccountId,
amount: Self::Balance,
until: Self::Moment,
reasons: WithdrawReasons,
);
/// Remove an existing lock.
fn remove_lock(
id: LockIdentifier,
who: &AccountId,
);
}
bitmask! {
/// Reasons for moving funds out of an account.
#[derive(Encode, Decode)]
pub mask WithdrawReasons: i8 where
/// Reason for moving funds out of an account.
#[derive(Encode, Decode)]
flags WithdrawReason {
/// In order to pay for (system) transaction costs.
TransactionPayment = 0b00000001,
/// In order to transfer ownership.
Transfer = 0b00000010,
/// In order to reserve some funds for a later return or repatriation
Reserve = 0b00000100,
/// In order to pay some other (higher-level) fees.
Fee = 0b00001000,
/// In order to tip a validator for transaction inclusion.
Tip = 0b00010000,
}
}
pub trait Time {
type Moment: SimpleArithmetic + FullCodec + Clone + Default + Copy;
fn now() -> Self::Moment;
}
impl WithdrawReasons {
/// Choose all variants except for `one`.
///
/// ```rust
/// # use palette_support::traits::{WithdrawReason, WithdrawReasons};
/// # fn main() {
/// assert_eq!(
/// WithdrawReason::Fee | WithdrawReason::Transfer | WithdrawReason::Reserve | WithdrawReason::Tip,
/// WithdrawReasons::except(WithdrawReason::TransactionPayment),
/// );
/// # }
/// ```
pub fn except(one: WithdrawReason) -> WithdrawReasons {
let mut mask = Self::all();
mask.toggle(one);
mask
}
}
/// Trait for type that can handle incremental changes to a set of account IDs.
pub trait ChangeMembers<AccountId: Clone + Ord> {
/// A number of members `incoming` just joined the set and replaced some `outgoing` ones. The
/// new set is given by `new`, and need not be sorted.
fn change_members(incoming: &[AccountId], outgoing: &[AccountId], mut new: Vec<AccountId>) {
new.sort_unstable();
Self::change_members_sorted(incoming, outgoing, &new[..]);
}
/// A number of members `_incoming` just joined the set and replaced some `_outgoing` ones. The
/// new set is thus given by `sorted_new` and **must be sorted**.
///
/// NOTE: This is the only function that needs to be implemented in `ChangeMembers`.
fn change_members_sorted(
incoming: &[AccountId],
outgoing: &[AccountId],
sorted_new: &[AccountId],
);
/// Set the new members; they **must already be sorted**. This will compute the diff and use it to
/// call `change_members_sorted`.
fn set_members_sorted(new_members: &[AccountId], old_members: &[AccountId]) {
let (incoming, outgoing) = Self::compute_members_diff(new_members, old_members);
Self::change_members_sorted(&incoming[..], &outgoing[..], &new_members);
}
/// Set the new members; they **must already be sorted**. This will compute the diff and use it to
/// call `change_members_sorted`.
fn compute_members_diff(
new_members: &[AccountId],
old_members: &[AccountId]
) -> (Vec<AccountId>, Vec<AccountId>) {
let mut old_iter = old_members.iter();
let mut new_iter = new_members.iter();
let mut incoming = Vec::new();
let mut outgoing = Vec::new();
let mut old_i = old_iter.next();
let mut new_i = new_iter.next();
loop {
match (old_i, new_i) {
(None, None) => break,
(Some(old), Some(new)) if old == new => {
old_i = old_iter.next();
new_i = new_iter.next();
}
(Some(old), Some(new)) if old < new => {
outgoing.push(old.clone());
old_i = old_iter.next();
}
(Some(old), None) => {
outgoing.push(old.clone());
old_i = old_iter.next();
}
(_, Some(new)) => {
incoming.push(new.clone());
new_i = new_iter.next();
}
}
}
(incoming, outgoing)
}
}
impl<T: Clone + Ord> ChangeMembers<T> for () {
fn change_members(_: &[T], _: &[T], _: Vec<T>) {}
fn change_members_sorted(_: &[T], _: &[T], _: &[T]) {}
fn set_members_sorted(_: &[T], _: &[T]) {}
}
/// Trait for type that can handle the initialization of account IDs at genesis.
pub trait InitializeMembers<AccountId> {
/// Initialize the members to the given `members`.
fn initialize_members(members: &[AccountId]);
}
impl<T> InitializeMembers<T> for () {
fn initialize_members(_: &[T]) {}
}
// A trait that is able to provide randomness.
pub trait Randomness<Output> {
/// Get a "random" value
///
/// Being a deterministic blockchain, real randomness is difficult to come by. This gives you
/// something that approximates it. `subject` is a context identifier and allows you to get a
/// different result to other callers of this function; use it like
/// `random(&b"my context"[..])`.
fn random(subject: &[u8]) -> Output;
/// Get the basic random seed.
///
/// In general you won't want to use this, but rather `Self::random` which allows you to give a
/// subject for the random result and whose value will be independently low-influence random
/// from any other such seeds.
fn random_seed() -> Output {
Self::random(&[][..])
}
}
+166
View File
@@ -0,0 +1,166 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
#[doc(hidden)]
#[allow(deprecated)]
pub use crate::sr_primitives::traits::ValidateUnsigned;
#[doc(hidden)]
pub use crate::sr_primitives::transaction_validity::{TransactionValidity, UnknownTransaction};
#[doc(hidden)]
pub use crate::sr_primitives::ApplyError;
/// Implement `ValidateUnsigned` for `Runtime`.
/// All given modules need to implement `ValidateUnsigned`.
///
/// # Example
///
/// ```
/// # mod timestamp {
/// # pub struct Module;
/// #
/// # impl palette_support::unsigned::ValidateUnsigned for Module {
/// # type Call = Call;
/// #
/// # fn validate_unsigned(call: &Self::Call) -> palette_support::unsigned::TransactionValidity {
/// # unimplemented!();
/// # }
/// # }
/// #
/// # pub enum Call {
/// # }
/// # }
/// #
/// # pub type Timestamp = timestamp::Module;
/// #
/// #
/// # pub enum Call {
/// # Timestamp(timestamp::Call),
/// # }
/// # #[allow(unused)]
/// pub struct Runtime;
///
/// palette_support::impl_outer_validate_unsigned! {
/// impl ValidateUnsigned for Runtime {
/// Timestamp
/// }
/// }
/// ```
#[macro_export]
macro_rules! impl_outer_validate_unsigned {
(
impl ValidateUnsigned for $runtime:ident {
$( $module:ident )*
}
) => {
#[allow(deprecated)] // Allow ValidateUnsigned
impl $crate::unsigned::ValidateUnsigned for $runtime {
type Call = Call;
fn pre_dispatch(call: &Self::Call) -> Result<(), $crate::unsigned::ApplyError> {
#[allow(unreachable_patterns)]
match call {
$( Call::$module(inner_call) => $module::pre_dispatch(inner_call), )*
// pre-dispatch should not stop inherent extrinsics, validation should prevent
// including arbitrary (non-inherent) extrinsics to blocks.
_ => Ok(()),
}
}
fn validate_unsigned(call: &Self::Call) -> $crate::unsigned::TransactionValidity {
#[allow(unreachable_patterns)]
match call {
$( Call::$module(inner_call) => $module::validate_unsigned(inner_call), )*
_ => $crate::unsigned::UnknownTransaction::NoUnsignedValidator.into(),
}
}
}
};
}
#[cfg(test)]
mod test_empty_call {
pub enum Call {}
#[allow(unused)]
pub struct Runtime;
impl_outer_validate_unsigned! {
impl ValidateUnsigned for Runtime {
}
}
}
#[cfg(test)]
mod test_partial_and_full_call {
pub mod timestamp {
pub struct Module;
#[allow(deprecated)] // Allow ValidateUnsigned
impl super::super::ValidateUnsigned for Module {
type Call = Call;
fn validate_unsigned(_call: &Self::Call) -> super::super::TransactionValidity {
unimplemented!();
}
}
pub enum Call {
Foo,
}
}
mod test_full_unsigned {
pub type Timestamp = super::timestamp::Module;
pub enum Call {
Timestamp(super::timestamp::Call),
}
pub struct Runtime;
impl_outer_validate_unsigned! {
impl ValidateUnsigned for Runtime {
Timestamp
}
}
#[test]
fn used() {
let _ = Call::Timestamp(super::timestamp::Call::Foo);
let _ = Runtime;
}
}
mod test_not_full_unsigned {
pub enum Call {
Timestamp(super::timestamp::Call),
}
pub struct Runtime;
impl_outer_validate_unsigned! {
impl ValidateUnsigned for Runtime {
}
}
#[test]
fn used() {
let _ = Call::Timestamp(super::timestamp::Call::Foo);
let _ = Runtime;
}
}
}