@@ -21,22 +21,65 @@ use cumulus_primitives_core::relay_chain;
21
21
use frame_support:: {
22
22
parameter_types,
23
23
traits:: {
24
- fungible:: { Balanced , Credit } ,
25
- OnUnbalanced ,
24
+ fungible:: { Balanced , Credit , Inspect } ,
25
+ tokens:: { Fortitude , Preservation } ,
26
+ DefensiveResult , OnUnbalanced ,
26
27
} ,
27
28
} ;
29
+ use frame_system:: Pallet as System ;
28
30
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 ;
30
34
use xcm:: latest:: prelude:: * ;
35
+ use xcm_executor:: traits:: TransactAsset ;
31
36
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 ( ) ;
37
45
}
38
46
}
39
47
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
+
40
83
/// A type containing the encoding of the coretime pallet in the Relay chain runtime. Used to
41
84
/// construct any remote calls. The codec index must correspond to the index of `Coretime` in the
42
85
/// `construct_runtime` of the Relay chain.
@@ -66,11 +109,7 @@ enum CoretimeProviderCalls {
66
109
67
110
parameter_types ! {
68
111
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" ) ;
74
113
}
75
114
76
115
/// Type that implements the `CoretimeInterface` for the allocation of Coretime. Meant to operate
@@ -205,26 +244,30 @@ impl CoretimeInterface for CoretimeAllocator {
205
244
}
206
245
}
207
246
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 ) ;
213
251
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
+ }
217
263
}
218
264
}
219
265
220
266
impl pallet_broker:: Config for Runtime {
221
267
type RuntimeEvent = RuntimeEvent ;
222
268
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 } > ;
228
271
type MaxLeasedCores = ConstU32 < 50 > ;
229
272
type MaxReservedCores = ConstU32 < 10 > ;
230
273
type Coretime = CoretimeAllocator ;
0 commit comments