mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Utility module for doing stuff like batch calls (#3759)
* Implement and test batch * Add files. * Remove comments. * Update srml/utility/src/lib.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Fixes
This commit is contained in:
Generated
+16
@@ -2486,6 +2486,7 @@ dependencies = [
|
||||
"srml-system 2.0.0",
|
||||
"srml-timestamp 2.0.0",
|
||||
"srml-treasury 2.0.0",
|
||||
"srml-utility 2.0.0",
|
||||
"substrate-authority-discovery-primitives 2.0.0",
|
||||
"substrate-client 2.0.0",
|
||||
"substrate-consensus-babe-primitives 2.0.0",
|
||||
@@ -4421,6 +4422,21 @@ dependencies = [
|
||||
"substrate-primitives 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "srml-utility"
|
||||
version = "2.0.0"
|
||||
dependencies = [
|
||||
"parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sr-io 2.0.0",
|
||||
"sr-primitives 2.0.0",
|
||||
"sr-std 2.0.0",
|
||||
"srml-balances 2.0.0",
|
||||
"srml-support 2.0.0",
|
||||
"srml-system 2.0.0",
|
||||
"substrate-primitives 2.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stable_deref_trait"
|
||||
version = "1.1.1"
|
||||
|
||||
@@ -103,6 +103,7 @@ members = [
|
||||
"srml/system",
|
||||
"srml/timestamp",
|
||||
"srml/treasury",
|
||||
"srml/utility",
|
||||
"node/cli",
|
||||
"node/executor",
|
||||
"node/primitives",
|
||||
|
||||
@@ -48,6 +48,7 @@ support = { package = "srml-support", path = "../../srml/support", default-featu
|
||||
system = { package = "srml-system", path = "../../srml/system", default-features = false }
|
||||
timestamp = { package = "srml-timestamp", path = "../../srml/timestamp", default-features = false }
|
||||
treasury = { package = "srml-treasury", path = "../../srml/treasury", default-features = false }
|
||||
utility = { package = "srml-utility", path = "../../srml/utility", default-features = false }
|
||||
|
||||
[build-dependencies]
|
||||
wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.2", path = "../../core/utils/wasm-builder-runner" }
|
||||
@@ -92,5 +93,6 @@ std = [
|
||||
"system/std",
|
||||
"timestamp/std",
|
||||
"treasury/std",
|
||||
"utility/std",
|
||||
"version/std",
|
||||
]
|
||||
|
||||
@@ -134,6 +134,11 @@ impl system::Trait for Runtime {
|
||||
type Version = Version;
|
||||
}
|
||||
|
||||
impl utility::Trait for Runtime {
|
||||
type Event = Event;
|
||||
type Call = Call;
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS;
|
||||
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
|
||||
@@ -492,6 +497,7 @@ construct_runtime!(
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: system::{Module, Call, Storage, Config, Event},
|
||||
Utility: utility::{Module, Call, Event<T>},
|
||||
Babe: babe::{Module, Call, Storage, Config, Inherent(Timestamp)},
|
||||
Timestamp: timestamp::{Module, Call, Storage, Inherent},
|
||||
Authorship: authorship::{Module, Call, Storage, Inherent},
|
||||
|
||||
@@ -196,10 +196,11 @@ decl_module! {
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
pub enum Event<T>
|
||||
where <T as system::Trait>::AccountId,
|
||||
<T as Trait>::Balance,
|
||||
<T as Trait>::AssetId {
|
||||
pub enum Event<T> where
|
||||
<T as system::Trait>::AccountId,
|
||||
<T as Trait>::Balance,
|
||||
<T as Trait>::AssetId,
|
||||
{
|
||||
/// Some assets were issued.
|
||||
Issued(AssetId, AccountId, Balance),
|
||||
/// Some assets were transferred.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
[package]
|
||||
name = "srml-utility"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", optional = true }
|
||||
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false }
|
||||
support = { package = "srml-support", path = "../support", default-features = false }
|
||||
system = { package = "srml-system", path = "../system", default-features = false }
|
||||
sr-primitives = { path = "../../core/sr-primitives", default-features = false }
|
||||
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }
|
||||
runtime-io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
|
||||
balances = { package = "srml-balances", path = "../balances" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"serde",
|
||||
"codec/std",
|
||||
"sr-primitives/std",
|
||||
"support/std",
|
||||
"system/std",
|
||||
"runtime-io/std",
|
||||
"rstd/std"
|
||||
]
|
||||
@@ -0,0 +1,173 @@
|
||||
// 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/>.
|
||||
|
||||
//! # Utility Module
|
||||
//! A module full of useful helpers for practical chain management.
|
||||
|
||||
// Ensure we're `no_std` when compiling for Wasm.
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use rstd::prelude::*;
|
||||
use support::{decl_module, decl_event, Parameter};
|
||||
use system::ensure_root;
|
||||
use sr_primitives::{
|
||||
traits::{Dispatchable, DispatchResult}, weights::SimpleDispatchInfo
|
||||
};
|
||||
|
||||
/// Configuration trait.
|
||||
pub trait Trait: system::Trait {
|
||||
/// The overarching event type.
|
||||
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
||||
|
||||
/// The overarching call type.
|
||||
type Call: Parameter + Dispatchable<Origin=Self::Origin>;
|
||||
}
|
||||
|
||||
pub type DispatchResultOf<T> = DispatchResult<<<T as Trait>::Call as Dispatchable>::Error>;
|
||||
|
||||
decl_event!(
|
||||
/// Events type.
|
||||
pub enum Event<T> where
|
||||
DispatchResult = DispatchResultOf<T>,
|
||||
{
|
||||
BatchExecuted(Vec<DispatchResult>),
|
||||
}
|
||||
);
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
/// Deposit one of this module's events by using the default implementation.
|
||||
fn deposit_event() = default;
|
||||
|
||||
/// Send a batch of dispatch calls (only root).
|
||||
#[weight = SimpleDispatchInfo::FreeOperational]
|
||||
fn batch(origin, calls: Vec<<T as Trait>::Call>) {
|
||||
ensure_root(origin)?;
|
||||
let results = calls.into_iter()
|
||||
.map(|call| call.dispatch(system::RawOrigin::Root.into()))
|
||||
.collect::<Vec<_>>();
|
||||
Self::deposit_event(RawEvent::BatchExecuted(results));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use support::{assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch};
|
||||
use runtime_io::with_externalities;
|
||||
use primitives::{H256, Blake2Hasher};
|
||||
use sr_primitives::{
|
||||
Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header
|
||||
};
|
||||
|
||||
impl_outer_origin! {
|
||||
pub enum Origin for Test {}
|
||||
}
|
||||
|
||||
impl_outer_dispatch! {
|
||||
pub enum Call for Test where origin: Origin {
|
||||
balances::Balances,
|
||||
utility::Utility,
|
||||
}
|
||||
}
|
||||
|
||||
// For testing the module, we construct most of a mock runtime. This means
|
||||
// first constructing a configuration type (`Test`) which `impl`s each of the
|
||||
// configuration traits of modules we want to use.
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Test;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 250;
|
||||
pub const MaximumBlockWeight: u32 = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
}
|
||||
impl system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = H256;
|
||||
type Call = Call;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type WeightMultiplierUpdate = ();
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: u64 = 0;
|
||||
pub const TransferFee: u64 = 0;
|
||||
pub const CreationFee: u64 = 0;
|
||||
pub const TransactionBaseFee: u64 = 0;
|
||||
pub const TransactionByteFee: u64 = 0;
|
||||
}
|
||||
impl balances::Trait for Test {
|
||||
type Balance = u64;
|
||||
type OnFreeBalanceZero = ();
|
||||
type OnNewAccount = ();
|
||||
type Event = ();
|
||||
type TransactionPayment = ();
|
||||
type TransferPayment = ();
|
||||
type DustRemoval = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type TransferFee = TransferFee;
|
||||
type CreationFee = CreationFee;
|
||||
type TransactionBaseFee = TransactionBaseFee;
|
||||
type TransactionByteFee = TransactionByteFee;
|
||||
type WeightToFee = ();
|
||||
}
|
||||
impl Trait for Test {
|
||||
type Event = ();
|
||||
type Call = Call;
|
||||
}
|
||||
type Balances = balances::Module<Test>;
|
||||
type Utility = Module<Test>;
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
balances::GenesisConfig::<Test> {
|
||||
balances: vec![(1, 10), (2, 0)],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_works() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
assert_eq!(Balances::free_balance(1), 10);
|
||||
assert_eq!(Balances::free_balance(2), 0);
|
||||
assert_noop!(Utility::batch(Origin::signed(1), vec![
|
||||
Call::Balances(balances::Call::force_transfer(1, 2, 5)),
|
||||
Call::Balances(balances::Call::force_transfer(1, 2, 5))
|
||||
]), "RequireRootOrigin");
|
||||
assert_ok!(Utility::batch(Origin::ROOT, vec![
|
||||
Call::Balances(balances::Call::force_transfer(1, 2, 5)),
|
||||
Call::Balances(balances::Call::force_transfer(1, 2, 5))
|
||||
]));
|
||||
assert_eq!(Balances::free_balance(1), 0);
|
||||
assert_eq!(Balances::free_balance(2), 10);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user