pallet-chain-manager 0.1.1

A session-driven orchestration pallet coordinating validator selection, participation, and settlement using offchain workers and pluggable models
Documentation
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
// SPDX-License-Identifier: MPL-2.0
//
// Part of Auguth Labs open-source softwares.
// Built for the Substrate framework.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2026 Auguth Labs (OPC) Pvt Ltd, India

// ===============================================================================
// ``````````````````````````````` XP MOCK RUNTIME ```````````````````````````````
// ===============================================================================

//! Mock runtime using [`pallet_xp::Pallet`] as the primary asset adapter
//! to assert failure of supply-based inflation.
//!
//! ## Purpose
//!
//! This module exists solely to verify that enabling
//! [`pallet_chain_manager::Config::InflateViaSupply`] with a non-issuance-based
//! asset results in a panic.
//!
//! ## Context
//!
//! The configuration mirrors the primary mock runtime, with the only difference
//! being the asset adapter:
//!
//! - [`pallet_balances::Pallet`] (originally safe): replaced by [`pallet_xp::Pallet`]
//! - [`pallet_chain_manager::Config::InflateViaSupply`] is set to `true`
//!
//! ## Rationale
//!
//! [`pallet_xp::Pallet`] does not support issuance-based queries via
//! [`frame_support::traits::fungible::Inspect::total_issuance`].
//!
//! When the reward model attempts to derive payout from total supply,
//! this leads to a panic through the fungible adapter implementation.
//!
//! ## Note
//!
//! [`pallet_xp::Pallet`] must use
//! [`pallet_chain_manager::Config::InflateViaSupply`] set to`false`,
//! as it does not support issuance-based queries. Enabling it may lead to
//! runtime panics when total supply is accessed.

#![cfg(feature = "std")]
#![allow(unused)]

// ===============================================================================
// ``````````````````````````````````` IMPORTS ```````````````````````````````````
// ===============================================================================

// --- Local crate imports ---
use crate::{
    self as pallet_chain_manager,
    crypto::AffidavitCryptoSr25519,
    mock::{
        AccountId, Asset, Commission, FairElection, ImOnlineUnsignedPriority, MaxKeys,
        MaxPeerInHeartbeats, MockFindAuthor, MockOnTimestampSet, Moment, MyBalanceContext,
        MyConstantPayoutContext, MyPenaltyThresholdContext, Offset, Period, SessionKeys, Signature,
        ALAN, ALICE, AMY, BOB, CHARLIE, DAVE, JAKE, JIM, LAYA, MIKE, NIX, PAUL,
    },
};

// --- FRAME Plugins ---
use frame_plugins::{
    balances::ShareBalanceFamily,
    elections::{fair, flat},
    influence::LinearModel,
    penalty::ThresholdPenalty,
    rewards::{payee::SharesPay, payout::ConstantPayout},
};

// --- FRAME Suite ---
use frame_suite::{Disposition, Ignore};

// --- FRAME Support ---
use frame_support::{derive_impl, pallet_prelude::*, traits::VariantCountOf};

// --- FRAME System ---
use frame_system::{
    mocking::MockUncheckedExtrinsic,
    offchain::{CreateInherent, CreateSignedTransaction, CreateTransactionBase, SigningTypes},
};

// --- External pallets ---
use pallet_session::PeriodicSessions;

use pallet_xp::types::GenesisAcc;
// --- Substrate primitives ---
use sp_core::{ConstBool, ConstU64};
use sp_runtime::{BuildStorage, FixedU64, MultiSigner};

// ===============================================================================
// ``````````````````````````````````` ALIASES ```````````````````````````````````
// ===============================================================================

/// Mock block type for runtime testing
pub type Block = frame_system::mocking::MockBlock<XpTest>;

// ===============================================================================
// `````````````````````````````````` CONSTANTS ``````````````````````````````````
// ===============================================================================

// ===============================================================================
// ``````````````````````````````````` RUNTIME ```````````````````````````````````
// ===============================================================================

#[frame_support::runtime]
pub mod runtime {
    #[runtime::runtime]
    #[runtime::derive(
        RuntimeCall,
        RuntimeEvent,
        RuntimeError,
        RuntimeOrigin,
        RuntimeFreezeReason,
        RuntimeHoldReason,
        RuntimeSlashReason,
        RuntimeLockId,
        RuntimeTask,
        RuntimeViewFunction
    )]
    pub struct XpTest;

    #[runtime::pallet_index(0)]
    pub type System = frame_system::Pallet<XpTest>;

    #[runtime::pallet_index(1)]
    pub type Commitment = pallet_commitment::Pallet<XpTest>;

    #[runtime::pallet_index(2)]
    pub type Xp = pallet_xp::Pallet<XpTest>;

    #[runtime::pallet_index(3)]
    pub type Authors = pallet_authors::Pallet<XpTest>;

    #[runtime::pallet_index(4)]
    pub type Session = pallet_session::Pallet<XpTest>;

    #[runtime::pallet_index(5)]
    pub type Historical = pallet_session::historical::Pallet<XpTest>;

    #[runtime::pallet_index(6)]
    pub type ImOnline = pallet_im_online::Pallet<XpTest>;

    #[runtime::pallet_index(7)]
    pub type Offences = pallet_offences::Pallet<XpTest>;

    #[runtime::pallet_index(8)]
    pub type Authorship = pallet_authorship::Pallet<XpTest>;

    #[runtime::pallet_index(9)]
    pub type ChainManager = pallet_chain_manager::Pallet<XpTest>;

    #[runtime::pallet_index(10)]
    pub type TimeStamp = pallet_timestamp::Pallet<XpTest>;
}

// ===============================================================================
// ``````````````````````````````````` CONFIGS ```````````````````````````````````
// ===============================================================================

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ``````````````````````````````````` SYSTEM ````````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for XpTest {
    type Block = Block;
    type AccountId = AccountId;
    type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// `````````````````````````````````` COMMITMENT `````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_commitment::Config for XpTest {
    type RuntimeEvent = RuntimeEvent;
    type Shares = u64;
    type Bias = FixedU64;
    type Asset = pallet_xp::Pallet<Self>;
    type Position = Disposition;
    type AssetHold = RuntimeHoldReason;
    type AssetFreeze = RuntimeFreezeReason;
    type MaxIndexEntries = ConstU32<3>;
    type MaxCommits = ConstU32<3>;
    type Commission = Commission;
    type Time = u32;
    type WeightInfo = ();
    type BalanceFamily<'a> = ShareBalanceFamily<'a>;
    type BalanceContext = MyBalanceContext<Commitment>;
    type EmitEvents = ConstBool<true>;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// `````````````````````````````````````` XP `````````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_xp::Config for XpTest {
    type Xp = u64;
    type Pulse = u32;
    type RuntimeEvent = RuntimeEvent;
    type RuntimeCall = RuntimeCall;
    type LockReason = RuntimeFreezeReason;
    type ReserveReason = RuntimeHoldReason;
    type Extensions = Ignore<Xp>;
    type WeightInfo = ();
    type EmitEvents = ConstBool<true>;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ``````````````````````````````````` AUTHORS ```````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_authors::Config for XpTest {
    type RuntimeEvent = RuntimeEvent;
    type CommitmentAdapter = pallet_commitment::Pallet<Self>;
    type AssetFreeze = RuntimeFreezeReason;
    type Asset = pallet_xp::Pallet<Self>;
    type Influence = u64;
    type InfluenceContext = ();
    type InfluenceModel = LinearModel;
    type FlatElectionContext = ();
    type FlatElectionModel = flat::TopDownFlatModel;
    type FairElectionContext = ();
    type FairElectionModel = fair::TopDownFairModel;
    type ActivityProvider = ChainManager;
    type WeightInfo = ();
    type EmitEvents = ConstBool<true>;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ``````````````````````````````````` SESSION ```````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_session::Config for XpTest {
    type RuntimeEvent = RuntimeEvent;
    type ValidatorId = AccountId;
    type ValidatorIdOf = ChainManager;
    type ShouldEndSession = PeriodicSessions<Period, Offset>;
    type NextSessionRotation = PeriodicSessions<Period, Offset>;
    type SessionManager = ChainManager;
    type SessionHandler = (ImOnline,);
    type Keys = SessionKeys;
    type WeightInfo = ();
    type DisablingStrategy = ();
}

impl pallet_session::historical::Config for XpTest {
    type FullIdentification = AccountId;
    type FullIdentificationOf = ChainManager;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ```````````````````````````````` CHAIN-MANAGER ````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_chain_manager::Config for XpTest {
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type RewardContext = ();
    type RewardModel = SharesPay;
    type InflationContext = MyConstantPayoutContext;
    type InflationModel = ConstantPayout;
    type RoleAdapter = Authors;
    type Asset = pallet_xp::Pallet<Self>;
    type WeightInfo = ();
    type InflateViaSupply = ConstBool<true>; // For Panic Test
    type PenaltyContext = MyPenaltyThresholdContext;
    type PenaltyModel = ThresholdPenalty;
    type NextSessionRotation = PeriodicSessions<Period, Offset>;
    type MaxAffidavitWeights = ConstU32<500>;
    type AffidavitCrypto = AffidavitCryptoSr25519;
    type ElectionAdapter = FairElection<Self>;
    type EmitEvents = ConstBool<true>;
    type Points = u64;
    type PointsAdapter = ChainManager;
    const MAX_FORKS: u32 = 10;
    const MAX_FORK_RECOVERY_TRAVERSAL: u32 = 30;
}

impl SigningTypes for XpTest {
    type Public = MultiSigner;
    type Signature = sp_runtime::MultiSignature;
}

impl CreateTransactionBase<RuntimeCall> for XpTest {
    type Extrinsic = MockUncheckedExtrinsic<XpTest>;
    type RuntimeCall = RuntimeCall;
}

impl CreateSignedTransaction<RuntimeCall> for XpTest {
    fn create_signed_transaction<
        C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>,
    >(
        call: RuntimeCall,
        _public: <Signature as sp_runtime::traits::Verify>::Signer,
        account: <XpTest as frame_system::Config>::AccountId,
        _nonce: <XpTest as frame_system::Config>::Nonce,
    ) -> Option<MockUncheckedExtrinsic<XpTest>> {
        Some(MockUncheckedExtrinsic::<XpTest>::new_signed(
            call,
            account,
            (),
            (), // SignedExtra
        ))
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ````````````````````````````````` I'M ONLINE ``````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_im_online::Config for XpTest {
    type AuthorityId = pallet_im_online::sr25519::AuthorityId;
    type RuntimeEvent = RuntimeEvent;
    type NextSessionRotation = PeriodicSessions<Period, Offset>;
    type ValidatorSet = pallet_session::historical::Pallet<XpTest>;
    type ReportUnresponsiveness = Offences;
    type UnsignedPriority = ImOnlineUnsignedPriority;
    type WeightInfo = ();
    type MaxKeys = MaxKeys;
    type MaxPeerInHeartbeats = MaxPeerInHeartbeats;
}

impl CreateTransactionBase<pallet_im_online::Call<XpTest>> for XpTest {
    type Extrinsic = MockUncheckedExtrinsic<XpTest>;
    type RuntimeCall = RuntimeCall;
}

impl CreateInherent<pallet_im_online::Call<XpTest>> for XpTest {
    fn create_inherent(call: Self::RuntimeCall) -> Self::Extrinsic {
        MockUncheckedExtrinsic::<XpTest>::new_bare(call)
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// `````````````````````````````````` OFFENCES ```````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_offences::Config for XpTest {
    type RuntimeEvent = RuntimeEvent;
    type IdentificationTuple = pallet_session::historical::IdentificationTuple<XpTest>;
    type OnOffenceHandler = ChainManager;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// `````````````````````````````````` AUTHORSHIP `````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_authorship::Config for XpTest {
    type FindAuthor = MockFindAuthor;
    type EventHandler = ImOnline;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// `````````````````````````````````` TIMESTAMP ``````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_timestamp::Config for XpTest {
    type Moment = Moment;
    type OnTimestampSet = MockOnTimestampSet;
    type MinimumPeriod = ConstU64<5>;
    type WeightInfo = ();
}

// ===============================================================================
// ````````````````````````` TEST ENVIRONMENT HELPER FNS `````````````````````````
// ===============================================================================

pub fn issuance_xp_test_ext() -> sp_io::TestExternalities {
    let mut t = frame_system::GenesisConfig::<XpTest>::default()
        .build_storage()
        .unwrap();
    pallet_xp::GenesisConfig::<XpTest> {
        genesis_acc: vec![
            GenesisAcc {
                owner: ALICE,
                id: ALICE,
            },
            GenesisAcc {
                owner: MIKE,
                id: MIKE,
            },
            GenesisAcc {
                owner: CHARLIE,
                id: CHARLIE,
            },
            GenesisAcc {
                owner: ALAN,
                id: ALAN,
            },
        ],
        ..Default::default()
    }
    .assimilate_storage(&mut t)
    .unwrap();

    pallet_authors::GenesisConfig::<XpTest> {
        min_collateral: 50,
        min_fund: 25,
        max_exposure: 1000,
        min_elected: 3,
        max_elected: 6,
        ..Default::default()
    }
    .assimilate_storage(&mut t)
    .unwrap();

    t.into()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mock::*;
    use crate::{Config, Internals as ChainInternals};
    use frame_suite::{roles::*, RewardAuthors};
    use frame_support::traits::tokens::{Fortitude, Precision};
    use pallet_authors::types::Funder;
    use sp_runtime::AccountId32;

    /// Validates guaranteed failure of payout derivation from total issuance when
    /// [`pallet_chain_manager::Config::InflateViaSupply`] is enabled (`true`).
    ///
    /// ## Pipeline
    ///
    /// - [`pallet_xp::Pallet`]: provides non-issuance-based asset  
    /// - [`pallet_commitment::Pallet`]: manages holds/locks  
    /// - [`pallet_authors::Pallet`]: derives author collateral and funding  
    /// - [`pallet_chain_manager::Pallet`]: computes rewards via  
    ///   [`pallet_chain_manager::Config::InflationModel`]  
    ///   (attempts to use [`frame_support::traits::fungible::Inspect::total_issuance`])
    ///
    /// ## Motivation
    ///
    /// [`pallet_xp::Pallet`] do not support issuance-based queries
    /// and may panic when total supply is accessed via their fungible adapter.
    ///
    /// This test-function ensures that with such an asset, enabling supply-based inflation
    /// results in a panic, rather than producing an incorrect payout.
    #[test]
    #[should_panic]
    fn payout_via_panics_when_inflate_via_supply_is_enabled_with_xp_asset() {
        issuance_xp_test_ext().execute_with(|| {
            set_user_balance_and_hold(ALICE, 250, 250).unwrap();
            set_user_balance_and_hold(CHARLIE, 250, 250).unwrap();
            set_user_balance_and_hold(ALAN, 250, 250).unwrap();
            set_user_balance_and_hold(MIKE, 250, 250).unwrap();

            RoleAdapter::enroll(&ALICE, 200, Fortitude::Force).unwrap();
            RoleAdapter::fund(
                &ALICE,
                &Funder::Direct(CHARLIE),
                100,
                Precision::Exact,
                Fortitude::Force,
            )
            .unwrap();
            RoleAdapter::fund(
                &ALICE,
                &Funder::Direct(ALAN),
                150,
                Precision::Exact,
                Fortitude::Force,
            )
            .unwrap();
            RoleAdapter::fund(
                &ALICE,
                &Funder::Direct(MIKE),
                125,
                Precision::Exact,
                Fortitude::Force,
            )
            .unwrap();

            let _ = ChainInternals::<XpTest>::payout_via();
        })
    }
}