1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use crate::{accounts_config::AccountManagerCurrencyAdapter, Hash as HashPrimitive, *};
use frame_support::{
    parameter_types,
    traits::{
        fungibles::{Balanced, Credit},
        ConstU32, ConstU8, Contains, OffchainWorker, OnFinalize, OnIdle, OnInitialize,
        OnRuntimeUpgrade,
    },
    weights::IdentityFee,
};
use pallet_asset_tx_payment::HandleCredit;
use polkadot_runtime_common::SlowAdjustingFeeUpdate;
use sp_runtime::traits::{BlakeTwo256, Zero};
parameter_types! {
    pub const MinimumPeriod: u64 = SLOT_DURATION / 2;
}

// Configure FRAME pallets to include in runtime.
impl frame_system::Config for Runtime {
    /// The data to be stored in an account.
    type AccountData = pallet_balances::AccountData<Balance>;
    /// The identifier used to distinguish between accounts.
    type AccountId = AccountId;
    /// The basic call filter to use in dispatchable.
    // type BaseCallFilter = MaintenanceMode; // todo: fix MaintananceMode compilation for v39 - XCMExecuteTransact trait unimplemented error
    type BaseCallFilter = frame_support::traits::Everything;
    /// The header type.
    type Block = Block;
    /// Maximum number of block number to block hash mappings to keep (oldest pruned first).
    type BlockHashCount = BlockHashCount;
    /// The maximum length of a block (in bytes).
    type BlockLength = circuit_runtime_types::RuntimeBlockLength;
    /// Block & extrinsics weights: base values and limits.
    type BlockWeights = circuit_runtime_types::RuntimeBlockWeights;
    /// The weight of database operations that the runtime can invoke.
    type DbWeight = RocksDbWeight;
    /// The type for hashing blocks and tries.
    type Hash = HashPrimitive;
    /// The hashing algorithm used.
    type Hashing = BlakeTwo256;
    /// The lookup mechanism to get account ID from whatever is passed in dispatchers.
    type Lookup = AccountIdLookup<AccountId, ()>;
    type MaxConsumers = frame_support::traits::ConstU32<16>;
    /// The index type for storing how many extrinsics an account has signed.
    type Nonce = u32;
    /// What to do if an account is fully reaped from the system.
    type OnKilledAccount = ();
    /// What to do if a new account is created.
    type OnNewAccount = ();
    /// The set code logic, just the default since we're not a parachain.
    type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
    /// Converts a module to the index of the module in `construct_runtime!`.
    ///
    /// This type is being generated by `construct_runtime!`.
    type PalletInfo = PalletInfo;
    /// The aggregated dispatch type that is available for extrinsics.
    type RuntimeCall = RuntimeCall;
    /// The ubiquitous event type.
    type RuntimeEvent = RuntimeEvent;
    /// The ubiquitous origin type.
    type RuntimeOrigin = RuntimeOrigin;
    /// This is used as an identifier of the chain. 42 is the generic substrate prefix.
    type SS58Prefix = SS58PrefixT1rn;
    /// Weight information for the extrinsics of this pallet.
    type SystemWeightInfo = ();
    /// Version of the runtime.
    type Version = Version;
}

impl pallet_randomness_collective_flip::Config for Runtime {}

impl pallet_timestamp::Config for Runtime {
    type MinimumPeriod = MinimumPeriod;
    /// A timestamp: milliseconds since the unix epoch.
    type Moment = u64;
    type OnTimestampSet = Aura;
    type WeightInfo = ();
}

parameter_types! {
    pub const ExistentialDeposit: u128 = 1_u128;
}

impl pallet_balances::Config for Runtime {
    type AccountStore = System;
    /// The type for recording an account's balance.
    type Balance = Balance;
    type DustRemoval = ();
    type ExistentialDeposit = ExistentialDeposit;
    type FreezeIdentifier = ();
    type MaxFreezes = ConstU32<0>;
    type MaxHolds = ConstU32<0>;
    type MaxLocks = ConstU32<50>;
    type MaxReserves = ();
    type ReserveIdentifier = [u8; 8];
    /// The ubiquitous event type.
    type RuntimeEvent = RuntimeEvent;
    type RuntimeHoldReason = RuntimeHoldReason;
    type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
    pub const TransactionByteFee: Balance = 1;
}

impl pallet_transaction_payment::Config for Runtime {
    type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
    type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
    type OnChargeTransaction = AccountManagerCurrencyAdapter<Balances, ()>;
    type OperationalFeeMultiplier = ConstU8<5>;
    type RuntimeEvent = RuntimeEvent;
    type WeightToFee = IdentityFee<Balance>;
}

/// A `HandleCredit` implementation that transfers 80% of the fees to the
/// block author and 20% to the treasury. Will drop and burn the assets
/// in case the transfer fails.
pub struct CreditToBlockAuthor;
impl HandleCredit<AccountId, Assets> for CreditToBlockAuthor {
    fn handle_credit(credit: Credit<AccountId, Assets>) {
        if let Some(author) = pallet_authorship::Pallet::<Runtime>::author() {
            let author_credit = credit
                .peek()
                .saturating_mul(80_u32.into())
                .saturating_div(<u32 as Into<Balance>>::into(100_u32));
            let (author_cut, treasury_cut) = credit.split(author_credit);
            // Drop the result which will trigger the `OnDrop` of the imbalance in case of error.
            match Assets::resolve(&author, author_cut) {
                Ok(_) => (),
                Err(_err) => {
                    log::error!("Failed to credit block author");
                },
            }
            match Assets::resolve(&Treasury::account_id(), treasury_cut) {
                Ok(_) => (),
                Err(_err) => {
                    log::error!("Failed to credit treasury");
                },
            }
        }
    }
}

impl pallet_sudo::Config for Runtime {
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = ();
}

impl pallet_utility::Config for Runtime {
    type PalletsOrigin = OriginCaller;
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = pallet_utility::weights::SubstrateWeight<Runtime>;
}

pub struct BaseCallFilter;
impl Contains<RuntimeCall> for BaseCallFilter {
    fn contains(c: &RuntimeCall) -> bool {
        match c {
            // System support
            RuntimeCall::System(_) => true,
            RuntimeCall::ParachainSystem(_) => true,
            RuntimeCall::Timestamp(_) => true,
            RuntimeCall::Preimage(_) => true,
            RuntimeCall::Scheduler(_) => true,
            RuntimeCall::Utility(_) => true,
            RuntimeCall::Identity(_) => true,
            // Monetary
            RuntimeCall::Balances(_) => true,
            RuntimeCall::Assets(_) => true,
            RuntimeCall::Treasury(_) => true,
            RuntimeCall::AccountManager(method) => matches!(
                method,
                pallet_account_manager::Call::deposit { .. }
                    | pallet_account_manager::Call::finalize { .. }
            ),
            // Collator support
            RuntimeCall::CollatorSelection(_) => true,
            RuntimeCall::Session(_) => true,
            // XCM helpers
            RuntimeCall::XcmpQueue(_) => true,
            RuntimeCall::PolkadotXcm(_) => false,
            RuntimeCall::DmpQueue(_) => true,
            // RuntimeCall::XBIPortal(_) => true,
            RuntimeCall::AssetRegistry(_) => true,
            // t3rn pallets
            RuntimeCall::XDNS(_) => true,
            RuntimeCall::ContractsRegistry(method) => matches!(
                method,
                pallet_contracts_registry::Call::add_new_contract { .. }
                    | pallet_contracts_registry::Call::purge { .. }
            ),
            RuntimeCall::Circuit(method) => matches!(
                method,
                pallet_circuit::Call::on_local_trigger { .. }
                    | pallet_circuit::Call::on_xcm_trigger { .. }
                    | pallet_circuit::Call::on_remote_gateway_trigger { .. }
                    | pallet_circuit::Call::cancel_xtx { .. }
                    | pallet_circuit::Call::revert { .. }
                    | pallet_circuit::Call::on_extrinsic_trigger { .. }
                    | pallet_circuit::Call::bid_sfx { .. }
                    | pallet_circuit::Call::confirm_side_effect { .. }
            ),
            RuntimeCall::Attesters(_) => true,
            // 3VM
            RuntimeCall::ThreeVm(_) => false,
            RuntimeCall::Contracts(method) => matches!(
                method,
                pallet_3vm_contracts::Call::call { .. }
                    | pallet_3vm_contracts::Call::instantiate_with_code { .. }
                    | pallet_3vm_contracts::Call::instantiate { .. }
                    | pallet_3vm_contracts::Call::upload_code { .. }
                    | pallet_3vm_contracts::Call::remove_code { .. }
            ),
            RuntimeCall::Evm(method) => matches!(
                method,
                pallet_3vm_evm::Call::withdraw { .. }
                    | pallet_3vm_evm::Call::call { .. }
                    | pallet_3vm_evm::Call::create { .. }
                    | pallet_3vm_evm::Call::create2 { .. } // | pallet_3vm_evm::Call::claim { .. } TODO: wheres this gone
            ),
            // Portal
            RuntimeCall::Portal(_) => true,
            _ => true,
        }
    }
}

/// Maintenance mode Call filter
///
/// For maintenance mode, we disallow everything
pub struct MaintenanceFilter;
impl Contains<RuntimeCall> for MaintenanceFilter {
    fn contains(c: &RuntimeCall) -> bool {
        match c {
            // We want to make calls to the system and scheduler pallets
            RuntimeCall::System(_) => true,
            RuntimeCall::Scheduler(_) => true,
            // Sometimes scheduler/system calls require utility calls, particularly batch
            RuntimeCall::Utility(_) => true,
            // We dont manually control these so likely we dont want to block them during maintenance mode
            RuntimeCall::Balances(_) => true,
            RuntimeCall::Assets(_) => true,
            // We wanna be able to make sudo calls in maintenance mode just incase
            RuntimeCall::Sudo(_) => true,
            RuntimeCall::ParachainSystem(_) => true,
            RuntimeCall::Timestamp(_) => true,
            RuntimeCall::Session(_) => true,
            RuntimeCall::RococoBridge(_) => true,
            RuntimeCall::KusamaBridge(_) => true,
            RuntimeCall::PolkadotBridge(_) => true,
            RuntimeCall::EthereumBridge(_) => true,
            RuntimeCall::SepoliaBridge(_) => true,
            #[allow(unreachable_patterns)] // We need this as an accidental catchall
            _ => false,
        }
    }
}

/// Hooks to run when in Maintenance Mode
pub struct MaintenanceHooks;

impl OnInitialize<BlockNumber> for MaintenanceHooks {
    fn on_initialize(n: BlockNumber) -> frame_support::weights::Weight {
        AllPalletsWithSystem::on_initialize(n)
    }
}

/// Only two pallets use `on_idle`: xcmp and dmp queues.
/// Empty on_idle, in case we want the pallets to execute it, should be provided here.
impl OnIdle<BlockNumber> for MaintenanceHooks {
    fn on_idle(_n: BlockNumber, _max_weight: Weight) -> Weight {
        Weight::zero()
    }
}

impl OnRuntimeUpgrade for MaintenanceHooks {
    fn on_runtime_upgrade() -> Weight {
        AllPalletsWithSystem::on_runtime_upgrade()
    }

    #[cfg(feature = "try-runtime")]
    fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
        AllPalletsWithSystem::pre_upgrade()
    }

    #[cfg(feature = "try-runtime")]
    fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
        AllPalletsWithSystem::post_upgrade()
    }
}

impl OnFinalize<BlockNumber> for MaintenanceHooks {
    fn on_finalize(n: BlockNumber) {
        AllPalletsWithSystem::on_finalize(n)
    }
}

impl OffchainWorker<BlockNumber> for MaintenanceHooks {
    fn offchain_worker(n: BlockNumber) {
        AllPalletsWithSystem::offchain_worker(n)
    }
}

impl pallet_maintenance_mode::Config for Runtime {
    type MaintenanceCallFilter = MaintenanceFilter;
    type MaintenanceExecutiveHooks = AllPalletsWithSystem;
    type MaintenanceOrigin = EnsureRoot<AccountId>;
    type NormalCallFilter = BaseCallFilter;
    type NormalExecutiveHooks = AllPalletsWithSystem;
    type RuntimeEvent = RuntimeEvent;
}

#[cfg(test)]
mod tests {
    use super::*;
    use codec::Compact;
    use pallet_circuit::Outcome;
    use sp_runtime::{AccountId32, MultiAddress};
    use t3rn_primitives::claimable::BenefitSource;

    /// Test that the calls that are allowed in base mode can be called
    #[test]
    fn base_filter_works_with_allowed_and_disallowed_calls() {
        // System support
        let call = frame_system::Call::remark { remark: vec![] }.into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_timestamp::Call::set { now: 0 }.into();
        assert!(BaseCallFilter::contains(&call));

        let prev_call = pallet_identity::Call::add_registrar {
            account: MultiAddress::Address32([0; 32]),
        }
        .into();
        assert!(BaseCallFilter::contains(&prev_call));

        // Monetary
        let call = pallet_account_manager::Call::finalize {
            charge_id: Default::default(),
            outcome: Outcome::Commit,
            maybe_recipient: Default::default(),
            maybe_actual_fees: Default::default(),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_xdns::Call::purge_gateway_record {
            requester: AccountId32::new([0; 32]),
            gateway_id: Default::default(),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_contracts_registry::Call::purge {
            requester: AccountId32::new([0; 32]),
            contract_id: Default::default(),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_circuit::Call::bid_sfx {
            sfx_id: Default::default(),
            bid_amount: 0,
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_balances::Call::transfer {
            dest: MultiAddress::Address32([0; 32]),
            value: 0,
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_assets::Call::create {
            id: Default::default(),
            admin: MultiAddress::Address32([0; 32]),
            min_balance: 0,
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_treasury::Call::<Runtime, ()>::propose_spend {
            value: 0,
            beneficiary: MultiAddress::Address32([0; 32]),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        // 3VM
        let call = pallet_3vm_evm::Call::withdraw {
            address: Default::default(),
            value: 0,
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_3vm_contracts::Call::call {
            dest: MultiAddress::Address32([0; 32]),
            data: vec![],
            gas_limit: 0.into(),
            value: 0,
            storage_deposit_limit: Some(Compact(0)),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        // Admin
        let call = pallet_sudo::Call::sudo {
            call: Box::new(frame_system::Call::remark { remark: vec![] }.into()),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        let call = pallet_portal::Call::register_gateway {
            gateway_id: Default::default(),
            token_id: Default::default(),
            verification_vendor: Default::default(),
            execution_vendor: Default::default(),
            codec: Default::default(),
            registrant: Default::default(),
            escrow_account: Default::default(),
            allowed_side_effects: Default::default(),
            token_props: Default::default(),
            encoded_registration_data: Default::default(),
        }
        .into();
        assert!(BaseCallFilter::contains(&call));

        // Anything else
        // assert!(!BaseCallFilter::contains(_));
    }

    /// Test that the calls that are not allowed in maintenance mode cannot be called
    #[test]
    fn maintenance_filter_works_with_allowed_and_disallowed_calls() {
        let call = frame_system::Call::remark { remark: vec![] }.into();
        assert!(MaintenanceFilter::contains(&call));

        let call = pallet_utility::Call::as_derivative {
            index: 0,
            call: Box::new(call),
        }
        .into();
        assert!(MaintenanceFilter::contains(&call));

        let call = pallet_balances::Call::transfer {
            dest: MultiAddress::Address32([0; 32]),
            value: 0,
        }
        .into();
        assert!(MaintenanceFilter::contains(&call));

        let call = pallet_assets::Call::create {
            id: Default::default(),
            admin: MultiAddress::Address32([0; 32]),
            min_balance: 0,
        }
        .into();
        assert!(MaintenanceFilter::contains(&call));

        let call = pallet_timestamp::Call::set { now: 0 }.into();
        assert!(MaintenanceFilter::contains(&call));

        let call = pallet_sudo::Call::sudo {
            call: Box::new(frame_system::Call::remark { remark: vec![] }.into()),
        }
        .into();
        assert!(MaintenanceFilter::contains(&call));

        let call = pallet_3vm_evm::Call::withdraw {
            address: Default::default(),
            value: 0,
        }
        .into();
        assert!(!MaintenanceFilter::contains(&call));

        let call = pallet_identity::Call::add_registrar {
            account: MultiAddress::Address32([0; 32]),
        }
        .into();
        assert!(!MaintenanceFilter::contains(&call));

        let call = pallet_treasury::Call::<Runtime, ()>::reject_proposal { proposal_id: 0 }.into();
        assert!(!MaintenanceFilter::contains(&call));

        let call = pallet_account_manager::Call::deposit {
            charge_id: Default::default(),
            payee: AccountId32::new([0; 32]),
            charge_fee: 0,
            offered_reward: 0,
            source: BenefitSource::BootstrapPool,
            role: pallet_circuit::CircuitRole::Relayer,
            recipient: Default::default(),
            maybe_asset_id: Default::default(),
        }
        .into();
        assert!(!MaintenanceFilter::contains(&call));

        let call = pallet_xdns::Call::purge_gateway_record {
            requester: AccountId32::new([0; 32]),
            gateway_id: Default::default(),
        }
        .into();
        assert!(!MaintenanceFilter::contains(&call));

        let call = pallet_contracts_registry::Call::purge {
            requester: AccountId32::new([0; 32]),
            contract_id: Default::default(),
        }
        .into();
        assert!(!MaintenanceFilter::contains(&call));

        let call = pallet_circuit::Call::bid_sfx {
            sfx_id: Default::default(),
            bid_amount: 0,
        }
        .into();
        assert!(!MaintenanceFilter::contains(&call));

        // Anything else
        // assert!(!MaintenanceFilter::contains(_));
    }
}