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

[Feature] StakeTracker P1 - VotersList #13079

Closed
wants to merge 104 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
2f0a914
[Feature] StakeTracker P1 - VotersList
ruseinov Jan 5, 2023
29e1f66
add stake-tracker
ruseinov Jan 5, 2023
71ad9ab
fmt
ruseinov Jan 5, 2023
c4c5248
make staking tests work with stake-tracker
ruseinov Jan 5, 2023
58bfb70
ignore obsolete test
ruseinov Jan 6, 2023
66d6f5a
clarification
ruseinov Jan 6, 2023
0de90d4
fix tests/benches feature-gated
ruseinov Jan 6, 2023
ad54a95
StakeTracker to node
ruseinov Jan 6, 2023
3b1d926
more fixes
ruseinov Jan 6, 2023
16bd6b1
more fixes
ruseinov Jan 6, 2023
938c4df
more fixes
ruseinov Jan 6, 2023
74a2a48
fix some build errs
ruseinov Jan 8, 2023
d829b91
doc fixes
ruseinov Jan 8, 2023
2ea7d05
fix nom-pools benches
ruseinov Jan 8, 2023
1344c24
Update primitives/staking/src/lib.rs
ruseinov Jan 9, 2023
32f50d2
Update primitives/staking/src/lib.rs
ruseinov Jan 9, 2023
c3a7bd6
Update primitives/staking/src/lib.rs
ruseinov Jan 9, 2023
4ce30ed
Update frame/staking/src/pallet/mod.rs
ruseinov Jan 9, 2023
2d28217
Update frame/staking/src/pallet/mod.rs
ruseinov Jan 9, 2023
44b318f
Update primitives/staking/src/currency_to_vote.rs
ruseinov Jan 9, 2023
a34aaab
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Jan 9, 2023
8838a07
fix OnStakingUpdate occurrences
ruseinov Jan 9, 2023
7d36dc9
add mock
ruseinov Jan 11, 2023
3446f0e
fixes
ruseinov Jan 11, 2023
ead8b81
remove defenisive, s, some tests
ruseinov Jan 12, 2023
387b7b4
basic tests finished
ruseinov Jan 12, 2023
9e561e9
still unsure what to do with un-decodables
ruseinov Jan 13, 2023
363eb6f
more comments
ruseinov Jan 13, 2023
2c6275e
fix migrations
kianenigma Jan 15, 2023
fc14202
Add NotDecodable error and tests
ruseinov Jan 16, 2023
a0db07f
fix try-runtime migration build
ruseinov Jan 17, 2023
978076a
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Jan 17, 2023
504695e
unused import fix
ruseinov Jan 17, 2023
5361a95
test fixes
ruseinov Jan 17, 2023
78094f2
some more fixes
ruseinov Jan 17, 2023
7925732
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Jan 19, 2023
561eb2b
fix unused
ruseinov Jan 19, 2023
1fffe7f
Revert "fix unused"
ruseinov Jan 19, 2023
a1a27d7
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Jan 24, 2023
ad60934
Merge branch 'master' into ru/feature/stake-tracker-voter
ruseinov Jan 30, 2023
2002537
Update frame/stake-tracker/src/mock.rs
ruseinov Jan 31, 2023
6108362
Update frame/stake-tracker/Cargo.toml
ruseinov Feb 1, 2023
f92dfb7
Update frame/stake-tracker/Cargo.toml
ruseinov Feb 1, 2023
533703a
Merge branch 'master' into ru/feature/stake-tracker-voter
ruseinov Feb 10, 2023
d6069c3
fmt
ruseinov Feb 10, 2023
edbcf89
get rid of ReadOnlySortedListProvider
ruseinov Feb 10, 2023
16671da
fix some errs
ruseinov Feb 10, 2023
aff4599
revert weird random change
ruseinov Feb 10, 2023
f15bd1c
more redundant import removals
ruseinov Feb 10, 2023
85e863d
more fixes
ruseinov Feb 10, 2023
3b816b8
more fixes
ruseinov Feb 10, 2023
676a7a9
more fixes
ruseinov Feb 10, 2023
54a5a6a
more fixes
ruseinov Feb 11, 2023
7e042ac
little fix
ruseinov Feb 11, 2023
3295a29
no more panics
ruseinov Feb 11, 2023
3f6b5b9
implement OnStakingUpdate for tuples
ruseinov Feb 11, 2023
489b71c
minor fixes
ruseinov Feb 11, 2023
aec8879
introduce a testing framework for OnStakingUpdate
ruseinov Feb 11, 2023
6bf3fcb
more small fixes
ruseinov Feb 11, 2023
c049653
re-order methods
ruseinov Feb 12, 2023
c987805
integration tests for events
ruseinov Feb 12, 2023
e6676f7
import fix
ruseinov Feb 12, 2023
e3ae48f
fix clippy
ruseinov Feb 12, 2023
06cd68c
sort out read-only lists
ruseinov Feb 12, 2023
391b0b4
fix feature gates
ruseinov Feb 12, 2023
7780763
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Feb 15, 2023
2d1fe57
Update frame/stake-tracker/src/lib.rs
ruseinov Feb 17, 2023
435d72b
address some review comments
ruseinov Feb 17, 2023
501243b
add some docs
ruseinov Feb 17, 2023
21a90ce
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Feb 17, 2023
8360474
revert reorder
ruseinov Feb 17, 2023
355145e
fix beefy tests
ruseinov Feb 17, 2023
84f8d8f
address some review comments
ruseinov Feb 19, 2023
17d0fbb
adjust docs
ruseinov Feb 19, 2023
c2d8ad4
more doc updates
ruseinov Feb 19, 2023
3f286e1
fix typo
ruseinov Feb 19, 2023
0246d7b
fix docs
ruseinov Feb 19, 2023
41b3c29
Merge branch 'master' into ru/feature/stake-tracker-voter
ruseinov Feb 22, 2023
d1b783c
Update frame/stake-tracker/src/lib.rs
ruseinov Feb 26, 2023
e12473f
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Feb 26, 2023
5736950
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Mar 5, 2023
7d136f9
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Mar 11, 2023
9332d75
address some of the PR comments
ruseinov Mar 13, 2023
f143653
defensive unstake
ruseinov Mar 13, 2023
605816d
re-arrange testing events
ruseinov Mar 13, 2023
66cd197
Merge branch 'master' into ru/feature/stake-tracker-voter
ruseinov Mar 13, 2023
a7250dd
fixup stake-tracker tests
ruseinov Mar 14, 2023
0cb3222
reinstate migration changes
ruseinov Mar 14, 2023
d79ac49
basic OnStakingUpdate staking tests
ruseinov Mar 14, 2023
9eb705a
more fixes
ruseinov Mar 14, 2023
6a305f8
more fixes
ruseinov Mar 14, 2023
20f02e9
rm deprecated
ruseinov Mar 14, 2023
9f28619
fix comment
ruseinov Mar 14, 2023
8bbfd2b
on_stake_update defensive tests
ruseinov Mar 14, 2023
f8dabaa
more defensive tests
ruseinov Mar 14, 2023
32509ad
fix rustdoc again
ruseinov Mar 15, 2023
ef6bbc3
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Mar 18, 2023
69e70f9
fix balances
ruseinov Mar 18, 2023
9ad762b
address comments
ruseinov Mar 18, 2023
03154ec
remove unnecessary loops
ruseinov Mar 21, 2023
ecf831e
fix some event-handlers to be fired post-action
ruseinov Mar 21, 2023
4fd1c49
fix do_add_nominator
ruseinov Mar 21, 2023
78d8d6f
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Mar 22, 2023
855100a
Merge remote-tracking branch 'origin/master' into ru/feature/stake-tr…
Mar 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 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 @@ -139,6 +139,7 @@ members = [
"frame/session/benchmarking",
"frame/society",
"frame/staking",
"frame/stake-tracker",
"frame/staking/reward-curve",
"frame/staking/reward-fn",
"frame/state-trie-migration",
Expand Down
3 changes: 3 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ pallet-root-testing = { version = "1.0.0-dev", default-features = false, path =
pallet-session = { version = "4.0.0-dev", features = [ "historical" ], path = "../../../frame/session", default-features = false }
pallet-session-benchmarking = { version = "4.0.0-dev", path = "../../../frame/session/benchmarking", default-features = false, optional = true }
pallet-staking = { version = "4.0.0-dev", default-features = false, path = "../../../frame/staking" }
pallet-stake-tracker = { version = "4.0.0-dev", default-features = false, path = "../../../frame/stake-tracker" }
pallet-staking-reward-curve = { version = "4.0.0-dev", default-features = false, path = "../../../frame/staking/reward-curve" }
pallet-state-trie-migration = { version = "4.0.0-dev", default-features = false, path = "../../../frame/state-trie-migration" }
pallet-scheduler = { version = "4.0.0-dev", default-features = false, path = "../../../frame/scheduler" }
Expand Down Expand Up @@ -174,6 +175,7 @@ std = [
"sp-runtime/std",
"sp-staking/std",
"pallet-staking/std",
"pallet-stake-tracker/std",
"pallet-state-trie-migration/std",
"sp-session/std",
"pallet-sudo/std",
Expand Down Expand Up @@ -303,6 +305,7 @@ try-runtime = [
"pallet-root-testing/try-runtime",
"pallet-session/try-runtime",
"pallet-staking/try-runtime",
"pallet-stake-tracker/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-scheduler/try-runtime",
"pallet-society/try-runtime",
Expand Down
10 changes: 9 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,14 +578,21 @@ impl pallet_staking::Config for Runtime {
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type VoterList = VoterList;
type VoterList = <Runtime as pallet_stake_tracker::Config>::VoterList;
// This a placeholder, to be introduced in the next PR as an instance of bags-list
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = ConstU32<32>;
type HistoryDepth = HistoryDepth;
type OnStakerSlash = NominationPools;
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = StakingBenchmarkingConfig;
type EventListener = StakeTracker;
}

impl pallet_stake_tracker::Config for Runtime {
type Currency = Balances;
type Staking = Staking;
type VoterList = VoterList;
}

impl pallet_fast_unstake::Config for Runtime {
Expand Down Expand Up @@ -1710,6 +1717,7 @@ construct_runtime!(
AssetTxPayment: pallet_asset_tx_payment,
ElectionProviderMultiPhase: pallet_election_provider_multi_phase,
Staking: pallet_staking,
StakeTracker: pallet_stake_tracker,
Session: pallet_session,
Democracy: pallet_democracy,
Council: pallet_collective::<Instance1>,
Expand Down
1 change: 1 addition & 0 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl pallet_staking::Config for Test {
type OnStakerSlash = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
type EventListener = ();
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
}

impl pallet_offences::Config for Test {
Expand Down
2 changes: 1 addition & 1 deletion frame/bags-list/fuzzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! More information about `honggfuzz` can be found
//! [here](https://docs.rs/honggfuzz/).

use frame_election_provider_support::{SortedListProvider, VoteWeight};
use frame_election_provider_support::{ReadOnlySortedListProvider, SortedListProvider, VoteWeight};
use honggfuzz::fuzz;
use pallet_bags_list::mock::{AccountId, BagsList, ExtBuilder};

Expand Down
1 change: 1 addition & 0 deletions frame/bags-list/remote-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
# frame
pallet-staking = { path = "../../staking", version = "4.0.0-dev" }
pallet-stake-tracker = { path = "../../stake-tracker", version = "4.0.0-dev" }
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
pallet-bags-list = { path = "../../bags-list", version = "4.0.0-dev" }
frame-election-provider-support = { path = "../../election-provider-support", version = "4.0.0-dev" }
frame-system = { path = "../../system", version = "4.0.0-dev" }
Expand Down
13 changes: 9 additions & 4 deletions frame/bags-list/remote-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Utilities for remote-testing pallet-bags-list.

use frame_election_provider_support::ScoreProvider;
use frame_election_provider_support::{ReadOnlySortedListProvider, ScoreProvider};
use pallet_bags_list::Instance1;
use sp_std::prelude::*;

Expand All @@ -32,12 +32,18 @@ pub mod try_state;
///
/// For example, this can be the `Runtime` type of the Polkadot runtime.
pub trait RuntimeT<I: 'static>:
pallet_staking::Config + pallet_bags_list::Config<I> + frame_system::Config
pallet_staking::Config
+ pallet_stake_tracker::Config
ruseinov marked this conversation as resolved.
Show resolved Hide resolved
+ pallet_bags_list::Config<I>
+ frame_system::Config
{
}
impl<
I: 'static,
T: pallet_staking::Config + pallet_bags_list::Config<I> + frame_system::Config,
T: pallet_staking::Config
+ pallet_stake_tracker::Config
+ pallet_bags_list::Config<I>
+ frame_system::Config,
> RuntimeT<I> for T
{
}
Expand All @@ -51,7 +57,6 @@ pub fn display_and_check_bags<Runtime: RuntimeT<Instance1>>(
currency_unit: u64,
currency_name: &'static str,
) {
use frame_election_provider_support::SortedListProvider;
use frame_support::traits::Get;

let min_nominator_bond = <pallet_staking::MinNominatorBond<Runtime>>::get();
Expand Down
3 changes: 2 additions & 1 deletion frame/bags-list/remote-tests/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//! Test to check the migration of the voter bag.

use crate::{RuntimeT, LOG_TARGET};
use frame_election_provider_support::ReadOnlySortedListProvider;
use frame_support::traits::PalletInfoAccess;
use pallet_staking::Nominators;
use remote_externalities::{Builder, Mode, OnlineConfig};
Expand Down Expand Up @@ -50,7 +51,7 @@ pub async fn execute<Runtime, Block>(

use frame_election_provider_support::SortedListProvider;
// run the actual migration
let moved = <Runtime as pallet_staking::Config>::VoterList::unsafe_regenerate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the previous code seems more correct to me. This test is testing the re-generation of pallet_staking::Config::VoterList. We don't care if it is backed by what..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this we'll need to move unsafe_regenerate to ReadOnly interface, which I guess is far, since it's an unsafe method.

let moved = <Runtime as pallet_stake_tracker::Config>::VoterList::unsafe_regenerate(
pallet_staking::Nominators::<Runtime>::iter().map(|(n, _)| n),
pallet_staking::Pallet::<Runtime>::weight_of_fn(),
);
Expand Down
2 changes: 1 addition & 1 deletion frame/bags-list/remote-tests/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Test to execute the snapshot using the voter bag.

use frame_election_provider_support::SortedListProvider;
use frame_election_provider_support::ReadOnlySortedListProvider;
use frame_support::traits::PalletInfoAccess;
use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};
Expand Down
2 changes: 1 addition & 1 deletion frame/bags-list/remote-tests/src/try_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Test to execute the sanity-check of the voter bag.

use frame_election_provider_support::SortedListProvider;
use frame_election_provider_support::ReadOnlySortedListProvider;
use frame_support::{
storage::generator::StorageMap,
traits::{Get, PalletInfoAccess},
Expand Down
74 changes: 38 additions & 36 deletions frame/bags-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! A semi-sorted list, where items hold an `AccountId` based on some `Score`. The
//! `AccountId` (`id` for short) might be synonym to a `voter` or `nominator` in some context, and
//! `Score` signifies the chance of each id being included in the final
//! [`SortedListProvider::iter`].
//! [`ReadOnlySortedListProvider::iter`].
//!
//! It implements [`frame_election_provider_support::SortedListProvider`] to provide a semi-sorted
//! list of accounts to another pallet. It needs some other pallet to give it some information about
Expand Down Expand Up @@ -54,7 +54,9 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::FullCodec;
use frame_election_provider_support::{ScoreProvider, SortedListProvider};
use frame_election_provider_support::{
ReadOnlySortedListProvider, ScoreProvider, SortedListProvider,
};
use frame_system::ensure_signed;
use sp_runtime::traits::{AtLeast32BitUnsigned, Bounded, StaticLookup};
use sp_std::prelude::*;
Expand Down Expand Up @@ -269,7 +271,7 @@ pub mod pallet {

#[cfg(feature = "try-runtime")]
fn try_state(_: BlockNumberFor<T>) -> Result<(), &'static str> {
<Self as SortedListProvider<T::AccountId>>::try_state()
<Self as ReadOnlySortedListProvider<T::AccountId>>::try_state()
}
}
}
Expand Down Expand Up @@ -299,7 +301,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}

impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I> {
impl<T: Config<I>, I: 'static> ReadOnlySortedListProvider<T::AccountId> for Pallet<T, I> {
type Error = ListError;
type Score = T::Score;

Expand All @@ -321,45 +323,21 @@ impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I>
fn contains(id: &T::AccountId) -> bool {
List::<T, I>::contains(id)
}

fn on_insert(id: T::AccountId, score: T::Score) -> Result<(), ListError> {
List::<T, I>::insert(id, score)
}

fn get_score(id: &T::AccountId) -> Result<T::Score, ListError> {
List::<T, I>::get_score(id)
}

fn on_update(id: &T::AccountId, new_score: T::Score) -> Result<(), ListError> {
Pallet::<T, I>::do_rebag(id, new_score).map(|_| ())
}

fn on_remove(id: &T::AccountId) -> Result<(), ListError> {
List::<T, I>::remove(id)
}

fn unsafe_regenerate(
all: impl IntoIterator<Item = T::AccountId>,
score_of: Box<dyn Fn(&T::AccountId) -> T::Score>,
) -> u32 {
// NOTE: This call is unsafe for the same reason as SortedListProvider::unsafe_regenerate.
// I.e. because it can lead to many storage accesses.
// So it is ok to call it as caller must ensure the conditions.
List::<T, I>::unsafe_regenerate(all, score_of)
}

fn try_state() -> Result<(), &'static str> {
List::<T, I>::try_state()
}

fn unsafe_clear() {
// NOTE: This call is unsafe for the same reason as SortedListProvider::unsafe_clear.
// I.e. because it can lead to many storage accesses.
// So it is ok to call it as caller must ensure the conditions.
List::<T, I>::unsafe_clear()
}
frame_election_provider_support::runtime_benchmarks_or_test_enabled! {
fn unsafe_clear() {
// NOTE: This call is unsafe for the same reason as SortedListProvider::unsafe_clear.
// I.e. because it can lead to many storage accesses.
// So it is ok to call it as caller must ensure the conditions.
List::<T, I>::unsafe_clear()
}

frame_election_provider_support::runtime_benchmarks_enabled! {
fn score_update_worst_case(who: &T::AccountId, is_increase: bool) -> Self::Score {
use frame_support::traits::Get as _;
let thresholds = T::BagThresholds::get();
Expand All @@ -383,8 +361,32 @@ impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I>
}
}

impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I> {
fn on_insert(id: T::AccountId, score: T::Score) -> Result<(), ListError> {
List::<T, I>::insert(id, score)
}

fn on_update(id: &T::AccountId, new_score: T::Score) -> Result<(), ListError> {
Pallet::<T, I>::do_rebag(id, new_score).map(|_| ())
}

fn on_remove(id: &T::AccountId) -> Result<(), ListError> {
List::<T, I>::remove(id)
}

fn unsafe_regenerate(
all: impl IntoIterator<Item = T::AccountId>,
score_of: Box<dyn Fn(&T::AccountId) -> T::Score>,
) -> u32 {
// NOTE: This call is unsafe for the same reason as SortedListProvider::unsafe_regenerate.
// I.e. because it can lead to many storage accesses.
// So it is ok to call it as caller must ensure the conditions.
List::<T, I>::unsafe_regenerate(all, score_of)
}
}

impl<T: Config<I>, I: 'static> ScoreProvider<T::AccountId> for Pallet<T, I> {
type Score = <Pallet<T, I> as SortedListProvider<T::AccountId>>::Score;
type Score = <Pallet<T, I> as ReadOnlySortedListProvider<T::AccountId>>::Score;

fn score(id: &T::AccountId) -> T::Score {
Node::<T, I>::get(id).map(|node| node.score()).unwrap_or_default()
Expand Down
2 changes: 1 addition & 1 deletion frame/bags-list/src/list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
mock::{test_utils::*, *},
ListBags, ListNodes,
};
use frame_election_provider_support::{SortedListProvider, VoteWeight};
use frame_election_provider_support::{ReadOnlySortedListProvider, VoteWeight};
use frame_support::{assert_ok, assert_storage_noop};

fn node(
Expand Down
Loading