Skip to content

Commit

Permalink
Fast Unstake Pallet (paritytech#12129)
Browse files Browse the repository at this point in the history
* add failing test for itamar

* an ugly example of fast unstake

* Revert "add failing test for itamar"

This reverts commit bdd1399.

* fast unstake wip

* clean it up a bit

* some comments

* on_idle logic

* fix

* comment

* new working version, checks all pass, looking good

* some notes

* add mock boilerplate

* more boilerplate

* simplify the weight stuff

* ExtBuilder for pools

* fmt

* rm bags-list, simplify setup_works

* mock + tests boilerplate

* make some benchmarks work

* mock boilerplate

* tests boilerplate

* run_to_block works

* add Error enums

* add test

* note

* make UnstakeRequest fields pub

* some tests

* fix origin

* fmt

* add fast_unstake_events_since_last_call

* text

* rewrite some benchmes and fix them -- the outcome is still strange

* Fix weights

* cleanup

* Update frame/election-provider-support/solution-type/src/single_page.rs

* fix build

* Fix pools tests

* iterate teset + mock

* test unfinished

* cleanup and add some tests

* add test successful_multi_queue

* comment

* rm Head check

* add TODO

* complete successful_multi_queue

* + test early_exit

* fix a lot of things above the beautiful atlantic ocean 🌊

* seemingly it is finished now

* Fix build

* ".git/.scripts/fmt.sh" 1

* Fix slashing amount as well

* better docs

* abstract types

* rm use

* import

* Update frame/nomination-pools/benchmarking/src/lib.rs

Co-authored-by: Nitwit <[email protected]>

* Update frame/fast-unstake/src/types.rs

Co-authored-by: Nitwit <[email protected]>

* Fix build

* fmt

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Keith Yeung <[email protected]>

* make bounded

* feedback from code review with Ankan

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/lib.rs

Co-authored-by: Roman Useinov <[email protected]>

* Update frame/fast-unstake/src/mock.rs

* update to master

* some final review comments

* fmt

* fix clippy

* remove unused

* ".git/.scripts/fmt.sh" 1

* make it all build again

* fmt

* undo fishy change

Co-authored-by: Ross Bulat <[email protected]>
Co-authored-by: command-bot <>
Co-authored-by: Nitwit <[email protected]>
Co-authored-by: Keith Yeung <[email protected]>
Co-authored-by: Roman Useinov <[email protected]>
  • Loading branch information
5 people authored and ark0f committed Feb 27, 2023
1 parent dfae062 commit 13d1ff9
Show file tree
Hide file tree
Showing 21 changed files with 2,650 additions and 29 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ members = [
"frame/contracts/rpc/runtime-api",
"frame/conviction-voting",
"frame/democracy",
"frame/fast-unstake",
"frame/try-runtime",
"frame/election-provider-multi-phase",
"frame/election-provider-support",
Expand Down
4 changes: 4 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pallet-democracy = { version = "4.0.0-dev", default-features = false, path = "..
pallet-election-provider-multi-phase = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-multi-phase" }
pallet-election-provider-support-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../../../frame/election-provider-support/benchmarking", optional = true }
pallet-elections-phragmen = { version = "5.0.0-dev", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-fast-unstake = { version = "4.0.0-dev", default-features = false, path = "../../../frame/fast-unstake" }
pallet-gilt = { version = "4.0.0-dev", default-features = false, path = "../../../frame/gilt" }
pallet-grandpa = { version = "4.0.0-dev", default-features = false, path = "../../../frame/grandpa" }
pallet-im-online = { version = "4.0.0-dev", default-features = false, path = "../../../frame/im-online" }
Expand Down Expand Up @@ -142,6 +143,7 @@ std = [
"pallet-conviction-voting/std",
"pallet-democracy/std",
"pallet-elections-phragmen/std",
"pallet-fast-unstake/std",
"frame-executive/std",
"pallet-gilt/std",
"pallet-grandpa/std",
Expand Down Expand Up @@ -220,6 +222,7 @@ runtime-benchmarks = [
"pallet-election-provider-multi-phase/runtime-benchmarks",
"pallet-election-provider-support-benchmarking/runtime-benchmarks",
"pallet-elections-phragmen/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks",
"pallet-gilt/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
Expand Down Expand Up @@ -272,6 +275,7 @@ try-runtime = [
"pallet-democracy/try-runtime",
"pallet-election-provider-multi-phase/try-runtime",
"pallet-elections-phragmen/try-runtime",
"pallet-fast-unstake/try-runtime",
"pallet-gilt/try-runtime",
"pallet-grandpa/try-runtime",
"pallet-im-online/try-runtime",
Expand Down
9 changes: 9 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,13 @@ impl pallet_staking::Config for Runtime {
type BenchmarkingConfig = StakingBenchmarkingConfig;
}

impl pallet_fast_unstake::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SlashPerEra = ConstU128<{ DOLLARS }>;
type ControlOrigin = frame_system::EnsureRoot<AccountId>;
type WeightInfo = ();
}

parameter_types! {
// phase durations. 1/4 of the last session for each.
pub const SignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4;
Expand Down Expand Up @@ -1655,6 +1662,7 @@ construct_runtime!(
NominationPools: pallet_nomination_pools,
RankedPolls: pallet_referenda::<Instance2>,
RankedCollective: pallet_ranked_collective,
FastUnstake: pallet_fast_unstake,
}
);

Expand Down Expand Up @@ -1741,6 +1749,7 @@ mod benches {
[pallet_election_provider_multi_phase, ElectionProviderMultiPhase]
[pallet_election_provider_support_benchmarking, EPSBench::<Runtime>]
[pallet_elections_phragmen, Elections]
[pallet_fast_unstake, FastUnstake]
[pallet_gilt, Gilt]
[pallet_grandpa, Grandpa]
[pallet_identity, Identity]
Expand Down
11 changes: 11 additions & 0 deletions frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ impl<T: Config> ElectionProvider for NoFallback<T> {
type DataProvider = T::DataProvider;
type Error = &'static str;

fn ongoing() -> bool {
false
}

fn elect() -> Result<Supports<T::AccountId>, Self::Error> {
// Do nothing, this will enable the emergency phase.
Err("NoFallback.")
Expand Down Expand Up @@ -1598,6 +1602,13 @@ impl<T: Config> ElectionProvider for Pallet<T> {
type Error = ElectionError<T>;
type DataProvider = T::DataProvider;

fn ongoing() -> bool {
match Self::current_phase() {
Phase::Off => false,
_ => true,
}
}

fn elect() -> Result<Supports<T::AccountId>, Self::Error> {
match Self::do_elect() {
Ok(supports) => {
Expand Down
4 changes: 4 additions & 0 deletions frame/election-provider-multi-phase/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ impl ElectionProvider for MockFallback {
type Error = &'static str;
type DataProvider = StakingMock;

fn ongoing() -> bool {
false
}

fn elect() -> Result<Supports<AccountId>, Self::Error> {
Self::elect_with_bounds(Bounded::max_value(), Bounded::max_value())
}
Expand Down
9 changes: 8 additions & 1 deletion frame/election-provider-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
//! type BlockNumber = BlockNumber;
//! type Error = &'static str;
//! type DataProvider = T::DataProvider;
//!
//! fn ongoing() -> bool { false }
//! fn elect() -> Result<Supports<AccountId>, Self::Error> {
//! Self::DataProvider::electable_targets(None)
//! .map_err(|_| "failed to elect")
Expand Down Expand Up @@ -370,6 +370,9 @@ pub trait ElectionProvider {
BlockNumber = Self::BlockNumber,
>;

/// Indicate if this election provider is currently ongoing an asynchronous election or not.
fn ongoing() -> bool;

/// Elect a new set of winners, without specifying any bounds on the amount of data fetched from
/// [`Self::DataProvider`]. An implementation could nonetheless impose its own custom limits.
///
Expand Down Expand Up @@ -420,6 +423,10 @@ where
fn elect() -> Result<Supports<AccountId>, Self::Error> {
Err("<NoElection as ElectionProvider> cannot do anything.")
}

fn ongoing() -> bool {
false
}
}

/// A utility trait for something to implement `ElectionDataProvider` in a sensible way.
Expand Down
8 changes: 8 additions & 0 deletions frame/election-provider-support/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ impl<T: Config> ElectionProvider for UnboundedExecution<T> {
type Error = Error;
type DataProvider = T::DataProvider;

fn ongoing() -> bool {
false
}

fn elect() -> Result<Supports<Self::AccountId>, Self::Error> {
// This should not be called if not in `std` mode (and therefore neither in genesis nor in
// testing)
Expand Down Expand Up @@ -167,6 +171,10 @@ impl<T: BoundedConfig> ElectionProvider for BoundedExecution<T> {
type Error = Error;
type DataProvider = T::DataProvider;

fn ongoing() -> bool {
false
}

fn elect() -> Result<Supports<Self::AccountId>, Self::Error> {
elect_with::<T>(Some(T::VotersBound::get() as usize), Some(T::TargetsBound::get() as usize))
}
Expand Down
70 changes: 70 additions & 0 deletions frame/fast-unstake/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[package]
name = "pallet-fast-unstake"
version = "4.0.0-dev"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "Unlicense"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME fast unstake pallet"
readme = "README.md"

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

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }

frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }

sp-io = { version = "6.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }
sp-std = { version = "4.0.0", default-features = false, path = "../../primitives/std" }
sp-staking = { default-features = false, path = "../../primitives/staking" }

pallet-balances = { default-features = false, path = "../balances" }
pallet-timestamp = { default-features = false, path = "../timestamp" }
pallet-staking = { default-features = false, path = "../staking" }
pallet-nomination-pools = { default-features = false, path = "../nomination-pools" }
frame-election-provider-support = { default-features = false, path = "../election-provider-support" }

frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }

[dev-dependencies]
pallet-staking-reward-curve = { version = "4.0.0-dev", path = "../staking/reward-curve" }
sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }
substrate-test-utils = { version = "4.0.0-dev", path = "../../test-utils" }
sp-tracing = { version = "5.0.0", path = "../../primitives/tracing" }

[features]
default = ["std"]
std = [
"codec/std",
"log/std",
"scale-info/std",

"frame-support/std",
"frame-system/std",

"sp-io/std",
"sp-staking/std",
"sp-runtime/std",
"sp-std/std",

"pallet-staking/std",
"pallet-nomination-pools/std",
"pallet-balances/std",
"pallet-timestamp/std",
"frame-election-provider-support/std",

"frame-benchmarking/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
Loading

0 comments on commit 13d1ff9

Please sign in to comment.