Skip to content

Commit

Permalink
bring in utility pallet code
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnReedV committed Feb 19, 2025
1 parent fb8ce6a commit a0ffb6a
Show file tree
Hide file tree
Showing 8 changed files with 1,831 additions and 5 deletions.
40 changes: 36 additions & 4 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ pallet-timestamp = { git = "https://github.com/paritytech/polkadot-sdk.git", tag
pallet-transaction-payment = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
pallet-utility = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
#pallet-utility = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
pallet-utility = { path = "pallets/utility", default-features = false }
pallet-root-testing = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }

sc-basic-authorship = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
sc-cli = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409" }
Expand Down
54 changes: 54 additions & 0 deletions pallets/utility/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "pallet-utility"
version = "38.0.0"
edition = "2021"
license = "Apache-2.0"
description = "FRAME utilities pallet"
readme = "README.md"

[lints]
workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
frame-benchmarking = { workspace = true, default-features = false, optional = true }
frame-support = { workspace = true, default-features = false }
frame-system = { workspace = true, default-features = false }
sp-core = { workspace = true, default-features = false }
sp-io = { workspace = true, default-features = false}
sp-runtime = { workspace = true, default-features = false}

[dev-dependencies]
pallet-balances = { default-features = true, workspace = true }
pallet-collective = { default-features = false, path = "../collective" }
pallet-timestamp = { default-features = true, workspace = true }
sp-core = { default-features = true, workspace = true }
pallet-root-testing = { workspace = true, default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
43 changes: 43 additions & 0 deletions pallets/utility/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Utility Module
A stateless module with helpers for dispatch management which does no re-authentication.

- [`utility::Config`](https://docs.rs/pallet-utility/latest/pallet_utility/pallet/trait.Config.html)
- [`Call`](https://docs.rs/pallet-utility/latest/pallet_utility/pallet/enum.Call.html)

## Overview

This module contains two basic pieces of functionality:
- Batch dispatch: A stateless operation, allowing any origin to execute multiple calls in a
single dispatch. This can be useful to amalgamate proposals, combining `set_code` with
corresponding `set_storage`s, for efficient multiple payouts with just a single signature
verify, or in combination with one of the other two dispatch functionality.
- Pseudonymal dispatch: A stateless operation, allowing a signed origin to execute a call from
an alternative signed origin. Each account has 2 * 2**16 possible "pseudonyms" (alternative
account IDs) and these can be stacked. This can be useful as a key management tool, where you
need multiple distinct accounts (e.g. as controllers for many staking accounts), but where
it's perfectly fine to have each of them controlled by the same underlying keypair.
Derivative accounts are, for the purposes of proxy filtering considered exactly the same as
the origin and are thus hampered with the origin's filters.

Since proxy filters are respected in all dispatches of this module, it should never need to be
filtered by any proxy.

## Interface

### Dispatchable Functions

#### For batch dispatch
- `batch` - Dispatch multiple calls from the sender's origin.

#### For pseudonymal dispatch
- `as_derivative` - Dispatch a call from a derivative signed origin.

[`Call`]: ./enum.Call.html
[`Config`]: ./trait.Config.html

License: Apache-2.0


## Release

Polkadot SDK stable2409
91 changes: 91 additions & 0 deletions pallets/utility/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Benchmarks for Utility Pallet

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use alloc::{vec, vec::Vec};
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
use frame_system::RawOrigin;

const SEED: u32 = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

benchmarks! {
where_clause { where <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin: Clone }
batch {
let c in 0 .. 1000;
let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark { remark: vec![] }.into();
calls.push(call);
}
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), calls)
verify {
assert_last_event::<T>(Event::BatchCompleted.into())
}

as_derivative {
let caller = account("caller", SEED, SEED);
let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(RawOrigin::Signed(caller), SEED as u16, call)

batch_all {
let c in 0 .. 1000;
let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark { remark: vec![] }.into();
calls.push(call);
}
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), calls)
verify {
assert_last_event::<T>(Event::BatchCompleted.into())
}

dispatch_as {
let caller = account("caller", SEED, SEED);
let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
let origin: T::RuntimeOrigin = RawOrigin::Signed(caller).into();
let pallets_origin: <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin = origin.caller().clone();
let pallets_origin = Into::<T::PalletsOrigin>::into(pallets_origin);
}: _(RawOrigin::Root, Box::new(pallets_origin), call)

force_batch {
let c in 0 .. 1000;
let mut calls: Vec<<T as Config>::RuntimeCall> = Vec::new();
for i in 0 .. c {
let call = frame_system::Call::remark { remark: vec![] }.into();
calls.push(call);
}
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), calls)
verify {
assert_last_event::<T>(Event::BatchCompleted.into())
}

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
}
Loading

0 comments on commit a0ffb6a

Please sign in to comment.