Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
use local storage; clear on ElectionFinalized
Browse files Browse the repository at this point in the history
  • Loading branch information
coriolinus committed Mar 29, 2021
1 parent cc70d37 commit 4b46a93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion frame/election-provider-multi-phase/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,6 @@ frame_benchmarking::benchmarks! {

impl_benchmark_test_suite!(
MultiPhase,
crate::mock::ExtBuilder::default().build(),
crate::mock::ExtBuilder::default().build_offchainify(0).0,
crate::mock::Runtime,
);
22 changes: 12 additions & 10 deletions frame/election-provider-multi-phase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ impl<T: Config> Pallet<T> {
}

fn do_elect() -> Result<(Supports<T::AccountId>, Weight), ElectionError> {
<QueuedSolution<T>>::take()
let result = <QueuedSolution<T>>::take()
.map_or_else(
|| match T::Fallback::get() {
FallbackStrategy::OnChain => Self::onchain_fallback()
Expand All @@ -1125,7 +1125,9 @@ impl<T: Config> Pallet<T> {
log!(warn, "Failed to finalize election round. reason {:?}", err);
}
err
})
});
unsigned::kill_solution::<T>();
result
}
}

Expand Down Expand Up @@ -1330,7 +1332,7 @@ mod tests {

#[test]
fn phase_rotation_works() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().build_offchainify(0).0.execute_with(|| {
// 0 ------- 15 ------- 25 ------- 30 ------- ------- 45 ------- 55 ------- 60
// | | | |
// Signed Unsigned Signed Unsigned
Expand Down Expand Up @@ -1395,7 +1397,7 @@ mod tests {

#[test]
fn signed_phase_void() {
ExtBuilder::default().phases(0, 10).build_and_execute(|| {
ExtBuilder::default().phases(0, 10).build_offchainify(0).0.execute_with(|| {
roll_to(15);
assert!(MultiPhase::current_phase().is_off());

Expand All @@ -1418,7 +1420,7 @@ mod tests {

#[test]
fn unsigned_phase_void() {
ExtBuilder::default().phases(10, 0).build_and_execute(|| {
ExtBuilder::default().phases(10, 0).build_offchainify(0).0.execute_with(|| {
roll_to(15);
assert!(MultiPhase::current_phase().is_off());

Expand All @@ -1441,7 +1443,7 @@ mod tests {

#[test]
fn both_phases_void() {
ExtBuilder::default().phases(0, 0).build_and_execute(|| {
ExtBuilder::default().phases(0, 0).build_offchainify(0).0.execute_with(|| {
roll_to(15);
assert!(MultiPhase::current_phase().is_off());

Expand All @@ -1464,7 +1466,7 @@ mod tests {
#[test]
fn early_termination() {
// an early termination in the signed phase, with no queued solution.
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().build_offchainify(0).0.execute_with(|| {
// signed phase started at block 15 and will end at 25.
roll_to(14);
assert_eq!(MultiPhase::current_phase(), Phase::Off);
Expand Down Expand Up @@ -1497,7 +1499,7 @@ mod tests {

#[test]
fn fallback_strategy_works() {
ExtBuilder::default().fallback(FallbackStrategy::OnChain).build_and_execute(|| {
ExtBuilder::default().fallback(FallbackStrategy::OnChain).build_offchainify(0).0.execute_with(|| {
roll_to(15);
assert_eq!(MultiPhase::current_phase(), Phase::Signed);

Expand All @@ -1516,7 +1518,7 @@ mod tests {
)
});

ExtBuilder::default().fallback(FallbackStrategy::Nothing).build_and_execute(|| {
ExtBuilder::default().fallback(FallbackStrategy::Nothing).build_offchainify(0).0.execute_with(|| {
roll_to(15);
assert_eq!(MultiPhase::current_phase(), Phase::Signed);

Expand All @@ -1530,7 +1532,7 @@ mod tests {

#[test]
fn snapshot_creation_fails_if_too_big() {
ExtBuilder::default().build_and_execute(|| {
ExtBuilder::default().build_offchainify(0).0.execute_with(|| {
Targets::set((0..(TargetIndex::max_value() as AccountId) + 1).collect::<Vec<_>>());

// signed phase failed to open.
Expand Down
10 changes: 5 additions & 5 deletions frame/election-provider-multi-phase/src/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ impl From<FeasibilityError> for MinerError {

/// Save a given call into OCW storage.
fn save_solution<T: Config>(call: &Call<T>) {
let storage = StorageValueRef::persistent(&OFFCHAIN_CACHED_CALL);
let storage = StorageValueRef::local(&OFFCHAIN_CACHED_CALL);
storage.set(&call);
}

/// Get a saved solution from OCW storage if it exists.
fn restore_solution<T: Config>() -> Result<Call<T>, MinerError> {
StorageValueRef::persistent(&OFFCHAIN_CACHED_CALL)
StorageValueRef::local(&OFFCHAIN_CACHED_CALL)
.get()
.flatten()
.ok_or(MinerError::NoStoredSolution)
}

/// Clear a saved solution from OCW storage.
fn kill_solution<T: Config>() {
let mut storage = StorageValueRef::persistent(&OFFCHAIN_CACHED_CALL);
pub(super) fn kill_solution<T: Config>() {
let mut storage = StorageValueRef::local(&OFFCHAIN_CACHED_CALL);
storage.clear();
}

Expand Down Expand Up @@ -1027,7 +1027,7 @@ mod tests {
// remove the cached submitted tx
// this ensures that when the resubmit window rolls around, we're ready to regenerate
// from scratch if necessary
let mut call_cache = StorageValueRef::persistent(&OFFCHAIN_CACHED_CALL);
let mut call_cache = StorageValueRef::local(&OFFCHAIN_CACHED_CALL);
assert!(matches!(call_cache.get::<Vec<u8>>(), Some(Some(_call))));
call_cache.clear();

Expand Down

0 comments on commit 4b46a93

Please sign in to comment.