Skip to content

Commit

Permalink
Add documentation around FRAME Origin (paritytech#3362)
Browse files Browse the repository at this point in the history
Does the following: 

- Add a reference doc page named `frame_runtime_types`, which explains
what types like `RuntimeOrigin`, `RuntimeCall` etc are.
- On top of it, it adds a reference doc page called `frame_origin` which
explains a few important patterns that we use around origins
- And finally brushes up `#[frame::origin]` docs. 
- Updates the theme, sidebar and favicon to look like: 

<img width="1728" alt="Screenshot 2024-02-20 at 12 16 00"
src="https://github.com/paritytech/polkadot-sdk/assets/5588131/6d60a16b-2081-411b-8869-43b91920cca9">


All of this was inspired by
https://substrate.stackexchange.com/questions/10992/how-do-you-find-the-public-key-for-the-medium-spender-track-origin/10993

closes paritytech/polkadot-sdk-docs#45
closes paritytech/polkadot-sdk-docs#43
contributes / overlaps with
paritytech#2638 cc @liamaharon
deprecation companion:
substrate-developer-hub/substrate-docs#2131
pba-content companion:
Polkadot-Blockchain-Academy/pba-content#977

---------

Co-authored-by: Radha <86818441+DrW3RK@users.noreply.github.com>
Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
  • Loading branch information
5 people committed Feb 27, 2024
1 parent 62fd5a5 commit f6b16f2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
6 changes: 6 additions & 0 deletions substrate/frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ pub mod runtime {
ConstU32, ConstU64, ConstU8,
};

/// Primary types used to parameterize `EnsureOrigin` and `EnsureRootWithArg`.
pub use frame_system::{
EnsureNever, EnsureNone, EnsureRoot, EnsureRootWithSuccess, EnsureSigned,
EnsureSignedBy,
};

/// Types to define your runtime version.
pub use sp_version::{create_runtime_str, runtime_version, RuntimeVersion};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn expand_outer_origin(
#[doc = #doc_string]
#[derive(Clone)]
pub struct RuntimeOrigin {
caller: OriginCaller,
pub caller: OriginCaller,
filter: #scrate::__private::sp_std::rc::Rc<Box<dyn Fn(&<#runtime as #system_path::Config>::RuntimeCall) -> bool>>,
}

Expand Down
17 changes: 3 additions & 14 deletions substrate/frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,22 +1480,11 @@ pub fn validate_unsigned(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
}

/// The `#[pallet::origin]` attribute allows you to define some origin for the pallet.
///
/// Item must be either a type alias, an enum, or a struct. It needs to be public.
///
/// E.g.:
///
/// ```ignore
/// #[pallet::origin]
/// pub struct Origin<T>(PhantomData<(T)>);
/// ```
///
/// **WARNING**: modifying origin changes the outer runtime origin. This outer runtime origin
/// can be stored on-chain (e.g. in `pallet-scheduler`), thus any change must be done with care
/// as it might require some migration.
/// ---
///
/// NOTE: for instantiable pallets, the origin must be generic over `T` and `I`.
/// **Rust-Analyzer users**: See the documentation of the Rust item in
/// `frame_support::pallet_macros::origin`.
#[proc_macro_attribute]
pub fn origin(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
Expand Down
68 changes: 61 additions & 7 deletions substrate/frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,9 +2274,8 @@ pub mod pallet_macros {
pub use frame_support_procedural::{
composite_enum, config, disable_frame_system_supertrait_check, error, event,
extra_constants, feeless_if, generate_deposit, generate_store, getter, hooks,
import_section, inherent, no_default, no_default_bounds, origin, pallet_section,
storage_prefix, storage_version, type_value, unbounded, validate_unsigned, weight,
whitelist_storage,
import_section, inherent, no_default, no_default_bounds, pallet_section, storage_prefix,
storage_version, type_value, unbounded, validate_unsigned, weight, whitelist_storage,
};

/// Allows a pallet to declare a set of functions as a *dispatchable extrinsic*. In
Expand Down Expand Up @@ -2718,29 +2717,29 @@ pub mod pallet_macros {
/// }
/// ```
pub use frame_support_procedural::storage;
/// This attribute is attached to a function inside an `impl` block annoated with
/// This attribute is attached to a function inside an `impl` block annotated with
/// [`pallet::tasks_experimental`](`tasks_experimental`) to define the conditions for a
/// given work item to be valid.
///
/// It takes a closure as input, which is then used to define the condition. The closure
/// should have the same signature as the function it is attached to, except that it should
/// return a `bool` instead.
pub use frame_support_procedural::task_condition;
/// This attribute is attached to a function inside an `impl` block annoated with
/// This attribute is attached to a function inside an `impl` block annotated with
/// [`pallet::tasks_experimental`](`tasks_experimental`) to define the index of a given
/// work item.
///
/// It takes an integer literal as input, which is then used to define the index. This
/// index should be unique for each function in the `impl` block.
pub use frame_support_procedural::task_index;
/// This attribute is attached to a function inside an `impl` block annoated with
/// This attribute is attached to a function inside an `impl` block annotated with
/// [`pallet::tasks_experimental`](`tasks_experimental`) to define an iterator over the
/// available work items for a task.
///
/// It takes an iterator as input that yields a tuple with same types as the function
/// arguments.
pub use frame_support_procedural::task_list;
/// This attribute is attached to a function inside an `impl` block annoated with
/// This attribute is attached to a function inside an `impl` block annotated with
/// [`pallet::tasks_experimental`](`tasks_experimental`) define the weight of a given work
/// item.
///
Expand Down Expand Up @@ -2773,6 +2772,61 @@ pub mod pallet_macros {
/// Now, this can be executed as follows:
#[doc = docify::embed!("src/tests/tasks.rs", tasks_work)]
pub use frame_support_procedural::tasks_experimental;

/// Allows a pallet to declare a type as an origin.
///
/// If defined as such, this type will be amalgamated at the runtime level into
/// `RuntimeOrigin`, very similar to [`call`], [`error`] and [`event`]. See
/// [`composite_enum`] for similar cases.
///
/// Origin is a complex FRAME topics and is further explained in `polkadot_sdk_docs`.
///
/// ## Syntax Variants
///
/// ```
/// #[frame_support::pallet]
/// mod pallet {
/// # use frame_support::pallet_prelude::*;
/// # #[pallet::config]
/// # pub trait Config: frame_system::Config {}
/// # #[pallet::pallet]
/// # pub struct Pallet<T>(_);
/// /// On the spot declaration.
/// #[pallet::origin]
/// #[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
/// pub enum Origin {
/// Foo,
/// Bar,
/// }
/// }
/// ```
///
/// Or, more commonly used:/
///
/// ```
/// #[frame_support::pallet]
/// mod pallet {
/// # use frame_support::pallet_prelude::*;
/// # #[pallet::config]
/// # pub trait Config: frame_system::Config {}
/// # #[pallet::pallet]
/// # pub struct Pallet<T>(_);
/// #[derive(PartialEq, Eq, Clone, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
/// pub enum RawOrigin {
/// Foo,
/// Bar,
/// }
///
/// #[pallet::origin]
/// pub type Origin = RawOrigin;
/// }
/// ```
///
/// ## Warning
///
/// Modifying any pallet's origin type will cause the runtime level origin type to also
/// change in encoding. If stored anywhere on-chain, this will require a data migration.
pub use frame_support_procedural::origin;
}

#[deprecated(note = "Will be removed after July 2023; Use `sp_runtime::traits` directly instead.")]
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ pub mod pallet {
+ Clone
+ OriginTrait<Call = Self::RuntimeCall, AccountId = Self::AccountId>;

#[docify::export(system_runtime_call)]
/// The aggregated `RuntimeCall` type.
#[pallet::no_default_bounds]
type RuntimeCall: Parameter
Expand Down

0 comments on commit f6b16f2

Please sign in to comment.