mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 04:41:03 +00:00
Improve call, and usage in pallet utility (#9418)
* WIP * WIP * WIP * add some tests and limit * remove wip test * fmt * Update bin/node/runtime/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * fmt * use primitives allocation limit Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
27d4177f93
commit
38db14089b
@@ -26,7 +26,10 @@ use rand::{prelude::SliceRandom, rngs::SmallRng, SeedableRng};
|
||||
use sp_arithmetic::{per_things::Percent, traits::One};
|
||||
use sp_npos_elections::IndexAssignment;
|
||||
use sp_runtime::InnerOf;
|
||||
use sp_std::convert::{TryFrom, TryInto};
|
||||
use sp_std::{
|
||||
boxed::Box,
|
||||
convert::{TryFrom, TryInto},
|
||||
};
|
||||
|
||||
const SEED: u32 = 999;
|
||||
|
||||
@@ -317,7 +320,7 @@ frame_benchmarking::benchmarks! {
|
||||
let caller = frame_benchmarking::whitelisted_caller();
|
||||
T::Currency::make_free_balance_be(&caller, T::Currency::minimum_balance() * 10u32.into());
|
||||
|
||||
}: _(RawOrigin::Signed(caller), solution, c)
|
||||
}: _(RawOrigin::Signed(caller), Box::new(solution), c)
|
||||
verify {
|
||||
assert!(<MultiPhase<T>>::signed_submissions().len() as u32 == c + 1);
|
||||
}
|
||||
@@ -344,9 +347,15 @@ frame_benchmarking::benchmarks! {
|
||||
|
||||
// encode the most significant storage item that needs to be decoded in the dispatch.
|
||||
let encoded_snapshot = <MultiPhase<T>>::snapshot().unwrap().encode();
|
||||
let encoded_call = <Call<T>>::submit_unsigned(raw_solution.clone(), witness).encode();
|
||||
let encoded_call = <Call<T>>::submit_unsigned(Box::new(raw_solution.clone()), witness).encode();
|
||||
}: {
|
||||
assert_ok!(<MultiPhase<T>>::submit_unsigned(RawOrigin::None.into(), raw_solution, witness));
|
||||
assert_ok!(
|
||||
<MultiPhase<T>>::submit_unsigned(
|
||||
RawOrigin::None.into(),
|
||||
Box::new(raw_solution),
|
||||
witness,
|
||||
)
|
||||
);
|
||||
let _decoded_snap = <RoundSnapshot<T::AccountId> as Decode>::decode(&mut &*encoded_snapshot)
|
||||
.unwrap();
|
||||
let _decoded_call = <Call<T> as Decode>::decode(&mut &*encoded_call).unwrap();
|
||||
|
||||
@@ -857,7 +857,7 @@ pub mod pallet {
|
||||
))]
|
||||
pub fn submit_unsigned(
|
||||
origin: OriginFor<T>,
|
||||
solution: RawSolution<CompactOf<T>>,
|
||||
solution: Box<RawSolution<CompactOf<T>>>,
|
||||
witness: SolutionOrSnapshotSize,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_none(origin)?;
|
||||
@@ -876,7 +876,7 @@ pub mod pallet {
|
||||
assert!(targets as u32 == witness.targets, "{}", error_message);
|
||||
|
||||
let ready =
|
||||
Self::feasibility_check(solution, ElectionCompute::Unsigned).expect(error_message);
|
||||
Self::feasibility_check(*solution, ElectionCompute::Unsigned).expect(error_message);
|
||||
|
||||
// Store the newly received solution.
|
||||
log!(info, "queued unsigned solution with score {:?}", ready.score);
|
||||
@@ -947,7 +947,7 @@ pub mod pallet {
|
||||
#[pallet::weight(T::WeightInfo::submit(*num_signed_submissions))]
|
||||
pub fn submit(
|
||||
origin: OriginFor<T>,
|
||||
solution: RawSolution<CompactOf<T>>,
|
||||
solution: Box<RawSolution<CompactOf<T>>>,
|
||||
num_signed_submissions: u32,
|
||||
) -> DispatchResult {
|
||||
let who = ensure_signed(origin)?;
|
||||
@@ -982,7 +982,8 @@ pub mod pallet {
|
||||
T::SignedRewardBase::get().saturating_add(call_fee)
|
||||
};
|
||||
|
||||
let submission = SignedSubmission { who: who.clone(), deposit, solution, reward };
|
||||
let submission =
|
||||
SignedSubmission { who: who.clone(), deposit, solution: *solution, reward };
|
||||
|
||||
// insert the submission if the queue has space or it's better than the weakest
|
||||
// eject the weakest if the queue was full
|
||||
@@ -1927,7 +1928,7 @@ mod tests {
|
||||
let solution = RawSolution { score: [(5 + s).into(), 0, 0], ..Default::default() };
|
||||
assert_ok!(MultiPhase::submit(
|
||||
crate::mock::Origin::signed(99),
|
||||
solution,
|
||||
Box::new(solution),
|
||||
MultiPhase::signed_submissions().len() as u32
|
||||
));
|
||||
}
|
||||
|
||||
@@ -499,7 +499,11 @@ mod tests {
|
||||
origin: Origin,
|
||||
solution: RawSolution<CompactOf<Runtime>>,
|
||||
) -> DispatchResult {
|
||||
MultiPhase::submit(origin, solution, MultiPhase::signed_submissions().len() as u32)
|
||||
MultiPhase::submit(
|
||||
origin,
|
||||
Box::new(solution),
|
||||
MultiPhase::signed_submissions().len() as u32,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -532,7 +536,7 @@ mod tests {
|
||||
|
||||
// now try and cheat by passing a lower queue length
|
||||
assert_noop!(
|
||||
MultiPhase::submit(Origin::signed(99), solution, 0),
|
||||
MultiPhase::submit(Origin::signed(99), Box::new(solution), 0),
|
||||
Error::<Runtime>::SignedInvalidWitness,
|
||||
);
|
||||
})
|
||||
|
||||
@@ -34,7 +34,7 @@ use sp_runtime::{
|
||||
traits::TrailingZeroInput,
|
||||
DispatchError, SaturatedConversion,
|
||||
};
|
||||
use sp_std::{cmp::Ordering, convert::TryFrom, vec::Vec};
|
||||
use sp_std::{boxed::Box, cmp::Ordering, convert::TryFrom, vec::Vec};
|
||||
|
||||
/// Storage key used to store the last block number at which offchain worker ran.
|
||||
pub(crate) const OFFCHAIN_LAST_BLOCK: &[u8] = b"parity/multi-phase-unsigned-election";
|
||||
@@ -208,7 +208,7 @@ impl<T: Config> Pallet<T> {
|
||||
let (raw_solution, witness) = Self::mine_and_check(iters)?;
|
||||
|
||||
let score = raw_solution.score.clone();
|
||||
let call: Call<T> = Call::submit_unsigned(raw_solution, witness).into();
|
||||
let call: Call<T> = Call::submit_unsigned(Box::new(raw_solution), witness).into();
|
||||
|
||||
log!(
|
||||
debug,
|
||||
@@ -773,7 +773,7 @@ mod tests {
|
||||
fn validate_unsigned_retracts_wrong_phase() {
|
||||
ExtBuilder::default().desired_targets(0).build_and_execute(|| {
|
||||
let solution = RawSolution::<TestCompact> { score: [5, 0, 0], ..Default::default() };
|
||||
let call = Call::submit_unsigned(solution.clone(), witness());
|
||||
let call = Call::submit_unsigned(Box::new(solution.clone()), witness());
|
||||
|
||||
// initial
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Off);
|
||||
@@ -842,7 +842,7 @@ mod tests {
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
let solution = RawSolution::<TestCompact> { score: [5, 0, 0], ..Default::default() };
|
||||
let call = Call::submit_unsigned(solution.clone(), witness());
|
||||
let call = Call::submit_unsigned(Box::new(solution.clone()), witness());
|
||||
|
||||
// initial
|
||||
assert!(<MultiPhase as ValidateUnsigned>::validate_unsigned(
|
||||
@@ -879,7 +879,7 @@ mod tests {
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
let solution = RawSolution::<TestCompact> { score: [5, 0, 0], ..Default::default() };
|
||||
let call = Call::submit_unsigned(solution.clone(), witness());
|
||||
let call = Call::submit_unsigned(Box::new(solution.clone()), witness());
|
||||
assert_eq!(solution.compact.unique_targets().len(), 0);
|
||||
|
||||
// won't work anymore.
|
||||
@@ -905,7 +905,7 @@ mod tests {
|
||||
|
||||
let solution =
|
||||
RawSolution::<TestCompact> { score: [5, 0, 0], ..Default::default() };
|
||||
let call = Call::submit_unsigned(solution.clone(), witness());
|
||||
let call = Call::submit_unsigned(Box::new(solution.clone()), witness());
|
||||
|
||||
assert_eq!(
|
||||
<MultiPhase as ValidateUnsigned>::validate_unsigned(
|
||||
@@ -931,7 +931,7 @@ mod tests {
|
||||
|
||||
// This is in itself an invalid BS solution.
|
||||
let solution = RawSolution::<TestCompact> { score: [5, 0, 0], ..Default::default() };
|
||||
let call = Call::submit_unsigned(solution.clone(), witness());
|
||||
let call = Call::submit_unsigned(Box::new(solution.clone()), witness());
|
||||
let outer_call: OuterCall = call.into();
|
||||
let _ = outer_call.dispatch(Origin::none());
|
||||
})
|
||||
@@ -951,7 +951,7 @@ mod tests {
|
||||
let mut correct_witness = witness();
|
||||
correct_witness.voters += 1;
|
||||
correct_witness.targets -= 1;
|
||||
let call = Call::submit_unsigned(solution.clone(), correct_witness);
|
||||
let call = Call::submit_unsigned(Box::new(solution.clone()), correct_witness);
|
||||
let outer_call: OuterCall = call.into();
|
||||
let _ = outer_call.dispatch(Origin::none());
|
||||
})
|
||||
@@ -972,7 +972,7 @@ mod tests {
|
||||
|
||||
// ensure this solution is valid.
|
||||
assert!(MultiPhase::queued_solution().is_none());
|
||||
assert_ok!(MultiPhase::submit_unsigned(Origin::none(), solution, witness));
|
||||
assert_ok!(MultiPhase::submit_unsigned(Origin::none(), Box::new(solution), witness));
|
||||
assert!(MultiPhase::queued_solution().is_some());
|
||||
})
|
||||
}
|
||||
@@ -1054,7 +1054,11 @@ mod tests {
|
||||
};
|
||||
let (solution, witness) = MultiPhase::prepare_election_result(result).unwrap();
|
||||
assert_ok!(MultiPhase::unsigned_pre_dispatch_checks(&solution));
|
||||
assert_ok!(MultiPhase::submit_unsigned(Origin::none(), solution, witness));
|
||||
assert_ok!(MultiPhase::submit_unsigned(
|
||||
Origin::none(),
|
||||
Box::new(solution),
|
||||
witness
|
||||
));
|
||||
assert_eq!(MultiPhase::queued_solution().unwrap().score[0], 10);
|
||||
|
||||
// trial 1: a solution who's score is only 2, i.e. 20% better in the first element.
|
||||
@@ -1096,7 +1100,11 @@ mod tests {
|
||||
|
||||
// and it is fine
|
||||
assert_ok!(MultiPhase::unsigned_pre_dispatch_checks(&solution));
|
||||
assert_ok!(MultiPhase::submit_unsigned(Origin::none(), solution, witness));
|
||||
assert_ok!(MultiPhase::submit_unsigned(
|
||||
Origin::none(),
|
||||
Box::new(solution),
|
||||
witness
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user