Deprecate ValidateUnsigned and prevent duplicate heartbeats (#3975)

* Add pre-dispatch checks for ValidateUnsigned

* Deprecate ValidateUnsigned.

* Bump specversion.

* Fix test.
This commit is contained in:
Tomasz Drwięga
2019-11-03 11:52:08 +01:00
committed by Gavin Wood
parent 45e79d617b
commit 69c4e2f7f0
7 changed files with 75 additions and 24 deletions
@@ -18,8 +18,10 @@
//! stage.
use crate::traits::{
self, Member, MaybeDisplay, SignedExtension, Dispatchable, ValidateUnsigned,
self, Member, MaybeDisplay, SignedExtension, Dispatchable,
};
#[allow(deprecated)]
use crate::traits::ValidateUnsigned;
use crate::weights::{GetDispatchInfo, DispatchInfo};
use crate::transaction_validity::TransactionValidity;
@@ -51,6 +53,7 @@ where
self.signed.as_ref().map(|x| &x.0)
}
#[allow(deprecated)] // Allow ValidateUnsigned
fn validate<U: ValidateUnsigned<Call = Self::Call>>(
&self,
info: DispatchInfo,
@@ -60,11 +63,13 @@ where
Extra::validate(extra, id, &self.function, info, len)
} else {
let valid = Extra::validate_unsigned(&self.function, info, len)?;
Ok(valid.combine_with(U::validate_unsigned(&self.function)?))
let unsigned_validation = U::validate_unsigned(&self.function)?;
Ok(valid.combine_with(unsigned_validation))
}
}
fn apply(
#[allow(deprecated)] // Allow ValidateUnsigned
fn apply<U: ValidateUnsigned<Call=Self::Call>>(
self,
info: DispatchInfo,
len: usize,
@@ -74,6 +79,7 @@ where
(Some(id), pre)
} else {
let pre = Extra::pre_dispatch_unsigned(&self.function, info, len)?;
U::pre_dispatch(&self.function)?;
(None, pre)
};
let res = self.function.dispatch(Origin::from(maybe_who));
+6 -2
View File
@@ -20,9 +20,11 @@ use serde::{Serialize, Serializer, Deserialize, de::Error as DeError, Deserializ
use std::{fmt::Debug, ops::Deref, fmt, cell::RefCell};
use crate::codec::{Codec, Encode, Decode};
use crate::traits::{
self, Checkable, Applyable, BlakeTwo256, OpaqueKeys, ValidateUnsigned,
self, Checkable, Applyable, BlakeTwo256, OpaqueKeys,
SignedExtension, Dispatchable,
};
#[allow(deprecated)]
use crate::traits::ValidateUnsigned;
use crate::{generic, KeyTypeId, ApplyResult};
use crate::weights::{GetDispatchInfo, DispatchInfo};
pub use primitives::{H256, sr25519};
@@ -323,6 +325,7 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
fn sender(&self) -> Option<&Self::AccountId> { self.0.as_ref().map(|x| &x.0) }
/// Checks to see if this is a valid *transaction*. It returns information on it if so.
#[allow(deprecated)] // Allow ValidateUnsigned
fn validate<U: ValidateUnsigned<Call=Self::Call>>(
&self,
_info: DispatchInfo,
@@ -333,7 +336,8 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
/// index and sender.
fn apply(
#[allow(deprecated)] // Allow ValidateUnsigned
fn apply<U: ValidateUnsigned<Call=Self::Call>>(
self,
info: DispatchInfo,
len: usize,
+20 -4
View File
@@ -755,9 +755,6 @@ pub trait SignedExtension: Codec + Debug + Sync + Send + Clone + Eq + PartialEq
/// Validate an unsigned transaction for the transaction queue.
///
/// Normally the default implementation is fine since `ValidateUnsigned`
/// is a better way of recognising and validating unsigned transactions.
///
/// This function can be called frequently by the transaction queue,
/// to obtain transaction validity against current state.
/// It should perform all checks that determine a valid unsigned transaction,
@@ -889,6 +886,7 @@ pub trait Applyable: Sized + Send + Sync {
fn sender(&self) -> Option<&Self::AccountId>;
/// Checks to see if this is a valid *transaction*. It returns information on it if so.
#[allow(deprecated)] // Allow ValidateUnsigned
fn validate<V: ValidateUnsigned<Call=Self::Call>>(
&self,
info: DispatchInfo,
@@ -897,7 +895,8 @@ pub trait Applyable: Sized + Send + Sync {
/// Executes all necessary logic needed prior to dispatch and deconstructs into function call,
/// index and sender.
fn apply(
#[allow(deprecated)] // Allow ValidateUnsigned
fn apply<V: ValidateUnsigned<Call=Self::Call>>(
self,
info: DispatchInfo,
len: usize,
@@ -966,10 +965,27 @@ pub trait RuntimeApiInfo {
/// the transaction for the transaction pool.
/// During block execution phase one need to perform the same checks anyway,
/// since this function is not being called.
#[deprecated(note = "Use SignedExtensions instead.")]
pub trait ValidateUnsigned {
/// The call to validate
type Call;
/// Validate the call right before dispatch.
///
/// This method should be used to prevent transactions already in the pool
/// (i.e. passing `validate_unsigned`) from being included in blocks
/// in case we know they now became invalid.
///
/// By default it's a good idea to call `validate_unsigned` from within
/// this function again to make sure we never include an invalid transaction.
///
/// Changes made to storage WILL be persisted if the call returns `Ok`.
fn pre_dispatch(call: &Self::Call) -> Result<(), crate::ApplyError> {
Self::validate_unsigned(call)
.map(|_| ())
.map_err(Into::into)
}
/// Return the validity of the call
///
/// This doesn't execute any side-effects; it merely checks