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

Audit fixes for election/staking decoupling part 2 #8167

Merged
merged 79 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
d998b12
Base features and traits.
kianenigma Jan 15, 2021
5aea9cc
pallet and unsigned phase
kianenigma Jan 15, 2021
632e107
Undo bad formattings.
kianenigma Jan 15, 2021
c52e65d
Master.into()
kianenigma Jan 18, 2021
b4fc5e1
some formatting cleanup.
kianenigma Jan 18, 2021
cc26881
Small self-cleanup.
kianenigma Jan 18, 2021
67a9fae
Master.into()
kianenigma Jan 27, 2021
75eca3f
Make it all build
kianenigma Jan 27, 2021
8daec3a
self-review
kianenigma Jan 27, 2021
49613ed
Some doc tests.
kianenigma Jan 27, 2021
35f1faf
Some changes from other PR
kianenigma Jan 27, 2021
e01cacc
Fix session test
kianenigma Jan 27, 2021
6278150
Merge branch 'master' into kiz-election-provider-2-two-phase-unsigned
shawntabrizi Feb 4, 2021
4c516cb
Update Cargo.lock
shawntabrizi Feb 4, 2021
8c8d1e6
Update frame/election-provider-multi-phase/src/lib.rs
kianenigma Feb 11, 2021
4b58c91
Some review comments
kianenigma Feb 11, 2021
f4029aa
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 11, 2021
f16ac88
Master.into()
kianenigma Feb 11, 2021
528917e
Rename + make encode/decode
kianenigma Feb 12, 2021
1a5794a
Do an assert as well, just in case.
kianenigma Feb 12, 2021
01e63ed
Fix build
kianenigma Feb 12, 2021
4ccecdf
Update frame/election-provider-multi-phase/src/unsigned.rs
kianenigma Feb 12, 2021
83a789e
Las comment
kianenigma Feb 12, 2021
adb3618
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 12, 2021
7c71df0
fix staking fuzzer.
kianenigma Feb 12, 2021
12b1640
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 12, 2021
6397d4a
Add one last layer of feasibility check as well.
kianenigma Feb 12, 2021
80d3c31
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 12, 2021
7d6f6ad
Last fixes to benchmarks
kianenigma Feb 12, 2021
2e28437
Some more docs.
kianenigma Feb 12, 2021
5fda744
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 12, 2021
1ce6c8e
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 12, 2021
f63c5b8
Some nits
kianenigma Feb 12, 2021
8029c01
Merge branch 'kiz-election-provider-2-two-phase-unsigned' of github.c…
kianenigma Feb 12, 2021
70f4191
Merge remote-tracking branch 'origin/master' into kiz-election-provid…
Feb 13, 2021
701e4a2
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Feb 13, 2021
ea531e1
Fix doc
kianenigma Feb 17, 2021
c2a3286
Upstream.into()
kianenigma Feb 17, 2021
16fe4f7
Master.into()
kianenigma Feb 17, 2021
2c6ac80
Mkae ci green
kianenigma Feb 17, 2021
0bcc776
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Feb 18, 2021
6caf093
Audit fixes for election-provider: part 2 signed phase.
kianenigma Feb 21, 2021
fc1095a
Master.into()
kianenigma Feb 25, 2021
d5012d5
Master.into()
kianenigma Mar 2, 2021
044f71f
Fix weight
kianenigma Mar 2, 2021
e090f23
Some grumbles.
kianenigma Mar 2, 2021
d37f770
Try and weigh to get_npos_voters
kianenigma Mar 3, 2021
924d114
Fix build
kianenigma Mar 3, 2021
6441891
Fix line width
kianenigma Mar 3, 2021
f0bb1a5
Merge remote-tracking branch 'origin/master' into kiz-election-provid…
Mar 3, 2021
1f24f6d
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Mar 3, 2021
b23781f
Fix tests.
kianenigma Mar 3, 2021
4cec55e
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Mar 5, 2021
2bb4ed9
Fix build
kianenigma Mar 5, 2021
f5100f3
Reorg some stuff
kianenigma Mar 5, 2021
96bce0e
More reorg.
kianenigma Mar 5, 2021
d5a35e3
Reorg done.
kianenigma Mar 5, 2021
14c7d4c
Master.into()
kianenigma Mar 5, 2021
745b76f
Fix build
kianenigma Mar 5, 2021
deeda2d
Another rename
kianenigma Mar 8, 2021
75f0931
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Mar 8, 2021
5c429b8
Fix build
kianenigma Mar 8, 2021
47a8ab3
Update frame/election-provider-multi-phase/src/mock.rs
kianenigma Mar 9, 2021
1a85cc9
nit
kianenigma Mar 9, 2021
4ecaeeb
Merge branch 'kiz-election-provider-2-audit' of github.com:paritytech…
kianenigma Mar 9, 2021
0d76b7a
better doc
kianenigma Mar 9, 2021
9143981
Line width
kianenigma Mar 9, 2021
30af70e
Fix build
kianenigma Mar 9, 2021
c6ee113
Master.into()
kianenigma Mar 14, 2021
4ea6371
Self-review
kianenigma Mar 14, 2021
d3d5957
Self-review
kianenigma Mar 14, 2021
f3e5397
Fix wan
kianenigma Mar 14, 2021
e3dafc2
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Mar 14, 2021
7ddb4c5
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Mar 14, 2021
062342b
Master.into()
kianenigma Mar 15, 2021
7cf67c6
fix build and review comments.
kianenigma Mar 15, 2021
1c7b20e
Update frame/election-provider-multi-phase/src/lib.rs
kianenigma Mar 16, 2021
13daa47
add comment
kianenigma Mar 16, 2021
0807ac0
Merge branch 'kiz-election-provider-2-audit' of github.com:paritytech…
kianenigma Mar 16, 2021
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
710 changes: 357 additions & 353 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ members = [
"frame/try-runtime",
"frame/elections",
"frame/election-provider-multi-phase",
"frame/election-provider-support",
"frame/example",
"frame/example-offchain-worker",
"frame/example-parallel",
Expand Down Expand Up @@ -145,7 +146,6 @@ members = [
"primitives/database",
"primitives/debug-derive",
"primitives/externalities",
"primitives/election-providers",
"primitives/finality-grandpa",
"primitives/inherents",
"primitives/io",
Expand Down
2 changes: 1 addition & 1 deletion frame/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pallet-offences = { version = "3.0.0", path = "../offences" }
pallet-staking = { version = "3.0.0", path = "../staking" }
pallet-staking-reward-curve = { version = "3.0.0", path = "../staking/reward-curve" }
sp-core = { version = "3.0.0", path = "../../primitives/core" }
sp-election-providers = { version = "3.0.0", path = "../../primitives/election-providers" }
frame-election-provider-support = { version = "3.0.0", path = "../election-provider-support" }

[features]
default = ["std"]
Expand Down
3 changes: 2 additions & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use sp_consensus_babe::{AuthorityId, AuthorityPair, Slot};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
use sp_staking::SessionIndex;
use pallet_staking::EraIndex;
use sp_election_providers::onchain;
use frame_election_provider_support::onchain;
use pallet_session::historical as pallet_session_historical;

type DummyValidatorId = u64;
Expand Down Expand Up @@ -187,6 +187,7 @@ parameter_types! {
impl onchain::Config for Test {
type AccountId = <Self as frame_system::Config>::AccountId;
type BlockNumber = <Self as frame_system::Config>::BlockNumber;
type BlockWeights = ();
type Accuracy = Perbill;
type DataProvider = Staking;
}
Expand Down
8 changes: 4 additions & 4 deletions frame/election-provider-multi-phase/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sp-std = { version = "3.0.0", default-features = false, path = "../../primitives
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
sp-npos-elections = { version = "3.0.0", default-features = false, path = "../../primitives/npos-elections" }
sp-arithmetic = { version = "3.0.0", default-features = false, path = "../../primitives/arithmetic" }
sp-election-providers = { version = "3.0.0", default-features = false, path = "../../primitives/election-providers" }
frame-election-provider-support = { version = "3.0.0", default-features = false, path = "../election-provider-support" }

# Optional imports for benchmarking
frame-benchmarking = { version = "3.1.0", default-features = false, path = "../benchmarking", optional = true }
Expand All @@ -41,9 +41,9 @@ substrate-test-utils = { version = "3.0.0", path = "../../test-utils" }
sp-io = { version = "3.0.0", path = "../../primitives/io" }
sp-core = { version = "3.0.0", path = "../../primitives/core" }
sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" }
sp-election-providers = { version = "3.0.0", features = ["runtime-benchmarks"], path = "../../primitives/election-providers" }
frame-election-provider-support = { version = "3.0.0", features = ["runtime-benchmarks"], path = "../election-provider-support" }
pallet-balances = { version = "3.0.0", path = "../balances" }
frame-benchmarking = { path = "../benchmarking" , version = "3.1.0"}
frame-benchmarking = { version = "3.1.0", path = "../benchmarking" }

[features]
default = ["std"]
Expand All @@ -60,7 +60,7 @@ std = [
"sp-runtime/std",
"sp-npos-elections/std",
"sp-arithmetic/std",
"sp-election-providers/std",
"frame-election-provider-support/std",
"log/std",
]
runtime-benchmarks = [
Expand Down
103 changes: 54 additions & 49 deletions frame/election-provider-multi-phase/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Substrate.

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

// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,17 +19,16 @@

use super::*;
use crate::Module as MultiPhase;

pub use frame_benchmarking::{account, benchmarks, whitelist_account, whitelisted_caller};
use frame_benchmarking::impl_benchmark_test_suite;
use frame_support::{assert_ok, traits::OnInitialize};
use frame_system::RawOrigin;
use rand::{prelude::SliceRandom, rngs::SmallRng, SeedableRng};
use sp_election_providers::Assignment;
use frame_election_provider_support::Assignment;
use sp_arithmetic::traits::One;
use sp_runtime::InnerOf;
use sp_std::convert::TryInto;

const SEED: u32 = 0;
const SEED: u32 = 999;

/// Creates a **valid** solution with exactly the given size.
///
Expand All @@ -55,9 +54,9 @@ fn solution_with_size<T: Config>(

// first generates random targets.
let targets: Vec<T::AccountId> =
(0..size.targets).map(|i| account("Targets", i, SEED)).collect();
(0..size.targets).map(|i| frame_benchmarking::account("Targets", i, SEED)).collect();

let mut rng = SmallRng::seed_from_u64(999u64);
let mut rng = SmallRng::seed_from_u64(SEED as u64);

// decide who are the winners.
let winners = targets
Expand All @@ -75,7 +74,7 @@ fn solution_with_size<T: Config>(
.choose_multiple(&mut rng, <CompactOf<T>>::LIMIT)
.cloned()
.collect::<Vec<_>>();
let voter = account::<T::AccountId>("Voter", i, SEED);
let voter = frame_benchmarking::account::<T::AccountId>("Voter", i, SEED);
(voter, stake, winner_votes)
})
.collect::<Vec<_>>();
Expand All @@ -89,7 +88,7 @@ fn solution_with_size<T: Config>(
.choose_multiple(&mut rng, <CompactOf<T>>::LIMIT)
.cloned()
.collect::<Vec<T::AccountId>>();
let voter = account::<T::AccountId>("Voter", i, SEED);
let voter = frame_benchmarking::account::<T::AccountId>("Voter", i, SEED);
(voter, stake, votes)
})
.collect::<Vec<_>>();
Expand All @@ -109,8 +108,9 @@ fn solution_with_size<T: Config>(
<DesiredTargets<T>>::put(desired_targets);
<Snapshot<T>>::put(RoundSnapshot { voters: all_voters.clone(), targets: targets.clone() });

// write the snapshot to staking or whoever is the data provider.
T::DataProvider::put_snapshot(all_voters.clone(), targets.clone());
// write the snapshot to staking or whoever is the data provider, in case it is needed further
// down the road.
T::DataProvider::put_snapshot(all_voters.clone(), targets.clone(), Some(stake));

let cache = helpers::generate_voter_cache::<T>(&all_voters);
let stake_of = helpers::stake_of_fn::<T>(&all_voters, &cache);
Expand Down Expand Up @@ -138,10 +138,12 @@ fn solution_with_size<T: Config>(
<CompactOf<T>>::from_assignment(assignments, &voter_index, &target_index).unwrap();
let score = compact.clone().score(&winners, stake_of, voter_at, target_at).unwrap();
let round = <MultiPhase<T>>::round();

assert!(score[0] > 0, "score is zero, this probably means that the stakes are not set.");
RawSolution { compact, score, round }
}

benchmarks! {
frame_benchmarking::benchmarks! {
on_initialize_nothing {
assert!(<MultiPhase<T>>::current_phase().is_off());
}: {
Expand All @@ -157,7 +159,7 @@ benchmarks! {
assert!(<MultiPhase<T>>::snapshot().is_none());
assert!(<MultiPhase<T>>::current_phase().is_off());
}: {
<MultiPhase<T>>::on_initialize_open_signed();
<MultiPhase<T>>::on_initialize_open_signed().unwrap();
} verify {
assert!(<MultiPhase<T>>::snapshot().is_some());
assert!(<MultiPhase<T>>::current_phase().is_signed());
Expand All @@ -167,29 +169,59 @@ benchmarks! {
assert!(<MultiPhase<T>>::snapshot().is_none());
assert!(<MultiPhase<T>>::current_phase().is_off());
}: {
<MultiPhase<T>>::on_initialize_open_unsigned(true, true, 1u32.into());
<MultiPhase<T>>::on_initialize_open_unsigned(true, true, 1u32.into()).unwrap();
} verify {
assert!(<MultiPhase<T>>::snapshot().is_some());
assert!(<MultiPhase<T>>::current_phase().is_unsigned());
}

on_initialize_open_unsigned_without_snapshot {
// need to assume signed phase was open before
<MultiPhase<T>>::on_initialize_open_signed();
<MultiPhase<T>>::on_initialize_open_signed().unwrap();
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
assert!(<MultiPhase<T>>::snapshot().is_some());
assert!(<MultiPhase<T>>::current_phase().is_signed());
}: {
<MultiPhase<T>>::on_initialize_open_unsigned(false, true, 1u32.into());
<MultiPhase<T>>::on_initialize_open_unsigned(false, true, 1u32.into()).unwrap();
} verify {
assert!(<MultiPhase<T>>::snapshot().is_some());
assert!(<MultiPhase<T>>::current_phase().is_unsigned());
}

// a call to `<Pallet as ElectionProvider>::elect` where we only return the queued solution.
elect_queued {
// assume largest values for the election status. These will merely affect the decoding.
let v = T::BenchmarkingConfig::VOTERS[1];
let t = T::BenchmarkingConfig::TARGETS[1];
let a = T::BenchmarkingConfig::ACTIVE_VOTERS[1];
let d = T::BenchmarkingConfig::DESIRED_TARGETS[1];

let witness = SolutionOrSnapshotSize { voters: v, targets: t };
let raw_solution = solution_with_size::<T>(witness, a, d);
let ready_solution =
<MultiPhase<T>>::feasibility_check(raw_solution, ElectionCompute::Signed).unwrap();

// these are set by the `solution_with_size` function.
assert!(<DesiredTargets<T>>::get().is_some());
assert!(<Snapshot<T>>::get().is_some());
assert!(<SnapshotMetadata<T>>::get().is_some());
<CurrentPhase<T>>::put(Phase::Signed);
// assume a queued solution is stored, regardless of where it comes from.
<QueuedSolution<T>>::put(ready_solution);
}: {
let _ = <MultiPhase<T> as ElectionProvider<T::AccountId, T::BlockNumber>>::elect();
} verify {
assert!(<MultiPhase<T>>::queued_solution().is_none());
assert!(<DesiredTargets<T>>::get().is_none());
assert!(<Snapshot<T>>::get().is_none());
assert!(<SnapshotMetadata<T>>::get().is_none());
assert_eq!(<CurrentPhase<T>>::get(), <Phase<T::BlockNumber>>::Off);
}

#[extra]
create_snapshot {
assert!(<MultiPhase<T>>::snapshot().is_none());
}: {
<MultiPhase::<T>>::create_snapshot()
<MultiPhase::<T>>::create_snapshot().unwrap()
} verify {
assert!(<MultiPhase<T>>::snapshot().is_some());
}
Expand Down Expand Up @@ -248,35 +280,8 @@ benchmarks! {
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::mock::*;

#[test]
fn test_benchmarks() {
ExtBuilder::default().build_and_execute(|| {
assert_ok!(test_benchmark_feasibility_check::<Runtime>());
});

ExtBuilder::default().build_and_execute(|| {
assert_ok!(test_benchmark_submit_unsigned::<Runtime>());
});

ExtBuilder::default().build_and_execute(|| {
assert_ok!(test_benchmark_on_initialize_open_unsigned_with_snapshot::<Runtime>());
});

ExtBuilder::default().build_and_execute(|| {
assert_ok!(test_benchmark_on_initialize_open_unsigned_without_snapshot::<Runtime>());
});

ExtBuilder::default().build_and_execute(|| {
assert_ok!(test_benchmark_on_initialize_nothing::<Runtime>());
});

ExtBuilder::default().build_and_execute(|| {
assert_ok!(test_benchmark_create_snapshot::<Runtime>());
});
}
}
impl_benchmark_test_suite!(
MultiPhase,
crate::mock::ExtBuilder::default().build(),
crate::mock::Runtime,
);
2 changes: 1 addition & 1 deletion frame/election-provider-multi-phase/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ macro_rules! log {
($level:tt, $pattern:expr $(, $values:expr)* $(,)?) => {
log::$level!(
target: $crate::LOG_TARGET,
concat!("🗳 ", $pattern) $(, $values)*
concat!("[#{:?}] 🗳 ", $pattern), <frame_system::Module<T>>::block_number() $(, $values)*
)
};
}
Expand Down
Loading