Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ macro_rules! log {
($level:tt, $patter:expr $(, $values:expr)* $(,)?) => {
frame_support::debug::$level!(
target: crate::LOG_TARGET,
$patter $(, $values)*
concat!("💸 ", $patter) $(, $values)*
)
};
}
Expand Down Expand Up @@ -1345,14 +1345,14 @@ decl_module! {
ElectionStatus::<T::BlockNumber>::Open(now)
);
add_weight(0, 1, 0);
log!(info, "💸 Election window is Open({:?}). Snapshot created", now);
log!(info, "Election window is Open({:?}). Snapshot created", now);
} else {
log!(warn, "💸 Failed to create snapshot at {:?}.", now);
log!(warn, "Failed to create snapshot at {:?}.", now);
}
}
}
} else {
log!(warn, "💸 Estimating next session change failed.");
log!(warn, "Estimating next session change failed.");
}
add_weight(0, 0, T::NextNewSession::weight(now))
}
Expand All @@ -1363,22 +1363,48 @@ decl_module! {
consumed_weight
}

/// Check if the current block number is the one at which the election window has been set
/// to open. If so, it runs the offchain worker code.
/// Offchain worker logic.
fn offchain_worker(now: T::BlockNumber) {
use offchain_election::{set_check_offchain_execution_status, compute_offchain_election};
use offchain_election::{
set_check_offchain_execution_status, compute_save_and_submit,
restore_or_compute_then_submit, OFFCHAIN_REPEAT,
};
// ensure that we don't run OCW in any case more at least with 5 blocks delay.
let threshold: T::BlockNumber = OFFCHAIN_REPEAT.into();

if Self::era_election_status().is_open_at(now) {
let offchain_status = set_check_offchain_execution_status::<T>(now);
if let Err(why) = offchain_status {
log!(warn, "💸 skipping offchain worker in open election window due to [{}]", why);
} else {
if let Err(e) = compute_offchain_election::<T>() {
log!(error, "💸 Error in election offchain worker: {:?}", e);
} else {
log!(debug, "💸 Executed offchain worker thread without errors.");
let election_status = Self::era_election_status();

log!(
trace,
"ocw at {:?}, election status = {:?}, queued_score = {:?}, offchain solution: {:?}",
now,
election_status,
Self::queued_score(),
offchain_election::get_solution::<T>()
.map(|call| (
offchain_election::ensure_solution_is_recent(call.clone()).is_ok(),
call.encode().len(),
))
);
match Self::era_election_status() {
ElectionStatus::Open(opened) if opened == now => {
// If era election status is open at the current block, mine a new solution
// then save and submit it.
let initial_output = set_check_offchain_execution_status::<T>(now, threshold)
.and_then(|_| compute_save_and_submit::<T>());
log!(info, "initial OCW output at {:?} = {:?}", now, initial_output);
},
ElectionStatus::Open(opened) if now > opened => {
if !QueuedScore::exists() {
// If the election window is open, and we don't have a queued solution,
// constantly try to challenge it by either resubmitting a saved solution,
// or mining a new one (just in the case that the previous was skipped).
let resubmit_output = set_check_offchain_execution_status::<T>(now, threshold)
.and_then(|_| restore_or_compute_then_submit::<T>());
log!(info, "resubmit OCW output at {:?} = {:?}", now, resubmit_output);
}
}
},
_ => {}
}
}

Expand Down Expand Up @@ -2306,7 +2332,7 @@ impl<T: Config> Module<T> {
{
log!(
warn,
"💸 Snapshot size too big [{} <> {}][{} <> {}].",
"Snapshot size too big [{} <> {}][{} <> {}].",
num_validators,
MAX_VALIDATORS,
num_nominators,
Expand Down Expand Up @@ -2626,7 +2652,7 @@ impl<T: Config> Module<T> {
validator_at,
).map_err(|e| {
// log the error since it is not propagated into the runtime error.
log!(warn, "💸 un-compacting solution failed due to {:?}", e);
log!(warn, "un-compacting solution failed due to {:?}", e);
Error::<T>::OffchainElectionBogusCompact
})?;

Expand All @@ -2641,7 +2667,7 @@ impl<T: Config> Module<T> {
// all of the indices must map to either a validator or a nominator. If this is ever
// not the case, then the locking system of staking is most likely faulty, or we
// have bigger problems.
log!(error, "💸 detected an error in the staking locking and snapshot.");
log!(error, "detected an error in the staking locking and snapshot.");
// abort.
return Err(Error::<T>::OffchainElectionBogusNominator.into());
}
Expand Down Expand Up @@ -2701,7 +2727,8 @@ impl<T: Config> Module<T> {
let exposures = Self::collect_exposure(supports);
log!(
info,
"💸 A better solution (with compute {:?} and score {:?}) has been validated and stored on chain.",
"A better solution (with compute {:?} and score {:?}) has been validated and stored \
on chain.",
compute,
submitted_score,
);
Expand Down Expand Up @@ -2901,7 +2928,7 @@ impl<T: Config> Module<T> {

log!(
info,
"💸 new validator set of size {:?} has been elected via {:?} for era {:?}",
"new validator set of size {:?} has been elected via {:?} for era {:?}",
elected_stashes.len(),
compute,
current_era,
Expand Down Expand Up @@ -2957,7 +2984,7 @@ impl<T: Config> Module<T> {
.map_err(|_|
log!(
error,
"💸 on-chain phragmen is failing due to a problem in the result. This must be a bug."
"on-chain phragmen is failing due to a problem in the result. This must be a bug."
)
)
.ok()?;
Expand Down Expand Up @@ -3025,7 +3052,7 @@ impl<T: Config> Module<T> {
// If we don't have enough candidates, nothing to do.
log!(
warn,
"💸 Chain does not have enough staking candidates to operate. Era {:?}.",
"Chain does not have enough staking candidates to operate. Era {:?}.",
Self::current_era()
);
None
Expand Down Expand Up @@ -3482,13 +3509,13 @@ impl<T: Config> frame_support::unsigned::ValidateUnsigned for Module<T> {
let invalid = to_invalid(error_with_post_info);
log!(
debug,
"💸 validate unsigned pre dispatch checks failed due to error #{:?}.",
"validate unsigned pre dispatch checks failed due to error #{:?}.",
invalid,
);
return invalid.into();
}

log!(debug, "💸 validateUnsigned succeeded for a solution at era {}.", era);
log!(debug, "validateUnsigned succeeded for a solution at era {}.", era);

ValidTransaction::with_tag_prefix("StakingOffchain")
// The higher the score[0], the better a solution is.
Expand Down
Loading