This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
refactor Offchain Worker examples #14280
Open
bernardoaraujor
wants to merge
77
commits into
master
Choose a base branch
from
bar/refactor-ocw-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 62 commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
3e87403
bootstrap ocw-ping-pong example
bernardoaraujor 0220b59
add tests for ocw-ping-pong-example
bernardoaraujor 5427c7a
rename offchain-worker-price-oracle example
bernardoaraujor f0a7e28
improve docs for ping-pong ocw example
bernardoaraujor 153ffc7
refactor price oracle ocw example
bernardoaraujor f5ac7cc
fix path for ocw-example-ping-pong on ci test script
bernardoaraujor 9632588
fix workspace paths for refactored ocw examples
bernardoaraujor 396f965
fix unused imports
bernardoaraujor b315942
lint
bernardoaraujor 1414f5d
fmt
bernardoaraujor 8006859
Merge branch 'master' into bar/refactor-ocw-examples
bernardoaraujor 7a25b06
update dependencies for offchain-worker-ping-pong
bernardoaraujor 7817d7b
lint ocw example docs
bernardoaraujor 92db6af
fix MaxAuthorities
bernardoaraujor 29018ff
add Authorities logic to ocw price oracle example
bernardoaraujor 26fa0f7
sanitize ocw ping pong example
bernardoaraujor ec8cd05
simplify ocw example READMEs
bernardoaraujor e0e1e9d
improve READMEs of ocw examples
bernardoaraujor 087e088
improve docs of ocw examples
bernardoaraujor 1478ccd
lint ocw example READMEs
bernardoaraujor e55ef38
lint ocw ping pong example docs
bernardoaraujor e340f8b
authorized OCWs don't need to pay fees
bernardoaraujor 585394c
lint ocw example README
bernardoaraujor f03b2ce
sanitize Cargo.toml for ocw-example-ping-pong
bernardoaraujor 34b9c81
simplify logic for ocw ping pong example (send all kinds of txs, rega…
bernardoaraujor ede32f2
fix ports for curl example of ocw examples
bernardoaraujor 83a097b
reduce UnsignedInterval for mock ocw-example-ping-pong
bernardoaraujor 4f4c40d
sanitize comments for ocw-example-ping-pong events
bernardoaraujor 0872689
ocw-example-ping-pong multiplexes unsigned tx type
bernardoaraujor 6434b81
check NextUnsignedAt earlier
bernardoaraujor 0344910
document that OCW account needs to be funded
bernardoaraujor 9cf6d06
fmt
bernardoaraujor 75b2068
sanitize ocw-example-ping-pong docs
bernardoaraujor 305416a
Update frame/examples/offchain-worker-ping-pong/README.md
bernardoaraujor cbd15da
rename pong_payload
bernardoaraujor 77e7929
sanitize comment
bernardoaraujor 1a5cd20
add missing events
bernardoaraujor 31723f9
rearrange pallet code for ocw examples
bernardoaraujor 24704aa
improve comments about keys on unsigned tx with signed payloads
bernardoaraujor 7fa8919
improve comments on ocw ping pong example
bernardoaraujor 25e1aeb
Update scripts/ci/gitlab/pipeline/test.yml
brunopgalvao 01af46e
remove markdown-link-check-disable
brunopgalvao 0f02340
remove markdown-link-check-disable
brunopgalvao 37814da
remove markdown-link-check-disable
brunopgalvao 1c97f28
add dev_mode
brunopgalvao 2728738
add dev_mode
brunopgalvao 46fb7cd
wrap using rewrap extension
brunopgalvao 1a78520
update comments with rewrap
brunopgalvao 17cee65
update comments with rewrap
brunopgalvao 3992198
cargo fmt -p pallet-example-offchain-worker-price-oracle
brunopgalvao 69c1ae4
cargo fmt -p pallet-example-offchain-worker-ping-pong
brunopgalvao f581c24
Merge branch 'master' into bar/refactor-ocw-examples
brunopgalvao a4c4216
add ocw examples as dependencies
brunopgalvao d4b76cb
remove duplicate cargo build
brunopgalvao 27364b1
Merge branch 'master' into bar/refactor-ocw-examples
brunopgalvao 4f4e456
use `DefaultConfig`
brunopgalvao b4bd829
implementation of default config for offchain-worker-ping-pong
AlexD10S 0a8010d
implementation of default config for offchain-worker-price-oracle
AlexD10S 67580f9
Merge branch 'master' into bar/refactor-ocw-examples
AlexD10S eef403f
fix issue with BlockNumber in offchain-worker examples
AlexD10S fea918c
cargo fmt
AlexD10S 2a29f02
Merge remote-tracking branch 'origin/master' into bar/refactor-ocw-ex…
ecf6135
Update frame/examples/offchain-worker-ping-pong/Cargo.toml
brunopgalvao c700339
start at 1.0.0
brunopgalvao 1b83748
Update frame/examples/offchain-worker-ping-pong/src/lib.rs
brunopgalvao 089f95e
remove weight 0, no need as pallet dev mode does this automatically
brunopgalvao 6537cd9
Update frame/examples/offchain-worker-ping-pong/Cargo.toml
brunopgalvao b034436
Update frame/examples/offchain-worker-ping-pong/src/lib.rs
brunopgalvao 3e604e0
Update frame/examples/offchain-worker-price-oracle/Cargo.toml
brunopgalvao a08af90
add doc comments for Authorities storage
brunopgalvao f96dacd
re-wrap to 100 line width
brunopgalvao 99ff657
remove RECENTLY_SENT alias
bernardoaraujor c19023d
fmt
bernardoaraujor 63b1029
enforce error propagation in ocw_pong_signed
bernardoaraujor c2368f4
fix comment about ValidateUnsigned trait
bernardoaraujor 5ec0432
fix typo
bernardoaraujor 4abbf7f
simplify syntax on for loop
bernardoaraujor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| [package] | ||
| name = "pallet-example-offchain-worker-ping-pong" | ||
| version = "4.0.0-dev" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2021" | ||
| license = "MIT-0" | ||
| homepage = "https://substrate.io" | ||
| repository = "https://github.com/paritytech/substrate/" | ||
| description = "FRAME example pallet for offchain worker (ping-pong)" | ||
| readme = "README.md" | ||
brunopgalvao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [package.metadata.docs.rs] | ||
| targets = ["x86_64-unknown-linux-gnu"] | ||
|
|
||
| [dependencies] | ||
| codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false } | ||
| lite-json = { version = "0.2.0", default-features = false } | ||
| log = { version = "0.4.17", default-features = false } | ||
| scale-info = { version = "2.5.0", default-features = false, features = ["derive"] } | ||
| frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" } | ||
| frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" } | ||
| sp-core = { version = "21.0.0", default-features = false, path = "../../../primitives/core" } | ||
| sp-io = { version = "23.0.0", default-features = false, path = "../../../primitives/io" } | ||
| sp-keystore = { version = "0.27.0", optional = true, path = "../../../primitives/keystore" } | ||
| sp-runtime = { version = "24.0.0", default-features = false, path = "../../../primitives/runtime" } | ||
| sp-std = { version = "8.0.0", default-features = false, path = "../../../primitives/std" } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "codec/std", | ||
| "frame-support/std", | ||
| "frame-system/std", | ||
| "lite-json/std", | ||
| "log/std", | ||
| "scale-info/std", | ||
| "sp-core/std", | ||
| "sp-io/std", | ||
| "sp-keystore", | ||
| "sp-runtime/std", | ||
| "sp-std/std", | ||
| ] | ||
| try-runtime = ["frame-support/try-runtime"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Ping-Pong Offchain Worker Example Pallet | ||
|
|
||
| A simple pallet demonstrating concepts, APIs and structures common to most offchain workers. | ||
|
|
||
| Run `cargo doc --package pallet-example-offchain-worker-ping-pong --open` to view this module's | ||
| documentation. | ||
|
|
||
| This is a simple example pallet to showcase how the runtime can and should interact with an offchain worker asynchronously. | ||
| It also showcases the potential pitfalls and security considerations that come with it. | ||
|
|
||
| It is based on [this example by `gnunicorn`](https://gnunicorn.github.io/substrate-offchain-cb/), | ||
| although an updated version with a few modifications. | ||
|
|
||
| The example plays simple ping-pong with off-chain workers: | ||
| Once a signed transaction to `ping` is submitted (by any user), Ping request is written into Storage. | ||
| Each ping request has a `nonce`, which is arbitrarily chosen by the user (not necessarily unique). | ||
|
|
||
| After every block, the offchain worker is triggered. If it sees a Ping request in the current | ||
| block, it reacts by sending a transaction to send a Pong with the corresponding `nonce`. When `pong_*` extrinsics are executed, | ||
| they emit an `PongAck*` event so we can track with existing UIs. | ||
|
|
||
| The `PongAck*` events come in two different flavors: | ||
| - `PongAckAuthenticated`: emitted when the call was made by an **authenticated** offchain worker (whitelisted via `Authorities` storage) | ||
| - `PongAckUnauthenticated`: emitted when the call was made by an **unauthenticated** offchain worker (or potentially malicious actors) | ||
|
|
||
| The security implications of `PongAckUnauthenticated` should be obvious: not **ONLY** offchain workers can | ||
| call `pong_unsigned_*`. **ANYONE** can do it, and they can actually use a different `nonce` | ||
| from the original ping (try it yourself!). If the `nonce` actually had an important meaning to the state of our chain, this would be a **VULNERABILITY**! | ||
|
|
||
| This is meant to highlight the importance of solid security assumptions when using unsigned transactions. | ||
| In other words: | ||
|
|
||
| ⚠️ **DO NOT USE UNSIGNED TRANSACTIONS UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING!** ⚠️ | ||
|
|
||
| Here's an example of how a node admin can inject some keys into the keystore, so that the OCW | ||
| can call `pong_signed`: | ||
|
|
||
| ```bash | ||
| $ curl --location --request POST 'http://localhost:9944' \ | ||
| --header 'Content-Type: application/json' \ | ||
| --data-raw '{ | ||
| "jsonrpc": "2.0", | ||
| "method": "author_insertKey", | ||
| "params": ["pong","bread tongue spell stadium clean grief coin rent spend total practice document","0xb6a8b4b6bf796991065035093d3265e314c3fe89e75ccb623985e57b0c2e0c30"], | ||
brunopgalvao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "id": 1 | ||
| }' | ||
| ``` | ||
|
|
||
| Then make sure that the corresponding address (`5GCCgshTQCfGkXy6kAkFDW1TZXAdsbCNZJ9Uz2c7ViBnwcVg`) has funds and is added to `Authorities` in the runtime by adding it via `add_authority` extrinsic (from `root`). | ||
|
|
||
| More complex management models and session | ||
| based key rotations should be considered, but that's outside the scope of this example. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.