On-Demand Integration
In this document we will go step by step on how to integrate on-demand coretime procurement for a parachain.
Basic understanding of the on-demand integration
Polkadot offers two ways for parachains to acquire Coretime: one by purchasing it in bulk from the Coretime chain, and the other through on-demand usage.
Both approaches have their pros and cons and can potentially also be used in combination.
With on-demand, the parachain produces blocks only when an on-demand order is created. Anyone can place an order; however, doing so incurs a cost for the caller.
In the current version, the provided modules offer a solution where collators are expected to place orders. We are dividing time into slots, each assigned to a specific collator responsible for placing an order. Collators are rewarded for placing an order when it is their turn.
Implementation Guide
The following guide explains how to implement on-demand modules for a parachain to automate order creation.
The modules are highly customizable, allowing each parachain to define its own rules for placing orders.
The template code that implements an on-demand parachain can be found here: https://github.com/RegionX-Labs/On-Demand/tree/master/template
Runtime
The on-demand pallet is a pallet designed to store configurations related to order creation. It exposes extrinsics that can be called by an AdminOrigin to update the configurations.
Additionally, this pallet is responsible for rewarding collators when they place an order within their slot. This part of the logic is implemented in an inherent, which tries to find the OnDemandOrderPlaced event in the specified relay chain block. If the order exists and the order placer is the expected collator, the pallet will reward the collator.
The following subsections will explain how to add the pallet to your parachain's runtime.
Add pallet-on-demandto Cargo.toml
Configure the pallet
Runtime API
Add the following implementation in the runtime/src/apis.rsfile:
Benchmarks
Add the following implementation in the runtime/src/benchmarks.rsfile:
Node
On the node side, we will set up an on-demand service that will be automatically started when running a collator node.
Add on-demand-serviceto Cargo.toml
OnDemandConfig
In the node/src/service.rs file we will configure OnDemandConfigwhich is passed to the on-demand service.
Below is an example implementation of the order placement criteria. Each parachain is responsible for defining its own rules; however, in this example, we determine whether an order should be placed based on the total fees of ready transactions in the pool. If the threshold is reached, we return true, signaling that an order should be created.
Now that we have all the configuration available we can add the code which starts the on-demand service. This should be added within the start_parachain_node function (it might have a different name), at the point where we are conditionally starting consensus participation for the node, call the on-demand service:
Pallet On-Demand Overview
In this section, we will go over the extrinsics for modifying the on-demand configuration.
1. set_slot_width
set_slot_widthAn extrinsic for setting the slot width to coordinate order placement among collators. The slot width is defined in relay chain blocks. Each slot has a supposed order placer responsible for placing an order. There is no penalty for not placing an order; however, collators are incentivized to do so through a reward.
The slot width should be set based on the expected time required for a collator to submit an order and for it to be included in a relay chain block.
For example, if set to 4, the expected order placer will change every 4 relay chain blocks.
Callable by AdminOrigin.
2. set_threshold_parameter
set_threshold_parameterExtrinsic for setting a threshold parameter of type Config::ThresholdParameter
This parameter can be used in the logic for deciding whether to place an order. We are keeping this generic, as we do not assume any specific logic for determining whether an order should be placed. It is provided simply to facilitate implementations that want to have an on-chain threshold parameter, which can be set by the AdminOrigin.
For example, this could store a 'fee threshold' for a block, where an order should be placed once the threshold is exceeded.
3. set_bulk_mode
set_bulk_modeIf the AdminOrigin decides that the parachain should rely on bulk Coretime and no longer reward order placement, it can enforce this by calling this extrinsic.
If set to true, the parachain will no longer reward order placements. However, this does not restrict anyone from placing an order; they simply won't receive a reward for doing so.
Last updated