From 8553b97930e56667e7548d040606da562d7c520b Mon Sep 17 00:00:00 2001 From: Euraxluo Date: Mon, 11 Dec 2023 19:48:26 +0800 Subject: [PATCH 1/5] Update use-macros-in-a-custom-pallet.md (#2094) fix with https://github.com/paritytech/substrate/pull/14437 --- .../build-application-logic/use-macros-in-a-custom-pallet.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md b/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md index c6aa96a7a..b8e13dcb9 100644 --- a/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md +++ b/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md @@ -238,7 +238,9 @@ To implement storage for the proof-of-existence pallet: ```rust #[pallet::storage] - pub(super) type Claims = StorageMap<_, Blake2_128Concat, T::Hash, (T::AccountId, T::BlockNumber)>; + pub(super) type Claims = StorageMap<_, Blake2_128Concat, T::Hash, (T::AccountId, BlockNumberFor)>; + #[pallet::hooks] + impl Hooks> for Pallet {} ``` 1. Save your changes. From 4f08f21e26195a3e486599cb538d30d1d4c68236 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Sun, 21 Jan 2024 14:17:41 +0800 Subject: [PATCH 2/5] Update Proof of Existence Tutorial (#2110) * Update use-macros-in-a-custom-pallet.md * more fix --- .../use-macros-in-a-custom-pallet.md | 49 +++++++------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md b/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md index b8e13dcb9..62b80801a 100644 --- a/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md +++ b/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md @@ -32,15 +32,11 @@ This tutorial requires compiling Rust code and takes approximately one to two ho ## Before you begin -For this tutorial, you download and use working code. Before you begin, verify the following: +Before you begin, verify the following: - You have configured your environment for Substrate development by installing [Rust and the Rust toolchain](/install/). -- You have completed [Build a local blockchain](/tutorials/build-a-blockchain/build-local-blockchain/) and have the Substrate node template installed locally. - -- You have used predefined accounts as described in [Simulate a network](/tutorials/build-a-blockchain/simulate-network/) to start nodes on a single computer. - -- You are generally familiar with software development and use command-line interfaces. +- You have cloned the [Substrate node template](https://github.com/substrate-developer-hub/substrate-node-template), which will be used as the starting point for this tutorial. ## Tutorial objectives @@ -52,11 +48,11 @@ By completing this tutorial, you will accomplish the following objectives: - Start a blockchain node that contains a custom pallet. -- Add front-end code that exposes the proof-of-existence pallet. +- Interact with a front-end that exposes the proof-of-existence pallet. -## Design the application +## Application design -The proof-of-existence application exposes the following callable functions: +The proof-of-existence application exposes two callable functions: - `create_claim()` allows a user to claim the existence of a file by uploading a hash. @@ -100,19 +96,12 @@ Therefore, the first step is to remove some files and content from the files in You won't be using the template code in this tutorial. However, you can review the template code to see what it provides before you delete it. -1. Delete all of the lines in the `lib.rs` file. - -1. Add the macro required to build both the native Rust binary (`std`) and the WebAssembly (`no_std`) binary. +1. Delete all of the lines in the `lib.rs` file, and replace it with the skeleton of a custom pallet: ```rust + // All pallets must be configured for `no_std`. #![cfg_attr(not(feature = "std"), no_std)] - ``` - - All of the pallets used in a runtime must be set to compile with the `no_std` features. -1. Add a skeleton set of pallet dependencies and [macros](/reference/frame-macros) that the custom pallet requires by copying the following code: - - ```rust // Re-export pallet items so that they can be accessed from the crate namespace. pub use pallet::*; @@ -122,7 +111,6 @@ Therefore, the first step is to remove some files and content from the files in use frame_system::pallet_prelude::*; #[pallet::pallet] - #[pallet::generate_store(pub(super) trait Store)] pub struct Pallet(_); #[pallet::config] // <-- Step 2. code block will replace this. @@ -140,8 +128,8 @@ Therefore, the first step is to remove some files and content from the files in ## Configure the pallet to emit events Every pallet has a [Rust "trait"](https://doc.rust-lang.org/book/ch10-02-traits.html) called `Config`. -You use this trait to configure the settings that your specific pallet requires. -For this tutorial, the configuration setting enables the pallet to emit events. +You use this trait to expose configurable options and connect your pallet to other parts of the runtime. +For this tutorial, we need to configure out pallet to emit events. To define the `Config` trait for the proof-of-existence pallet: @@ -163,13 +151,12 @@ To define the `Config` trait for the proof-of-existence pallet: ## Implement pallet events Now that you've configured the pallet to emit events, you are ready to define those events. -As described in [Design the application](#design-the-application), the proof-of-existence pallet emits an event under the following conditions: +Based on the [application design](#application-design), we want the proof-of-existence pallet to emit an event under the following conditions: - When a new claim is added to the blockchain. - When a claim is revoked. -Each event also displays an `AccountId` to identify who triggered the -event and the proof-of-existence claim (as `Hash`) that is being stored or removed. +Each event can include an `AccountId` to identify who triggered the event and a `Hash`, representing the proof-of-existence claim that is being stored or removed. To implement the pallet events: @@ -239,8 +226,6 @@ To implement storage for the proof-of-existence pallet: ```rust #[pallet::storage] pub(super) type Claims = StorageMap<_, Blake2_128Concat, T::Hash, (T::AccountId, BlockNumberFor)>; - #[pallet::hooks] - impl Hooks> for Pallet {} ``` 1. Save your changes. @@ -270,8 +255,8 @@ To implement this logic in the proof-of-existence pallet: // Dispatchable functions must be annotated with a weight and must return a DispatchResult. #[pallet::call] impl Pallet { - #[pallet::weight(0)] - #[pallet::call_index(1)] + #[pallet::weight(Weight::default())] + #[pallet::call_index(0)] pub fn create_claim(origin: OriginFor, claim: T::Hash) -> DispatchResult { // Check that the extrinsic was signed and get the signer. // This function will return an error if the extrinsic is not signed. @@ -292,8 +277,8 @@ To implement this logic in the proof-of-existence pallet: Ok(()) } - #[pallet::weight(0)] - #[pallet::call_index(2)] + #[pallet::weight(Weight::default())] + #[pallet::call_index(1)] pub fn revoke_claim(origin: OriginFor, claim: T::Hash) -> DispatchResult { // Check that the extrinsic was signed and get the signer. // This function will return an error if the extrinsic is not signed. @@ -320,10 +305,10 @@ To implement this logic in the proof-of-existence pallet: 1. Check that your code compiles by running the following command: ```bash - cargo check -p node-template-runtime --release + cargo check -p pallet-template ``` - The `[-p](https://doc.rust-lang.org/cargo/commands/cargo-check.html#options) node-template-runtime` directive tells cargo to only check the `node_template_runtime` package. + The `-p` flag tells cargo to only check the `pallet-template` that you have been modifying and saving you some compile time. You can refer to the node template [solution](https://github.com/substrate-developer-hub/substrate-node-template/blob/tutorials/solutions/proof-of-existence/pallets/template/src/lib.rs) if you get stuck. From 5423beeb66ca2996c1373a9bc6f333d7f6d424ed Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Sun, 21 Jan 2024 14:48:32 +0800 Subject: [PATCH 3/5] More Proof of Existence Fixes (#2111) * Update use-macros-in-a-custom-pallet.md * more fix * make work with runtime with weights * fix dead link --- content/md/en/docs/build/build-process.md | 2 +- .../use-macros-in-a-custom-pallet.md | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/content/md/en/docs/build/build-process.md b/content/md/en/docs/build/build-process.md index d69bd7cbe..7929a0727 100644 --- a/content/md/en/docs/build/build-process.md +++ b/content/md/en/docs/build/build-process.md @@ -138,6 +138,6 @@ This option is primarily used for faster compile time when you don't need to upd ## Where to go next -- [Wasm-builder README](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/utils/wasm-builder/README.md) +- [Wasm-builder source](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/utils/wasm-builder/src/lib.rs) - [Rust compilation options](https://doc.rust-lang.org/cargo/commands/cargo-build.html#compilation-options) - [Discussion: Removing the native runtime](https://github.com/paritytech/substrate/issues/10579) diff --git a/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md b/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md index 62b80801a..197db12f0 100644 --- a/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md +++ b/content/md/en/docs/tutorials/build-application-logic/use-macros-in-a-custom-pallet.md @@ -119,10 +119,18 @@ Therefore, the first step is to remove some files and content from the files in #[pallet::storage] // <-- Step 5. code block will replace this. #[pallet::call] // <-- Step 6. code block will replace this. } + + pub mod weights { + // Placeholder struct for the pallet weights + pub struct SubstrateWeight(core::marker::PhantomData); + } ``` You now have a framework that includes placeholders for _events_, _errors_, _storage_, and _callable functions_. + > NOTE: Pallet weights are outside the scope of this tutorial. If you want to learn more about weights, you can read our documentation [here](/reference/how-to-guides/weights/). + + 1. Save your changes. ## Configure the pallet to emit events @@ -143,6 +151,9 @@ To define the `Config` trait for the proof-of-existence pallet: pub trait Config: frame_system::Config { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; + /// Pallets use weights to measure the complexity of the callable functions. + /// Configuring weights is outside the scope of this tutorial, so we will leave it empty for now. + type WeightInfo; } ``` @@ -302,7 +313,7 @@ To implement this logic in the proof-of-existence pallet: 1. Save your changes and close the file. -1. Check that your code compiles by running the following command: +1. Check that your pallet compiles by running the following command: ```bash cargo check -p pallet-template From d8e60b5bfe327ae1a6abf72c2cb82752cc3b0e5b Mon Sep 17 00:00:00 2001 From: dzhelezov Date: Sun, 21 Jan 2024 09:54:05 +0300 Subject: [PATCH 4/5] Add SCALE codec by subsquid (#2100) Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- content/md/en/docs/reference/scale-codec.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/md/en/docs/reference/scale-codec.md b/content/md/en/docs/reference/scale-codec.md index 09efa5527..4c3575025 100644 --- a/content/md/en/docs/reference/scale-codec.md +++ b/content/md/en/docs/reference/scale-codec.md @@ -74,7 +74,7 @@ SCALE Codec has been implemented in other languages, including: - C: [`MatthewDarnell/cScale`](https://github.com/MatthewDarnell/cScale) - C++: [`soramitsu/scale-codec-cpp`](https://github.com/soramitsu/scale-codec-cpp) - JavaScript: [`polkadot-js/api`](https://github.com/polkadot-js/api) -- TypeScript: [`parity-scale-codec-ts`](https://github.com/paritytech/parity-scale-codec-ts), [`scale-ts`](https://github.com/unstoppablejs/unstoppablejs/tree/main/packages/scale-ts#scale-ts), [`soramitsu/scale-codec-js-library`](https://github.com/soramitsu/scale-codec-js-library) +- TypeScript: [`parity-scale-codec-ts`](https://github.com/paritytech/parity-scale-codec-ts), [`scale-ts`](https://github.com/unstoppablejs/unstoppablejs/tree/main/packages/scale-ts#scale-ts), [`soramitsu/scale-codec-js-library`](https://github.com/soramitsu/scale-codec-js-library), [`subsquid/scale-codec`](https://github.com/subsquid/squid-sdk/tree/master/substrate/scale-codec) - AssemblyScript: [`LimeChain/as-scale-codec`](https://github.com/LimeChain/as-scale-codec) - Haskell: [`airalab/hs-web3`](https://github.com/airalab/hs-web3/tree/master/packages/scale) - Java: [`emeraldpay/polkaj`](https://github.com/emeraldpay/polkaj) From 290c33321624bfca7f8de4997dc4e5e6c132a1aa Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Mon, 22 Jan 2024 10:13:47 +0800 Subject: [PATCH 5/5] Change "Self::BlockNumber" to "BlockNumberFor" (#2104) change ```rust Self::BlockNumber ``` ```rust BlockNumberFor ``` Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- .../how-to-guides/pallet-design/incorporate-randomness.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/md/en/docs/reference/how-to-guides/pallet-design/incorporate-randomness.md b/content/md/en/docs/reference/how-to-guides/pallet-design/incorporate-randomness.md index c3d476cf1..d63ead82b 100644 --- a/content/md/en/docs/reference/how-to-guides/pallet-design/incorporate-randomness.md +++ b/content/md/en/docs/reference/how-to-guides/pallet-design/incorporate-randomness.md @@ -30,7 +30,7 @@ The guide also illustrates how to add entropy to the randomness value by assigni ```rust #[pallet::config] pub trait frame_system::Config { - type MyRandomness: Randomness; + type MyRandomness: Randomness>; } ```