Estimate call fee (#9395)

* Estimate call fee

* More fix

* Fix

* Update frame/support/src/traits/misc.rs

Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>

* try and fix fmt stuff

* fmt aain

Co-authored-by: Parity Benchmarking Bot <admin@parity.io>
Co-authored-by: Zeke Mostov <32168567+emostov@users.noreply.github.com>
This commit is contained in:
Kian Paimani
2021-07-23 10:53:32 +02:00
committed by GitHub
parent d18c6a5200
commit c2e246d372
8 changed files with 60 additions and 19 deletions
@@ -237,7 +237,7 @@ use frame_support::{
};
use frame_system::{ensure_none, offchain::SendTransactionTypes};
use sp_arithmetic::{
traits::{CheckedAdd, Zero},
traits::{CheckedAdd, Saturating, Zero},
UpperOf,
};
use sp_npos_elections::{
@@ -554,7 +554,7 @@ pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::EstimateCallFee};
use frame_system::pallet_prelude::*;
#[pallet::config]
@@ -566,6 +566,9 @@ pub mod pallet {
/// Currency type.
type Currency: ReservableCurrency<Self::AccountId> + Currency<Self::AccountId>;
/// Something that can predict the fee of a call. Used to sensibly distribute rewards.
type EstimateCallFee: EstimateCallFee<Call<Self>, BalanceOf<Self>>;
/// Duration of the unsigned phase.
#[pallet::constant]
type UnsignedPhase: Get<Self::BlockNumber>;
@@ -973,7 +976,13 @@ pub mod pallet {
// create the submission
let deposit = Self::deposit_for(&solution, size);
let submission = SignedSubmission { who: who.clone(), deposit, solution };
let reward = {
let call = Call::submit(solution.clone(), num_signed_submissions);
let call_fee = T::EstimateCallFee::estimate_call_fee(&call, None.into());
T::SignedRewardBase::get().saturating_add(call_fee)
};
let submission = SignedSubmission { who: who.clone(), deposit, 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
@@ -261,7 +261,6 @@ parameter_types! {
pub static SignedDepositByte: Balance = 0;
pub static SignedDepositWeight: Balance = 0;
pub static SignedRewardBase: Balance = 7;
pub static SignedRewardMax: Balance = 10;
pub static SignedMaxWeight: Weight = BlockWeights::get().max_block;
pub static MinerMaxIterations: u32 = 5;
pub static MinerTxPriority: u64 = 100;
@@ -356,6 +355,7 @@ impl multi_phase::weights::WeightInfo for DualMockWeightInfo {
impl crate::Config for Runtime {
type Event = Event;
type Currency = Balances;
type EstimateCallFee = frame_support::traits::ConstU32<8>;
type SignedPhase = SignedPhase;
type UnsignedPhase = UnsignedPhase;
type SolutionImprovementThreshold = SolutionImprovementThreshold;
@@ -51,6 +51,8 @@ pub struct SignedSubmission<AccountId, Balance: HasCompact, CompactSolution> {
pub deposit: Balance,
/// The raw solution itself.
pub solution: RawSolution<CompactSolution>,
/// The reward that should potentially be paid for this solution, if accepted.
pub reward: Balance,
}
impl<AccountId, Balance, CompactSolution> Ord
@@ -351,10 +353,8 @@ impl<T: Config> Pallet<T> {
let SolutionOrSnapshotSize { voters, targets } =
Self::snapshot_metadata().unwrap_or_default();
let reward = T::SignedRewardBase::get();
while let Some(best) = all_submissions.pop_last() {
let SignedSubmission { solution, who, deposit } = best;
let SignedSubmission { solution, who, deposit, reward } = best;
let active_voters = solution.compact.voter_count() as u32;
let feasibility_weight = {
// defensive only: at the end of signed phase, snapshot will exits.
@@ -567,7 +567,7 @@ mod tests {
assert_eq!(balances(&99), (95, 5));
assert!(MultiPhase::finalize_signed_phase().0);
assert_eq!(balances(&99), (100 + 7, 0));
assert_eq!(balances(&99), (100 + 7 + 8, 0));
})
}
@@ -616,7 +616,7 @@ mod tests {
assert!(MultiPhase::finalize_signed_phase().0);
// 99 is rewarded.
assert_eq!(balances(&99), (100 + 7, 0));
assert_eq!(balances(&99), (100 + 7 + 8, 0));
// 999 gets everything back.
assert_eq!(balances(&999), (100, 0));
})
@@ -807,7 +807,7 @@ mod tests {
assert!(MultiPhase::finalize_signed_phase().0);
// 99 is rewarded.
assert_eq!(balances(&99), (100 + 7, 0));
assert_eq!(balances(&99), (100 + 7 + 8, 0));
// 999 is slashed.
assert_eq!(balances(&999), (95, 0));
// 9999 gets everything back.