Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions prdoc/pr_10716.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
title: Migrate `pallet-example-offchain-worker` to use `TransactionExtension` API

doc:
- audience: Runtime Dev
description: |-
Migrates `pallet-example-offchain-worker` from the deprecated `ValidateUnsigned` trait
to the modern `TransactionExtension` API using the `#[pallet::authorize]` attribute.

This change demonstrates how to validate unsigned transactions using the new approach,
which provides better composability and flexibility for transaction validation.

The pallet now uses `#[pallet::authorize]` on unsigned transaction calls to validate:
- Block number is within the expected window
- Price data is valid
- For signed payloads, signature verification using the authority's public key

This serves as a reference example for other pallets migrating away from `ValidateUnsigned`.

crates:
- name: pallet-example-offchain-worker
bump: major
25 changes: 20 additions & 5 deletions substrate/frame/examples/offchain-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ concepts, APIs and structures common to most offchain workers.
Run `cargo doc --package pallet-example-offchain-worker --open` to view this module's
documentation.

- [`pallet_example_offchain_worker::Trait`](./trait.Trait.html)
- [`Config`](./trait.Config.html)
- [`Call`](./enum.Call.html)
- [`Module`](./struct.Module.html)
- [`Pallet`](./struct.Pallet.html)

**This pallet serves as an example showcasing Substrate off-chain worker and is not meant to be
used in production.**
Expand All @@ -19,11 +19,26 @@ used in production.**
In this example we are going to build a very simplistic, naive and definitely NOT
production-ready oracle for BTC/USD price.
Offchain Worker (OCW) will be triggered after every block, fetch the current price
and prepare either signed or unsigned transaction to feed the result back on chain.
and prepare either signed or general transaction to feed the result back on chain.
The on-chain logic will simply aggregate the results and store last `64` values to compute
the average price.
Additional logic in OCW is put in place to prevent spamming the network with both signed
and unsigned transactions, and custom `UnsignedValidator` makes sure that there is only
one unsigned transaction floating in the network.
and general transactions. The pallet uses the `#[pallet::authorize]` attribute to validate
general transactions, ensuring that only one general transaction can be accepted per
`AuthorizedTxInterval` blocks.

## General Transaction Validation

This pallet demonstrates how to validate general transactions using the modern
`#[pallet::authorize]` attribute instead of the deprecated `ValidateUnsigned` trait.

The `#[pallet::authorize]` attribute is used on the general transaction calls:
- `submit_price_authorized` - Validates a simple general transaction
- `submit_price_authorized_with_signed_payload` - Validates a general transaction with a signed payload

The authorization logic checks:
1. Block number is within the expected window (via `NextAuthorizedAt` storage)
2. The price data is valid
3. For signed payloads, verifies the signature using the authority's public key

License: MIT-0
Loading
Loading