Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does on demand coretime work? #173

Closed
Tracked by #63
poppyseedDev opened this issue May 2, 2024 · 2 comments
Closed
Tracked by #63

How does on demand coretime work? #173

poppyseedDev opened this issue May 2, 2024 · 2 comments
Labels
$$$$$ ~ 600-800 USD READ ONLY Requires research into how something should be implemented, write a thorough description. SubstrateDevs This label is great for substrate devs, who detest frontend as I was told 😂

Comments

@poppyseedDev
Copy link
Contributor

poppyseedDev commented May 2, 2024

Explain what the reading of the state mean:

instaPoolIo
instaPoolContribution
instaPoolHistory

Extrinsic:
pool(... )
purchaseCredit(...) is already implemented, unclear is how to use what to input for it's amount

Make also a list of events and if it's possible to use some of them instead of reading the state directly.
https://lastic.squids.live/rococo-coretime/graphql

This issue is a bit longer one, I think some of the code is written on the coretime chain and some functions are on the relay chain.


How to implement On Demand Coretime?

It works in the following way:

  • person should buy the core, then call this extrinsic
  • to visualize do ....

issue resolve:
To implement it you should interact with the following functions:


Ask under the issue if something is unclear, comment if you are ready to take this issue on :)

@poppyseedDev poppyseedDev mentioned this issue May 2, 2024
5 tasks
@poppyseedDev poppyseedDev added SubstrateDevs This label is great for substrate devs, who detest frontend as I was told 😂 $$$ ~151-300usd READ ONLY Requires research into how something should be implemented, write a thorough description. labels May 2, 2024
@poppyseedDev poppyseedDev added $$$$$ ~ 600-800 USD and removed $$$ ~151-300usd labels May 20, 2024
@tien
Copy link
Contributor

tien commented Jun 5, 2024

Polkadot On-demand Coretime

Storages

InstaPooIo

/// Record of Coretime entering or leaving the On-demand Coretime Pool.
#[pallet::storage]
pub type InstaPoolIo<T> = StorageMap<_, Blake2_128Concat, Timeslice, PoolIoRecord, ValueQuery>;

InstaPoolContribution

/// Record of a single contribution to the On-demand Coretime Pool.
#[pallet::storage]
pub type InstaPoolContribution<T> =
    StorageMap<_, Blake2_128Concat, RegionId, ContributionRecordOf<T>, OptionQuery>;

InstaPoolHistory

/// Total InstaPool rewards for each Timeslice and the number of core parts which contributed.
#[pallet::storage]
pub type InstaPoolHistory<T> =
    StorageMap<_, Blake2_128Concat, Timeslice, InstaPoolHistoryRecordOf<T>>;

Extrinsics

Pool

Source

/// Place a Bulk Coretime Region into the On-demand Coretime Pool.
///
/// - `origin`: Must be a Signed origin of the account which owns the Region `region_id`.
/// - `region_id`: The Region which should be assigned to the Pool.
/// - `payee`: The account which is able to collect any revenue due for the usage of this
///   Coretime.
#[pallet::call_index(11)]
pub fn pool(
    origin: OriginFor<T>,
    region_id: RegionId,
    payee: T::AccountId,
    finality: Finality,
) -> DispatchResultWithPostInfo {
    let who = ensure_signed(origin)?;
    Self::do_pool(region_id, Some(who), payee, finality)?;
    Ok(if finality == Finality::Final { Pays::No } else { Pays::Yes }.into())
}

Description

This shall have the effect of placing an item in the workplan corresponding to the region's properties and assigned to the On-demand Coretime Pool. The details of the region will be recorded in order to allow for a pro rata share of the On-demand Coretime Sales Revenue at the time of the Region relative to any other providers in the Pool.

Purchase credit

Source

/// Purchase credit for use in the On-demand Coretime Pool.
///
/// - `origin`: Must be a Signed origin able to pay at least `amount`.
/// - `amount`: The amount of credit to purchase.
/// - `beneficiary`: The account on the Relay-chain which controls the credit (generally
///   this will be the collator's hot wallet).
#[pallet::call_index(13)]
pub fn purchase_credit(
    origin: OriginFor<T>,
    amount: BalanceOf<T>,
    beneficiary: RelayAccountIdOf<T>,
) -> DispatchResult {
    let who = ensure_signed(origin)?;
    Self::do_purchase_credit(who, amount, beneficiary)?;
    Ok(())
}

Description

Any account with at least amount spendable funds may call this. This increases the On-demand Coretime Credit balance on the Relay-chain of the beneficiary by the given amount.

This Credit is consumable on the Relay-chain as part of the Task scheduling system. When consumed, revenue is recorded and provided to the Coretime-chain for proper distribution. The API for doing this is specified in RFC-5.

Events

pub enum Event<T: Config> {
    /// A Region of Bulk Coretime has been purchased.
    Purchased {
        /// The identity of the purchaser.
        who: T::AccountId,
        /// The identity of the Region.
        region_id: RegionId,
        /// The price paid for this Region.
        price: BalanceOf<T>,
        /// The duration of the Region.
        duration: Timeslice,
    },
    /// The workload of a core has become renewable.
    Renewable {
        /// The core whose workload can be renewed.
        core: CoreIndex,
        /// The price at which the workload can be renewed.
        price: BalanceOf<T>,
        /// The time at which the workload would recommence of this renewal. The call to renew
        /// cannot happen before the beginning of the interlude prior to the sale for regions
        /// which begin at this time.
        begin: Timeslice,
        /// The actual workload which can be renewed.
        workload: Schedule,
    },
    /// A workload has been renewed.
    Renewed {
        /// The identity of the renewer.
        who: T::AccountId,
        /// The price paid for this renewal.
        price: BalanceOf<T>,
        /// The index of the core on which the `workload` was previously scheduled.
        old_core: CoreIndex,
        /// The index of the core on which the renewed `workload` has been scheduled.
        core: CoreIndex,
        /// The time at which the `workload` will begin on the `core`.
        begin: Timeslice,
        /// The number of timeslices for which this `workload` is newly scheduled.
        duration: Timeslice,
        /// The workload which was renewed.
        workload: Schedule,
    },
    /// Ownership of a Region has been transferred.
    Transferred {
        /// The Region which has been transferred.
        region_id: RegionId,
        /// The duration of the Region.
        duration: Timeslice,
        /// The old owner of the Region.
        old_owner: Option<T::AccountId>,
        /// The new owner of the Region.
        owner: Option<T::AccountId>,
    },
    /// A Region has been split into two non-overlapping Regions.
    Partitioned {
        /// The Region which was split.
        old_region_id: RegionId,
        /// The new Regions into which it became.
        new_region_ids: (RegionId, RegionId),
    },
    /// A Region has been converted into two overlapping Regions each of lesser regularity.
    Interlaced {
        /// The Region which was interlaced.
        old_region_id: RegionId,
        /// The new Regions into which it became.
        new_region_ids: (RegionId, RegionId),
    },
    /// A Region has been assigned to a particular task.
    Assigned {
        /// The Region which was assigned.
        region_id: RegionId,
        /// The duration of the assignment.
        duration: Timeslice,
        /// The task to which the Region was assigned.
        task: TaskId,
    },
    /// A Region has been added to the On-demand Coretime Pool.
    Pooled {
        /// The Region which was added to the On-demand Coretime Pool.
        region_id: RegionId,
        /// The duration of the Region.
        duration: Timeslice,
    },
    /// A new number of cores has been requested.
    CoreCountRequested {
        /// The number of cores requested.
        core_count: CoreIndex,
    },
    /// The number of cores available for scheduling has changed.
    CoreCountChanged {
        /// The new number of cores available for scheduling.
        core_count: CoreIndex,
    },
    /// There is a new reservation for a workload.
    ReservationMade {
        /// The index of the reservation.
        index: u32,
        /// The workload of the reservation.
        workload: Schedule,
    },
    /// A reservation for a workload has been cancelled.
    ReservationCancelled {
        /// The index of the reservation which was cancelled.
        index: u32,
        /// The workload of the now cancelled reservation.
        workload: Schedule,
    },
    /// A new sale has been initialized.
    SaleInitialized {
        /// The local block number at which the sale will/did start.
        sale_start: BlockNumberFor<T>,
        /// The length in blocks of the Leadin Period (where the price is decreasing).
        leadin_length: BlockNumberFor<T>,
        /// The price of Bulk Coretime at the beginning of the Leadin Period.
        start_price: BalanceOf<T>,
        /// The price of Bulk Coretime after the Leadin Period.
        regular_price: BalanceOf<T>,
        /// The first timeslice of the Regions which are being sold in this sale.
        region_begin: Timeslice,
        /// The timeslice on which the Regions which are being sold in the sale terminate.
        /// (i.e. One after the last timeslice which the Regions control.)
        region_end: Timeslice,
        /// The number of cores we want to sell, ideally. Selling this amount would result in
        /// no change to the price for the next sale.
        ideal_cores_sold: CoreIndex,
        /// Number of cores which are/have been offered for sale.
        cores_offered: CoreIndex,
    },
    /// A new lease has been created.
    Leased {
        /// The task to which a core will be assigned.
        task: TaskId,
        /// The timeslice contained in the sale period after which this lease will
        /// self-terminate (and therefore the earliest timeslice at which the lease may no
        /// longer apply).
        until: Timeslice,
    },
    /// A lease is about to end.
    LeaseEnding {
        /// The task to which a core was assigned.
        task: TaskId,
        /// The timeslice at which the task will no longer be scheduled.
        when: Timeslice,
    },
    /// The sale rotation has been started and a new sale is imminent.
    SalesStarted {
        /// The nominal price of an Region of Bulk Coretime.
        price: BalanceOf<T>,
        /// The maximum number of cores which this pallet will attempt to assign.
        core_count: CoreIndex,
    },
    /// The act of claiming revenue has begun.
    RevenueClaimBegun {
        /// The region to be claimed for.
        region: RegionId,
        /// The maximum number of timeslices which should be searched for claimed.
        max_timeslices: Timeslice,
    },
    /// A particular timeslice has a non-zero claim.
    RevenueClaimItem {
        /// The timeslice whose claim is being processed.
        when: Timeslice,
        /// The amount which was claimed at this timeslice.
        amount: BalanceOf<T>,
    },
    /// A revenue claim has (possibly only in part) been paid.
    RevenueClaimPaid {
        /// The account to whom revenue has been paid.
        who: T::AccountId,
        /// The total amount of revenue claimed and paid.
        amount: BalanceOf<T>,
        /// The next region which should be claimed for the continuation of this contribution.
        next: Option<RegionId>,
    },
    /// Some On-demand Coretime Pool credit has been purchased.
    CreditPurchased {
        /// The account which purchased the credit.
        who: T::AccountId,
        /// The Relay-chain account to which the credit will be made.
        beneficiary: RelayAccountIdOf<T>,
        /// The amount of credit purchased.
        amount: BalanceOf<T>,
    },
    /// A Region has been dropped due to being out of date.
    RegionDropped {
        /// The Region which no longer exists.
        region_id: RegionId,
        /// The duration of the Region.
        duration: Timeslice,
    },
    /// Some historical On-demand Core Pool contribution record has been dropped.
    ContributionDropped {
        /// The Region whose contribution is no longer exists.
        region_id: RegionId,
    },
    /// Some historical On-demand Core Pool payment record has been initialized.
    HistoryInitialized {
        /// The timeslice whose history has been initialized.
        when: Timeslice,
        /// The amount of privately contributed Coretime to the On-demand Coretime Pool.
        private_pool_size: CoreMaskBitCount,
        /// The amount of Coretime contributed to the On-demand Coretime Pool by the
        /// Polkadot System.
        system_pool_size: CoreMaskBitCount,
    },
    /// Some historical On-demand Core Pool payment record has been dropped.
    HistoryDropped {
        /// The timeslice whose history is no longer available.
        when: Timeslice,
        /// The amount of revenue the system has taken.
        revenue: BalanceOf<T>,
    },
    /// Some historical On-demand Core Pool payment record has been ignored because the
    /// timeslice was already known. Governance may need to intervene.
    HistoryIgnored {
        /// The timeslice whose history is was ignored.
        when: Timeslice,
        /// The amount of revenue which was ignored.
        revenue: BalanceOf<T>,
    },
    /// Some historical On-demand Core Pool Revenue is ready for payout claims.
    ClaimsReady {
        /// The timeslice whose history is available.
        when: Timeslice,
        /// The amount of revenue the Polkadot System has already taken.
        system_payout: BalanceOf<T>,
        /// The total amount of revenue remaining to be claimed.
        private_payout: BalanceOf<T>,
    },
    /// A Core has been assigned to one or more tasks and/or the Pool on the Relay-chain.
    CoreAssigned {
        /// The index of the Core which has been assigned.
        core: CoreIndex,
        /// The Relay-chain block at which this assignment should take effect.
        when: RelayBlockNumberOf<T>,
        /// The workload to be done on the Core.
        assignment: Vec<(CoreAssignment, PartsOf57600)>,
    },
    /// Some historical On-demand Core Pool payment record has been dropped.
    AllowedRenewalDropped {
        /// The timeslice whose renewal is no longer available.
        when: Timeslice,
        /// The core whose workload is no longer available to be renewed for `when`.
        core: CoreIndex,
    },
}

How to implement On Demand Coretime?

On-demand parachain (buy core to use)

High level overview

As a parachain team

I want to purchase on-demand core credit

So that my parachain can produce block when needed

User flow

Purchasing credit
  1. Purchase on-demand core credit using purchase_credit(amount: Balance, beneficiary: RelayChainAccountId) via Lastic
  2. Parachain code can assign core to task using the purchased credit
Checking credit balance

WIP

Trader (buy core for profit)

High level overview

As a user

I want to purchase bulk core time & add it to the On-demand Coretime Pool

So that when On-demand parachain (previously called parathread) consume my core time, I'll be rewarded as the beneficiary of that core time

User flow

Adding core time to the On-demand Coretime Pool
  1. Purchase core using purchase(price_limit: Balance)
  2. Place the purchased core into the On-demand Coretime Pool using pool(region: RegionId, beneficiary: AccountId, finality: Finality)
Receive visualization & get notified when purchased core time get consumed
Current coretime contribution

Read storage InstaPoolContribution to get a list of all unconsumed coretime contribution keyed by RegionId, which can be filtered by user account on value of type ContributionRecordOf.payee

Consumed coretime contribution (paid out)

Index event RevenueClaimPaid which provide the following information of which can be put on a graph or table:

RevenueClaimPaid {
    /// The account to whom revenue has been paid.
    who: T::AccountId,
    /// The total amount of revenue claimed and paid.
    amount: BalanceOf<T>,
    /// The next region which should be claimed for the continuation of this contribution.
    next: Option<RegionId>,
},

@poppyseedDev
Copy link
Contributor Author

I switched up the order a bit to allow for more clarity for the reader. Looks like a lot of things on the extrinsics side need to be implemented.

To be continued 👍


Polkadot On-demand Coretime

What is On-demand Coretime?

On-demand or instantaneous coretime refers to the ability to allocate computational resources (cores) on the Polkadot Ubiquitous Computer quickly and dynamically. This capability allows for efficient and flexible use of computational resources based on immediate needs.

The inner workings of On-demand Coretime are specified in RFC-5.

Definitions:

  • Instantaneous Coretime Pool: A pool of coretime that can be allocated instantly to various tasks or users.

How to implement On-demand Coretime?

The following sections describe the user flows necessary to enable seamless interaction with the on-demand coretime:

User Flow for Parachain Teams

This flow is for parachain teams who already have a ParaId and code they want to test, but need coretime to do so.

Steps:

  1. Purchase On-demand Core Credit:

    • Use the purchase_credit(amount: Balance, beneficiary: RelayChainAccountId) extrinsic on the broker pallet on the Coretime chain.
    • This increases the On-demand Coretime Credit balance on the Relay-chain of the beneficiary by the given amount.
  2. Assign Core to Task:

    • Parachain code can assign core to tasks using the purchased credit.
    • Use the assign_core(core: CoreIndex, begin: BlockNumber, assignment: Vec<(CoreAssignment, PartsOf57600)>, end_hint: Option<BlockNumber>) function to allocate cores for specific tasks.

Checking Credit Balance

Currently, checking the credit balance is a work in progress. For now, Coretime credits have been disabled. Follow the progress here: Parity issue #2210.

Trader Flow for Profit

This flow is for traders who aim to buy coretime for profit and place it into the Instantaneous Coretime Pool to earn a return on investment.

Steps:

  1. Purchase Core:

    • Use the purchase(price_limit: Balance) extrinsic to buy coretime.
    • Ensure the price limit is set appropriately to maximize profit potential.
  2. Add Coretime to the Instantaneous Coretime Pool:

    • Use the pool(region: RegionId, beneficiary: AccountId, finality: Finality) extrinsic from the broker pallet on the Coretime chain to place the purchased core into the pool.
    • This allows the coretime to be utilized by on-demand parachains, generating revenue.

Receive Visualization & Notifications for Consumed Coretime

  1. Current Coretime Contribution:
    • Read the InstaPoolContribution storage to get a list of all unconsumed Coretime contributions keyed by RegionId.
    • Filter the results by user account on the value of type ContributionRecordOf.payee.
/// Record of a single contribution to the On-demand Coretime Pool.
#[pallet::storage]
pub type InstaPoolContribution<T> =
    StorageMap<_, Blake2_128Concat, RegionId, ContributionRecordOf<T>, OptionQuery>;
  1. Consumed Coretime Contribution (Paid Out):
    • Index the RevenueClaimPaid event, which provides detailed information that can be visualized on a graph or table:
RevenueClaimPaid {
    /// The account to whom revenue has been paid.
    who: T::AccountId,
    /// The total amount of revenue claimed and paid.
    amount: BalanceOf<T>,
    /// The next region which should be claimed for the continuation of this contribution.
    next: Option<RegionId>,
},

Storages

InstaPooIo

/// Record of Coretime entering or leaving the On-demand Coretime Pool.
#[pallet::storage]
pub type InstaPoolIo<T> = StorageMap<_, Blake2_128Concat, Timeslice, PoolIoRecord, ValueQuery>;

InstaPoolContribution

/// Record of a single contribution to the On-demand Coretime Pool.
#[pallet::storage]
pub type InstaPoolContribution<T> =
    StorageMap<_, Blake2_128Concat, RegionId, ContributionRecordOf<T>, OptionQuery>;

InstaPoolHistory

/// Total InstaPool rewards for each Timeslice and the number of core parts which contributed.
#[pallet::storage]
pub type InstaPoolHistory<T> =
    StorageMap<_, Blake2_128Concat, Timeslice, InstaPoolHistoryRecordOf<T>>;

Extrinsics

Pool

Source

/// Place a Bulk Coretime Region into the On-demand Coretime Pool.
///
/// - `origin`: Must be a Signed origin of the account which owns the Region `region_id`.
/// - `region_id`: The Region which should be assigned to the Pool.
/// - `payee`: The account which is able to collect any revenue due for the usage of this
///   Coretime.
#[pallet::call_index(11)]
pub fn pool(
    origin: OriginFor<T>,
    region_id: RegionId,
    payee: T::AccountId,
    finality: Finality,
) -> DispatchResultWithPostInfo {
    let who = ensure_signed(origin)?;
    Self::do_pool(region_id, Some(who), payee, finality)?;
    Ok(if finality == Finality::Final { Pays::No } else { Pays::Yes }.into())
}

Description

This shall have the effect of placing an item in the workplan corresponding to the region's properties and assigned to the On-demand Coretime Pool. The details of the region will be recorded in order to allow for a pro rata share of the On-demand Coretime Sales Revenue at the time of the Region relative to any other providers in the Pool.

Purchase credit

Source

/// Purchase credit for use in the On-demand Coretime Pool.
///
/// - `origin`: Must be a Signed origin able to pay at least `amount`.
/// - `amount`: The amount of credit to purchase.
/// - `beneficiary`: The account on the Relay-chain which controls the credit (generally
///   this will be the collator's hot wallet).
#[pallet::call_index(13)]
pub fn purchase_credit(
    origin: OriginFor<T>,
    amount: BalanceOf<T>,
    beneficiary: RelayAccountIdOf<T>,
) -> DispatchResult {
    let who = ensure_signed(origin)?;
    Self::do_purchase_credit(who, amount, beneficiary)?;
    Ok(())
}

Description

Any account with at least amount spendable funds may call this. This increases the On-demand Coretime Credit balance on the Relay-chain of the beneficiary by the given amount.

This Credit is consumable on the Relay-chain as part of the Task scheduling system. When consumed, revenue is recorded and provided to the Coretime-chain for proper distribution. The API for doing this is specified in RFC-5.

Events

pub enum Event<T: Config> {
    /// A Region has been added to the On-demand Coretime Pool.
    Pooled {
        /// The Region which was added to the On-demand Coretime Pool.
        region_id: RegionId,
        /// The duration of the Region.
        duration: Timeslice,
    },
    /// Some On-demand Coretime Pool credit has been purchased.
    CreditPurchased {
        /// The account which purchased the credit.
        who: T::AccountId,
        /// The Relay-chain account to which the credit will be made.
        beneficiary: RelayAccountIdOf<T>,
        /// The amount of credit purchased.
        amount: BalanceOf<T>,
    },
    /// Some historical On-demand Core Pool contribution record has been dropped.
    ContributionDropped {
        /// The Region whose contribution is no longer exists.
        region_id: RegionId,
    },
    /// Some historical On-demand Core Pool payment record has been initialized.
    HistoryInitialized {
        /// The timeslice whose history has been initialized.
        when: Timeslice,
        /// The amount of privately contributed Coretime to the On-demand Coretime Pool.
        private_pool_size: CoreMaskBitCount,
        /// The amount of Coretime contributed to the On-demand Coretime Pool by the
        /// Polkadot System.
        system_pool_size: CoreMaskBitCount,
    },
    /// Some historical On-demand Core Pool payment record has been dropped.
    HistoryDropped {
        /// The timeslice whose history is no longer available.
        when: Timeslice,
        /// The amount of revenue the system has taken.
        revenue: BalanceOf<T>,
    },
    /// Some historical On-demand Core Pool payment record has been ignored because the
    /// timeslice was already known. Governance may need to intervene.
    HistoryIgnored {
        /// The timeslice whose history is was ignored.
        when: Timeslice,
        /// The amount of revenue which was ignored.
        revenue: BalanceOf<T>,
    },
    /// Some historical On-demand Core Pool Revenue is ready for payout claims.
    ClaimsReady {
        /// The timeslice whose history is available.
        when: Timeslice,
        /// The amount of revenue the Polkadot System has already taken.
        system_payout: BalanceOf<T>,
        /// The total amount of revenue remaining to be claimed.
        private_payout: BalanceOf<T>,
    },
    /// Some historical On-demand Core Pool payment record has been dropped.
    AllowedRenewalDropped {
        /// The timeslice whose renewal is no longer available.
        when: Timeslice,
        /// The core whose workload is no longer available to be renewed for `when`.
        core: CoreIndex,
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
$$$$$ ~ 600-800 USD READ ONLY Requires research into how something should be implemented, write a thorough description. SubstrateDevs This label is great for substrate devs, who detest frontend as I was told 😂
Projects
None yet
Development

No branches or pull requests

2 participants