fix restart issue of staking miner (#4073)

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Kian Paimani
2021-10-14 13:12:01 +01:00
committed by GitHub
parent 56e9f5fedc
commit 51b694e05c
4 changed files with 170 additions and 138 deletions
@@ -16,7 +16,7 @@
//! The emergency-solution command.
use crate::{prelude::*, Error, SharedConfig};
use crate::{prelude::*, EmergencySolutionConfig, Error, SharedConfig};
use codec::Encode;
use frame_election_provider_support::SequentialPhragmen;
use std::io::Write;
@@ -25,25 +25,35 @@ macro_rules! emergency_solution_cmd_for { ($runtime:ident) => { paste::paste! {
/// Execute the emergency-solution command.
pub(crate) async fn [<emergency_solution_cmd_ $runtime>](
shared: SharedConfig,
config: EmergencySolutionConfig,
) -> Result<(), Error<$crate::[<$runtime _runtime_exports>]::Runtime>> {
use $crate::[<$runtime _runtime_exports>]::*;
let mut ext = crate::create_election_ext::<Runtime, Block>(shared.uri.clone(), None, vec![]).await?;
ext.execute_with(|| {
assert!(EPM::Pallet::<Runtime>::current_phase().is_emergency());
// NOTE: this internally calls feasibility_check, but we just re-do it here as an easy way
// to get a `ReadySolution`.
let (raw_solution, _) =
<EPM::Pallet<Runtime>>::mine_solution::<SequentialPhragmen<AccountId, sp_runtime::Perbill>>()?;
log::info!(target: LOG_TARGET, "mined solution with {:?}", &raw_solution.score);
let ready_solution = EPM::Pallet::<Runtime>::feasibility_check(raw_solution, EPM::ElectionCompute::Signed)?;
let encoded_ready = ready_solution.encode();
let mut ready_solution = EPM::Pallet::<Runtime>::feasibility_check(raw_solution, EPM::ElectionCompute::Signed)?;
// maybe truncate.
if let Some(take) = config.take {
log::info!(target: LOG_TARGET, "truncating {} winners to {}", ready_solution.supports.len(), take);
ready_solution.supports.sort_unstable_by_key(|(_, s)| s.total);
ready_solution.supports.truncate(take);
}
// write to file and stdout.
let encoded_support = ready_solution.supports.encode();
let mut solution_file = std::fs::File::create("solution.bin")?;
let mut supports_file = std::fs::File::create("solution.supports.bin")?;
solution_file.write_all(&encoded_ready)?;
supports_file.write_all(&encoded_support)?;
log::info!(target: LOG_TARGET, "ReadySolution: size {:?} / score = {:?}", encoded_ready.len(), ready_solution.score);
log::info!(target: LOG_TARGET, "ReadySolution: size {:?} / score = {:?}", ready_solution.encoded_size(), ready_solution.score);
log::trace!(target: LOG_TARGET, "Supports: {}", sp_core::hexdisplay::HexDisplay::from(&encoded_support));
Ok(())
})
}