Skip to content

Commit 72841b7

Browse files
committed
refactor into MinerConfig
1 parent deb7daa commit 72841b7

14 files changed

Lines changed: 834 additions & 749 deletions

File tree

substrate/bin/node/runtime/src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -914,14 +914,31 @@ pub(crate) mod multi_block_impls {
914914
parameter_types! {
915915
pub Pages: u32 = 4;
916916
// nominators snapshot size
917-
pub VoterSnapshotPerBlock: u32 = 22500 / 4;
917+
pub VoterSnapshotPerBlock: u32 = 22500 / Pages::get();
918918
// validator snapshot size
919919
pub TargetSnapshotPerBlock: u32 = 1000;
920920
pub SignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4;
921-
pub SignedValidation: u32 = 8;
921+
pub SignedValidation: u32 = Pages::get() * 2;
922922
pub UnsignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4;
923923
}
924924

925+
impl multi_block::unsigned::miner::MinerConfig for Runtime {
926+
type AccountId = AccountId;
927+
type Hash = Hash;
928+
type MaxBackersPerWinner = <Self as multi_block::verifier::Config>::MaxBackersPerWinner;
929+
type MaxBackersPerWinnerFinal =
930+
<Self as multi_block::verifier::Config>::MaxBackersPerWinnerFinal;
931+
type MaxWinnersPerPage = <Self as multi_block::verifier::Config>::MaxWinnersPerPage;
932+
type MaxVotesPerVoter =
933+
<<Self as multi_block::Config>::DataProvider as ElectionDataProvider>::MaxVotesPerVoter;
934+
type MaxLength = MinerMaxLength;
935+
type Solver = <Runtime as multi_block::unsigned::Config>::OffchainSolver;
936+
type Pages = Pages;
937+
type Solution = NposSolution16;
938+
type VoterSnapshotPerBlock = <Runtime as multi_block::Config>::VoterSnapshotPerBlock;
939+
type TargetSnapshotPerBlock = <Runtime as multi_block::Config>::TargetSnapshotPerBlock;
940+
}
941+
925942
impl multi_block::Config for Runtime {
926943
type AdminOrigin = EnsureRoot<AccountId>;
927944
type RuntimeEvent = RuntimeEvent;
@@ -939,10 +956,10 @@ pub(crate) mod multi_block_impls {
939956
// TODO: sanity check that the length of all phases is within reason.
940957
type SignedPhase = SignedPhase;
941958
type UnsignedPhase = UnsignedPhase;
942-
type Solution = NposSolution16;
943959
type TargetSnapshotPerBlock = TargetSnapshotPerBlock;
944960
type VoterSnapshotPerBlock = VoterSnapshotPerBlock;
945961
type Verifier = MultiBlockVerifier;
962+
type MinerConfig = Self;
946963
type WeightInfo = ();
947964
}
948965

@@ -979,8 +996,6 @@ pub(crate) mod multi_block_impls {
979996
impl multi_block::unsigned::Config for Runtime {
980997
// TODO: split into MinerConfig so the staker miner can easily configure these.
981998
// miner configs.
982-
type MinerMaxLength = MinerMaxLength;
983-
type MinerMaxWeight = MinerMaxWeight;
984999
type OffchainSolver = <Runtime as multi_phase::Config>::Solver;
9851000

9861001
// offchain usage of miner configs
@@ -1032,7 +1047,7 @@ parameter_types! {
10321047
pub ElectionBoundsMultiPhase: ElectionBounds = ElectionBoundsBuilder::default()
10331048
.voters_count(5000.into()).targets_count(10.into()).build();
10341049
pub ElectionBoundsOnChain: ElectionBounds = ElectionBoundsBuilder::default()
1035-
.voters_count(50_000.into()).targets_count(1000.into()).build();
1050+
.voters_count(1000.into()).targets_count(1000.into()).build();
10361051

10371052
pub MaxNominations: u32 = <NposSolution16 as frame_election_provider_support::NposSolution>::LIMIT as u32;
10381053
/// The maximum winners that can be elected by the Election pallet which is equivalent to the

substrate/frame/election-provider-multi-block/src/helpers.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
2020
use crate::{
2121
types::{PageIndex, VoterOf},
22-
AllVoterPagesOf, Config, SolutionTargetIndexOf, SolutionVoterIndexOf, VoteWeight,
22+
unsigned::miner::MinerConfig,
23+
AllVoterPagesOf, SolutionTargetIndexOf, SolutionVoterIndexOf, VoteWeight,
2324
};
2425
use frame_support::{traits::Get, BoundedVec};
2526
use sp_runtime::SaturatedConversion;
@@ -35,6 +36,7 @@ macro_rules! log {
3536
};
3637
}
3738

39+
#[macro_export]
3840
macro_rules! sublog {
3941
($level:tt, $sub_pallet:tt, $pattern:expr $(, $values:expr)* $(,)?) => {
4042
#[cfg(not(feature = "std"))]
@@ -47,8 +49,18 @@ macro_rules! sublog {
4749
};
4850
}
4951

52+
#[macro_export]
53+
macro_rules! miner_log {
54+
($level:tt, $pattern:expr $(, $values:expr)* $(,)?) => {
55+
log::$level!(
56+
target: $crate::LOG_PREFIX,
57+
concat!("[⛏️miner] 🗳🗳🗳 ", $pattern) $(, $values)*
58+
)
59+
};
60+
}
61+
5062
/// Generate an `efficient closure of voters and the page in which they live in.
51-
pub fn generate_voter_page_fn<T: Config>(
63+
pub fn generate_voter_page_fn<T: MinerConfig>(
5264
paged_snapshot: &AllVoterPagesOf<T>,
5365
) -> impl Fn(&T::AccountId) -> Option<PageIndex> {
5466
let mut cache: BTreeMap<T::AccountId, PageIndex> = BTreeMap::new();
@@ -73,7 +85,7 @@ pub fn generate_voter_page_fn<T: Config>(
7385
/// voters.
7486
///
7587
/// This can be used to efficiently build index getter closures.
76-
pub fn generate_voter_cache<T: Config, AnyBound: Get<u32>>(
88+
pub fn generate_voter_cache<T: MinerConfig, AnyBound: Get<u32>>(
7789
snapshot: &BoundedVec<VoterOf<T>, AnyBound>,
7890
) -> BTreeMap<T::AccountId, usize> {
7991
let mut cache: BTreeMap<T::AccountId, usize> = BTreeMap::new();
@@ -94,7 +106,7 @@ pub fn generate_voter_cache<T: Config, AnyBound: Get<u32>>(
94106
/// ## Warning
95107
///
96108
/// Note that this will represent the snapshot data from which the `cache` is generated.
97-
pub fn voter_index_fn<T: Config>(
109+
pub fn voter_index_fn<T: MinerConfig>(
98110
cache: &BTreeMap<T::AccountId, usize>,
99111
) -> impl Fn(&T::AccountId) -> Option<SolutionVoterIndexOf<T>> + '_ {
100112
move |who| {
@@ -108,7 +120,7 @@ pub fn voter_index_fn<T: Config>(
108120
///
109121
/// Same as [`voter_index_fn`] but the returned function owns all its necessary data; nothing is
110122
/// borrowed.
111-
pub fn voter_index_fn_owned<T: Config>(
123+
pub fn voter_index_fn_owned<T: MinerConfig>(
112124
cache: BTreeMap<T::AccountId, usize>,
113125
) -> impl Fn(&T::AccountId) -> Option<SolutionVoterIndexOf<T>> {
114126
move |who| {
@@ -123,7 +135,7 @@ pub fn voter_index_fn_owned<T: Config>(
123135
/// ## Warning
124136
///
125137
/// Note that this will represent the snapshot data from which the `cache` is generated.
126-
pub fn voter_index_fn_usize<T: Config>(
138+
pub fn voter_index_fn_usize<T: MinerConfig>(
127139
cache: &BTreeMap<T::AccountId, usize>,
128140
) -> impl Fn(&T::AccountId) -> Option<usize> + '_ {
129141
move |who| cache.get(who).cloned()
@@ -136,7 +148,7 @@ pub fn voter_index_fn_usize<T: Config>(
136148
///
137149
/// Not meant to be used in production.
138150
#[cfg(test)]
139-
pub fn voter_index_fn_linear<T: Config>(
151+
pub fn voter_index_fn_linear<T: MinerConfig>(
140152
snapshot: &Vec<VoterOf<T>>,
141153
) -> impl Fn(&T::AccountId) -> Option<SolutionVoterIndexOf<T>> + '_ {
142154
move |who| {
@@ -154,7 +166,7 @@ pub fn voter_index_fn_linear<T: Config>(
154166
/// Note: to the extent possible, the returned function should be cached and reused. Producing that
155167
/// function requires a `O(n log n)` data transform. Each invocation of that function completes
156168
/// in `O(log n)`.
157-
pub fn target_index_fn<T: Config>(
169+
pub fn target_index_fn<T: MinerConfig>(
158170
snapshot: &Vec<T::AccountId>,
159171
) -> impl Fn(&T::AccountId) -> Option<SolutionTargetIndexOf<T>> + '_ {
160172
let cache: BTreeMap<_, _> =
@@ -174,7 +186,7 @@ pub fn target_index_fn<T: Config>(
174186
///
175187
/// Not meant to be used in production.
176188
#[cfg(test)]
177-
pub fn target_index_fn_linear<T: Config>(
189+
pub fn target_index_fn_linear<T: MinerConfig>(
178190
snapshot: &Vec<T::AccountId>,
179191
) -> impl Fn(&T::AccountId) -> Option<SolutionTargetIndexOf<T>> + '_ {
180192
move |who| {
@@ -187,7 +199,7 @@ pub fn target_index_fn_linear<T: Config>(
187199

188200
/// Create a function that can map a voter index ([`SolutionVoterIndexOf`]) to the actual voter
189201
/// account using a linearly indexible snapshot.
190-
pub fn voter_at_fn<T: Config>(
202+
pub fn voter_at_fn<T: MinerConfig>(
191203
snapshot: &Vec<VoterOf<T>>,
192204
) -> impl Fn(SolutionVoterIndexOf<T>) -> Option<T::AccountId> + '_ {
193205
move |i| {
@@ -199,7 +211,7 @@ pub fn voter_at_fn<T: Config>(
199211

200212
/// Create a function that can map a target index ([`SolutionTargetIndexOf`]) to the actual target
201213
/// account using a linearly indexible snapshot.
202-
pub fn target_at_fn<T: Config>(
214+
pub fn target_at_fn<T: MinerConfig>(
203215
snapshot: &Vec<T::AccountId>,
204216
) -> impl Fn(SolutionTargetIndexOf<T>) -> Option<T::AccountId> + '_ {
205217
move |i| {
@@ -213,7 +225,7 @@ pub fn target_at_fn<T: Config>(
213225
///
214226
/// This is not optimized and uses a linear search.
215227
#[cfg(test)]
216-
pub fn stake_of_fn_linear<T: Config>(
228+
pub fn stake_of_fn_linear<T: MinerConfig>(
217229
snapshot: &Vec<VoterOf<T>>,
218230
) -> impl Fn(&T::AccountId) -> VoteWeight + '_ {
219231
move |who| {
@@ -231,7 +243,7 @@ pub fn stake_of_fn_linear<T: Config>(
231243
///
232244
/// The cache need must be derived from the same snapshot. Zero is returned if a voter is
233245
/// non-existent.
234-
pub fn stake_of_fn<'a, T: Config, AnyBound: Get<u32>>(
246+
pub fn stake_of_fn<'a, T: MinerConfig, AnyBound: Get<u32>>(
235247
snapshot: &'a BoundedVec<VoterOf<T>, AnyBound>,
236248
cache: &'a BTreeMap<T::AccountId, usize>,
237249
) -> impl Fn(&T::AccountId) -> VoteWeight + 'a {

0 commit comments

Comments
 (0)