Files
pezkuwi-subxt/substrate/core/phragmen/benches/phragmen.rs
T
Robert Habermeier c200ce757b Fixing BABE epochs to change between blocks (#3583)
* always fetch epoch from runtime

* node integration tests don't test light nodes

* give stand-in full node a FULL role

* rejig babe APIs

* introduce next-epoch-descriptor type

* overhaul srml-BABE epoch logic

* ensure VRF outputs end up in the right epoch-randomness

* rewrite `do_initialize` to remove unnecessary loop

* begin accounting for next epoch in epoch function

* slots passes header to epoch_data

* pass slot_number to SlotWorker::epoch_data

* begin extracting epoch-change logic into its own module

* aux methods for block weight

* aux methods for genesis configuration

* comment-out most, refactor header-check pipeline

* mostly flesh out verifier again

* reinstantiate babe BlockImport implementation

* reinstate import-queue instantiation

* reintroduce slot-worker implementation

* reinstate pretty much all the rest

* move fork-choice logic to BlockImport

* fix some, but not all errors

* patch test-runtime

* make is_descendent of slightly more generic

* get skeleton compiling when passing is_descendent_of

* make descendent-of-builder more succinct

* restore ordering of authority_index / slot_number

* start fiddling with tests

* fix warnings

* improve initialization architecture and handle genesis

* tests use correct block-import

* fix BABE tests

* fix some compiler errors

* fix node-cli compilation

* all crates compile

* bump runtime versions and fix some warnings

* tweak fork-tree search implementation

* do backtracking search in fork-tree

* node-cli integration tests now work

* fix broken assumption in test_connectivity

* babe tests fail for the right reasons.

* test genesis epoch logic for epoch_changes

* test that epochs can change between blocks

* First BABE SRML test

* Testing infrastructure for BABE

Also includes a trivial additional test.

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* A little more test progress

* More work on BABE testing

* Try to get the tests working

* Implement `UintAuthorityId`-based test mocks

* Fix compilation errors

* Adjust to upstream changes

* Block numbers are ignored in BABE epoch calculation

* authority_index() should ignore invalid authorities

* Fix compile error

* Add tests that session transitions happen

* Check if BABE produces logs

It currently does not.

* Fix test suite

This was really nasty, due to a type confusion that showed up as an
off-by-1 buffer error.

* Add additional tests

Most of these were derived from the current output, so they are only
useful to guard against regressions.

* Make the tests more readable

Also bump impl_version.

* Fix excessive line width

* Remove unused imports

* Update srml/babe/src/lib.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* try to fix imports

* Fix build errors in test suite

* tests did not pass

* Try to get at least one digest to be output

Currently, the code emits either no digests (if I don’t call
`Session::rotate_session()` or two digests (if I do), which is wrong.

* More tests

They still don’t work, but this should help debugging.

* fix silly error

* Don’t even try to compile a broken test

* remove broken check_epoch test and add one for genesis epoch

* Check that the length of the pre-digests is correct

* Bump `impl_version`

* use epoch_for_descendent_of even for genesis

* account for competing block 1s

* finish srml-babe docs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* address grumbles
2019-09-23 16:03:05 +02:00

199 lines
6.5 KiB
Rust

// Copyright 2019 Parity Technologies
//
// 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 of the phragmen election algorithm.
//! Note that execution times will not be accurate in an absolute scale, since
//! - Everything is executed in the context of `TestExternalities`
//! - Everything is executed in native environment.
#![cfg(feature = "bench")]
#![feature(test)]
extern crate test;
use test::Bencher;
use rand::{self, Rng};
extern crate substrate_phragmen as phragmen;
use phragmen::{Support, SupportMap, ACCURACY};
use std::collections::BTreeMap;
use sr_primitives::traits::{Convert, SaturatedConversion};
const VALIDATORS: u64 = 1000;
const NOMINATORS: u64 = 10_000;
const EDGES: u64 = 2;
const TO_ELECT: usize = 100;
const STAKE: Balance = 1000;
type Balance = u128;
type AccountId = u64;
pub struct TestCurrencyToVote;
impl Convert<Balance, u64> for TestCurrencyToVote {
fn convert(x: Balance) -> u64 { x.saturated_into() }
}
impl Convert<u128, Balance> for TestCurrencyToVote {
fn convert(x: u128) -> Balance { x.saturated_into() }
}
fn do_phragmen(
b: &mut Bencher,
num_vals: u64,
num_noms: u64,
count: usize,
votes_per: u64,
eq_iters: usize,
_eq_tolerance: u128,
) {
assert!(num_vals > votes_per);
let rr = |a, b| rand::thread_rng().gen_range(a as usize, b as usize) as Balance;
// prefix to distinguish the validator and nominator account ranges.
let np = 10_000;
let mut candidates = vec![];
let mut voters = vec![];
let mut slashable_balance_of: BTreeMap<AccountId, Balance> = BTreeMap::new();
(1 ..= num_vals)
.for_each(|acc| {
candidates.push(acc);
slashable_balance_of.insert(acc, STAKE + rr(10, 50));
});
(np ..= (np + num_noms))
.for_each(|acc| {
let mut stashes_to_vote = candidates.clone();
let votes = (0 .. votes_per)
.map(|_| {
stashes_to_vote.remove(rr(0, stashes_to_vote.len()) as usize)
})
.collect::<Vec<AccountId>>();
voters.push((acc, votes));
slashable_balance_of.insert(acc, STAKE + rr(10, 50));
});
let slashable_balance = |who: &AccountId| -> Balance {
*slashable_balance_of.get(who).unwrap()
};
b.iter(|| {
let r = phragmen::elect::<AccountId, Balance, _, TestCurrencyToVote>(
count,
1_usize,
candidates.clone(),
voters.clone(),
slashable_balance,
true,
).unwrap();
// Do the benchmarking with equalize.
if eq_iters > 0 {
let elected_stashes = r.winners;
let mut assignments = r.assignments;
let to_votes = |b: Balance|
<TestCurrencyToVote as Convert<Balance, u128>>::convert(b) as u128;
let ratio_of = |b, r: u128| r.saturating_mul(to_votes(b)) / ACCURACY;
// Initialize the support of each candidate.
let mut supports = <SupportMap<u64>>::new();
elected_stashes
.iter()
.map(|(e, _)| (e, to_votes(slashable_balance(e))))
.for_each(|(e, s)| {
let item = Support { own: s, total: s, ..Default::default() };
supports.insert(e.clone(), item);
});
for (n, assignment) in assignments.iter_mut() {
for (c, r) in assignment.iter_mut() {
let nominator_stake = slashable_balance(n);
let other_stake = ratio_of(nominator_stake, *r);
if let Some(support) = supports.get_mut(c) {
support.total = support.total.saturating_add(other_stake);
support.others.push((n.clone(), other_stake));
}
*r = other_stake;
}
}
let tolerance = 0_u128;
let iterations = 2_usize;
phragmen::equalize::<_, _, _, TestCurrencyToVote>(
assignments,
&mut supports,
tolerance,
iterations,
slashable_balance,
);
}
})
}
macro_rules! phragmen_benches {
($($name:ident: $tup:expr,)*) => {
$(
#[bench]
fn $name(b: &mut Bencher) {
let (v, n, t, e, eq_iter, eq_tol) = $tup;
println!("----------------------");
println!(
"++ Benchmark: {} Validators // {} Nominators // {} Edges-per-nominator // {} \
total edges // electing {} // Equalize: {} iterations -- {} tolerance",
v, n, e, e * n, t, eq_iter, eq_tol,
);
do_phragmen(b, v, n, t, e, eq_iter, eq_tol);
}
)*
}
}
phragmen_benches! {
bench_1_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_2: (VALIDATORS*2, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_3: (VALIDATORS*4, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_4: (VALIDATORS*8, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_1_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_1_2_eq: (VALIDATORS*2, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_1_3_eq: (VALIDATORS*4, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_1_4_eq: (VALIDATORS*8, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_0_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_0_2: (VALIDATORS, NOMINATORS, TO_ELECT * 4, EDGES, 0, 0),
bench_0_3: (VALIDATORS, NOMINATORS, TO_ELECT * 8, EDGES, 0, 0),
bench_0_4: (VALIDATORS, NOMINATORS, TO_ELECT * 16, EDGES , 0, 0),
bench_0_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_0_2_eq: (VALIDATORS, NOMINATORS, TO_ELECT * 4, EDGES, 2, 0),
bench_0_3_eq: (VALIDATORS, NOMINATORS, TO_ELECT * 8, EDGES, 2, 0),
bench_0_4_eq: (VALIDATORS, NOMINATORS, TO_ELECT * 16, EDGES , 2, 0),
bench_2_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0),
bench_2_2: (VALIDATORS, NOMINATORS*2, TO_ELECT, EDGES, 0, 0),
bench_2_3: (VALIDATORS, NOMINATORS*4, TO_ELECT, EDGES, 0, 0),
bench_2_4: (VALIDATORS, NOMINATORS*8, TO_ELECT, EDGES, 0, 0),
bench_2_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_2_2_eq: (VALIDATORS, NOMINATORS*2, TO_ELECT, EDGES, 2, 0),
bench_2_3_eq: (VALIDATORS, NOMINATORS*4, TO_ELECT, EDGES, 2, 0),
bench_2_4_eq: (VALIDATORS, NOMINATORS*8, TO_ELECT, EDGES, 2, 0),
bench_3_1: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 0, 0 ),
bench_3_2: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*2, 0, 0),
bench_3_3: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*4, 0, 0),
bench_3_4: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*8, 0, 0),
bench_3_1_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES, 2, 0),
bench_3_2_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*2, 2, 0),
bench_3_3_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*4, 2, 0),
bench_3_4_eq: (VALIDATORS, NOMINATORS, TO_ELECT, EDGES*8, 2, 0),
}