Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
25bddc9
mockup
kianenigma Sep 11, 2020
7bef52f
Add files
kianenigma Sep 11, 2020
4f3dfa4
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Sep 23, 2020
bc30ce1
More mockup
kianenigma Sep 24, 2020
9e52fbc
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 1, 2020
14ebc95
Lots more changes and tests. Initial design of signed is forseable now.
kianenigma Oct 2, 2020
0587fc9
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 8, 2020
233d4e1
More changes for the signed phase, now going to the cluster-fu*k of g…
kianenigma Oct 9, 2020
9c880f3
Finally decoupled solution from the pallet.. such a PITA.
kianenigma Oct 9, 2020
0cb35eb
Much simplifications and cleanups.
kianenigma Oct 9, 2020
c1ff3ec
A skeleton for the unsigned version.
kianenigma Oct 9, 2020
85a346e
Master.into()
kianenigma Oct 13, 2020
6a2542d
Staking builds and tests compile, some tests fail
kianenigma Oct 13, 2020
c361911
All staking tests work
kianenigma Oct 14, 2020
218db10
Unsigned almost done, primitive crate added.
kianenigma Oct 15, 2020
5e21a79
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 15, 2020
25c1bc3
Use decl_error
kianenigma Oct 15, 2020
d53785c
Add basic benchmark setup, needs more work
kianenigma Oct 16, 2020
525f6e0
Comment.
kianenigma Oct 16, 2020
3026316
Checkpoint.
kianenigma Oct 20, 2020
9841e3b
node-runtime builds.
kianenigma Oct 20, 2020
16c0550
Builds and all good?
kianenigma Dec 3, 2020
f197b6b
Undo some formatting mistakes
kianenigma Dec 3, 2020
5005281
Working on benchmarks'
kianenigma Dec 7, 2020
bd0d331
Fix bench
kianenigma Dec 8, 2020
67dd4d1
Strange merge -- everything builds.
kianenigma Dec 8, 2020
eac4571
Everything compiles and good -- need to fix benches.
kianenigma Dec 8, 2020
a7c4841
Some Staking stuff left to do.
kianenigma Dec 9, 2020
2ca3dcf
Master.into()
kianenigma Dec 14, 2020
4922897
Split the snapshot; type for CompactSolution and MaxNominations
kianenigma Dec 15, 2020
ef593a7
Fix a bunch more issues.
kianenigma Dec 15, 2020
8661814
check new design
kianenigma Dec 18, 2020
57f7294
Everything within the module works and compiles.
kianenigma Dec 21, 2020
af304d1
Stkaing also builds
kianenigma Dec 21, 2020
80e7450
Everything builds with the new scheme.
kianenigma Dec 25, 2020
4b4712a
Master.into()
kianenigma Dec 26, 2020
206933a
Simplify elect API.
kianenigma Dec 26, 2020
af9b97d
Fix all other tests.
kianenigma Dec 26, 2020
9663fea
make the staking api better and well tested.
kianenigma Jan 4, 2021
7f4f5b7
Almost done with conversion
kianenigma Jan 4, 2021
8fb11f1
Move to pallet V2 + construct_runtime
kianenigma Jan 5, 2021
bd6d4d9
Master.into()
kianenigma Jan 5, 2021
65f5ac1
Revert staking changes;
kianenigma Jan 5, 2021
3860885
Revert Staking fuzzer.
kianenigma Jan 5, 2021
392f35a
Fix fuzzers
kianenigma Jan 5, 2021
1337e1b
Update frame/election-providers/src/two_phase/mod.rs
kianenigma Jan 6, 2021
4f6ec0d
a round of self review
kianenigma Jan 6, 2021
b3113f7
A large number of bugfixes after local test and self-review
kianenigma Jan 7, 2021
abca039
Upstream.into()
kianenigma Jan 7, 2021
6bc578a
Master.into()
kianenigma Jan 7, 2021
876f666
Apply suggestions from code review
kianenigma Jan 7, 2021
81df1df
Update frame/election-providers/src/two_phase/benchmarking.rs
kianenigma Jan 7, 2021
e3862ce
Fix some review grumbles.
kianenigma Jan 8, 2021
ca06f8e
Fix some docs
kianenigma Jan 8, 2021
6206c32
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 8, 2021
95d7a3c
Master.into()
kianenigma Jan 8, 2021
63776ff
Maybe relocalte the code?
kianenigma Jan 8, 2021
4732854
Master.into()
kianenigma Jan 8, 2021
a922f0b
Witness -> SoltionSize
kianenigma Jan 8, 2021
6d8247a
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 8, 2021
e9f4437
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 8, 2021
02069e1
Update frame/session/src/lib.rs
kianenigma Jan 12, 2021
000ecdc
Revamp macros
kianenigma Jan 12, 2021
cb6f156
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 12, 2021
df04ef3
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 12, 2021
ee2a316
Master.into()
kianenigma Jan 12, 2021
0d92ed5
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 12, 2021
88a96a0
Fix build
kianenigma Jan 12, 2021
6480ce1
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 12, 2021
8a68915
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 12, 2021
f4e2c03
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Jan 12, 2021
c41ecb0
fix all the tests.
kianenigma Jan 13, 2021
b42336a
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 13, 2021
f5e23f9
Fix the strange issue with cargo lock
kianenigma Jan 13, 2021
56bb93c
revamp closures.
kianenigma Jan 14, 2021
257795c
Update frame/two-phase-election-provider/src/benchmarking.rs
kianenigma Jan 14, 2021
9c114bc
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 14, 2021
1ac97f5
Some review grumbles.
kianenigma Jan 14, 2021
366b15b
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Jan 14, 2021
afa5507
Upstream.into()
kianenigma Jan 14, 2021
5d206ab
Fix build
kianenigma Jan 14, 2021
ea193c7
Bring back benches
kianenigma Jan 14, 2021
60e57d7
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 14, 2021
77f53a3
Add a bunch of more fixes: signed weight check, tests etc.
kianenigma Jan 15, 2021
9ce9d6b
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 15, 2021
b30dfb3
Fix build
kianenigma Jan 15, 2021
6001d7d
Master.into()
kianenigma Jan 15, 2021
a066e4a
Last small tweaks
kianenigma Jan 15, 2021
10f6e7b
Fix all tests.
kianenigma Jan 15, 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
Prev Previous commit
Next Next commit
Much simplifications and cleanups.
  • Loading branch information
kianenigma committed Oct 9, 2020
commit 0cb35eb3deffb32e05469f325dd06accdc71d7b9
2 changes: 1 addition & 1 deletion frame/election-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub trait ElectionProvider<AccountId> {
///
/// The implementation should, if possible, use the accuracy `P` to compute the election result.
fn elect<P: PerThing128>(
to_elect: usize, // TODO: consider making this u32
to_elect: usize,
targets: Vec<AccountId>,
voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
) -> Result<FlatSupportMap<AccountId>, Self::Error>
Expand Down
9 changes: 1 addition & 8 deletions frame/election-providers/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ impl<AccountId: IdentifierT> ElectionProvider<AccountId> for OnChainSequentialPh
where
ExtendedBalance: From<<P as PerThing>::Inner>,
{
// TODO: we really don't need to do this conversion all the time. With
// https://github.com/paritytech/substrate/pull/6685 merged, we should make variants of
// seq_phragmen and others that return a different return type. In fact, I think I should
// rebase this branch there and just build there as well.
// TODO: Okay even if not the above, then def. make the extension traits for converting
// between validator Major and Nominator Major result types, and make the conversions be
// lossless and painless to happen.

let mut stake_map: BTreeMap<AccountId, VoteWeight> = BTreeMap::new();
voters.iter().for_each(|(v, s, _)| {
stake_map.insert(v.clone(), *s);
Expand All @@ -49,6 +41,7 @@ impl<AccountId: IdentifierT> ElectionProvider<AccountId> for OnChainSequentialPh

sp_npos_elections::seq_phragmen::<_, P>(to_elect, targets, voters, None)
.and_then(|e| {
// these could use potential simplifications.
let ElectionResult {
winners,
assignments,
Expand Down
19 changes: 17 additions & 2 deletions frame/election-providers/src/two_phase/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,23 @@ impl From<sp_npos_elections::Error> for FeasibilityError {
}

/// The weights for this pallet.
pub trait WeightInfo {}
impl WeightInfo for () {}
pub trait WeightInfo {
fn feasibility_check() -> Weight;
fn submit() -> Weight;
fn submit_unsigned() -> Weight;
}

impl WeightInfo for () {
fn feasibility_check() -> Weight {
Default::default()
}
fn submit() -> Weight {
Default::default()
}
fn submit_unsigned() -> Weight {
Default::default()
}
}

pub trait Trait: frame_system::Trait {
/// Event type.
Expand Down
92 changes: 42 additions & 50 deletions frame/election-providers/src/two_phase/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ where
DesiredTargets::put(desired_targets);
}

/// Finish the singed phase.
/// Finish the singed phase. Process the signed submissions from best to worse until a valid one
/// is found, rewarding the best oen and slashing the invalid ones along the way.
///
/// Returns true if we have a good solution in the signed phase.
///
/// This drains the [`SignedSubmissions`], potentially storing the best valid one in
/// [`QueuedSolution`].
pub fn finalize_signed_phase() -> bool {
let mut all_submission: Vec<SignedSubmission<_, _, _>> = <SignedSubmissions<T>>::take();
let mut found_solution = false;
Expand Down Expand Up @@ -85,7 +89,6 @@ where
// Any unprocessed solution is not pointless to even ponder upon. Feasible or malicious,
// they didn't end up being used. Unreserve the bonds.
all_submission.into_iter().for_each(|not_processed| {
dbg!(&not_processed);
let SignedSubmission { who, deposit, .. } = not_processed;
let _remaining = T::Currency::unreserve(&who, deposit);
debug_assert!(_remaining.is_zero());
Expand All @@ -96,21 +99,20 @@ where

/// Find a proper position in the queue for the signed queue, whilst maintaining the order of
/// solution quality.
///
/// The length of the queue will always be kept less than or equal to `T::MaxSignedSubmissions`.
pub fn insert_submission(
who: &T::AccountId,
queue: &mut Vec<SignedSubmission<T::AccountId, BalanceOf<T>, CompactOf<T>>>,
solution: RawSolution<CompactOf<T>>,
) -> Option<usize> {
// TODO: consider using VecDeQue or sth like that?

// from the last score, compare and see if the current one is better. If none, then the
// awarded index is 0.
let outcome = queue
.iter()
.enumerate()
.rev()
.find_map(|(i, s)| {

if is_score_better::<Perbill>(
solution.score,
s.solution.score,
Expand All @@ -136,7 +138,9 @@ where
reward,
solution,
};
// TODO: write proof that this cannot panic
// Proof: `at` must always less than or equal queue.len() for this not to panic.
// It is either 0 (in which case `0 <= queue.len()`) or one of the queue indices
// + 1. The biggest queue index is `queue.len() - 1`, thus `at <= queue.len()`.
queue.insert(at, submission);
if queue.len() as u32 > T::MaxSignedSubmissions::get() {
queue.remove(0);
Expand All @@ -151,12 +155,25 @@ where
outcome
}

/// Collect sufficient deposit to store this solution this chain.
///
/// The deposit is composed of 3 main elements:
///
/// 1. base deposit, fixed for all submissions.
/// 2. a per-byte deposit, for renting the state usage.
/// 3. a per-weight deposit, for the potential weight usage in an upcoming on_initialize
pub fn deposit_for(solution: &RawSolution<CompactOf<T>>) -> BalanceOf<T> {
let encoded_len: BalanceOf<T> = solution.using_encoded(|e| e.len() as u32).into();
// TODO
T::SignedDepositBase::get() + T::SignedDepositByte::get() * encoded_len
let feasibility_weight = T::WeightInfo::feasibility_check();

let len_deposit = T::SignedDepositByte::get() * encoded_len;
let weight_deposit = T::SignedDepositWeight::get() * feasibility_weight.saturated_into();

T::SignedDepositBase::get() + len_deposit + weight_deposit
}

/// The reward for this solution, if successfully chosen as the best one at the end of the
/// signed phase.
pub fn reward_for(solution: &RawSolution<CompactOf<T>>) -> BalanceOf<T> {
T::SignedRewardBase::get()
+ T::SignedRewardFactor::get() * solution.score[0].saturated_into::<BalanceOf<T>>()
Expand Down Expand Up @@ -410,85 +427,60 @@ mod tests {
ExtBuilder::default()
.max_signed_submission(3)
.build_and_execute(|| {
let scores = || TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>();
roll_to(5);

let solution = RawSolution {
score: [5, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(
TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>(),
vec![5]
);
assert_eq!(scores(), vec![5]);

let solution = RawSolution {
score: [8, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(
TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>(),
vec![5, 8]
);
assert_eq!(scores(), vec![5, 8]);

let solution = RawSolution {
score: [3, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(
TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>(),
vec![3, 5, 8]
);
assert_eq!(scores(), vec![3, 5, 8]);

let solution = RawSolution {
score: [6, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(
TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>(),
vec![5, 6, 8]
);
assert_eq!(scores(), vec![5, 6, 8]);

let solution = RawSolution {
score: [6, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(
TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>(),
vec![6, 6, 8]
);
assert_eq!(scores(), vec![6, 6, 8]);

let solution = RawSolution {
score: [10, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(
TwoPhase::signed_submissions()
.into_iter()
.map(|s| s.solution.score[0])
.collect::<Vec<_>>(),
vec![6, 8, 10]
);
assert_eq!(scores(), vec![6, 8, 10]);

let solution = RawSolution {
score: [12, 0, 0],
..Default::default()
};
assert_ok!(TwoPhase::submit(Origin::signed(99), solution));
assert_eq!(scores(), vec![8, 10, 12]);
})
}

Expand Down