New sub-trait to mock staking miner (#11350)

* new separate config trait for staking miner

* fix some docs and stuff

* relax trait bounds

* some cleanup

* Update frame/election-provider-multi-phase/src/unsigned.rs

* add comment

* self review and fix build

* fix docs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Kian Paimani
2022-05-11 18:45:59 +01:00
committed by GitHub
parent 479dc63af4
commit d06d20d65b
7 changed files with 479 additions and 465 deletions
@@ -16,7 +16,7 @@
// limitations under the License.
use super::*;
use crate as multi_phase;
use crate::{self as multi_phase, unsigned::MinerConfig};
use frame_election_provider_support::{
data_provider, onchain, ElectionDataProvider, NposSolution, SequentialPhragmen,
};
@@ -239,6 +239,13 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = ();
}
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
pub enum MockedWeightInfo {
Basic,
Complex,
Real,
}
parameter_types! {
pub static Targets: Vec<AccountId> = vec![10, 20, 30, 40];
pub static Voters: Vec<VoterOf<Runtime>> = vec![
@@ -269,7 +276,7 @@ parameter_types! {
pub static OffchainRepeat: BlockNumber = 5;
pub static MinerMaxWeight: Weight = BlockWeights::get().max_block;
pub static MinerMaxLength: u32 = 256;
pub static MockWeightInfo: bool = false;
pub static MockWeightInfo: MockedWeightInfo = MockedWeightInfo::Real;
pub static MaxElectingVoters: VoterIndex = u32::max_value();
pub static MaxElectableTargets: TargetIndex = TargetIndex::max_value();
@@ -314,85 +321,6 @@ impl InstantElectionProvider for MockFallback {
}
}
// Hopefully this won't be too much of a hassle to maintain.
pub struct DualMockWeightInfo;
impl multi_phase::weights::WeightInfo for DualMockWeightInfo {
fn on_initialize_nothing() -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::on_initialize_nothing()
}
}
fn create_snapshot_internal(v: u32, t: u32) -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::create_snapshot_internal(v, t)
}
}
fn on_initialize_open_signed() -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::on_initialize_open_signed()
}
}
fn on_initialize_open_unsigned() -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::on_initialize_open_unsigned()
}
}
fn elect_queued(a: u32, d: u32) -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::elect_queued(a, d)
}
}
fn finalize_signed_phase_accept_solution() -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::finalize_signed_phase_accept_solution()
}
}
fn finalize_signed_phase_reject_solution() -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::finalize_signed_phase_reject_solution()
}
}
fn submit() -> Weight {
if MockWeightInfo::get() {
Zero::zero()
} else {
<() as multi_phase::weights::WeightInfo>::submit()
}
}
fn submit_unsigned(v: u32, t: u32, a: u32, d: u32) -> Weight {
if MockWeightInfo::get() {
// 10 base
// 5 per edge.
(10 as Weight).saturating_add((5 as Weight).saturating_mul(a as Weight))
} else {
<() as multi_phase::weights::WeightInfo>::submit_unsigned(v, t, a, d)
}
}
fn feasibility_check(v: u32, t: u32, a: u32, d: u32) -> Weight {
if MockWeightInfo::get() {
// 10 base
// 5 per edge.
(10 as Weight).saturating_add((5 as Weight).saturating_mul(a as Weight))
} else {
<() as multi_phase::weights::WeightInfo>::feasibility_check(v, t, a, d)
}
}
}
parameter_types! {
pub static Balancing: Option<(usize, ExtendedBalance)> = Some((0, 0));
}
@@ -410,6 +338,24 @@ impl BenchmarkingConfig for TestBenchmarkingConfig {
const MAXIMUM_TARGETS: u32 = 200;
}
impl MinerConfig for Runtime {
type AccountId = AccountId;
type MaxLength = MinerMaxLength;
type MaxWeight = MinerMaxWeight;
type MaxVotesPerVoter = <StakingMock as ElectionDataProvider>::MaxVotesPerVoter;
type Solution = TestNposSolution;
fn solution_weight(v: u32, t: u32, a: u32, d: u32) -> Weight {
match MockWeightInfo::get() {
MockedWeightInfo::Basic =>
(10 as Weight).saturating_add((5 as Weight).saturating_mul(a as Weight)),
MockedWeightInfo::Complex => (0 * v + 0 * t + 1000 * a + 0 * d) as Weight,
MockedWeightInfo::Real =>
<() as multi_phase::weights::WeightInfo>::feasibility_check(v, t, a, d),
}
}
}
impl crate::Config for Runtime {
type Event = Event;
type Currency = Balances;
@@ -419,8 +365,6 @@ impl crate::Config for Runtime {
type BetterUnsignedThreshold = BetterUnsignedThreshold;
type BetterSignedThreshold = BetterSignedThreshold;
type OffchainRepeat = OffchainRepeat;
type MinerMaxWeight = MinerMaxWeight;
type MinerMaxLength = MinerMaxLength;
type MinerTxPriority = MinerTxPriority;
type SignedRewardBase = SignedRewardBase;
type SignedDepositBase = SignedDepositBase;
@@ -432,14 +376,14 @@ impl crate::Config for Runtime {
type SlashHandler = ();
type RewardHandler = ();
type DataProvider = StakingMock;
type WeightInfo = DualMockWeightInfo;
type WeightInfo = ();
type BenchmarkingConfig = TestBenchmarkingConfig;
type Fallback = MockFallback;
type GovernanceFallback = NoFallback<Self>;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type Solution = TestNposSolution;
type MaxElectingVoters = MaxElectingVoters;
type MaxElectableTargets = MaxElectableTargets;
type MinerConfig = Self;
type Solver = SequentialPhragmen<AccountId, SolutionAccuracyOf<Runtime>, Balancing>;
}
@@ -562,7 +506,7 @@ impl ExtBuilder {
<MinerMaxWeight>::set(weight);
self
}
pub fn mock_weight_info(self, mock: bool) -> Self {
pub fn mock_weight_info(self, mock: MockedWeightInfo) -> Self {
<MockWeightInfo>::set(mock);
self
}