Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

refactor Offchain Worker examples #14280

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
3e87403
bootstrap ocw-ping-pong example
bernardoaraujor May 29, 2023
0220b59
add tests for ocw-ping-pong-example
bernardoaraujor May 31, 2023
5427c7a
rename offchain-worker-price-oracle example
bernardoaraujor May 31, 2023
f0a7e28
improve docs for ping-pong ocw example
bernardoaraujor May 31, 2023
153ffc7
refactor price oracle ocw example
bernardoaraujor May 31, 2023
f5ac7cc
fix path for ocw-example-ping-pong on ci test script
bernardoaraujor May 31, 2023
9632588
fix workspace paths for refactored ocw examples
bernardoaraujor May 31, 2023
396f965
fix unused imports
bernardoaraujor May 31, 2023
b315942
lint
bernardoaraujor May 31, 2023
1414f5d
fmt
bernardoaraujor May 31, 2023
8006859
Merge branch 'master' into bar/refactor-ocw-examples
bernardoaraujor May 31, 2023
7a25b06
update dependencies for offchain-worker-ping-pong
bernardoaraujor May 31, 2023
7817d7b
lint ocw example docs
bernardoaraujor May 31, 2023
92db6af
fix MaxAuthorities
bernardoaraujor May 31, 2023
29018ff
add Authorities logic to ocw price oracle example
bernardoaraujor May 31, 2023
26fa0f7
sanitize ocw ping pong example
bernardoaraujor May 31, 2023
ec8cd05
simplify ocw example READMEs
bernardoaraujor May 31, 2023
e0e1e9d
improve READMEs of ocw examples
bernardoaraujor May 31, 2023
087e088
improve docs of ocw examples
bernardoaraujor May 31, 2023
1478ccd
lint ocw example READMEs
bernardoaraujor May 31, 2023
e55ef38
lint ocw ping pong example docs
bernardoaraujor May 31, 2023
e340f8b
authorized OCWs don't need to pay fees
bernardoaraujor May 31, 2023
585394c
lint ocw example README
bernardoaraujor May 31, 2023
f03b2ce
sanitize Cargo.toml for ocw-example-ping-pong
bernardoaraujor May 31, 2023
34b9c81
simplify logic for ocw ping pong example (send all kinds of txs, rega…
bernardoaraujor May 31, 2023
ede32f2
fix ports for curl example of ocw examples
bernardoaraujor May 31, 2023
83a097b
reduce UnsignedInterval for mock ocw-example-ping-pong
bernardoaraujor May 31, 2023
4f4c40d
sanitize comments for ocw-example-ping-pong events
bernardoaraujor May 31, 2023
0872689
ocw-example-ping-pong multiplexes unsigned tx type
bernardoaraujor May 31, 2023
6434b81
check NextUnsignedAt earlier
bernardoaraujor May 31, 2023
0344910
document that OCW account needs to be funded
bernardoaraujor May 31, 2023
9cf6d06
fmt
bernardoaraujor Jun 1, 2023
75b2068
sanitize ocw-example-ping-pong docs
bernardoaraujor Jun 1, 2023
305416a
Update frame/examples/offchain-worker-ping-pong/README.md
bernardoaraujor Jun 8, 2023
cbd15da
rename pong_payload
bernardoaraujor Jun 8, 2023
77e7929
sanitize comment
bernardoaraujor Jun 8, 2023
1a5cd20
add missing events
bernardoaraujor Jun 8, 2023
31723f9
rearrange pallet code for ocw examples
bernardoaraujor Jun 8, 2023
24704aa
improve comments about keys on unsigned tx with signed payloads
bernardoaraujor Jun 8, 2023
7fa8919
improve comments on ocw ping pong example
bernardoaraujor Jun 8, 2023
25e1aeb
Update scripts/ci/gitlab/pipeline/test.yml
brunopgalvao Jun 20, 2023
01af46e
remove markdown-link-check-disable
brunopgalvao Jun 20, 2023
0f02340
remove markdown-link-check-disable
brunopgalvao Jun 20, 2023
37814da
remove markdown-link-check-disable
brunopgalvao Jun 20, 2023
1c97f28
add dev_mode
brunopgalvao Jun 20, 2023
2728738
add dev_mode
brunopgalvao Jun 20, 2023
46fb7cd
wrap using rewrap extension
brunopgalvao Jun 20, 2023
1a78520
update comments with rewrap
brunopgalvao Jun 20, 2023
17cee65
update comments with rewrap
brunopgalvao Jun 20, 2023
3992198
cargo fmt -p pallet-example-offchain-worker-price-oracle
brunopgalvao Jun 20, 2023
69c1ae4
cargo fmt -p pallet-example-offchain-worker-ping-pong
brunopgalvao Jun 20, 2023
f581c24
Merge branch 'master' into bar/refactor-ocw-examples
brunopgalvao Jun 20, 2023
a4c4216
add ocw examples as dependencies
brunopgalvao Jun 20, 2023
d4b76cb
remove duplicate cargo build
brunopgalvao Jun 21, 2023
27364b1
Merge branch 'master' into bar/refactor-ocw-examples
brunopgalvao Jun 21, 2023
4f4e456
use `DefaultConfig`
brunopgalvao Jul 16, 2023
b4bd829
implementation of default config for offchain-worker-ping-pong
AlexD10S Jul 20, 2023
0a8010d
implementation of default config for offchain-worker-price-oracle
AlexD10S Jul 20, 2023
67580f9
Merge branch 'master' into bar/refactor-ocw-examples
AlexD10S Jul 20, 2023
eef403f
fix issue with BlockNumber in offchain-worker examples
AlexD10S Jul 21, 2023
fea918c
cargo fmt
AlexD10S Jul 21, 2023
2a29f02
Merge remote-tracking branch 'origin/master' into bar/refactor-ocw-ex…
Jul 27, 2023
ecf6135
Update frame/examples/offchain-worker-ping-pong/Cargo.toml
brunopgalvao Jul 28, 2023
c700339
start at 1.0.0
brunopgalvao Jul 28, 2023
1b83748
Update frame/examples/offchain-worker-ping-pong/src/lib.rs
brunopgalvao Jul 28, 2023
089f95e
remove weight 0, no need as pallet dev mode does this automatically
brunopgalvao Jul 28, 2023
6537cd9
Update frame/examples/offchain-worker-ping-pong/Cargo.toml
brunopgalvao Jul 28, 2023
b034436
Update frame/examples/offchain-worker-ping-pong/src/lib.rs
brunopgalvao Jul 28, 2023
3e604e0
Update frame/examples/offchain-worker-price-oracle/Cargo.toml
brunopgalvao Jul 28, 2023
a08af90
add doc comments for Authorities storage
brunopgalvao Jul 28, 2023
f96dacd
re-wrap to 100 line width
brunopgalvao Jul 28, 2023
99ff657
remove RECENTLY_SENT alias
bernardoaraujor Aug 11, 2023
c19023d
fmt
bernardoaraujor Aug 11, 2023
63b1029
enforce error propagation in ocw_pong_signed
bernardoaraujor Aug 11, 2023
c2368f4
fix comment about ValidateUnsigned trait
bernardoaraujor Aug 11, 2023
5ec0432
fix typo
bernardoaraujor Aug 11, 2023
4abbf7f
simplify syntax on for loop
bernardoaraujor Aug 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -147,7 +147,8 @@ members = [
"frame/election-provider-support/solution-type/fuzzer",
"frame/examples",
"frame/examples/basic",
"frame/examples/offchain-worker",
"frame/examples/offchain-worker-ping-pong",
"frame/examples/offchain-worker-price-oracle",
"frame/examples/kitchensink",
"frame/examples/dev-mode",
"frame/examples/split",
Expand Down
9 changes: 6 additions & 3 deletions frame/examples/Cargo.toml
Expand Up @@ -14,7 +14,8 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
pallet-example-basic = { default-features = false, path = "./basic" }
pallet-default-config-example = { default-features = false, path = "./default-config" }
pallet-example-offchain-worker = { default-features = false, path = "./offchain-worker" }
pallet-example-offchain-worker-ping-pong = { default-features = false, path = "./offchain-worker-ping-pong" }
pallet-example-offchain-worker-price-oracle = { default-features = false, path = "./offchain-worker-price-oracle" }
pallet-example-kitchensink = { default-features = false, path = "./kitchensink" }
pallet-dev-mode = { default-features = false, path = "./dev-mode" }
pallet-example-split = { default-features = false, path = "./split" }
Expand All @@ -24,15 +25,17 @@ default = [ "std" ]
std = [
"pallet-example-basic/std",
"pallet-default-config-example/std",
"pallet-example-offchain-worker/std",
"pallet-example-offchain-worker-ping-pong/std",
"pallet-example-offchain-worker-price-oracle/std",
"pallet-example-kitchensink/std",
"pallet-dev-mode/std",
"pallet-example-split/std",
]
try-runtime = [
"pallet-example-basic/try-runtime",
"pallet-default-config-example/try-runtime",
"pallet-example-offchain-worker/try-runtime",
"pallet-example-offchain-worker-ping-pong/try-runtime",
"pallet-example-offchain-worker-price-oracle/try-runtime",
"pallet-example-kitchensink/try-runtime",
"pallet-dev-mode/try-runtime",
"pallet-example-split/try-runtime",
Expand Down
44 changes: 44 additions & 0 deletions frame/examples/offchain-worker-ping-pong/Cargo.toml
@@ -0,0 +1,44 @@
[package]
name = "pallet-example-offchain-worker-ping-pong"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
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
publish = false

[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"]
52 changes: 52 additions & 0 deletions frame/examples/offchain-worker-ping-pong/README.md
@@ -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.