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
// 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

// ===============================================================================
// ```````````````````````````` BALANCES MOCK RUNTIME ````````````````````````````
// ===============================================================================

//! Mock runtime using [`pallet_balances::Pallet`] as the primary asset adapter
//! implementing fungible traits.

#![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,
    },
};

// --- 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;

// --- 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;

// --- 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<BalancesTest>;

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

/// Base unit for balances (smallest indivisible denomination).
pub const UNIT: Asset = 1_000_000_000_000;

/// One-thousandth of a [`UNIT`] (10^-3 UNIT).
pub const MILLI_UNIT: Asset = 1_000_000_000;

/// One-millionth of a [`UNIT`] (10^-6 UNIT).
pub const MICRO_UNIT: Asset = 1_000_000;

/// Minimum balance required to keep an account alive.
///
/// Accounts with a balance below this value may be reaped.
pub const EXISTENTIAL_DEPOSIT: Asset = MILLI_UNIT;

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

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

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

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

    #[runtime::pallet_index(2)]
    pub type Balances = pallet_balances::Pallet<BalancesTest>;

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

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

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

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

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

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

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

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

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

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

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

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

impl pallet_commitment::Config for BalancesTest {
    type RuntimeEvent = RuntimeEvent;
    type Shares = u64;
    type Bias = FixedU64;
    type Asset = pallet_balances::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>;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ``````````````````````````````````` BALANCES ``````````````````````````````````
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl pallet_balances::Config for BalancesTest {
    type MaxLocks = ConstU32<50>;
    type MaxReserves = ();
    type ReserveIdentifier = [u8; 8];
    type Balance = Asset;
    type RuntimeEvent = RuntimeEvent;
    type DustRemoval = ();
    type ExistentialDeposit = ConstU64<EXISTENTIAL_DEPOSIT>;
    type AccountStore = System;
    type WeightInfo = ();
    type FreezeIdentifier = RuntimeFreezeReason;
    type MaxFreezes = VariantCountOf<RuntimeFreezeReason>;
    type RuntimeHoldReason = RuntimeHoldReason;
    type RuntimeFreezeReason = RuntimeFreezeReason;
    type DoneSlashHandler = ();
}

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

impl pallet_authors::Config for BalancesTest {
    type RuntimeEvent = RuntimeEvent;
    type CommitmentAdapter = pallet_commitment::Pallet<Self>;
    type AssetFreeze = RuntimeFreezeReason;
    type Asset = pallet_balances::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 BalancesTest {
    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 BalancesTest {
    type FullIdentification = AccountId;
    type FullIdentificationOf = ChainManager;
}

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

impl pallet_chain_manager::Config for BalancesTest {
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type RewardContext = ();
    type RewardModel = SharesPay;
    type InflationContext = MyConstantPayoutContext;
    type InflationModel = ConstantPayout;
    type RoleAdapter = Authors;
    type Asset = pallet_balances::Pallet<Self>;
    type WeightInfo = ();
    type InflateViaSupply = ConstBool<true>;
    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 BalancesTest {
    type Public = MultiSigner;
    type Signature = sp_runtime::MultiSignature;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn balances_mock_test_ext() -> sp_io::TestExternalities {
    let mut t = frame_system::GenesisConfig::<BalancesTest>::default()
        .build_storage()
        .unwrap();
    pallet_authors::GenesisConfig::<BalancesTest> {
        min_collateral: 50,
        min_fund: 25,
        max_exposure: 1000,
        min_elected: 3,
        max_elected: 6,
        ..Default::default()
    }
    .assimilate_storage(&mut t)
    .unwrap();

    t.into()
}

// ===============================================================================
// ```````````````````````````````````` TESTS ````````````````````````````````````
// ===============================================================================

#[cfg(test)]
mod tests {

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

    // --- Local crate imports ---
    use super::*;
    use crate::Internals;

    // --- FRAME Suite ---
    use frame_suite::RewardAuthors;

    // --- FRAME Support ---
    use frame_support::traits::fungible::Unbalanced;

    // ===============================================================================
    // `````````````````````````````` INFLATE VIA SUPPLY `````````````````````````````
    // ===============================================================================

    /// Validates payout derivation from total issuance when
    /// [`pallet_chain_manager::Config::InflateViaSupply`] is enabled (`true`).
    ///
    /// ## Pipeline
    ///
    /// - [`pallet_balances`]: provides issuance-backed asset  
    /// - [`pallet_commitment`]: manages holds/locks  
    /// - [`pallet_authors`]: derives author collateral and funding  
    /// - [`pallet_chain_manager`]: computes rewards via  
    ///   [`pallet_chain_manager::Config::InflationModel`]  
    ///   (uses [`frame_support::traits::fungible::Inspect::total_issuance`])
    ///
    /// ## Motivation
    ///
    /// Some assets (e.g. [`XP`](pallet_xp)) do not support issuance-based queries
    /// and may panic when total supply is accessed via their
    /// [`fungible adapter`](pallet_xp::fungible).
    ///
    /// This test ensures that with an issuance-backed asset
    /// ([`pallet_balances`]), payout correctly derives from total issuance
    /// under supply-based inflation.
    #[test]
    fn payout_uses_total_issuance_when_inflate_via_supply_enabled() {
        balances_mock_test_ext().execute_with(|| {
            Balances::set_total_issuance(100000);
            let payout = Internals::<BalancesTest>::payout_via();
            assert_eq!(payout, 100000);

            Balances::set_total_issuance(250000);
            let payout = Internals::<BalancesTest>::payout_via();
            assert_eq!(payout, 250000);
        })
    }
}