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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
//! # OCIF Staking pallet
//! A pallet for allowing INV-Cores to be staked towards.
//!
//! ## Overview
//!
//! This pallet provides functionality to allow 2 sets of entities to participate in distribution of tokens
//! available in a predefined pot account.
//! The tokens provided to the pot account are to be handled by the Runtime,
//! either directly or with the assistance of another pallet.
//!
//! The 2 entity sets will be referred to in code as Cores and Stakers:
//!
//! ### Cores
//! Cores are virtual accounts that have an ID used to derive their own account address,
//! their task in the process is to register themselves and have Stakers lock tokens in favor of a specifc Core.
//! Cores receive their fraction of the pot rewards based on the total amount staked towards them by Stakers,
//! however, a Core must have total stake above the defined threshold (making it `active`), otherwise they won't be entitled to rewards.
//!
//! ### Stakers
//! Stakers are any account existing on the chain, their task is to lock tokens in favor of a Core.
//! Unlike Cores, Stakers get their fraction of the rewards based on their own stake and regardless of
//! the `active` state of the Core they staked towards.
//!
//! ## Relevant runtime configs
//!
//! * `BlocksPerEra` - Defines how many blocks constitute an era.
//! * `RegisterDeposit` - Defines the deposit amount for a Core to register in the system.
//! * `MaxStakersPerCore` - Defines the maximum amount of Stakers allowed staking simultaneously towards the same Core.
//! * `MinimumStakingAmount` - Defines the minimum amount a Staker has to stake to participate.
//! * `UnbondingPeriod` - Defines the period, in eras, that it takes to unbond a stake.
//! * `RewardRatio` - Defines the ratio of balance from the pot to distribute to Cores and Stakers, respectively.
//! * `StakeThresholdForActiveCore` - Defines the threshold of stake a Core needs to surpass to become active.
//!
//! **Example Runtime implementation can be found in [src/testing/mock.rs](./src/testing/mock.rs)**
//!
//! ## Dispatchable Functions
//!
//! * `register_core` - Registers a Core in the system.
//! * `unregister_core` - Unregisters a Core from the system, starting the unbonding period for the Stakers.
//! * `change_core_metadata` - Changes the metadata tied to a Core.
//! * `stake` - Stakes tokens towards a Core.
//! * `unstake` - Unstakes tokens from a core and starts the unbonding period for those tokens.
//! * `withdraw_unstaked` - Withdraws tokens that have already been through the unbonding period.
//! * `staker_claim_rewards` - Claims rewards available for a Staker.
//! * `core_claim_rewards` - Claims rewards available for a Core.
//! * `halt_unhalt_pallet` - Allows Root to trigger a halt of the system, eras will stop counting and rewards won't be distributed.
//!
//! [`Call`]: ./enum.Call.html
//! [`Config`]: ./trait.Config.html

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
    dispatch::{Pays, PostDispatchInfo},
    ensure,
    pallet_prelude::*,
    traits::{
        Currency, ExistenceRequirement, Get, HandleMessage, Imbalance, LockIdentifier,
        LockableCurrency, OnUnbalanced, ProcessMessage, QueuePausedQuery, ReservableCurrency,
        WithdrawReasons,
    },
    weights::{Weight, WeightToFee},
    BoundedSlice, PalletId,
};
use frame_system::{ensure_signed, pallet_prelude::*};
use sp_runtime::{
    traits::{AccountIdConversion, Saturating, Zero},
    Perbill,
};
use sp_std::{
    convert::{From, TryInto},
    vec::Vec,
};

pub mod primitives;
use primitives::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod testing;
pub mod weights;

pub use weights::WeightInfo;

/// Staking lock identifier.
const LOCK_ID: LockIdentifier = *b"ocif-stk";

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
    use pallet_inv4::{
        origin::{ensure_multisig, INV4Origin},
        CoreAccountDerivation,
    };

    use pallet_message_queue::{self};

    use super::*;

    /// The balance type of this pallet.
    pub type BalanceOf<T> =
        <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

    #[pallet::pallet]
    pub struct Pallet<T>(PhantomData<T>);

    /// The opaque token type for an imbalance. This is returned by unbalanced operations and must be dealt with.
    type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
        <T as frame_system::Config>::AccountId,
    >>::NegativeImbalance;

    /// The core metadata type of this pallet.
    pub type CoreMetadataOf<T> = CoreMetadata<
        BoundedVec<u8, <T as Config>::MaxNameLength>,
        BoundedVec<u8, <T as Config>::MaxDescriptionLength>,
        BoundedVec<u8, <T as Config>::MaxImageUrlLength>,
    >;

    /// The core information type, containing a core's AccountId and CoreMetadataOf.
    pub type CoreInfoOf<T> = CoreInfo<<T as frame_system::Config>::AccountId, CoreMetadataOf<T>>;

    /// Alias type for the era identifier type.
    pub type Era = u32;

    #[pallet::config]
    pub trait Config:
        frame_system::Config + pallet_inv4::Config + pallet_message_queue::Config
    {
        /// The overarching event type.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// The currency used in staking.
        type Currency: LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>
            + ReservableCurrency<Self::AccountId>;

        // type CoreId: Parameter
        //     + Member
        //     + AtLeast32BitUnsigned
        //     + Default
        //     + Copy
        //     + Display
        //     + MaxEncodedLen
        //     + Clone
        //     + From<<Self as pallet_inv4::Config>::CoreId>;

        /// Number of blocks per era.
        #[pallet::constant]
        type BlocksPerEra: Get<BlockNumberFor<Self>>;

        /// Deposit amount that will be reserved as part of new core registration.
        #[pallet::constant]
        type RegisterDeposit: Get<BalanceOf<Self>>;

        /// Maximum number of unique stakers per core.
        #[pallet::constant]
        type MaxStakersPerCore: Get<u32>;

        /// Minimum amount user must have staked on a core.
        /// User can stake less if they already have the minimum staking amount staked.
        #[pallet::constant]
        type MinimumStakingAmount: Get<BalanceOf<Self>>;

        /// Account Identifier from which the internal Pot is generated.
        #[pallet::constant]
        type PotId: Get<PalletId>;

        /// The minimum amount required to keep an account open.
        #[pallet::constant]
        type ExistentialDeposit: Get<BalanceOf<Self>>;

        /// Max number of unlocking chunks per account Id <-> core Id pairing.
        /// If value is zero, unlocking becomes impossible.
        #[pallet::constant]
        type MaxUnlocking: Get<u32>;

        /// Number of eras that need to pass until unstaked value can be withdrawn.
        /// When set to `0`, it's equal to having no unbonding period.
        #[pallet::constant]
        type UnbondingPeriod: Get<u32>;

        /// Max number of unique `EraStake` values that can exist for a `(staker, core)` pairing.
        ///
        /// When stakers claims rewards, they will either keep the number of `EraStake` values the same or they will reduce them by one.
        /// Stakers cannot add an additional `EraStake` value by calling `bond&stake` or `unbond&unstake` if they've reached the max number of values.
        ///
        /// This ensures that history doesn't grow indefinitely - if there are too many chunks, stakers should first claim their former rewards
        /// before adding additional `EraStake` values.
        #[pallet::constant]
        type MaxEraStakeValues: Get<u32>;

        /// Reward ratio of the pot to be distributed between the core and stakers, respectively.
        #[pallet::constant]
        type RewardRatio: Get<(u32, u32)>;

        /// Threshold of staked tokens necessary for a core to become active.
        #[pallet::constant]
        type StakeThresholdForActiveCore: Get<BalanceOf<Self>>;

        /// Maximum length of a core's name.
        #[pallet::constant]
        type MaxNameLength: Get<u32>;

        /// Maximum length of a core's description.
        #[pallet::constant]
        type MaxDescriptionLength: Get<u32>;

        /// Maximum length of a core's image URL.
        #[pallet::constant]
        type MaxImageUrlLength: Get<u32>;

        /// Weight information for extrinsics in this pallet.
        type WeightInfo: WeightInfo;

        /// Message queue interface.
        type StakingMessage: HandleMessage;

        /// Weight to fee conversion provider, from pallet_transaction_payment.
        type WeightToFee: WeightToFee<Balance = BalanceOf<Self>>;

        /// Fee charghing interface.
        type OnUnbalanced: OnUnbalanced<NegativeImbalanceOf<Self>>;
    }

    /// General information about the staker.
    #[pallet::storage]
    #[pallet::getter(fn ledger)]
    pub type Ledger<T: Config> =
        StorageMap<_, Blake2_128Concat, T::AccountId, AccountLedger<BalanceOf<T>>, ValueQuery>;

    /// The current era index.
    #[pallet::storage]
    #[pallet::getter(fn current_era)]
    pub type CurrentEra<T> = StorageValue<_, Era, ValueQuery>;

    /// Accumulator for block rewards during an era. It is reset at every new era.
    #[pallet::storage]
    #[pallet::getter(fn reward_accumulator)]
    pub type RewardAccumulator<T> = StorageValue<_, RewardInfo<BalanceOf<T>>, ValueQuery>;

    /// Stores the block number of when the next era starts.
    #[pallet::storage]
    #[pallet::getter(fn next_era_starting_block)]
    pub type NextEraStartingBlock<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;

    /// Simple map where CoreId points to the respective core information.
    #[pallet::storage]
    #[pallet::getter(fn core_info)]
    pub(crate) type RegisteredCore<T: Config> =
        StorageMap<_, Blake2_128Concat, T::CoreId, CoreInfoOf<T>>;

    /// General information about an era.
    #[pallet::storage]
    #[pallet::getter(fn general_era_info)]
    pub type GeneralEraInfo<T: Config> = StorageMap<_, Twox64Concat, Era, EraInfo<BalanceOf<T>>>;

    /// Staking information about a core in a particular era.
    #[pallet::storage]
    #[pallet::getter(fn core_stake_info)]
    pub type CoreEraStake<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        T::CoreId,
        Twox64Concat,
        Era,
        CoreStakeInfo<BalanceOf<T>>,
    >;

    /// Info about staker's stakes on a particular core.
    #[pallet::storage]
    #[pallet::getter(fn staker_info)]
    pub type GeneralStakerInfo<T: Config> = StorageDoubleMap<
        _,
        Blake2_128Concat,
        T::CoreId,
        Blake2_128Concat,
        T::AccountId,
        StakerInfo<BalanceOf<T>>,
        ValueQuery,
    >;

    /// Denotes whether the pallet is halted (disabled).
    #[pallet::storage]
    #[pallet::getter(fn is_halted)]
    pub type Halted<T: Config> = StorageValue<_, bool, ValueQuery>;

    /// Placeholder for the core being unregistered and its stake info.
    #[pallet::storage]
    #[pallet::getter(fn core_unregistering_staker_info)]
    pub type UnregisteredCoreStakeInfo<T: Config> =
        StorageMap<_, Blake2_128Concat, T::CoreId, CoreStakeInfo<BalanceOf<T>>, OptionQuery>;

    /// Placeholder for the core being unregistered and its stakers.
    #[pallet::storage]
    #[pallet::getter(fn core_unregistering_staker_list)]
    pub type UnregisteredCoreStakers<T: Config> = StorageMap<
        _,
        Blake2_128Concat,
        T::CoreId,
        BoundedVec<T::AccountId, T::MaxStakersPerCore>,
        OptionQuery,
    >;

    #[pallet::event]
    #[pallet::generate_deposit(pub(crate) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Account has staked funds to a core.
        Staked {
            staker: T::AccountId,
            core: T::CoreId,
            amount: BalanceOf<T>,
        },

        /// Account has unstaked funds from a core.
        Unstaked {
            staker: T::AccountId,
            core: T::CoreId,
            amount: BalanceOf<T>,
        },

        /// Account has withdrawn unbonded funds.
        Withdrawn {
            staker: T::AccountId,
            amount: BalanceOf<T>,
        },

        /// New core registered for staking.
        CoreRegistered { core: T::CoreId },

        /// Core unregistered.
        CoreUnregistered { core: T::CoreId },

        /// Beginning of a new era.
        NewEra { era: u32 },

        /// Staker claimed rewards.
        StakerClaimed {
            staker: T::AccountId,
            core: T::CoreId,
            era: u32,
            amount: BalanceOf<T>,
        },

        /// Rewards claimed for core.
        CoreClaimed {
            core: T::CoreId,
            destination_account: T::AccountId,
            era: u32,
            amount: BalanceOf<T>,
        },

        /// Halt status changed.
        HaltChanged { is_halted: bool },

        /// Core metadata changed.
        MetadataChanged {
            core: T::CoreId,
            old_metadata: CoreMetadata<Vec<u8>, Vec<u8>, Vec<u8>>,
            new_metadata: CoreMetadata<Vec<u8>, Vec<u8>, Vec<u8>>,
        },

        /// Staker moved an amount of stake to another core.
        StakeMoved {
            staker: T::AccountId,
            from_core: T::CoreId,
            to_core: T::CoreId,
            amount: BalanceOf<T>,
        },
        /// Core is being unregistered.
        CoreUnregistrationQueueStarted { core: T::CoreId },
        /// Core ungregistration chunk was processed.
        CoreUnregistrationChunksProcessed {
            core: T::CoreId,
            accounts_processed_in_this_chunk: u64,
            accounts_left: u64,
        },
        /// Sharded execution of the core unregistration process finished.
        CoreUnregistrationQueueFinished { core: T::CoreId },
    }

    #[pallet::error]
    pub enum Error<T> {
        /// Staking nothing.
        StakingNothing,
        /// Attempted to stake less than the minimum amount.
        InsufficientBalance,
        /// Maximum number of stakers reached.
        MaxStakersReached,
        /// Core not found.
        CoreNotFound,
        /// No stake available for withdrawal.
        NoStakeAvailable,
        /// Core is not unregistered.
        NotUnregisteredCore,
        /// Unclaimed rewards available.
        UnclaimedRewardsAvailable,
        /// Unstaking nothing.
        UnstakingNothing,
        /// Nothing available for withdrawal.
        NothingToWithdraw,
        /// Core already registered.
        CoreAlreadyRegistered,
        /// Unknown rewards for era.
        UnknownEraReward,
        /// Unexpected stake info for era.
        UnexpectedStakeInfoEra,
        /// Too many unlocking chunks.
        TooManyUnlockingChunks,
        /// Reward already claimed.
        RewardAlreadyClaimed,
        /// Incorrect era.
        IncorrectEra,
        /// Too many era stake values.
        TooManyEraStakeValues,
        /// Not a staker.
        NotAStaker,
        /// No permission.
        NoPermission,
        /// Name exceeds maximum length.
        MaxNameExceeded,
        /// Description exceeds maximum length.
        MaxDescriptionExceeded,
        /// Image URL exceeds maximum length.
        MaxImageExceeded,
        /// Core not registered.
        NotRegistered,
        /// Halted.
        Halted,
        /// No halt change.
        NoHaltChange,
        /// Attempted to move stake to the same core.
        MoveStakeToSameCore,
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        fn on_initialize(now: BlockNumberFor<T>) -> Weight {
            // If the pallet is halted we don't process a new era.
            if Self::is_halted() {
                return T::DbWeight::get().reads(1);
            }

            let previous_era = Self::current_era();
            let next_era_starting_block = Self::next_era_starting_block();

            // Process a new era if past the start block of next era or if this is the first ever era.
            if now >= next_era_starting_block || previous_era.is_zero() {
                let blocks_per_era = T::BlocksPerEra::get();
                let next_era = previous_era + 1;
                CurrentEra::<T>::put(next_era);

                NextEraStartingBlock::<T>::put(now + blocks_per_era);

                let reward = RewardAccumulator::<T>::take();
                let (consumed_weight, new_active_stake) = Self::rotate_staking_info(previous_era);
                Self::reward_balance_snapshot(previous_era, reward, new_active_stake);

                Self::deposit_event(Event::<T>::NewEra { era: next_era });

                consumed_weight + T::DbWeight::get().reads_writes(5, 3)
            } else {
                T::DbWeight::get().reads(3)
            }
        }
    }

    #[pallet::call]
    impl<T: Config> Pallet<T>
    where
        Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
            From<<T as frame_system::Config>::RuntimeOrigin>,
        T::AccountId: From<[u8; 32]>,
    {
        /// Used to register core for staking.
        ///
        /// The origin has to be the core origin.
        ///
        /// As part of this call, `RegisterDeposit` will be reserved from the core account.
        ///
        /// - `name`: Name of the core.
        /// - `description`: Description of the core.
        /// - `image`: Image URL of the core.
        #[pallet::call_index(0)]
        #[pallet::weight(
            <T as Config>::WeightInfo::register_core(
                name.len() as u32,
                description.len() as u32,
                image.len() as u32
            )
        )]
        pub fn register_core(
            origin: OriginFor<T>,
            name: BoundedVec<u8, T::MaxNameLength>,
            description: BoundedVec<u8, T::MaxDescriptionLength>,
            image: BoundedVec<u8, T::MaxImageUrlLength>,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let core = ensure_multisig::<T, OriginFor<T>>(origin)?;
            let core_account = core.to_account_id();
            let core_id = core.id;

            ensure!(
                !RegisteredCore::<T>::contains_key(core_id),
                Error::<T>::CoreAlreadyRegistered,
            );

            let metadata: CoreMetadataOf<T> = CoreMetadata {
                name,
                description,
                image,
            };

            <T as pallet::Config>::Currency::reserve(&core_account, T::RegisterDeposit::get())?;

            RegisteredCore::<T>::insert(
                core_id,
                CoreInfo {
                    account: core_account,
                    metadata,
                },
            );

            Self::deposit_event(Event::<T>::CoreRegistered { core: core_id });

            Ok(PostDispatchInfo {
                actual_weight: None,
                pays_fee: Pays::No,
            })
        }

        /// Unregister existing core for staking, making it ineligible for rewards from current era onwards and
        /// starts the unbonding period for the stakers.
        ///
        /// The origin has to be the core origin.
        ///
        /// Deposit is returned to the core account.
        ///
        /// - `core_id`: Id of the core to be unregistered.
        #[pallet::call_index(1)]
        #[pallet::weight(
            <T as Config>::WeightInfo::unregister_core()
        )]
        pub fn unregister_core(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let core = ensure_multisig::<T, OriginFor<T>>(origin.clone())?;
            let core_account = core.to_account_id();
            let core_id = core.id;

            ensure!(
                RegisteredCore::<T>::get(core_id).is_some(),
                Error::<T>::NotRegistered
            );

            let current_era = Self::current_era();

            let all_stakers: BoundedVec<T::AccountId, T::MaxStakersPerCore> =
                BoundedVec::truncate_from(
                    GeneralStakerInfo::<T>::iter_key_prefix(core_id).collect::<Vec<T::AccountId>>(),
                );

            let all_fee = <T as Config>::WeightToFee::weight_to_fee(
                &(all_stakers.len() as u32 * <T as Config>::WeightInfo::unstake()),
            );

            UnregisteredCoreStakers::<T>::insert(core_id, all_stakers);

            let mut core_stake_info =
                Self::core_stake_info(core_id, current_era).unwrap_or_default();
            UnregisteredCoreStakeInfo::<T>::insert(core_id, core_stake_info.clone());
            GeneralEraInfo::<T>::mutate(current_era, |value| {
                if let Some(x) = value {
                    x.staked = x.staked.saturating_sub(core_stake_info.total);
                }
            });
            core_stake_info.total = Zero::zero();
            CoreEraStake::<T>::insert(core_id, current_era, core_stake_info.clone());

            let reserve_deposit = T::RegisterDeposit::get();
            <T as Config>::Currency::unreserve(&core_account, reserve_deposit);

            T::OnUnbalanced::on_unbalanced(<T as Config>::Currency::withdraw(
                &core_account,
                reserve_deposit.min(all_fee),
                WithdrawReasons::TRANSACTION_PAYMENT,
                ExistenceRequirement::KeepAlive,
            )?);

            RegisteredCore::<T>::remove(core_id);

            let total_stakers = core_stake_info.number_of_stakers;

            let message = primitives::UnregisterMessage::<T> {
                core_id,
                era: current_era,
                stakers_to_unstake: total_stakers,
            }
            .encode();

            T::StakingMessage::handle_message(BoundedSlice::truncate_from(message.as_slice()));

            Self::deposit_event(Event::<T>::CoreUnregistrationQueueStarted { core: core_id });

            Self::deposit_event(Event::<T>::CoreUnregistered { core: core_id });

            Ok(Some(<T as Config>::WeightInfo::unregister_core()).into())
        }

        /// Used to change the metadata of a core.
        ///
        /// The origin has to be the core origin.
        ///
        /// - `name`: Name of the core.
        /// - `description`: Description of the core.
        /// - `image`: Image URL of the core.
        #[pallet::call_index(2)]
        #[pallet::weight(
            <T as Config>::WeightInfo::change_core_metadata(
                name.len() as u32,
                description.len() as u32,
                image.len() as u32
            )
        )]
        pub fn change_core_metadata(
            origin: OriginFor<T>,
            name: BoundedVec<u8, T::MaxNameLength>,
            description: BoundedVec<u8, T::MaxDescriptionLength>,
            image: BoundedVec<u8, T::MaxImageUrlLength>,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let core_origin = ensure_multisig::<T, OriginFor<T>>(origin)?;
            let core_id = core_origin.id;

            RegisteredCore::<T>::try_mutate(core_id, |core| {
                let mut new_core = core.take().ok_or(Error::<T>::NotRegistered)?;

                let new_metadata: CoreMetadataOf<T> = CoreMetadata {
                    name: name.clone(),
                    description: description.clone(),
                    image: image.clone(),
                };

                let old_metadata = new_core.metadata;

                new_core.metadata = new_metadata;

                *core = Some(new_core);

                Self::deposit_event(Event::<T>::MetadataChanged {
                    core: core_id,
                    old_metadata: CoreMetadata {
                        name: old_metadata.name.into_inner(),
                        description: old_metadata.description.into_inner(),
                        image: old_metadata.image.into_inner(),
                    },
                    new_metadata: CoreMetadata {
                        name: name.to_vec(),
                        description: description.to_vec(),
                        image: image.to_vec(),
                    },
                });

                Ok(().into())
            })
        }

        /// Lock up and stake balance of the origin account.
        ///
        /// `value` must be more than the `minimum_stake` specified by `MinimumStakingAmount`
        /// unless account already has bonded value equal or more than 'minimum_stake'.
        ///
        /// The dispatch origin for this call must be _Signed_ by the staker's account.
        ///
        /// - `core_id`: Id of the core to stake towards.
        /// - `value`: Amount to stake.
        #[pallet::call_index(3)]
        #[pallet::weight(<T as Config>::WeightInfo::stake())]
        pub fn stake(
            origin: OriginFor<T>,
            core_id: T::CoreId,
            #[pallet::compact] value: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let staker = ensure_signed(origin)?;

            ensure!(
                Self::core_info(core_id).is_some(),
                Error::<T>::NotRegistered
            );

            let mut ledger = Self::ledger(&staker);
            let available_balance = Self::available_staking_balance(&staker, &ledger);
            let value_to_stake = value.min(available_balance);

            ensure!(value_to_stake > Zero::zero(), Error::<T>::StakingNothing);

            let current_era = Self::current_era();
            let mut staking_info = Self::core_stake_info(core_id, current_era).unwrap_or_default();
            let mut staker_info = Self::staker_info(core_id, &staker);

            Self::internal_stake(
                &mut staker_info,
                &mut staking_info,
                value_to_stake,
                current_era,
            )?;

            ledger.locked = ledger.locked.saturating_add(value_to_stake);

            GeneralEraInfo::<T>::mutate(current_era, |value| {
                if let Some(x) = value {
                    x.staked = x.staked.saturating_add(value_to_stake);
                    x.locked = x.locked.saturating_add(value_to_stake);
                }
            });

            Self::update_ledger(&staker, ledger);
            Self::update_staker_info(&staker, core_id, staker_info);
            CoreEraStake::<T>::insert(core_id, current_era, staking_info);

            Self::deposit_event(Event::<T>::Staked {
                staker,
                core: core_id,
                amount: value_to_stake,
            });
            Ok(().into())
        }

        /// Start unbonding process and unstake balance from the core.
        ///
        /// The unstaked amount will no longer be eligible for rewards but still won't be unlocked.
        /// User needs to wait for the unbonding period to finish before being able to withdraw
        /// the funds via `withdraw_unstaked` call.
        ///
        /// In case remaining staked balance is below minimum staking amount,
        /// entire stake will be unstaked.
        ///
        /// - `core_id`: Id of the core to unstake from.
        #[pallet::call_index(4)]
        #[pallet::weight(<T as Config>::WeightInfo::unstake())]
        pub fn unstake(
            origin: OriginFor<T>,
            core_id: T::CoreId,
            #[pallet::compact] value: BalanceOf<T>,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let staker = ensure_signed(origin)?;

            ensure!(value > Zero::zero(), Error::<T>::UnstakingNothing);
            ensure!(
                Self::core_info(core_id).is_some(),
                Error::<T>::NotRegistered
            );

            let current_era = Self::current_era();
            let mut staker_info = Self::staker_info(core_id, &staker);
            let mut core_stake_info =
                Self::core_stake_info(core_id, current_era).unwrap_or_default();

            let value_to_unstake =
                Self::internal_unstake(&mut staker_info, &mut core_stake_info, value, current_era)?;

            let mut ledger = Self::ledger(&staker);
            ledger.unbonding_info.add(UnlockingChunk {
                amount: value_to_unstake,
                unlock_era: current_era + T::UnbondingPeriod::get(),
            });

            ensure!(
                ledger.unbonding_info.len() <= T::MaxUnlocking::get(),
                Error::<T>::TooManyUnlockingChunks
            );

            Self::update_ledger(&staker, ledger);

            GeneralEraInfo::<T>::mutate(current_era, |value| {
                if let Some(x) = value {
                    x.staked = x.staked.saturating_sub(value_to_unstake);
                }
            });
            Self::update_staker_info(&staker, core_id, staker_info);
            CoreEraStake::<T>::insert(core_id, current_era, core_stake_info);

            Self::deposit_event(Event::<T>::Unstaked {
                staker,
                core: core_id,
                amount: value_to_unstake,
            });

            Ok(().into())
        }

        /// Withdraw all funds that have completed the unbonding process.
        ///
        /// If there are unbonding chunks which will be fully unbonded in future eras,
        /// they will remain and can be withdrawn later.
        ///
        /// The dispatch origin for this call must be _Signed_ by the staker's account.
        #[pallet::call_index(5)]
        #[pallet::weight(<T as Config>::WeightInfo::withdraw_unstaked())]
        pub fn withdraw_unstaked(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let staker = ensure_signed(origin)?;

            let mut ledger = Self::ledger(&staker);
            let current_era = Self::current_era();

            let (valid_chunks, future_chunks) = ledger.unbonding_info.partition(current_era);
            let withdraw_amount = valid_chunks.sum();

            ensure!(!withdraw_amount.is_zero(), Error::<T>::NothingToWithdraw);

            ledger.locked = ledger.locked.saturating_sub(withdraw_amount);
            ledger.unbonding_info = future_chunks;

            Self::update_ledger(&staker, ledger);
            GeneralEraInfo::<T>::mutate(current_era, |value| {
                if let Some(x) = value {
                    x.locked = x.locked.saturating_sub(withdraw_amount)
                }
            });

            Self::deposit_event(Event::<T>::Withdrawn {
                staker,
                amount: withdraw_amount,
            });

            Ok(().into())
        }

        /// Claim the staker's rewards.
        ///
        /// In case reward cannot be claimed or was already claimed, an error is raised.
        ///
        /// The dispatch origin for this call must be _Signed_ by the staker's account.
        ///
        /// - `core_id`: Id of the core to claim rewards from.
        #[pallet::call_index(6)]
        #[pallet::weight(<T as Config>::WeightInfo::staker_claim_rewards())]
        pub fn staker_claim_rewards(
            origin: OriginFor<T>,
            core_id: T::CoreId,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let staker = ensure_signed(origin)?;

            let mut staker_info = Self::staker_info(core_id, &staker);
            let (era, staked) = staker_info.claim();
            ensure!(staked > Zero::zero(), Error::<T>::NoStakeAvailable);

            let current_era = Self::current_era();
            ensure!(era < current_era, Error::<T>::IncorrectEra);

            let staking_info = Self::core_stake_info(core_id, era).unwrap_or_default();

            let mut staker_reward = Zero::zero();

            if staking_info.total > Zero::zero() {
                let reward_and_stake =
                    Self::general_era_info(era).ok_or(Error::<T>::UnknownEraReward)?;

                let (_, stakers_joint_reward) =
                    Self::core_stakers_split(&staking_info, &reward_and_stake);
                staker_reward =
                    Perbill::from_rational(staked, staking_info.total) * stakers_joint_reward;

                let reward_imbalance = <T as pallet::Config>::Currency::withdraw(
                    &Self::account_id(),
                    staker_reward,
                    WithdrawReasons::TRANSFER,
                    ExistenceRequirement::AllowDeath,
                )?;

                <T as pallet::Config>::Currency::resolve_creating(&staker, reward_imbalance);
                Self::update_staker_info(&staker, core_id, staker_info);
            }

            Self::deposit_event(Event::<T>::StakerClaimed {
                staker,
                core: core_id,
                era,
                amount: staker_reward,
            });

            Ok(().into())
        }

        /// Claim core reward for the specified era.
        ///
        /// In case reward cannot be claimed or was already claimed, an error is raised.
        ///
        /// - `core_id`: Id of the core to claim rewards from.
        /// - `era`: Era for which rewards are to be claimed.
        #[pallet::call_index(7)]
        #[pallet::weight(<T as Config>::WeightInfo::core_claim_rewards())]
        pub fn core_claim_rewards(
            origin: OriginFor<T>,
            core_id: T::CoreId,
            #[pallet::compact] era: Era,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            ensure_signed(origin)?;

            let current_era = Self::current_era();
            ensure!(era < current_era, Error::<T>::IncorrectEra);

            let mut core_stake_info = Self::core_stake_info(core_id, era).unwrap_or_default();
            ensure!(
                !core_stake_info.reward_claimed,
                Error::<T>::RewardAlreadyClaimed,
            );
            ensure!(
                core_stake_info.total > Zero::zero(),
                Error::<T>::NoStakeAvailable,
            );

            let reward_and_stake =
                Self::general_era_info(era).ok_or(Error::<T>::UnknownEraReward)?;

            let (reward, _) = Self::core_stakers_split(&core_stake_info, &reward_and_stake);

            let reward_imbalance = <T as pallet::Config>::Currency::withdraw(
                &Self::account_id(),
                reward,
                WithdrawReasons::TRANSFER,
                ExistenceRequirement::AllowDeath,
            )?;

            let core_account =
                <pallet_inv4::Pallet<T> as CoreAccountDerivation<T>>::derive_core_account(core_id);

            <T as pallet::Config>::Currency::resolve_creating(&core_account, reward_imbalance);
            Self::deposit_event(Event::<T>::CoreClaimed {
                core: core_id,
                destination_account: core_account,
                era,
                amount: reward,
            });

            core_stake_info.reward_claimed = true;
            CoreEraStake::<T>::insert(core_id, era, core_stake_info);

            Ok(().into())
        }

        /// Halt or unhalt the pallet.
        ///
        /// The dispatch origin for this call must be _Root_.
        ///
        /// - `halt`: `true` to halt, `false` to unhalt.
        #[pallet::call_index(8)]
        #[pallet::weight((<T as Config>::WeightInfo::halt_unhalt_pallet(), Pays::No))]
        pub fn halt_unhalt_pallet(origin: OriginFor<T>, halt: bool) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;

            let is_halted = Self::is_halted();

            ensure!(is_halted ^ halt, Error::<T>::NoHaltChange);

            Self::internal_halt_unhalt(halt);

            Self::deposit_event(Event::<T>::HaltChanged { is_halted: halt });

            Ok(().into())
        }

        /// Move stake from one core to another.
        ///
        /// The dispatch origin for this call must be _Signed_ by the staker's account.
        ///
        /// - `from_core`: Id of the core to move stake from.
        /// - `amount`: Amount to move.
        /// - `to_core`: Id of the core to move stake to.
        #[pallet::call_index(9)]
        #[pallet::weight(<T as Config>::WeightInfo::move_stake())]
        pub fn move_stake(
            origin: OriginFor<T>,
            from_core: T::CoreId,
            #[pallet::compact] amount: BalanceOf<T>,
            to_core: T::CoreId,
        ) -> DispatchResultWithPostInfo {
            Self::ensure_not_halted()?;

            let staker = ensure_signed(origin)?;

            ensure!(from_core != to_core, Error::<T>::MoveStakeToSameCore);
            ensure!(
                Self::core_info(to_core).is_some(),
                Error::<T>::NotRegistered
            );

            let current_era = Self::current_era();
            let mut from_staker_info = Self::staker_info(from_core, &staker);
            let mut from_core_info =
                Self::core_stake_info(from_core, current_era).unwrap_or_default();

            let unstaked_amount = Self::internal_unstake(
                &mut from_staker_info,
                &mut from_core_info,
                amount,
                current_era,
            )?;

            let mut to_staker_info = Self::staker_info(to_core, &staker);
            let mut to_core_info = Self::core_stake_info(to_core, current_era).unwrap_or_default();

            Self::internal_stake(
                &mut to_staker_info,
                &mut to_core_info,
                unstaked_amount,
                current_era,
            )?;

            CoreEraStake::<T>::insert(from_core, current_era, from_core_info);
            Self::update_staker_info(&staker, from_core, from_staker_info);

            CoreEraStake::<T>::insert(to_core, current_era, to_core_info);
            Self::update_staker_info(&staker, to_core, to_staker_info);

            Self::deposit_event(Event::<T>::StakeMoved {
                staker,
                from_core,
                to_core,
                amount: unstaked_amount,
            });

            Ok(().into())
        }
    }

    impl<T: Config> Pallet<T> {
        /// Internal function responsible for validating a stake and updating in-place
        /// both the staker's and core staking info.
        fn internal_stake(
            staker_info: &mut StakerInfo<BalanceOf<T>>,
            staking_info: &mut CoreStakeInfo<BalanceOf<T>>,
            amount: BalanceOf<T>,
            current_era: Era,
        ) -> Result<(), Error<T>> {
            ensure!(
                !staker_info.latest_staked_value().is_zero()
                    || staking_info.number_of_stakers < T::MaxStakersPerCore::get(),
                Error::<T>::MaxStakersReached
            );
            if staker_info.latest_staked_value().is_zero() {
                staking_info.number_of_stakers = staking_info.number_of_stakers.saturating_add(1);
            }

            staker_info
                .stake(current_era, amount)
                .map_err(|_| Error::<T>::UnexpectedStakeInfoEra)?;
            ensure!(
                staker_info.len() < T::MaxEraStakeValues::get(),
                Error::<T>::TooManyEraStakeValues
            );
            ensure!(
                staker_info.latest_staked_value() >= T::MinimumStakingAmount::get(),
                Error::<T>::InsufficientBalance,
            );

            let new_total = staking_info.total.saturating_add(amount);

            staking_info.total = new_total;

            Ok(())
        }

        /// Internal function responsible for validating an unstake and updating in-place
        /// both the staker's and core staking info.
        fn internal_unstake(
            staker_info: &mut StakerInfo<BalanceOf<T>>,
            core_stake_info: &mut CoreStakeInfo<BalanceOf<T>>,
            amount: BalanceOf<T>,
            current_era: Era,
        ) -> Result<BalanceOf<T>, Error<T>> {
            let staked_value = staker_info.latest_staked_value();
            ensure!(staked_value > Zero::zero(), Error::<T>::NoStakeAvailable);

            let remaining = staked_value.saturating_sub(amount);
            let value_to_unstake = if remaining < T::MinimumStakingAmount::get() {
                core_stake_info.number_of_stakers =
                    core_stake_info.number_of_stakers.saturating_sub(1);
                staked_value
            } else {
                amount
            };

            let new_total = core_stake_info.total.saturating_sub(value_to_unstake);

            core_stake_info.total = new_total;

            ensure!(
                value_to_unstake > Zero::zero(),
                Error::<T>::UnstakingNothing
            );

            staker_info
                .unstake(current_era, value_to_unstake)
                .map_err(|_| Error::<T>::UnexpectedStakeInfoEra)?;
            ensure!(
                staker_info.len() < T::MaxEraStakeValues::get(),
                Error::<T>::TooManyEraStakeValues
            );

            Ok(value_to_unstake)
        }

        pub(crate) fn account_id() -> T::AccountId {
            T::PotId::get().into_account_truncating()
        }

        /// Update the ledger for a staker. This will also update the stash lock.
        /// This lock will lock the entire funds except paying for further transactions.
        fn update_ledger(staker: &T::AccountId, ledger: AccountLedger<BalanceOf<T>>) {
            if ledger.is_empty() {
                Ledger::<T>::remove(staker);
                <T as pallet::Config>::Currency::remove_lock(LOCK_ID, staker);
            } else {
                <T as pallet::Config>::Currency::set_lock(
                    LOCK_ID,
                    staker,
                    ledger.locked,
                    WithdrawReasons::all(),
                );
                Ledger::<T>::insert(staker, ledger);
            }
        }

        /// The block rewards are accumulated on the pallet's account during an era.
        /// This function takes a snapshot of the pallet's balance accrued during current era
        /// and stores it for future distribution
        ///
        /// This is called just at the beginning of an era.
        fn reward_balance_snapshot(
            era: Era,
            rewards: RewardInfo<BalanceOf<T>>,
            new_active_stake: BalanceOf<T>,
        ) {
            let mut era_info = Self::general_era_info(era).unwrap_or_default();

            GeneralEraInfo::<T>::insert(
                era + 1,
                EraInfo {
                    rewards: Default::default(),
                    staked: era_info.staked,
                    active_stake: new_active_stake,
                    locked: era_info.locked,
                },
            );

            era_info.rewards = rewards;
            era_info.active_stake = new_active_stake;

            GeneralEraInfo::<T>::insert(era, era_info);
        }

        /// Adds `stakers` and `cores` rewards to the reward pool.
        ///
        /// - `inflation`: Total inflation for the era.
        pub fn rewards(inflation: NegativeImbalanceOf<T>) {
            let (core_part, stakers_part) = <T as Config>::RewardRatio::get();

            let (core, stakers) = inflation.ration(core_part, stakers_part);

            RewardAccumulator::<T>::mutate(|accumulated_reward| {
                accumulated_reward.core = accumulated_reward.core.saturating_add(core.peek());
                accumulated_reward.stakers =
                    accumulated_reward.stakers.saturating_add(stakers.peek());
            });

            <T as pallet::Config>::Currency::resolve_creating(
                &Self::account_id(),
                stakers.merge(core),
            );
        }

        /// Updates staker info for a core.
        fn update_staker_info(
            staker: &T::AccountId,
            core_id: T::CoreId,
            staker_info: StakerInfo<BalanceOf<T>>,
        ) {
            if staker_info.is_empty() {
                GeneralStakerInfo::<T>::remove(core_id, staker)
            } else {
                GeneralStakerInfo::<T>::insert(core_id, staker, staker_info)
            }
        }

        /// Returns available staking balance for the potential staker.
        fn available_staking_balance(
            staker: &T::AccountId,
            ledger: &AccountLedger<BalanceOf<T>>,
        ) -> BalanceOf<T> {
            let free_balance = <T as pallet::Config>::Currency::free_balance(staker)
                .saturating_sub(<T as pallet::Config>::ExistentialDeposit::get());

            free_balance.saturating_sub(ledger.locked)
        }

        /// Returns total value locked by staking.
        ///
        /// Note that this can differ from _total staked value_ since some funds might be undergoing the unbonding period.
        pub fn tvl() -> BalanceOf<T> {
            let current_era = Self::current_era();
            if let Some(era_info) = Self::general_era_info(current_era) {
                era_info.locked
            } else {
                Zero::zero()
            }
        }

        /// Calculate reward split between core and stakers.
        ///
        /// Returns (core reward, joint stakers reward)
        pub(crate) fn core_stakers_split(
            core_info: &CoreStakeInfo<BalanceOf<T>>,
            era_info: &EraInfo<BalanceOf<T>>,
        ) -> (BalanceOf<T>, BalanceOf<T>) {
            let core_stake_portion = if core_info.active {
                Perbill::from_rational(core_info.total, era_info.active_stake)
            } else {
                Perbill::zero()
            };
            let stakers_stake_portion = Perbill::from_rational(core_info.total, era_info.staked);

            let core_reward_part = core_stake_portion * era_info.rewards.core;
            let stakers_joint_reward = stakers_stake_portion * era_info.rewards.stakers;

            (core_reward_part, stakers_joint_reward)
        }

        /// Used to copy all `CoreStakeInfo` from the ending era over to the next era.
        fn rotate_staking_info(current_era: Era) -> (Weight, BalanceOf<T>) {
            let next_era = current_era + 1;

            let mut consumed_weight = Weight::zero();

            let mut new_active_stake: BalanceOf<T> = Zero::zero();

            for core_id in RegisteredCore::<T>::iter_keys() {
                consumed_weight = consumed_weight.saturating_add(T::DbWeight::get().reads(1));

                if let Some(mut staking_info) = Self::core_stake_info(core_id, current_era) {
                    if staking_info.total >= <T as Config>::StakeThresholdForActiveCore::get() {
                        staking_info.active = true;
                        new_active_stake += staking_info.total;
                    } else {
                        staking_info.active = false;
                    }

                    staking_info.reward_claimed = false;
                    CoreEraStake::<T>::insert(core_id, next_era, staking_info);

                    consumed_weight =
                        consumed_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
                } else {
                    consumed_weight = consumed_weight.saturating_add(T::DbWeight::get().reads(1));
                }
            }

            (consumed_weight, new_active_stake)
        }

        /// Sets the halt state of the pallet.
        pub fn internal_halt_unhalt(halt: bool) {
            Halted::<T>::put(halt);
        }

        /// Ensure the pallet is not halted.
        pub fn ensure_not_halted() -> Result<(), Error<T>> {
            if Self::is_halted() {
                Err(Error::<T>::Halted)
            } else {
                Ok(())
            }
        }

        /// Sharded execution of the core unregistration process.
        ///
        /// This function is called by the [`ProcessMessage`] trait implemented in [`primitives::ProcessUnregistrationMessages`]
        pub(crate) fn process_core_unregistration_shard(
            stakers: u32,
            core_id: T::CoreId,
            start_era: Era,
            chunk_size: u64,
        ) -> DispatchResultWithPostInfo {
            let mut staker_info_prefix =
                Self::core_unregistering_staker_list(core_id).unwrap_or_default();

            let mut corrected_staker_length_fee = Zero::zero();

            let mut core_stake_info =
                Self::core_unregistering_staker_info(core_id).unwrap_or_default();

            let mut unsteked_count: u64 = 0;
            while let Some(staker) = staker_info_prefix.pop() {
                let mut staker_info = Self::staker_info(core_id, &staker);

                let latest_staked_value = staker_info.latest_staked_value();

                if let Ok(value_to_unstake) = Self::internal_unstake(
                    &mut staker_info,
                    &mut core_stake_info,
                    latest_staked_value,
                    start_era,
                ) {
                    UnregisteredCoreStakeInfo::<T>::insert(core_id, core_stake_info.clone());
                    let mut ledger = Self::ledger(&staker);
                    ledger.unbonding_info.add(UnlockingChunk {
                        amount: value_to_unstake,
                        unlock_era: start_era + T::UnbondingPeriod::get(),
                    });

                    ensure!(
                        ledger.unbonding_info.len() <= T::MaxUnlocking::get(),
                        Error::<T>::TooManyUnlockingChunks
                    );

                    Self::update_ledger(&staker, ledger);

                    Self::update_staker_info(&staker, core_id, staker_info);

                    Self::deposit_event(Event::<T>::Unstaked {
                        staker: staker.clone(),
                        core: core_id,
                        amount: value_to_unstake,
                    });
                    corrected_staker_length_fee += <T as Config>::WeightInfo::unstake();
                } else {
                    // if the staker has moved or already unstaked `internal_unstake` will do one read and return err.
                    corrected_staker_length_fee += T::DbWeight::get().reads(1);
                }

                unsteked_count += 1;

                if unsteked_count >= chunk_size {
                    let total_remaning_stakers = stakers.saturating_sub(unsteked_count as u32);
                    let message: Vec<u8> = primitives::UnregisterMessage::<T> {
                        core_id,
                        stakers_to_unstake: total_remaning_stakers,
                        era: start_era,
                    }
                    .encode();

                    T::StakingMessage::handle_message(BoundedSlice::truncate_from(
                        message.as_slice(),
                    ));

                    Self::deposit_event(Event::<T>::CoreUnregistrationChunksProcessed {
                        core: core_id,
                        accounts_processed_in_this_chunk: unsteked_count,
                        accounts_left: total_remaning_stakers as u64,
                    });
                    UnregisteredCoreStakeInfo::<T>::insert(core_id, core_stake_info.clone());
                    UnregisteredCoreStakers::<T>::insert(core_id, staker_info_prefix);

                    return Ok(Some(corrected_staker_length_fee).into());
                }
            }

            Self::deposit_event(Event::<T>::CoreUnregistrationQueueFinished { core: core_id });
            UnregisteredCoreStakers::<T>::remove(core_id);
            UnregisteredCoreStakeInfo::<T>::remove(core_id);

            Ok(Some(corrected_staker_length_fee).into())
        }
    }
}

impl<T: Config> QueuePausedQuery<T> for Pallet<T> {
    fn is_paused(_origin: &T) -> bool {
        Pallet::<T>::is_halted()
    }
}

pub type MessageOriginOf<T> =
    <<T as pallet_message_queue::Config>::MessageProcessor as ProcessMessage>::Origin;