Skip to content

Commit f1db2c6

Browse files
antonvas0me0ne-unkn0wnseadandabkchr
authored
Coretime: Add request revenue info (#3940)
Enables the `request_revenue` and `notify_revenue` parts of [RFC 5 - Coretime Interface](https://polkadot-fellows.github.io/RFCs/approved/0005-coretime-interface.html) TODO: - [x] Finish first pass at implementation - [x] ~~Need to explicitly burn uncollected and dropped revenue~~ Accumulate it instead - [x] Confirm working on zombienet - [x] Tests - [ ] Enable XCM `request_revenue` sending on Coretime chain on Kusama and Polkadot Fixes: #2209 --------- Co-authored-by: Dmitry Sinyavin <dmitry.sinyavin@parity.io> Co-authored-by: command-bot <> Co-authored-by: s0me0ne-unkn0wn <48632512+s0me0ne-unkn0wn@users.noreply.github.com> Co-authored-by: Dónal Murray <donal.murray@parity.io> Co-authored-by: Bastian Köcher <git@kchr.de>
1 parent 4a7155e commit f1db2c6

File tree

39 files changed

+1831
-916
lines changed

39 files changed

+1831
-916
lines changed

Cargo.lock

Lines changed: 8 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cumulus/parachains/runtimes/coretime/coretime-rococo/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,6 @@ try-runtime = [
199199
"sp-runtime/try-runtime",
200200
]
201201

202-
fast-runtime = []
202+
fast-runtime = [
203+
"rococo-runtime-constants/fast-runtime",
204+
]

cumulus/parachains/runtimes/coretime/coretime-rococo/src/coretime.rs

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,65 @@ use cumulus_primitives_core::relay_chain;
2121
use frame_support::{
2222
parameter_types,
2323
traits::{
24-
fungible::{Balanced, Credit},
25-
OnUnbalanced,
24+
fungible::{Balanced, Credit, Inspect},
25+
tokens::{Fortitude, Preservation},
26+
DefensiveResult, OnUnbalanced,
2627
},
2728
};
29+
use frame_system::Pallet as System;
2830
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf};
29-
use parachains_common::{AccountId, Balance, BlockNumber};
31+
use parachains_common::{AccountId, Balance};
32+
use rococo_runtime_constants::system_parachain::coretime;
33+
use sp_runtime::traits::AccountIdConversion;
3034
use xcm::latest::prelude::*;
35+
use xcm_executor::traits::TransactAsset;
3136

32-
pub struct CreditToCollatorPot;
33-
impl OnUnbalanced<Credit<AccountId, Balances>> for CreditToCollatorPot {
34-
fn on_nonzero_unbalanced(credit: Credit<AccountId, Balances>) {
35-
let staking_pot = CollatorSelection::account_id();
36-
let _ = <Balances as Balanced<_>>::resolve(&staking_pot, credit);
37+
pub struct BurnCoretimeRevenue;
38+
impl OnUnbalanced<Credit<AccountId, Balances>> for BurnCoretimeRevenue {
39+
fn on_nonzero_unbalanced(amount: Credit<AccountId, Balances>) {
40+
let acc = RevenueAccumulationAccount::get();
41+
if !System::<Runtime>::account_exists(&acc) {
42+
System::<Runtime>::inc_providers(&acc);
43+
}
44+
Balances::resolve(&acc, amount).defensive_ok();
3745
}
3846
}
3947

48+
type AssetTransactor = <xcm_config::XcmConfig as xcm_executor::Config>::AssetTransactor;
49+
50+
fn burn_at_relay(stash: &AccountId, value: Balance) -> Result<(), XcmError> {
51+
let dest = Location::parent();
52+
let stash_location =
53+
Junction::AccountId32 { network: None, id: stash.clone().into() }.into_location();
54+
let asset = Asset { id: AssetId(Location::parent()), fun: Fungible(value) };
55+
let dummy_xcm_context = XcmContext { origin: None, message_id: [0; 32], topic: None };
56+
57+
let withdrawn = AssetTransactor::withdraw_asset(&asset, &stash_location, None)?;
58+
59+
AssetTransactor::can_check_out(&dest, &asset, &dummy_xcm_context)?;
60+
61+
let parent_assets = Into::<Assets>::into(withdrawn)
62+
.reanchored(&dest, &Here.into())
63+
.defensive_map_err(|_| XcmError::ReanchorFailed)?;
64+
65+
PolkadotXcm::send_xcm(
66+
Here,
67+
Location::parent(),
68+
Xcm(vec![
69+
Instruction::UnpaidExecution {
70+
weight_limit: WeightLimit::Unlimited,
71+
check_origin: None,
72+
},
73+
ReceiveTeleportedAsset(parent_assets.clone()),
74+
BurnAsset(parent_assets),
75+
]),
76+
)?;
77+
78+
AssetTransactor::check_out(&dest, &asset, &dummy_xcm_context);
79+
80+
Ok(())
81+
}
82+
4083
/// A type containing the encoding of the coretime pallet in the Relay chain runtime. Used to
4184
/// construct any remote calls. The codec index must correspond to the index of `Coretime` in the
4285
/// `construct_runtime` of the Relay chain.
@@ -66,11 +109,7 @@ enum CoretimeProviderCalls {
66109

67110
parameter_types! {
68111
pub const BrokerPalletId: PalletId = PalletId(*b"py/broke");
69-
}
70-
71-
parameter_types! {
72-
pub storage CoreCount: Option<CoreIndex> = None;
73-
pub storage CoretimeRevenue: Option<(BlockNumber, Balance)> = None;
112+
pub RevenueAccumulationAccount: AccountId = BrokerPalletId::get().into_sub_account_truncating(b"burnstash");
74113
}
75114

76115
/// Type that implements the `CoretimeInterface` for the allocation of Coretime. Meant to operate
@@ -205,26 +244,30 @@ impl CoretimeInterface for CoretimeAllocator {
205244
}
206245
}
207246

208-
fn check_notify_revenue_info() -> Option<(RCBlockNumberOf<Self>, Self::Balance)> {
209-
let revenue = CoretimeRevenue::get();
210-
CoretimeRevenue::set(&None);
211-
revenue
212-
}
247+
fn on_new_timeslice(_t: pallet_broker::Timeslice) {
248+
let stash = RevenueAccumulationAccount::get();
249+
let value =
250+
Balances::reducible_balance(&stash, Preservation::Expendable, Fortitude::Polite);
213251

214-
#[cfg(feature = "runtime-benchmarks")]
215-
fn ensure_notify_revenue_info(when: RCBlockNumberOf<Self>, revenue: Self::Balance) {
216-
CoretimeRevenue::set(&Some((when, revenue)));
252+
if value > 0 {
253+
log::debug!(target: "runtime::coretime", "Going to burn {value} stashed tokens at RC");
254+
match burn_at_relay(&stash, value) {
255+
Ok(()) => {
256+
log::debug!(target: "runtime::coretime", "Succesfully burnt {value} tokens");
257+
},
258+
Err(err) => {
259+
log::error!(target: "runtime::coretime", "burn_at_relay failed: {err:?}");
260+
},
261+
}
262+
}
217263
}
218264
}
219265

220266
impl pallet_broker::Config for Runtime {
221267
type RuntimeEvent = RuntimeEvent;
222268
type Currency = Balances;
223-
type OnRevenue = CreditToCollatorPot;
224-
#[cfg(feature = "fast-runtime")]
225-
type TimeslicePeriod = ConstU32<10>;
226-
#[cfg(not(feature = "fast-runtime"))]
227-
type TimeslicePeriod = ConstU32<80>;
269+
type OnRevenue = BurnCoretimeRevenue;
270+
type TimeslicePeriod = ConstU32<{ coretime::TIMESLICE_PERIOD }>;
228271
type MaxLeasedCores = ConstU32<50>;
229272
type MaxReservedCores = ConstU32<10>;
230273
type Coretime = CoretimeAllocator;

cumulus/parachains/runtimes/coretime/coretime-rococo/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub type Migrations = (
117117
cumulus_pallet_xcmp_queue::migration::v5::MigrateV4ToV5<Runtime>,
118118
pallet_broker::migration::MigrateV0ToV1<Runtime>,
119119
pallet_broker::migration::MigrateV1ToV2<Runtime>,
120+
pallet_broker::migration::MigrateV2ToV3<Runtime>,
120121
// permanent
121122
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
122123
);

0 commit comments

Comments
 (0)