From 36c9a7fc2413256d5fd2253ada3db19f517c8d66 Mon Sep 17 00:00:00 2001 From: s0me0ne-unkn0wn <48632512+s0me0ne-unkn0wn@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:49:53 +0200 Subject: [PATCH] Kusama coretime revenue integration (#384) Based on #381 CC @ggwpez --- relay/kusama/Cargo.toml | 2 +- relay/kusama/constants/Cargo.toml | 1 + relay/kusama/constants/src/lib.rs | 2 +- .../coretime/coretime-kusama/Cargo.toml | 2 +- .../coretime/coretime-kusama/src/coretime.rs | 81 ++++++++++++++++--- .../coretime/coretime-kusama/src/lib.rs | 1 + 6 files changed, 76 insertions(+), 13 deletions(-) diff --git a/relay/kusama/Cargo.toml b/relay/kusama/Cargo.toml index b2df38038f..e3e0bffb9b 100644 --- a/relay/kusama/Cargo.toml +++ b/relay/kusama/Cargo.toml @@ -317,7 +317,7 @@ metadata-hash = ["substrate-wasm-builder?/metadata-hash"] on-chain-release-build = ["sp-api/disable-logging", "metadata-hash"] # Set timing constants (e.g. session period) to faster versions to speed up testing. -fast-runtime = [] +fast-runtime = ["kusama-runtime-constants/fast-runtime"] runtime-metrics = ["runtime-parachains/runtime-metrics", "sp-io/with-tracing"] diff --git a/relay/kusama/constants/Cargo.toml b/relay/kusama/constants/Cargo.toml index 1c8a03a016..1c27740baf 100644 --- a/relay/kusama/constants/Cargo.toml +++ b/relay/kusama/constants/Cargo.toml @@ -29,3 +29,4 @@ std = [ "sp-weights/std", "xcm-builder/std", ] +fast-runtime = [] diff --git a/relay/kusama/constants/src/lib.rs b/relay/kusama/constants/src/lib.rs index 09deca9fdf..739446be85 100644 --- a/relay/kusama/constants/src/lib.rs +++ b/relay/kusama/constants/src/lib.rs @@ -126,7 +126,7 @@ pub mod system_parachain { /// WARNING: This constant is used accross chains, so additional care should be taken /// when changing it. #[cfg(feature = "fast-runtime")] - pub const TIMESLICE_PERIOD: u32 = 10; + pub const TIMESLICE_PERIOD: u32 = 20; #[cfg(not(feature = "fast-runtime"))] pub const TIMESLICE_PERIOD: u32 = 80; } diff --git a/system-parachains/coretime/coretime-kusama/Cargo.toml b/system-parachains/coretime/coretime-kusama/Cargo.toml index 810e50bffd..78e591e1ef 100644 --- a/system-parachains/coretime/coretime-kusama/Cargo.toml +++ b/system-parachains/coretime/coretime-kusama/Cargo.toml @@ -204,7 +204,7 @@ try-runtime = [ "sp-runtime/try-runtime", ] -fast-runtime = [] +fast-runtime = ["kusama-runtime-constants/fast-runtime"] # Enable metadata hash generation at compile time for the `CheckMetadataHash` extension. metadata-hash = ["substrate-wasm-builder?/metadata-hash"] diff --git a/system-parachains/coretime/coretime-kusama/src/coretime.rs b/system-parachains/coretime/coretime-kusama/src/coretime.rs index 1afe28a080..e7c5211873 100644 --- a/system-parachains/coretime/coretime-kusama/src/coretime.rs +++ b/system-parachains/coretime/coretime-kusama/src/coretime.rs @@ -21,14 +21,18 @@ use cumulus_primitives_core::relay_chain; use frame_support::{ parameter_types, traits::{ - fungible::{Balanced, Credit}, - OnUnbalanced, + fungible::{Balanced, Credit, Inspect}, + tokens::{Fortitude, Preservation}, + DefensiveResult, OnUnbalanced, }, }; +use frame_system::Pallet as System; use kusama_runtime_constants::system_parachain::coretime; use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600, RCBlockNumberOf}; +use parachains_common::{AccountId, Balance}; use sp_runtime::traits::AccountIdConversion; use xcm::latest::prelude::*; +use xcm_executor::traits::TransactAsset; /// 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 @@ -65,13 +69,52 @@ parameter_types! { /// Burn revenue from coretime sales. See /// [RFC-010](https://polkadot-fellows.github.io/RFCs/approved/0010-burn-coretime-revenue.html). -pub struct BurnRevenue; -impl OnUnbalanced> for BurnRevenue { - fn on_nonzero_unbalanced(credit: Credit) { - let _ = >::resolve(&CoretimeBurnAccount::get(), credit); +pub struct BurnCoretimeRevenue; +impl OnUnbalanced> for BurnCoretimeRevenue { + fn on_nonzero_unbalanced(amount: Credit) { + let acc = CoretimeBurnAccount::get(); + if !System::::account_exists(&acc) { + System::::inc_providers(&acc); + } + Balances::resolve(&acc, amount).defensive_ok(); } } +type AssetTransactor = ::AssetTransactor; + +fn burn_at_relay(stash: &AccountId, value: Balance) -> Result<(), XcmError> { + let dest = Location::parent(); + let stash_location = + Junction::AccountId32 { network: None, id: stash.clone().into() }.into_location(); + let asset = Asset { id: AssetId(Location::parent()), fun: Fungible(value) }; + let dummy_xcm_context = XcmContext { origin: None, message_id: [0; 32], topic: None }; + + let withdrawn = AssetTransactor::withdraw_asset(&asset, &stash_location, None)?; + + AssetTransactor::can_check_out(&dest, &asset, &dummy_xcm_context)?; + + let parent_assets = Into::::into(withdrawn) + .reanchored(&dest, &Here.into()) + .defensive_map_err(|_| XcmError::ReanchorFailed)?; + + PolkadotXcm::send_xcm( + Here, + Location::parent(), + Xcm(vec![ + Instruction::UnpaidExecution { + weight_limit: WeightLimit::Unlimited, + check_origin: None, + }, + ReceiveTeleportedAsset(parent_assets.clone()), + BurnAsset(parent_assets), + ]), + )?; + + AssetTransactor::check_out(&dest, &asset, &dummy_xcm_context); + + Ok(()) +} + parameter_types! { /// The revenue from on-demand coretime sales. This is distributed amonst those who contributed /// regions to the pool. @@ -184,9 +227,27 @@ impl CoretimeInterface for CoretimeAllocator { } } - #[cfg(feature = "runtime-benchmarks")] - fn ensure_notify_revenue_info(when: RCBlockNumberOf, revenue: Self::Balance) { - CoretimeRevenue::set(&Some((when, revenue))); + fn on_new_timeslice(t: pallet_broker::Timeslice) { + // Burn roughly once per day. Unchecked math: RHS hardcoded as non-zero. + if t % 180 != 0 { + return + } + + let stash = CoretimeBurnAccount::get(); + let value = + Balances::reducible_balance(&stash, Preservation::Expendable, Fortitude::Polite); + + if value > 0 { + log::debug!(target: "runtime::coretime", "Going to burn {value} stashed tokens at RC"); + match burn_at_relay(&stash, value) { + Ok(()) => { + log::debug!(target: "runtime::coretime", "Succesfully burnt {value} tokens"); + }, + Err(err) => { + log::error!(target: "runtime::coretime", "burn_at_relay failed: {err:?}"); + }, + } + } } } @@ -197,7 +258,7 @@ parameter_types! { impl pallet_broker::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Currency = Balances; - type OnRevenue = BurnRevenue; + type OnRevenue = BurnCoretimeRevenue; type TimeslicePeriod = ConstU32<{ coretime::TIMESLICE_PERIOD }>; type MaxLeasedCores = ConstU32<50>; type MaxReservedCores = ConstU32<10>; diff --git a/system-parachains/coretime/coretime-kusama/src/lib.rs b/system-parachains/coretime/coretime-kusama/src/lib.rs index aefd31d6f8..e3a6e56091 100644 --- a/system-parachains/coretime/coretime-kusama/src/lib.rs +++ b/system-parachains/coretime/coretime-kusama/src/lib.rs @@ -115,6 +115,7 @@ pub type Migrations = ( cumulus_pallet_xcmp_queue::migration::v5::MigrateV4ToV5, pallet_broker::migration::MigrateV0ToV1, pallet_broker::migration::MigrateV1ToV2, + pallet_broker::migration::MigrateV2ToV3, ); /// Executive: handles dispatch to the various modules.