1use config::*;
2pub mod prelude;
3
4pub use prelude::*;
5
6pub fn mine<Config>(
7 method: Method,
8 voters: Vec<(
9 Config::AccountId,
10 VoteWeight,
11 BoundedVec<Config::AccountId, Config::MaxVotesPerVoter>,
12 )>,
13 targets: Vec<Config::AccountId>,
14 round: u32,
15 desired_targets: u32,
16) -> Result<RawSolution<SolutionOf<Config>>, anyhow::Error>
17where
18 Config: MinerConfig,
19{
20 let (solution, score, _solution_or_snapshot_size, _trimming_status) =
21 match method {
22 Method::SeqPhragmen => Miner::<Config>::mine_solution_with_snapshot::<
23 SeqSolver<Config>,
24 >(voters, targets, desired_targets)
25 .expect("seq miner failed"),
26 Method::PhragMMS => Miner::<Config>::mine_solution_with_snapshot::<
27 MmsSolver<Config>,
28 >(voters, targets, desired_targets)
29 .expect("mms miner failed"),
30 };
31 Ok(RawSolution {
33 solution,
34 score,
35 round,
36 })
37}
38
39pub struct Solutions {
40 pub raw_solution: Vec<u8>,
41 pub ready_solution: Vec<u8>,
42}
43
44pub fn solve(
45 chain: &str,
46 method: &str,
47 snapshot_bytes: &[u8],
48 round: u32,
49 desired_targets: u32,
50 iterations: usize,
51 tolerance: u128,
52) -> Result<Solutions, anyhow::Error> {
53 BalanceIterations::set(iterations);
54 Tolerance::set(tolerance);
55 let method = method.parse::<Method>()?;
56 let chain = chain.parse::<Chain>()?;
57 let mut snapshot_bytes = snapshot_bytes;
58 with_chain!(chain, {
59 type Snapshot = RoundSnapshot<
60 AccountId,
61 Voter<AccountId, <Config as MinerConfig>::MaxVotesPerVoter>,
62 >;
63
64 let snapshot: Snapshot = Decode::decode(&mut snapshot_bytes)?;
65
66 let raw_solution: RawSolution<SolutionOf<Config>> = mine::<Config>(
67 method,
68 snapshot.voters.clone(),
69 snapshot.targets.clone(),
70 round,
71 desired_targets,
72 )?;
73
74 let ready_solution: ReadySolution<
75 <Config as MinerConfig>::AccountId,
76 <Config as MinerConfig>::MaxWinners,
77 > = Miner::<Config>::feasibility_check(
78 raw_solution.clone(),
79 ElectionCompute::Signed,
80 desired_targets,
81 snapshot,
82 round,
83 Some(ElectionScore::default()),
84 )
85 .map_err(|e| anyhow::anyhow!("feasibility check failed: {:?}", e))?;
86
87 Ok(Solutions {
88 raw_solution: raw_solution.encode(),
89 ready_solution: ready_solution.encode(),
90 })
91 }) }