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

ValidateUnsigned: Improve docs. #12870

Merged
merged 2 commits into from
Dec 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,26 +1335,29 @@ pub trait GetNodeBlockType {
type NodeBlock: self::Block;
}

/// Something that can validate unsigned extrinsics for the transaction pool.
/// Provide validation for unsigned extrinsics.
///
/// Note that any checks done here are only used for determining the validity of
/// 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.
/// This trait provides two functions [`pre_dispatch`](Self::pre_dispatch) and
/// [`validate_unsigned`](Self::validate_unsigned). The first one being called right before
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// dispatching the call wrapped by an unsigned extrinsic. The second one mainly being used in the
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// context of the transaction pool to check the validity of the call wrapped by an unsigned
/// extrinsic.
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
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// (i.e. passing `validate_unsigned`) from being included in blocks
/// (i.e. passing [`validate_unsigned`](Self::validate_unsigned)) from being included in blocks
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// 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.
/// By default it's a good idea to call [`validate_unsigned`](Self::validate_unsigned) from
/// within this function again to make sure we never include an invalid transaction. Otherwise
/// the implementation of the call or this methid will need to provide proper validation to
/// ensure that the transaction is valid.
///
/// Changes made to storage WILL be persisted if the call returns `Ok`.
/// Changes made to storage *WILL* be persisted if the call returns `Ok`.
fn pre_dispatch(call: &Self::Call) -> Result<(), TransactionValidityError> {
Self::validate_unsigned(TransactionSource::InBlock, call)
.map(|_| ())
Expand All @@ -1363,8 +1366,14 @@ pub trait ValidateUnsigned {

/// Return the validity of the call
///
/// This doesn't execute any side-effects; it merely checks
/// whether the transaction would panic if it were included or not.
/// This doesn't execute any side-effects; it merely checks whether the call would be rejected
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// by the runtime in an unsigned extrinsic.
///
/// The validity checks should be as lightweight as possible as every node will execute this
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// code before the unsigned extrinsic enters the transaction pool and also periodically after
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// this to ensure the validity. To prevent dos-ing a network with unsigned extrinsics these
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// validity checks should include some checks around uniqueness, e.g. like checking that the
bkchr marked this conversation as resolved.
Show resolved Hide resolved
/// unsigned extrinsic was send by an authority in the active set.
///
/// Changes made to storage should be discarded by caller.
fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity;
Expand Down