@@ -8,12 +8,11 @@ This `offchain-demo` pallet is included in the
88[ ocw-runtime] ( https://github.com/substrate-developer-hub/recipes/tree/master/runtimes/ocw-runtime ) .
99That runtime can be used in the kitchen node.
1010
11- In order to use the Offchain worker, the node must inject some keys into its keystore, and that is
12- enabled with a feature flag.
11+ In order to utilize the off-chain worker, the node must inject keys into its keystore. To do so, we
12+ open the ` nodes/kitchen-node/Cargo.toml ` file, enable the ` ocw-runtime ` package and comment out the
13+ ` super-runtime ` package.
1314
14- First, edit ` nodes/kitchen-node/Cargo.toml ` to enable the ` ocw-runtime ` .
15-
16- Then, build the kitchen node with these commands.
15+ Then we build the kitchen node with ` ocw ` feature flag:
1716
1817``` bash
1918# Switch to kitchen-node directory
@@ -25,7 +24,7 @@ cargo build --release --features ocw
2524
2625## Life-cycle of Off-chain Worker
2726
28- Running the ` kitchen-node ` , you will see the off-chain worker is run after each block generation
27+ Running the ` kitchen-node ` we see off-chain worker runs after each block generation
2928phase, as shown by ` Entering off-chain workers ` in the node output message:
3029
3130```
@@ -44,34 +43,33 @@ phase, as shown by `Entering off-chain workers` in the node output message:
4443...
4544```
4645
47- Referring to the code at
46+ If we refer to the code at
4847[ ` pallets/offchain-demo/src/lib.rs ` ] ( https://github.com/substrate-developer-hub/recipes/tree/master/pallets/offchain-demo/src/lib.rs ) ,
4948there is an ` offchain_worker ` function inside ` decl_module! ` . This is the entry point of the
50- off-chain worker that is executed once after each block generation, so we put all the off-chain
51- logic here.
49+ off-chain worker which is executed once per block generation, so all the off-chain
50+ logic is here.
5251
5352Two kinds of transactions can be sent back on-chain from off-chain workers, ** Signed Transactions**
5453and ** Unsigned Transactions** . Signed transactions are used if the transaction requires the sender
55- to be specified. Unsigned transactions are used when the sender does not need to be known, and
56- additional logic is written in the code to provide extra data verification. Let's walk through how
57- to set up each one.
54+ to be specified. Unsigned transactions are used when the sender does not need to be known and
55+ additional logic in the code provides additional data verification.
56+
57+ Let's walk through how to set each one up:
5858
5959## Signed Transactions
6060
6161> ** Notes** : This example will have account ` Alice ` submitting signed transactions to the node in
62- off-chain worker, and these transactions have associated fees. If you run the node in development
63- mode (with ` --dev ` flag) using the default sr25519 crypto signature, ` Alice ` will have initial fund
64- deposited and this example will run all fine.
62+ > the off-chain worker, and these transactions have associated fees. If we run the node in development
63+ > mode (with ` --dev ` flag) using the default sr25519 crypto signature, ` Alice ` will have initial funds
64+ > deposited and this example will run all fine.
6565>
66- > If you are running either not in development mode or switched to another crypto signature, please
67- > beware to have account ` Alice ` setup and be funded to run this example.
66+ > If this is not done in development mode or switched to another crypto signature, please
67+ > be aware that ` Alice ` must be setup and be funded to run this example.
6868
6969### Setup
7070
71- For signed transactions, the first thing you will notice is that we have defined another sub-module
72- here:
71+ For signed transactions, we have to define a crypto signature sub-module:
7372
74- src:
7573[ ` pallets/offchain-demo/src/lib.rs ` ] ( https://github.com/substrate-developer-hub/recipes/tree/master/pallets/offchain-demo/src/lib.rs )
7674
7775``` rust
@@ -84,7 +82,7 @@ pub mod crypto {
8482}
8583```
8684
87- This is the application key to be used as the prefix for this pallet in underlying storage.
85+ ` KEY_TYPE ` is the application key prefix for the pallet in underlying storage.
8886
8987Second, we have added an additional associated type ` AuthorityId ` .
9088
@@ -98,16 +96,13 @@ pub trait Trait: system::Trait {
9896}
9997```
10098
101- This associated type needs to be specified by the runtime when the runtime is to include this pallet
102- (implement this pallet trait).
99+ This associated type needs to be specified by the runtime when it implements this pallet trait.
103100
104- Now if we build the ` kitchen-node ` , we will see the compiler complain that there are three trait
105- bounds ` Runtime: frame_system::offchain::CreateSignedTransaction ` ,
106- ` frame_system::offchain::SigningTypes ` , and ` frame_system::offchain::SendTransactionTypes ` are not
107- satisfied. We learn that when using ` SubmitSignedTransaction ` , we also need to have our runtime
108- implement the
101+ Now if we build the ` kitchen-node ` , we will see the compiler return with three trait
102+ bounds that are not satisfied: ` Runtime: frame_system::offchain::CreateSignedTransaction ` ,
103+ ` frame_system::offchain::SigningTypes ` , and ` frame_system::offchain::SendTransactionTypes ` .
104+ We also learn that when using ` SubmitSignedTransaction ` , our runtime need to implement
109105[ ` CreateSignedTransaction ` trait] ( https://substrate.dev/rustdocs/v2.0.0-rc5/frame_system/offchain/trait.CreateSignedTransaction.html ) .
110- So let's implement this in our runtime.
111106
112107src:
113108[ ` runtimes/ocw-runtime/src/lib.rs ` ] ( https://github.com/substrate-developer-hub/recipes/tree/master/runtimes/ocw-runtime/src/lib.rs )
@@ -158,13 +153,13 @@ where
158153// ...snip
159154```
160155
161- There is a lot happening in the code. But basically we are :
156+ The overall goal is to execute the following :
162157
163- - Signing the ` call ` and ` extra ` , also called signed extension, and
164- - Making the call(` call ` , which includes the call paramters) and passing the sender ` address ` ,
165- signature of the data ` signature ` , and its signed extension ` extra ` on-chain as a transaction.
158+ - Sign the ` call ` and ` extra ` , also called ' signed extension'
159+ - Making the call (` call ` , which includes the call paramters) and passing the sender ` address ` ,
160+ signature of the data ` signature ` , and ensuring its signed extension ` extra ` on-chain as a transaction.
166161
167- ` SignedExtra ` data type is defined later in the runtime.
162+ ` SignedExtra ` data type will be defined later in the runtime.
168163
169164src:
170165[ ` runtimes/ocw-runtime/src/lib.rs ` ] ( https://github.com/substrate-developer-hub/recipes/tree/master/runtimes/ocw-runtime/src/lib.rs )
@@ -233,17 +228,17 @@ We have a function reference to `Call::submit_number_signed(submission)`. This i
233228are going to submit back to on-chain, and passing it to
234229` T::SubmitSignedTransaction::submit_signed(call) ` .
235230
236- You will notice that we run a for loop in the returned result. This implies that this call may make
237- multiple transactions and return multiple results. It is because this call actually signs and sends
231+ Notice that we run a loop in the returned result. This implies that the call may make
232+ multiple transactions and return multiple results. The call is both signing and sending
238233the transaction with each of the accounts that can be found locally under the application crypto
239- (which we defined earlier in ` pub mod crypto {...} ` ). You can view this as the local accounts that
240- are managed under this pallet namespace. Right now, we only have one key in the app crypto, so only
234+ (which we defined earlier in ` pub mod crypto {...} ` ). This can be seen as the local accounts that
235+ are managed under this pallet namespace. As we only have one key in the app crypto, so only
241236one signed transaction is made.
242237
243- Eventually, the ` call ` transaction is made on-chain via the ` create_transaction ` function we defined
238+ Eventually, the ` call ` transaction will be made on-chain via the ` create_transaction ` function we defined
244239earlier when we implemented ` CreateTransaction ` trait in our runtime.
245240
246- If you are wondering where we insert the local account in the pallet app crypto, it is actually in
241+ The local account used to sign the transaction is inserted in the pallet app crypto, and lives in
247242the outer node's [ service] ( https://substrate.dev/rustdocs/v2.0.0-rc5/sc_service/index.html ) .
248243
249244src:
@@ -276,8 +271,8 @@ pub fn new_full(config: Configuration<GenesisConfig>)
276271
277272### Setup
278273
279- By default, unsigned transactions are rejected by the Substrate runtime unless they are explicitly
280- allowed. So we need to write the logic to allow unsigned transactions for certain particular
274+ By default, unsigned transactions are rejected by the runtime unless they are explicitly
275+ allowed. So we need to write logic to allow unsigned transactions for certain particular
281276dispatched functions as follows:
282277
283278src:
@@ -304,18 +299,17 @@ impl<T: Trait> support::unsigned::ValidateUnsigned for Module<T> {
304299}
305300```
306301
307- We implement ` ValidateUnsigned ` , and the allowance logic is added inside the ` validate_unsigned `
308- function. We check if the call is to ` Call::submit_number_unsigned ` and returns ` Ok() ` if this is
309- the case. Otherwise, ` InvalidTransaction::Call ` .
302+ By implementing ` ValidateUnsigned ` , the allowance logic is added inside the ` validate_unsigned `
303+ function. We verify that if the call is ` Call::submit_number_unsigned ` we return ` Ok() ` , otherwise ` InvalidTransaction::Call ` .
310304
311- The ` ValidTransaction ` object has some fields that touch on concepts that we have not discussed
305+ Note that the ` ValidTransaction ` object has some fields that touch on concepts that we have not discussed
312306before:
313307
314308- ` priority ` : Ordering of two transactions, given their dependencies are satisfied.
315- - ` requires ` : List of tags this transaction depends on. Not shown in the above code as it is not
316- necessary in this case.
309+ - ` requires ` : List of tags the transaction depends on.
317310- ` provides ` : List of tags provided by this transaction. Successfully importing the transaction
318- will enable other transactions that depend on these tags to be included as well. ` provides ` and
311+ will enable other transactions that depend on these tags to be included as well.
312+ - Both` provides ` and
319313 ` requires ` tags allow Substrate to build a dependency graph of transactions and import them in
320314 the right order.
321315- ` longevity ` : Transaction longevity, which describes the minimum number of blocks the transaction
@@ -329,7 +323,7 @@ We are using the
329323build up this object.
330324
331325Finally, to tell the runtime that we have our own ` ValidateUnsigned ` logic, we also need to pass
332- this as a parameter when constructing the runtime.
326+ this as a parameter when constructing the runtime:
333327
334328src:
335329[ ` runtimes/ocw-runtime/src/lib.rs ` ] ( https://github.com/substrate-developer-hub/recipes/tree/master/runtimes/ocw-runtime/src/lib.rs )
@@ -370,7 +364,7 @@ fn send_unsigned(block_number: T::BlockNumber) -> Result<(), Error<T>> {
370364}
371365```
372366
373- As in signed transactions, we prepare a function reference with its parameters and then call
367+ As in signed transactions, we prepare a function reference with its parameters and call
374368` T::SubmitUnsignedTransaction::submit_unsigned ` .
375369
376370## Testing
0 commit comments