-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add broker pallet to coretime-westend
#3272
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b754d5d
Add broker pallet to coretime-westend
seadanda d5538e9
Fix typo in broker pallet
seadanda 367044a
Also fix broker typo in coretime-westend...
seadanda f77c395
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
14ca1fe
".git/.scripts/commands/bench-all/bench-all.sh" --runtime=coretime-we…
b2d1869
Add weight function that bench bot removed
seadanda 02f306a
Add prdoc
seadanda a519c32
Remove extra bootnodes and session keys
seadanda 0f0c4cf
Merge branch 'master' into donal-coretime-westend
seadanda 115071f
Fix universal_origin weights properly for coretime
seadanda 7afb516
Add coretime-westend to polkadot-parachain and fix chainspec script
seadanda f750148
Remove pallet-sudo from coretime-westend
seadanda 3046d93
Generate chainspec as at f750148
seadanda 8f83682
Change coretime log levels to debug
seadanda 8a95ed6
Merge branch 'master' into donal-coretime-westend
seadanda 70a0f4e
Rename CurrencyTransactor
seadanda dc70c4f
Use chainspecs via symlink like other parachains
seadanda a5a791b
Resolve AccountID TODO
seadanda deeef97
Refine hardcoded weights to reflect benchmarks
seadanda 7cf5eac
Change coretime pallet index for westend runtime
seadanda eb00b8c
Update westend chainspec
seadanda 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.
Large diffs are not rendered by default.
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
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
238 changes: 238 additions & 0 deletions
238
cumulus/parachains/runtimes/coretime/coretime-westend/src/coretime.rs
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,238 @@ | ||
| // Copyright 2022 Parity Technologies (UK) Ltd. | ||
| // This file is part of Cumulus. | ||
|
|
||
| // Cumulus is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Cumulus is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Cumulus. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use crate::*; | ||
| use codec::{Decode, Encode}; | ||
| use cumulus_pallet_parachain_system::RelaychainDataProvider; | ||
| use cumulus_primitives_core::relay_chain; | ||
| use frame_support::{ | ||
| parameter_types, | ||
| traits::{ | ||
| fungible::{Balanced, Credit}, | ||
| OnUnbalanced, | ||
| }, | ||
| }; | ||
| use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf}; | ||
| use parachains_common::{AccountId, Balance, BlockNumber}; | ||
| use xcm::latest::prelude::*; | ||
|
|
||
| // TODO: check AccountId import | ||
| pub struct CreditToCollatorPot; | ||
| impl OnUnbalanced<Credit<AccountId, Balances>> for CreditToCollatorPot { | ||
| fn on_nonzero_unbalanced(credit: Credit<polkadot_core_primitives::AccountId, Balances>) { | ||
| let staking_pot = CollatorSelection::account_id(); | ||
| let _ = <Balances as Balanced<_>>::resolve(&staking_pot, credit); | ||
| } | ||
| } | ||
|
|
||
| /// A type containing the encoding of the coretime pallet in the Relay chain runtime. Used to | ||
| /// construct any remote calls. The codec index must correspond to the index of `Coretime` in the | ||
| /// `construct_runtime` of the Relay chain. | ||
| #[derive(Encode, Decode)] | ||
| enum RelayRuntimePallets { | ||
| #[codec(index = 74)] | ||
seadanda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Coretime(CoretimeProviderCalls), | ||
| } | ||
|
|
||
| /// Call encoding for the calls needed from the relay coretime pallet. | ||
| #[derive(Encode, Decode)] | ||
| enum CoretimeProviderCalls { | ||
| #[codec(index = 1)] | ||
| RequestCoreCount(CoreIndex), | ||
| #[codec(index = 2)] | ||
| RequestRevenueInfoAt(relay_chain::BlockNumber), | ||
| #[codec(index = 3)] | ||
| CreditAccount(AccountId, Balance), | ||
| #[codec(index = 4)] | ||
| AssignCore( | ||
| CoreIndex, | ||
| relay_chain::BlockNumber, | ||
| Vec<(CoreAssignment, PartsOf57600)>, | ||
| Option<relay_chain::BlockNumber>, | ||
| ), | ||
| } | ||
|
|
||
| parameter_types! { | ||
| pub const BrokerPalletId: PalletId = PalletId(*b"py/broke"); | ||
| } | ||
|
|
||
| parameter_types! { | ||
| pub storage CoreCount: Option<CoreIndex> = None; | ||
| pub storage CoretimeRevenue: Option<(BlockNumber, Balance)> = None; | ||
| } | ||
|
|
||
| /// Type that implements the `CoretimeInterface` for the allocation of Coretime. Meant to operate | ||
| /// from the parachain context. That is, the parachain provides a market (broker) for the sale of | ||
| /// coretime, but assumes a `CoretimeProvider` (i.e. a Relay Chain) to actually provide cores. | ||
| pub struct CoretimeAllocator; | ||
| impl CoretimeInterface for CoretimeAllocator { | ||
| type AccountId = AccountId; | ||
| type Balance = Balance; | ||
| type RelayChainBlockNumberProvider = RelaychainDataProvider<Runtime>; | ||
|
|
||
| fn request_core_count(count: CoreIndex) { | ||
| use crate::coretime::CoretimeProviderCalls::RequestCoreCount; | ||
| let request_core_count_call = RelayRuntimePallets::Coretime(RequestCoreCount(count)); | ||
|
|
||
| let message = Xcm(vec![ | ||
| Instruction::UnpaidExecution { | ||
| weight_limit: WeightLimit::Unlimited, | ||
| check_origin: None, | ||
| }, | ||
| Instruction::Transact { | ||
| origin_kind: OriginKind::Native, | ||
| require_weight_at_most: Weight::from_parts(1000000000, 200000), | ||
| call: request_core_count_call.encode().into(), | ||
| }, | ||
| ]); | ||
|
|
||
| match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) { | ||
| Ok(_) => log::debug!( | ||
| target: "runtime::coretime", | ||
| "Request to update schedulable cores sent successfully." | ||
| ), | ||
| Err(e) => log::error!( | ||
| target: "runtime::coretime", | ||
| "Failed to send request to update schedulable cores: {:?}", | ||
| e | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| fn request_revenue_info_at(when: RCBlockNumberOf<Self>) { | ||
| use crate::coretime::CoretimeProviderCalls::RequestRevenueInfoAt; | ||
| let request_revenue_info_at_call = | ||
| RelayRuntimePallets::Coretime(RequestRevenueInfoAt(when)); | ||
|
|
||
| let message = Xcm(vec![ | ||
| Instruction::UnpaidExecution { | ||
| weight_limit: WeightLimit::Unlimited, | ||
| check_origin: None, | ||
| }, | ||
| Instruction::Transact { | ||
| origin_kind: OriginKind::Native, | ||
| require_weight_at_most: Weight::from_parts(1000000000, 200000), | ||
| call: request_revenue_info_at_call.encode().into(), | ||
| }, | ||
| ]); | ||
|
|
||
| match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) { | ||
| Ok(_) => log::debug!( | ||
| target: "runtime::coretime", | ||
| "Request for revenue information sent successfully." | ||
| ), | ||
| Err(e) => log::error!( | ||
| target: "runtime::coretime", | ||
| "Request for revenue information failed to send: {:?}", | ||
| e | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| fn credit_account(who: Self::AccountId, amount: Self::Balance) { | ||
| use crate::coretime::CoretimeProviderCalls::CreditAccount; | ||
| let credit_account_call = RelayRuntimePallets::Coretime(CreditAccount(who, amount)); | ||
|
|
||
| let message = Xcm(vec![ | ||
| Instruction::UnpaidExecution { | ||
| weight_limit: WeightLimit::Unlimited, | ||
| check_origin: None, | ||
| }, | ||
| Instruction::Transact { | ||
| origin_kind: OriginKind::Native, | ||
| require_weight_at_most: Weight::from_parts(1000000000, 200000), | ||
| call: credit_account_call.encode().into(), | ||
| }, | ||
| ]); | ||
|
|
||
| match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) { | ||
| Ok(_) => log::debug!( | ||
| target: "runtime::coretime", | ||
| "Instruction to credit account sent successfully." | ||
| ), | ||
| Err(e) => log::error!( | ||
| target: "runtime::coretime", | ||
| "Instruction to credit account failed to send: {:?}", | ||
| e | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| fn assign_core( | ||
| core: CoreIndex, | ||
| begin: RCBlockNumberOf<Self>, | ||
| assignment: Vec<(CoreAssignment, PartsOf57600)>, | ||
| end_hint: Option<RCBlockNumberOf<Self>>, | ||
| ) { | ||
| use crate::coretime::CoretimeProviderCalls::AssignCore; | ||
| let assign_core_call = | ||
| RelayRuntimePallets::Coretime(AssignCore(core, begin, assignment, end_hint)); | ||
|
|
||
| let message = Xcm(vec![ | ||
| Instruction::UnpaidExecution { | ||
| weight_limit: WeightLimit::Unlimited, | ||
| check_origin: None, | ||
| }, | ||
| Instruction::Transact { | ||
| origin_kind: OriginKind::Native, | ||
| require_weight_at_most: Weight::from_parts(1_000_000_000, 200000), | ||
| call: assign_core_call.encode().into(), | ||
| }, | ||
| ]); | ||
|
|
||
| match PolkadotXcm::send_xcm(Here, Location::parent(), message.clone()) { | ||
| Ok(_) => log::debug!( | ||
| target: "runtime::coretime", | ||
| "Core assignment sent successfully." | ||
| ), | ||
| Err(e) => log::error!( | ||
| target: "runtime::coretime", | ||
| "Core assignment failed to send: {:?}", | ||
| e | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| fn check_notify_revenue_info() -> Option<(RCBlockNumberOf<Self>, Self::Balance)> { | ||
| let revenue = CoretimeRevenue::get(); | ||
| CoretimeRevenue::set(&None); | ||
| revenue | ||
| } | ||
|
|
||
| #[cfg(feature = "runtime-benchmarks")] | ||
| fn ensure_notify_revenue_info(when: RCBlockNumberOf<Self>, revenue: Self::Balance) { | ||
| CoretimeRevenue::set(&Some((when, revenue))); | ||
| } | ||
| } | ||
|
|
||
| impl pallet_broker::Config for Runtime { | ||
| type RuntimeEvent = RuntimeEvent; | ||
| type Currency = Balances; | ||
| type OnRevenue = CreditToCollatorPot; | ||
| #[cfg(feature = "fast-runtime")] | ||
| type TimeslicePeriod = ConstU32<10>; | ||
| #[cfg(not(feature = "fast-runtime"))] | ||
| type TimeslicePeriod = ConstU32<80>; | ||
| // We don't actually need any leases at launch but set to 10 in case we want to sudo some in. | ||
| type MaxLeasedCores = ConstU32<10>; | ||
| type MaxReservedCores = ConstU32<10>; | ||
| type Coretime = CoretimeAllocator; | ||
| type ConvertBalance = sp_runtime::traits::Identity; | ||
| type WeightInfo = weights::pallet_broker::WeightInfo<Runtime>; | ||
| type PalletId = BrokerPalletId; | ||
| type AdminOrigin = EnsureRoot<AccountId>; | ||
| type PriceAdapter = pallet_broker::Linear; | ||
| } | ||
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.