Unsigned Validation best practices (#5563)

* Configurable Unsigned Priority.

* Use the new builder.

* Fix tests.

* Fix benches.

* Remove unused import.

* Rename for_pallet
This commit is contained in:
Tomasz Drwięga
2020-04-08 11:17:21 +02:00
committed by GitHub
parent 571f1daa49
commit 762bcbab03
10 changed files with 194 additions and 26 deletions
+15 -9
View File
@@ -292,7 +292,7 @@ use sp_runtime::{
},
transaction_validity::{
TransactionValidityError, TransactionValidity, ValidTransaction, InvalidTransaction,
TransactionSource,
TransactionSource, TransactionPriority,
},
};
use sp_staking::{
@@ -782,6 +782,12 @@ pub trait Trait: frame_system::Trait {
/// For each validator only the `$MaxNominatorRewardedPerValidator` biggest stakers can claim
/// their reward. This used to limit the i/o cost for the nominator payout.
type MaxNominatorRewardedPerValidator: Get<u32>;
/// A configuration for base priority of unsigned transactions.
///
/// This is exposed so that it can be tuned for particular runtime, when
/// multiple pallets send unsigned transactions.
type UnsignedPriority: Get<TransactionPriority>;
}
/// Mode of era-forcing.
@@ -3224,24 +3230,24 @@ impl<T: Trait> frame_support::unsigned::ValidateUnsigned for Module<T> {
era,
);
Ok(ValidTransaction {
ValidTransaction::with_tag_prefix("StakingOffchain")
// The higher the score[0], the better a solution is.
priority: score[0].saturated_into(),
// no requires.
requires: vec![],
.priority(T::UnsignedPriority::get().saturating_add(score[0].saturated_into()))
// Defensive only. A single solution can exist in the pool per era. Each validator
// will run OCW at most once per era, hence there should never exist more than one
// transaction anyhow.
provides: vec![("StakingOffchain", era).encode()],
.and_provides(era)
// Note: this can be more accurate in the future. We do something like
// `era_end_block - current_block` but that is not needed now as we eagerly run
// offchain workers now and the above should be same as `T::ElectionLookahead`
// without the need to query more storage in the validation phase. If we randomize
// offchain worker, then we might re-consider this.
longevity: TryInto::<u64>::try_into(T::ElectionLookahead::get()).unwrap_or(DEFAULT_LONGEVITY),
.longevity(TryInto::<u64>::try_into(
T::ElectionLookahead::get()).unwrap_or(DEFAULT_LONGEVITY)
)
// We don't propagate this. This can never the validated at a remote node.
propagate: false,
})
.propagate(false)
.build()
} else {
InvalidTransaction::Call.into()
}
+2
View File
@@ -272,6 +272,7 @@ parameter_types! {
pub const BondingDuration: EraIndex = 3;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &I_NPOS;
pub const MaxNominatorRewardedPerValidator: u32 = 64;
pub const UnsignedPriority: u64 = 1 << 20;
}
impl Trait for Test {
@@ -293,6 +294,7 @@ impl Trait for Test {
type Call = Call;
type SubmitTransaction = SubmitTransaction;
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type UnsignedPriority = UnsignedPriority;
}
pub type Extrinsic = TestXt<Call, ()>;
+1 -1
View File
@@ -3165,7 +3165,7 @@ mod offchain_phragmen {
&inner,
),
TransactionValidity::Ok(ValidTransaction {
priority: 1125, // the proposed slot stake.
priority: (1 << 20) + 1125, // the proposed slot stake.
requires: vec![],
provides: vec![("StakingOffchain", active_era()).encode()],
longevity: 3,