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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
// 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
// ===============================================================================
// ```````````````````````````` ROLE MANAGEMENT SUITE ````````````````````````````
// ===============================================================================
//! Defines a **unified abstraction layer** for managing *role-based logic*
//! within a runtime system.
//!
//! It provides **trait-based contracts** that describe the lifecycle, funding,
//! and economic behaviors of entities that assume specific roles, such as:
//!
//! - Validators and collators
//! - Governance council members
//! - Oracle operators or data feeders
//! - Curators, auditors, or relayers
//!
//! ## Overview
//!
//! Each trait in this module represents a **composable building block** for defining
//! how a role behaves in a decentralized system. These abstractions are designed to be
//! generic, interoperable, and extensible across multiple pallets or runtime modules.
//!
//! ### Currently Included Traits
//!
//! - [`RoleManager`] - Core lifecycle and collateral management for role-bearing entities.
//! - [`FundRoles`] - Extends `RoleManager` with *backing* and *funding* capabilities.
//! - [`CompensateRoles`] - Extends `RoleManager` with *reward* and *penalty* mechanics.
//! - [`RoleProbation`] - Extends `RoleManager` with **probation** and **permanence** privileges.
//!
//! ### Future Extensions
//!
//! These traits will compose together to form a **role framework** enabling complex
//! behaviors (like staking, governance, delegation, conflict resolutions, etc)
//! to be implemented consistently across different runtime modules.
//!
//! ## Design Philosophy
//!
//! - **Composability:** Each role trait defines minimal, orthogonal functionality that
//! can be combined with others to form rich behavior.
//! - **Abstraction over implementation:** These traits are *interfaces*, not storage-bound
//! logic. Concrete modules implement them.
//! - **Interoperability:** Enables higher-level systems (e.g. governance, incentives,
//! or auditing) to operate generically across role types.
//! - **Auditability:** Built-in temporal tracking (timestamps, holds, etc.) ensures
//! transparent lifecycle and accounting for each role.
//!
//! This approach allows shared governance, staking, and incentive systems to operate on
//! **generic roles** without being tightly coupled to any single pallet or implementation.
// ===============================================================================
// ``````````````````````````````````` IMPORTS ```````````````````````````````````
// ===============================================================================
// --- Local crate imports ---
use crate::;
// --- FRAME Support ---
use ;
// --- Substrate primitives ---
use ;
// ===============================================================================
// ```````````````````````````````` ROLE MANAGER `````````````````````````````````
// ===============================================================================
/// A **universal abstraction** for managing *roles* within a runtime context.
///
/// This trait defines the full lifecycle of a role-bearing entity - from enrollment
/// and collateral management to status transitions and resignation.
///
/// It is intended to be the foundational interface for any module that wishes to
/// assign and track *roles* such as:
///
/// - **Validators** - entities providing consensus participation and staking collateral.
/// - **Council Members / Governance Actors** - accounts participating in decision-making.
/// - **Oracle Operators** - off-chain data feeders bonded with collateral.
/// - **Bounty Curators / Auditors / Relayers** - specialized economic actors.
///
/// By providing a unified API for checking role existence, managing enrollment conditions,
/// and handling collateral and lifecycle events, this trait enables **role composability**
/// and modular runtime design.
///
/// ## Type Parameters
///
/// - `Candidate`: The identifier type representing an account or entity that can assume a role.
/// Typically this is an `AccountId`, but it can also be a multi-entity struct or hash ID.
///
/// ## Invariants
///
/// - Collateral associated with a `Candidate` **must remain locked** while the
/// role is active.
/// - Status transitions must be **atomic and consistent** - i.e.,
/// once [`Self::set_status`] succeeds, [`Self::get_status`] should immediately
/// reflect the new state.
///
/// ## Example Implementations
///
/// ### Example 1: Validator Role
///
/// A validator role might involve participants who are responsible for block production.
/// In this context:
/// - `Status` could include `Pending`, `Active`, `Slashed`, or `Resigned`.
/// - Candidates are required to provide a minimum collateral to enroll.
/// - The system checks whether a candidate is already enrolled before allowing enrollment.
/// - The trait implementation manages enrollment, resignation, collateral tracking, and
/// status updates.
///
/// ### Example 2: Governance Council Member
///
/// A council member role represents participants in a governance body:
/// - `Status` could include `Candidate`, `Active`, `Expelled`, or `Retired`.
/// - Enrollment may not require collateral but must still enforce eligibility rules.
/// - Resignation or removal is validated against membership in the council.
/// - The trait implementation allows tracking of active members, status changes, and
/// role lifecycle events.
///
/// These examples illustrate how the `RoleManager` trait can be applied to **different
/// kinds of roles**, demonstrating the flexibility of the trait without tying it to a
/// specific runtime storage or pallet.
///
/// ## Usage
///
/// - This trait is **not tied to a specific module/pallet**. It can be implemented by multiple
/// role-managing pallets (e.g. `pallet_validators`, `pallet_council`, etc.).
/// - Generic logic (e.g. in governance or reputation systems) can rely on `RoleManager`
/// to interact with roles abstractly without hardcoding pallet internals.
// ===============================================================================
// ````````````````````````````````` FUND ROLES ``````````````````````````````````
// ===============================================================================
/// Extends [`RoleManager`] to introduce **funding and backing mechanics**
/// for role-based systems.
///
/// This trait defines a common interface for *roles that require financial
/// support or collateralization from external entities* (called **backers**).
/// It models relationships where one or more backers provide funds to a candidate
/// in exchange for shared exposure, yield, or delegated influence.
///
/// Typical applications include:
///
/// - **Validator nomination systems** - nominators/delegators (backers) stake
/// funds behind validators (candidates).
/// - **DAO or governance roles** - community members back representatives or
/// proposal leaders.
/// - **DeFi-style credit systems** - where backers fund loan candidates, or
/// insurance underwriters provide coverage.
/// - **Service networks** - e.g., oracle or relayer pools where reputation and
/// collateral are pooled.
///
/// ## Type Parameters
/// - `Candidate`: The entity or account being backed.
///
/// ## Invariants
///
/// - Each backer must fund with at least [`Self::min_fund`] units of asset.
/// - When a `Candidate` is **not available** (See [`RoleManager::is_available`]),
/// backers may withdraw all or some of their stake (context-basis).
/// - [`Self::fund`] and [`Self::draw`] operations must maintain **consistent
/// accounting symmetry** between [`Self::backers_of`] and [`Self::backed_for`].
///
/// ## Example Implementations
///
/// ### Example 1: Nominated Validator System
///
/// In a nominated staking system:
/// - Candidates are validators who receive backing from multiple nominators (backers).
/// - Each backer must meet a minimum funding requirement when supporting a candidate.
/// - The total funding for a candidate cannot exceed a maximum exposure limit.
/// - Funding increases the candidate's active collateral, while drawing allows backers
/// to withdraw.
/// - This example shows how the trait enforces eligibility, updates backing relationships,
/// and maintains real-time funded values.
///
/// ### Example 2: Governance Role Backing
///
/// In a governance system where community members can financially support representatives:
/// - Candidates are council members or proposal leads.
/// - Backers provide funds to indicate confidence and to incentivize participation.
/// - The trait ensures backers cannot overexpose themselves or fund below minimum thresholds.
/// - Candidates may become defaulted if they violate rules or fail to perform, triggering
/// potential fund loss.
/// - Rewards, penalties, or collateral adjustments can be layered on top, creating a
/// dynamic funding ecosystem.
///
/// These examples illustrate how `FundRoles` can handle **different backing scenarios**,
/// from staking and validator support to governance funding, while maintaining **real-time
/// tracking** of funds and enforcing role-related invariants.
///
/// ## Usage
///
/// Implement this trait for any role that allows external capital participation or support.
/// Higher-level logic such as slashing, reward distribution, or governance power weighting
/// can be layered on top by combining [`CompensateRoles`] or custom traits.
// ===============================================================================
// `````````````````````````````` COMPENSATE ROLES ```````````````````````````````
// ===============================================================================
/// Extends [`RoleManager`] to introduce **reward and penalty mechanics**
/// for role-based systems.
///
/// This trait manages economic incentives and behavioral enforcement for candidates,
/// tracking rewards, penalties, and temporary holdings. Typical applications include
/// (but not constrained to):
///
/// - **Validators** - rewards for block production, penalties for missed duties.
/// - **Council members** - incentives for active participation, slashing for misconduct.
/// - **Oracle operators** - rewards for accurate reporting, penalties for stale or
/// incorrect data.
///
/// ## Concepts
///
/// - **Hold**: The total reservation of a candidate's assets, includes rewards and other
/// backings (collaterals, fundings, etc).
/// - **Reward**: An addition to a candidate's assets for performing expected duties.
/// - **Penalty**: A fractional deduction (ratio) applied to assets for misbehavior or
/// non-performance.
/// - **Forgive**: Reverses penalties, partially or fully, for rehabilitation or
/// governance action.
/// - **Reclaim**: Withdraw previously rewarded assets for redistribution or correction.
///
/// All returned values (assets, rewards, penalties) are **real-time**, reflecting the
/// candidate's current effective state.
///
/// ## Example Implementations
///
/// ### Example 1: Validator Rewards and Penalties
///
/// In a blockchain validator system:
/// - Validators earn rewards for producing blocks, finalizing chains, or performing
/// other expected duties.
/// - Misbehavior, missed blocks, or downtime results in penalties, which reduce the
/// validator's hold.
/// - Rewards and penalties are recorded with timestamps, allowing auditability and
/// historical tracking.
/// - The system may forgive temporary penalties or reclaim rewards in case of protocol
/// corrections.
/// - Real-time queries (get_rewards_of, get_penalties_of) reflect the current effective
/// state of the validator.
///
/// ### Example 2: Council Member Performance Incentives
///
/// In a governance council:
/// - Council members receive rewards for attending sessions, voting, or completing
/// assigned tasks.
/// - Failure to participate or violating rules triggers penalties, possibly reducing
/// influence or rewards.
/// - Holds represent assets reserved for potential penalties or pending rewards.
/// - Forgiveness mechanisms allow partial reversal of penalties for rehabilitation or
/// governance decisions.
/// - Reclaiming rewards may occur if a proposal is invalidated or performance is reversed.
/// - Real-time computations ensure all incentives and penalties are up-to-date when queried.
///
/// These examples illustrate how `CompensateRoles` can manage **dynamic reward and penalty
/// systems** across different roles, ensuring fairness, accountability, and flexibility in
/// role-based operations.
///
/// ## Invariants
///
/// - Total penalties cannot exceed the candidate's current hold or collateral.
/// - Implementations should track timestamps accurately to maintain auditability.
// ===============================================================================
// ``````````````````````````````` ROLE PROBATION ````````````````````````````````
// ===============================================================================
/// Extends [`RoleManager`] to introduce **probation and permanent status mechanics**
/// for role-based systems.
///
/// This trait manages the probation lifecycle of candidates, tracking their risk,
/// confirmation, and eligibility for permanent status. Typical applications include:
/// - **Employees** - probation periods before permanent employment.
/// - **Validators or council members** - temporary risk periods before confirmed full status.
/// - **Accounts or participants** - temporary monitoring before achieving full privileges.
///
/// ## Concepts
///
/// - **Probation / Risk**: Candidate is under evaluation; actions or failures
/// may have consequences.
/// - **Permanent / Confirmation**: Candidate has successfully passed evaluation;
/// fully confirmed.
/// - **Secure**: Temporarily avoids risk without confirming permanent status.
///
/// ## Probation Flow
/// - A new enrolled candidate starts in **probation** status.
/// - Upon negative performance, they risk losing permanence.
/// - Upon meeting required criteria, they are **confirmed permanent**.
/// - If a permanent candidate violates policies, permanent status may be revoked.
///
/// ## Invariants
/// - Candidates should not simultaneously be in probation and permanent status.
// ===============================================================================
// ```````````````````````````````` ROLE ACTIVITY ````````````````````````````````
// ===============================================================================
/// A lightweight abstraction for determining whether a role-bearing `Candidate` is
/// currently **idle** or **actively performing duties**.
///
/// This trait models **real-time operational activity**, independent of role
/// lifecycle, status, or eligibility. It is intended to compose with role
/// management, funding, and compensation logic.
///
/// ## Semantics
///
/// The core API uses an **inverted pattern which allows activity to be represented
/// with structured context rather than a lossy boolean.
///
/// The associated `Activity` type must represent **non-fatal engagement**
/// and be convertible into [`DispatchError`].