Skip to content

Commit 0683050

Browse files
apopiakbkchr
authored andcommitted
add notes and warnings to ProvideInherent docs (paritytech#9730)
* add notes and warnings to ProvideInherent docs * rephrase ProvideInherent doc comments * more comment refinement * remove multiple inherents note * remove repetition Co-authored-by: Bastian Köcher <[email protected]> * replace inherent example in docs * add note about who checks is_inherent_required * Apply suggestions from code review Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
1 parent 2a4813a commit 0683050

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

frame/support/src/inherent.rs

+26-9
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ pub use sp_inherents::{
2424
CheckInherentsResult, InherentData, InherentIdentifier, IsFatalError, MakeFatalError,
2525
};
2626

27-
/// A pallet that provides or verifies an inherent extrinsic.
27+
/// A pallet that provides or verifies an inherent extrinsic will implement this trait.
2828
///
29-
/// The pallet may provide the inherent, verify an inherent, or both provide and verify.
29+
/// The pallet may provide an inherent, verify an inherent, or both provide and verify.
30+
///
31+
/// Briefly, inherent extrinsics ("inherents") are extrinsics that are added to a block by the block
32+
/// producer. See [`sp_inherents`] for more documentation on inherents.
3033
pub trait ProvideInherent {
3134
/// The call type of the pallet.
3235
type Call;
@@ -36,6 +39,12 @@ pub trait ProvideInherent {
3639
const INHERENT_IDENTIFIER: self::InherentIdentifier;
3740

3841
/// Create an inherent out of the given `InherentData`.
42+
///
43+
/// NOTE: All checks necessary to ensure that the inherent is correct and that can be done in
44+
/// the runtime should happen in the returned `Call`.
45+
/// E.g. if this provides the timestamp, the call will check that the given timestamp is
46+
/// increasing the old timestamp by more than a minimum and it will also check that the
47+
/// timestamp hasn't already been set in the current block.
3948
fn create_inherent(data: &InherentData) -> Option<Self::Call>;
4049

4150
/// Determines whether this inherent is required in this block.
@@ -44,15 +53,17 @@ pub trait ProvideInherent {
4453
/// implementation returns this.
4554
///
4655
/// - `Ok(Some(e))` indicates that this inherent is required in this block. `construct_runtime!`
47-
/// will call this function from in its implementation of `fn check_extrinsics`.
56+
/// will call this function in its implementation of `fn check_extrinsics`.
4857
/// If the inherent is not present, it will return `e`.
4958
///
5059
/// - `Err(_)` indicates that this function failed and further operations should be aborted.
5160
///
52-
/// NOTE: If inherent is required then the runtime asserts that the block contains at least
61+
/// NOTE: If the inherent is required then the runtime asserts that the block contains at least
5362
/// one inherent for which:
5463
/// * type is [`Self::Call`],
5564
/// * [`Self::is_inherent`] returns true.
65+
///
66+
/// NOTE: This is currently only checked by block producers, not all full nodes.
5667
fn is_inherent_required(_: &InherentData) -> Result<Option<Self::Error>, Self::Error> {
5768
Ok(None)
5869
}
@@ -64,21 +75,27 @@ pub trait ProvideInherent {
6475
/// included in the block by its author. Whereas the second parameter represents the inherent
6576
/// data that the verifying node calculates.
6677
///
67-
/// NOTE: A block can contains multiple inherent.
78+
/// This is intended to allow for checks that cannot be done within the runtime such as, e.g.,
79+
/// the timestamp.
80+
///
81+
/// # Warning
82+
///
83+
/// This check is not guaranteed to be run by all full nodes and cannot be relied upon for
84+
/// ensuring that the block is correct.
6885
fn check_inherent(_: &Self::Call, _: &InherentData) -> Result<(), Self::Error> {
6986
Ok(())
7087
}
7188

7289
/// Return whether the call is an inherent call.
7390
///
74-
/// NOTE: Signed extrinsics are not inherent, but signed extrinsic with the given call variant
75-
/// can be dispatched.
91+
/// NOTE: Signed extrinsics are not inherents, but a signed extrinsic with the given call
92+
/// variant can be dispatched.
7693
///
7794
/// # Warning
7895
///
79-
/// In FRAME, inherent are enforced to be before other extrinsics, for this reason,
96+
/// In FRAME, inherents are enforced to be executed before other extrinsics. For this reason,
8097
/// pallets with unsigned transactions **must ensure** that no unsigned transaction call
8198
/// is an inherent call, when implementing `ValidateUnsigned::validate_unsigned`.
82-
/// Otherwise block producer can produce invalid blocks by including them after non inherent.
99+
/// Otherwise block producers can produce invalid blocks by including them after non inherents.
83100
fn is_inherent(call: &Self::Call) -> bool;
84101
}

primitives/inherents/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
//! Substrate inherent extrinsics
18+
//! Substrate Inherent Extrinsics
1919
//!
2020
//! Inherent extrinsics are extrinsics that are inherently added to each block. However, it is up to
21-
//! runtime implementation to require an inherent for each block or to make it optional. Inherents
22-
//! are mainly used to pass data from the block producer to the runtime. So, inherents require some
23-
//! part that is running on the client side and some part that is running on the runtime side. Any
24-
//! data that is required by an inherent is passed as [`InherentData`] from the client to the
25-
//! runtime when the inherents are constructed.
21+
//! the runtime implementation to require an inherent for each block or to make it optional.
22+
//! Inherents are mainly used to pass data from the block producer to the runtime. So, inherents
23+
//! require some part that is running on the client side and some part that is running on the
24+
//! runtime side. Any data that is required by an inherent is passed as [`InherentData`] from the
25+
//! client to the runtime when the inherents are constructed.
2626
//!
2727
//! The process of constructing and applying inherents is the following:
2828
//!

0 commit comments

Comments
 (0)