radix-engine 1.3.1

Reference implementation of Radix Engine, from the Radix DLT project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
use crate::blueprints::consensus_manager::*;
use crate::blueprints::util::SecurifiedRoleAssignment;
use crate::errors::ApplicationError;
use crate::errors::RuntimeError;
use crate::internal_prelude::*;
use crate::{event_schema, roles_template};
use radix_engine_interface::api::field_api::LockFlags;
use radix_engine_interface::api::{
    AttachedModuleId, FieldValue, SystemApi, ACTOR_REF_GLOBAL, ACTOR_STATE_OUTER_OBJECT,
    ACTOR_STATE_SELF,
};
use radix_engine_interface::blueprints::consensus_manager::*;
use radix_engine_interface::blueprints::package::{
    AuthConfig, BlueprintDefinitionInit, BlueprintType, FunctionAuth, MethodAuthTemplate,
};
use radix_engine_interface::blueprints::resource::*;
use radix_engine_interface::object_modules::metadata::UncheckedUrl;
use radix_engine_interface::{burn_roles, metadata_init, mint_roles, rule};
use radix_native_sdk::modules::metadata::Metadata;
use radix_native_sdk::resource::NativeVault;
use radix_native_sdk::resource::ResourceManager;
use radix_native_sdk::resource::{NativeBucket, NativeNonFungibleBucket};
use radix_native_sdk::runtime::Runtime;
use sbor::rust::mem;

use super::{
    ClaimXrdEvent, RegisterValidatorEvent, StakeEvent, UnregisterValidatorEvent, UnstakeEvent,
    UpdateAcceptingStakeDelegationStateEvent,
};

pub const VALIDATOR_PROTOCOL_VERSION_NAME_LEN: usize = 32;

/// A performance-driven limit on the number of simultaneously pending "delayed withdrawal"
/// operations on any validator's owner's stake units vault.
pub const OWNER_STAKE_UNITS_PENDING_WITHDRAWALS_LIMIT: usize = 100;

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub struct ValidatorSubstate {
    /// A key used internally for storage of registered validators sorted by their stake descending.
    /// It is only useful when the validator is registered and has non-zero stake - hence, the field
    /// is [`None`] otherwise.
    /// Note: in theory, this value could be always computed from the [`is_registered`] status and
    /// the amount stored in [`stake_xrd_vault_id`]; we simply keep it cached to simplify certain
    /// updates.
    pub sorted_key: Option<SortedKey>,

    /// This validator's public key.
    pub key: Secp256k1PublicKey,

    /// Whether this validator is currently interested in participating in the consensus.
    pub is_registered: bool,

    /// Whether this validator is currently accepting delegated stake or not
    pub accepts_delegated_stake: bool,

    /// A fraction of the effective emission amount which gets transferred to the validator's owner
    /// (by staking it and depositing the stake units to the [`locked_owner_stake_unit_vault_id`]).
    /// Note: it is a decimal factor, not a percentage (i.e. `0.015` means "1.5%" here).
    /// Note: it may be overridden by [`validator_fee_change_request`], if it contains a change
    /// which already became effective.
    pub validator_fee_factor: Decimal,

    /// The most recent request to change the [`validator_fee_factor`] (which requires a delay).
    /// Note: the value from this request will be used instead of [`validator_fee_factor`] if the
    /// request has already reached its effective epoch.
    /// Note: when another change is requested, the value from this (previous) one is moved to the
    /// [`validator_fee_factor`] - provided that it became already effective. Otherwise, this
    /// request is overwritten by the new one.
    pub validator_fee_change_request: Option<ValidatorFeeChangeRequest>,

    /// A type of fungible resource representing stake units specific to this validator.
    /// Conceptually, "staking to validator A" means "contributing to the validator's staking pool,
    /// and receiving the validator's stake units which act as the pool units for the staking pool".
    pub stake_unit_resource: ResourceAddress,

    /// A vault holding the XRDs currently staked to this validator.
    pub stake_xrd_vault_id: Own,

    /// A type of non-fungible token used as a receipt for unstaked stake units.
    /// Unstaking burns the SUs and inactivates the staked XRDs (i.e. moves it from the regular
    /// [`stake_xrd_vault_id`] to the [`pending_xrd_withdraw_vault_id`]), and then requires to claim
    /// the XRDs using this NFT after a delay (see [`UnstakeData.claim_epoch`]).
    pub claim_nft: ResourceAddress,

    /// A vault holding the XRDs that were unstaked (see the [`unstake_nft`]) but not yet claimed.
    pub pending_xrd_withdraw_vault_id: Own,

    /// A vault holding the SUs that this validator's owner voluntarily decided to temporarily lock
    /// here, as a public display of their confidence in this validator's future reliability.
    /// Withdrawing SUs from this vault is subject to a delay (which is configured separately from
    /// the regular unstaking delay, see [`ConsensusManagerConfigSubstate.num_owner_stake_units_unlock_epochs`]).
    /// This vault is private to the owner (i.e. the owner's badge is required for any interaction
    /// with this vault).
    pub locked_owner_stake_unit_vault_id: Own,

    /// A vault holding the SUs which the owner has decided to withdraw from their "public display"
    /// vault (see [`locked_owner_stake_unit_vault_id`]) but which have not yet been unlocked after
    /// the mandatory delay (see [`pending_owner_stake_unit_withdrawals`]).
    pub pending_owner_stake_unit_unlock_vault_id: Own,

    /// All currently pending "delayed withdrawal" operations of the owner's stake units vault (see
    /// [`locked_owner_stake_unit_vault_id`]).
    /// This maps an epoch number to an amount of stake units that become unlocked at that epoch.
    /// Note: because of performance considerations, a maximum size of this map is limited to
    /// [`OWNER_STAKE_UNITS_PENDING_WITHDRAWALS_LIMIT`]: starting another withdrawal will first
    /// attempt to move any already-available amount to [`already_unlocked_owner_stake_unit_amount`]
    /// and only then will fail if the limit is exceeded.
    pub pending_owner_stake_unit_withdrawals: BTreeMap<Epoch, Decimal>,

    /// An amount of owner's stake units that has already waited for a sufficient number of epochs
    /// in the [`pending_owner_stake_unit_withdrawals`] and was automatically moved from there.
    /// The very next [`finish_unlock_owner_stake_units()`] operation will release this amount.
    pub already_unlocked_owner_stake_unit_amount: Decimal,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
#[sbor(transparent)]
pub struct ValidatorProtocolUpdateReadinessSignalSubstate {
    pub protocol_version_name: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor, ManifestSbor)]
pub struct UnstakeData {
    pub name: String,

    /// An epoch number at (or after) which the pending unstaked XRD may be claimed.
    /// Note: on unstake, it is fixed to be [`ConsensusManagerConfigSubstate.num_unstake_epochs`] away.
    pub claim_epoch: Epoch,

    /// An XRD amount to be claimed.
    pub claim_amount: Decimal,
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub struct ValidatorFeeChangeRequest {
    /// An epoch number at (or after) which the fee change is effective.
    /// To be specific: when a next epoch `N` begins, we perform accounting of emissions due for
    /// previous epoch `N-1` - this means that we will use this [`new_validator_fee_factor`] only if
    /// `epoch_effective <= N-1`, and [`ValidatorSubstate.validator_fee_factor`] otherwise.
    /// Note: when requesting a fee decrease, this will be "next epoch"; and when requesting an
    /// increase, this will be set to [`ConsensusManagerConfigSubstate.num_fee_increase_delay_epochs`]
    /// epochs away.
    pub epoch_effective: Epoch,

    /// A requested new value of [`ConsensusManagerSubstate.validator_fee_factor`].
    pub new_fee_factor: Decimal,
}

impl NonFungibleData for UnstakeData {
    const MUTABLE_FIELDS: &'static [&'static str] = &[];
}

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub enum ValidatorError {
    InvalidClaimResource,
    InvalidGetRedemptionAmount,
    UnexpectedDecimalComputationError,
    EpochUnlockHasNotOccurredYet,
    PendingOwnerStakeWithdrawalLimitReached,
    InvalidValidatorFeeFactor,
    ValidatorIsNotAcceptingDelegatedStake,
    InvalidProtocolVersionNameLength { expected: usize, actual: usize },
    EpochMathOverflow,
}

declare_native_blueprint_state! {
    blueprint_ident: Validator,
    blueprint_snake_case: validator,
    features: {
    },
    fields: {
        state: {
            ident: State,
            field_type: {
                kind: StaticSingleVersioned,
            },
            condition: Condition::Always,
        },
        protocol_update_readiness_signal: {
            ident: ProtocolUpdateReadinessSignal,
            field_type: {
                kind: StaticSingleVersioned,
            },
            condition: Condition::Always,
        },
    },
    collections: {
    }
}

pub type ValidatorStateV1 = ValidatorSubstate;
pub type ValidatorProtocolUpdateReadinessSignalV1 = ValidatorProtocolUpdateReadinessSignalSubstate;

#[derive(Debug, Clone, Eq, PartialEq, ScryptoSbor, ManifestSbor)]
enum UpdateSecondaryIndex {
    Create {
        index_key: SortedKey,
        key: Secp256k1PublicKey,
        stake: Decimal,
    },
    UpdateStake {
        index_key: SortedKey,
        new_index_key: SortedKey,
        new_stake_amount: Decimal,
    },
    UpdatePublicKey {
        index_key: SortedKey,
        key: Secp256k1PublicKey,
    },
    Remove {
        index_key: SortedKey,
    },
}

pub struct ValidatorBlueprint;

impl ValidatorBlueprint {
    pub fn definition() -> BlueprintDefinitionInit {
        let mut aggregator = TypeAggregator::<ScryptoCustomTypeKind>::new();

        let feature_set = ValidatorFeatureSet::all_features();
        let state = ValidatorStateSchemaInit::create_schema_init(&mut aggregator);

        let mut functions = index_map_new();
        functions.insert(
            VALIDATOR_REGISTER_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorRegisterInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorRegisterOutput>(),
                ),
                export: VALIDATOR_REGISTER_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_UNREGISTER_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUnregisterInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUnregisterOutput>(),
                ),
                export: VALIDATOR_UNREGISTER_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_STAKE_AS_OWNER_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorStakeAsOwnerInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorStakeAsOwnerOutput>(),
                ),
                export: VALIDATOR_STAKE_AS_OWNER_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_STAKE_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorStakeInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorStakeOutput>(),
                ),
                export: VALIDATOR_STAKE_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_UNSTAKE_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUnstakeInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUnstakeOutput>(),
                ),
                export: VALIDATOR_UNSTAKE_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_CLAIM_XRD_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorClaimXrdInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorClaimXrdOutput>(),
                ),
                export: VALIDATOR_CLAIM_XRD_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_UPDATE_KEY_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUpdateKeyInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUpdateKeyOutput>(),
                ),
                export: VALIDATOR_UPDATE_KEY_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_UPDATE_FEE_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUpdateFeeInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorUpdateFeeOutput>(),
                ),
                export: VALIDATOR_UPDATE_FEE_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_UPDATE_ACCEPT_DELEGATED_STAKE_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorUpdateAcceptDelegatedStakeInput>()),
                output: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorUpdateAcceptDelegatedStakeOutput>()),
                export: VALIDATOR_UPDATE_ACCEPT_DELEGATED_STAKE_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_ACCEPTS_DELEGATED_STAKE_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorAcceptsDelegatedStakeInput>(),
                ),
                output: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorAcceptsDelegatedStakeOutput>(),
                ),
                export: VALIDATOR_ACCEPTS_DELEGATED_STAKE_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_TOTAL_STAKE_XRD_AMOUNT_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref()),
                input: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorTotalStakeXrdAmountInput>(),
                ),
                output: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorTotalStakeXrdAmountOutput>(),
                ),
                export: VALIDATOR_TOTAL_STAKE_XRD_AMOUNT_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_TOTAL_STAKE_UNIT_SUPPLY_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref()),
                input: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorTotalStakeUnitSupplyInput>(),
                ),
                output: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorTotalStakeUnitSupplyOutput>(),
                ),
                export: VALIDATOR_TOTAL_STAKE_UNIT_SUPPLY_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_GET_REDEMPTION_VALUE_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorGetRedemptionValueInput>(),
                ),
                output: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorGetRedemptionValueOutput>(),
                ),
                export: VALIDATOR_GET_REDEMPTION_VALUE_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_SIGNAL_PROTOCOL_UPDATE_READINESS_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorSignalProtocolUpdateReadinessInput>()),
                output: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorSignalProtocolUpdateReadinessOutput>()),
                export: VALIDATOR_SIGNAL_PROTOCOL_UPDATE_READINESS_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_GET_PROTOCOL_UPDATE_READINESS_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorGetProtocolUpdateReadinessInput>()),
                output: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorGetProtocolUpdateReadinessOutput>()),
                export: VALIDATOR_GET_PROTOCOL_UPDATE_READINESS_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_LOCK_OWNER_STAKE_UNITS_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorLockOwnerStakeUnitsInput>(),
                ),
                output: TypeRef::Static(
                    aggregator
                        .add_child_type_and_descendents::<ValidatorLockOwnerStakeUnitsOutput>(),
                ),
                export: VALIDATOR_LOCK_OWNER_STAKE_UNITS_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_START_UNLOCK_OWNER_STAKE_UNITS_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorStartUnlockOwnerStakeUnitsInput>()),
                output: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorStartUnlockOwnerStakeUnitsOutput>()),
                export: VALIDATOR_START_UNLOCK_OWNER_STAKE_UNITS_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_FINISH_UNLOCK_OWNER_STAKE_UNITS_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorFinishUnlockOwnerStakeUnitsInput>()),
                output: TypeRef::Static(aggregator
                    .add_child_type_and_descendents::<ValidatorFinishUnlockOwnerStakeUnitsOutput>()),
                export: VALIDATOR_FINISH_UNLOCK_OWNER_STAKE_UNITS_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_APPLY_EMISSION_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorApplyEmissionInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorApplyEmissionOutput>(),
                ),
                export: VALIDATOR_APPLY_EMISSION_IDENT.to_string(),
            },
        );
        functions.insert(
            VALIDATOR_APPLY_REWARD_IDENT.to_string(),
            FunctionSchemaInit {
                receiver: Some(ReceiverInfo::normal_ref_mut()),
                input: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorApplyRewardInput>(),
                ),
                output: TypeRef::Static(
                    aggregator.add_child_type_and_descendents::<ValidatorApplyRewardOutput>(),
                ),
                export: VALIDATOR_APPLY_REWARD_IDENT.to_string(),
            },
        );

        let event_schema = event_schema! {
            aggregator,
            [
                RegisterValidatorEvent,
                UnregisterValidatorEvent,
                StakeEvent,
                UnstakeEvent,
                ClaimXrdEvent,
                ProtocolUpdateReadinessSignalEvent,
                UpdateAcceptingStakeDelegationStateEvent,
                ValidatorEmissionAppliedEvent,
                ValidatorRewardAppliedEvent
            ]
        };

        let schema = generate_full_schema(aggregator);

        BlueprintDefinitionInit {
            blueprint_type: BlueprintType::Inner {
                outer_blueprint: CONSENSUS_MANAGER_BLUEPRINT.to_string(),
            },
            is_transient: false,
            feature_set,
            dependencies: indexset!(),
            schema: BlueprintSchemaInit {
                generics: vec![],
                schema,
                state,
                events: event_schema,
                types: BlueprintTypeSchemaInit::default(),
                functions: BlueprintFunctionsSchemaInit { functions },
                hooks: BlueprintHooksInit::default(),
            },
            royalty_config: PackageRoyaltyConfig::default(),
            auth_config: AuthConfig {
                function_auth: FunctionAuth::AllowAll,
                method_auth: MethodAuthTemplate::StaticRoleDefinition(roles_template! {
                    methods {
                        VALIDATOR_UNSTAKE_IDENT => MethodAccessibility::Public;
                        VALIDATOR_CLAIM_XRD_IDENT => MethodAccessibility::Public;
                        VALIDATOR_STAKE_IDENT => MethodAccessibility::Public;
                        VALIDATOR_ACCEPTS_DELEGATED_STAKE_IDENT => MethodAccessibility::Public;
                        VALIDATOR_TOTAL_STAKE_XRD_AMOUNT_IDENT => MethodAccessibility::Public;
                        VALIDATOR_TOTAL_STAKE_UNIT_SUPPLY_IDENT => MethodAccessibility::Public;
                        VALIDATOR_GET_REDEMPTION_VALUE_IDENT => MethodAccessibility::Public;
                        VALIDATOR_STAKE_AS_OWNER_IDENT => [OWNER_ROLE];
                        VALIDATOR_REGISTER_IDENT => [OWNER_ROLE];
                        VALIDATOR_UNREGISTER_IDENT => [OWNER_ROLE];
                        VALIDATOR_UPDATE_KEY_IDENT => [OWNER_ROLE];
                        VALIDATOR_UPDATE_FEE_IDENT => [OWNER_ROLE];
                        VALIDATOR_LOCK_OWNER_STAKE_UNITS_IDENT => [OWNER_ROLE];
                        VALIDATOR_START_UNLOCK_OWNER_STAKE_UNITS_IDENT => [OWNER_ROLE];
                        VALIDATOR_FINISH_UNLOCK_OWNER_STAKE_UNITS_IDENT => [OWNER_ROLE];
                        VALIDATOR_UPDATE_ACCEPT_DELEGATED_STAKE_IDENT => [OWNER_ROLE];
                        VALIDATOR_SIGNAL_PROTOCOL_UPDATE_READINESS_IDENT => [OWNER_ROLE];
                        VALIDATOR_GET_PROTOCOL_UPDATE_READINESS_IDENT => MethodAccessibility::OuterObjectOnly;
                        VALIDATOR_APPLY_EMISSION_IDENT => MethodAccessibility::OuterObjectOnly;
                        VALIDATOR_APPLY_REWARD_IDENT => MethodAccessibility::OuterObjectOnly;
                    }
                }),
            },
        }
    }

    pub fn register<Y: SystemApi<RuntimeError>>(api: &mut Y) -> Result<(), RuntimeError> {
        Self::register_update(true, api)
    }

    pub fn unregister<Y: SystemApi<RuntimeError>>(api: &mut Y) -> Result<(), RuntimeError> {
        Self::register_update(false, api)
    }

    pub fn stake_as_owner<Y: SystemApi<RuntimeError>>(
        xrd_bucket: Bucket,
        api: &mut Y,
    ) -> Result<Bucket, RuntimeError> {
        Ok(Self::stake_internal(xrd_bucket, true, api)?.into())
    }

    pub fn stake<Y: SystemApi<RuntimeError>>(
        xrd_bucket: Bucket,
        api: &mut Y,
    ) -> Result<Bucket, RuntimeError> {
        Ok(Self::stake_internal(xrd_bucket, false, api)?.into())
    }

    fn stake_internal<Y: SystemApi<RuntimeError>>(
        xrd_bucket: Bucket,
        is_owner: bool,
        api: &mut Y,
    ) -> Result<FungibleBucket, RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.field_index(),
            LockFlags::MUTABLE,
        )?;

        let mut validator = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        if !is_owner && !validator.accepts_delegated_stake {
            api.field_close(handle)?;

            // TODO: Should this be an Option returned instead similar to Account?
            return Err(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(
                    ValidatorError::ValidatorIsNotAcceptingDelegatedStake,
                ),
            ));
        }

        let xrd_bucket_amount = xrd_bucket.amount(api)?;

        // Stake
        let (stake_unit_bucket, new_stake_amount) = {
            let mut stake_unit_resman = ResourceManager(validator.stake_unit_resource);
            let mut xrd_vault = Vault(validator.stake_xrd_vault_id);
            let stake_unit_mint_amount = Self::calculate_stake_unit_amount(
                xrd_bucket_amount,
                xrd_vault.amount(api)?,
                stake_unit_resman.total_supply(api)?.unwrap(),
            )?;

            let stake_unit_bucket = stake_unit_resman.mint_fungible(stake_unit_mint_amount, api)?;
            xrd_vault.put(xrd_bucket, api)?;
            let new_stake_amount = xrd_vault.amount(api)?;
            (stake_unit_bucket, new_stake_amount)
        };

        // Update ConsensusManager
        let new_index_key =
            Self::index_update(&validator, validator.is_registered, new_stake_amount, api)?;

        validator.sorted_key = new_index_key;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(validator),
        )?;

        Runtime::emit_event(
            api,
            StakeEvent {
                xrd_staked: xrd_bucket_amount,
            },
        )?;

        Ok(stake_unit_bucket)
    }

    pub fn unstake<Y: SystemApi<RuntimeError>>(
        stake_unit_bucket: Bucket,
        api: &mut Y,
    ) -> Result<NonFungibleBucket, RuntimeError> {
        let stake_unit_bucket_amount = stake_unit_bucket.amount(api)?;

        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.field_index(),
            LockFlags::MUTABLE,
        )?;
        let mut validator_substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        // Unstake
        let (unstake_bucket, new_stake_amount) = {
            let xrd_amount = Self::calculate_redemption_value(
                stake_unit_bucket_amount,
                &validator_substate,
                api,
            )?;

            let mut stake_vault = Vault(validator_substate.stake_xrd_vault_id);
            let mut unstake_vault = Vault(validator_substate.pending_xrd_withdraw_vault_id);
            let nft_resman = ResourceManager(validator_substate.claim_nft);
            let mut stake_unit_resman = ResourceManager(validator_substate.stake_unit_resource);

            stake_unit_resman.burn(stake_unit_bucket, api)?;

            let manager_handle = api.actor_open_field(
                ACTOR_STATE_OUTER_OBJECT,
                ConsensusManagerField::State.into(),
                LockFlags::read_only(),
            )?;
            let manager_substate = api
                .field_read_typed::<ConsensusManagerStateFieldPayload>(manager_handle)?
                .fully_update_and_into_latest_version();
            let current_epoch = manager_substate.epoch;
            api.field_close(manager_handle)?;

            let config_handle = api.actor_open_field(
                ACTOR_STATE_OUTER_OBJECT,
                ConsensusManagerField::Configuration.into(),
                LockFlags::read_only(),
            )?;
            let config_substate = api
                .field_read_typed::<ConsensusManagerConfigurationFieldPayload>(config_handle)?
                .fully_update_and_into_latest_version();
            api.field_close(config_handle)?;

            let claim_epoch = current_epoch
                .after(config_substate.config.num_unstake_epochs)
                .ok_or(RuntimeError::ApplicationError(
                    ApplicationError::ValidatorError(ValidatorError::EpochMathOverflow),
                ))?;
            let data = UnstakeData {
                name: "Stake Claim".into(),
                claim_epoch,
                claim_amount: xrd_amount,
            };

            let bucket = stake_vault.take(xrd_amount, api)?;
            unstake_vault.put(bucket, api)?;
            let (unstake_bucket, _) = nft_resman.mint_non_fungible_single_ruid(data, api)?;

            let new_stake_amount = stake_vault.amount(api)?;

            (unstake_bucket, new_stake_amount)
        };

        // Update ConsensusManager
        let new_index_key = Self::index_update(
            &validator_substate,
            validator_substate.is_registered,
            new_stake_amount,
            api,
        )?;

        validator_substate.sorted_key = new_index_key;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(validator_substate),
        )?;

        Runtime::emit_event(
            api,
            UnstakeEvent {
                stake_units: stake_unit_bucket_amount,
            },
        )?;

        Ok(unstake_bucket)
    }

    pub fn signal_protocol_update_readiness<Y: SystemApi<RuntimeError>>(
        protocol_version_name: String,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        if protocol_version_name.len() != VALIDATOR_PROTOCOL_VERSION_NAME_LEN {
            return Err(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(
                    ValidatorError::InvalidProtocolVersionNameLength {
                        expected: VALIDATOR_PROTOCOL_VERSION_NAME_LEN,
                        actual: protocol_version_name.len(),
                    },
                ),
            ));
        }

        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::ProtocolUpdateReadinessSignal.into(),
            LockFlags::MUTABLE,
        )?;
        let mut signal = api
            .field_read_typed::<ValidatorProtocolUpdateReadinessSignalFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        signal.protocol_version_name = Some(protocol_version_name.clone());
        api.field_write_typed(
            handle,
            &ValidatorProtocolUpdateReadinessSignalFieldPayload::from_content_source(signal),
        )?;
        api.field_close(handle)?;

        Runtime::emit_event(
            api,
            ProtocolUpdateReadinessSignalEvent {
                protocol_version_name,
            },
        )?;

        Ok(())
    }

    pub fn get_protocol_update_readiness<Y: SystemApi<RuntimeError>>(
        api: &mut Y,
    ) -> Result<Option<String>, RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::ProtocolUpdateReadinessSignal.into(),
            LockFlags::read_only(),
        )?;
        let signal = api
            .field_read_typed::<ValidatorProtocolUpdateReadinessSignalFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        api.field_close(handle)?;

        Ok(signal.protocol_version_name)
    }

    fn register_update<Y: SystemApi<RuntimeError>>(
        new_registered: bool,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.field_index(),
            LockFlags::MUTABLE,
        )?;

        let mut validator = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        // No update
        if validator.is_registered == new_registered {
            return Ok(());
        }

        let stake_amount = {
            let stake_vault = Vault(validator.stake_xrd_vault_id);
            stake_vault.amount(api)?
        };

        let index_key = Self::index_update(&validator, new_registered, stake_amount, api)?;

        validator.is_registered = new_registered;
        validator.sorted_key = index_key;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(validator),
        )?;

        if new_registered {
            Runtime::emit_event(api, RegisterValidatorEvent)?;
        } else {
            Runtime::emit_event(api, UnregisterValidatorEvent)?;
        }

        Ok(())
    }

    fn index_update<Y: SystemApi<RuntimeError>>(
        validator: &ValidatorSubstate,
        new_registered: bool,
        new_stake_amount: Decimal,
        api: &mut Y,
    ) -> Result<Option<SortedKey>, RuntimeError> {
        let validator_address: ComponentAddress =
            ComponentAddress::new_or_panic(api.actor_get_node_id(ACTOR_REF_GLOBAL)?.into());
        let new_sorted_key =
            Self::to_sorted_key(new_registered, new_stake_amount, validator_address)?;

        let update = if let Some(cur_index_key) = &validator.sorted_key {
            if let Some(new_index_key) = &new_sorted_key {
                Some(UpdateSecondaryIndex::UpdateStake {
                    index_key: cur_index_key.clone(),
                    new_index_key: new_index_key.clone(),
                    new_stake_amount,
                })
            } else {
                Some(UpdateSecondaryIndex::Remove {
                    index_key: cur_index_key.clone(),
                })
            }
        } else {
            new_sorted_key
                .as_ref()
                .map(|new_index_key| UpdateSecondaryIndex::Create {
                    index_key: new_index_key.clone(),
                    stake: new_stake_amount,
                    key: validator.key,
                })
        };

        if let Some(update) = update {
            Self::update_validator(update, api)?;
        }

        Ok(new_sorted_key)
    }

    pub fn claim_xrd<Y: SystemApi<RuntimeError>>(
        bucket: Bucket,
        api: &mut Y,
    ) -> Result<Bucket, RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.field_index(),
            LockFlags::read_only(),
        )?;
        let validator = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        let mut nft_resman = ResourceManager(validator.claim_nft);
        let resource_address = validator.claim_nft;
        let mut unstake_vault = Vault(validator.pending_xrd_withdraw_vault_id);

        if !resource_address.eq(&bucket.resource_address(api)?) {
            return Err(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(ValidatorError::InvalidClaimResource),
            ));
        }

        let current_epoch = {
            let mgr_handle = api.actor_open_field(
                ACTOR_STATE_OUTER_OBJECT,
                ConsensusManagerField::State.field_index(),
                LockFlags::read_only(),
            )?;
            let mgr_substate = api
                .field_read_typed::<ConsensusManagerStateFieldPayload>(mgr_handle)?
                .fully_update_and_into_latest_version();
            let epoch = mgr_substate.epoch;
            api.field_close(mgr_handle)?;
            epoch
        };

        let mut unstake_amount = Decimal::zero();

        for id in bucket.non_fungible_local_ids(api)? {
            let data: UnstakeData = nft_resman.get_non_fungible_data(id, api)?;
            if current_epoch < data.claim_epoch {
                return Err(RuntimeError::ApplicationError(
                    ApplicationError::ValidatorError(ValidatorError::EpochUnlockHasNotOccurredYet),
                ));
            }
            unstake_amount = unstake_amount.checked_add(data.claim_amount).ok_or(
                RuntimeError::ApplicationError(ApplicationError::ValidatorError(
                    ValidatorError::UnexpectedDecimalComputationError,
                )),
            )?;
        }
        nft_resman.burn(bucket, api)?;

        let claimed_bucket = unstake_vault.take(unstake_amount, api)?;

        let amount = claimed_bucket.amount(api)?;
        Runtime::emit_event(
            api,
            ClaimXrdEvent {
                claimed_xrd: amount,
            },
        )?;

        Ok(claimed_bucket)
    }

    pub fn update_key<Y: SystemApi<RuntimeError>>(
        key: Secp256k1PublicKey,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut validator = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        // Update Consensus Manager
        {
            if let Some(index_key) = &validator.sorted_key {
                let update = UpdateSecondaryIndex::UpdatePublicKey {
                    index_key: index_key.clone(),
                    key,
                };

                Self::update_validator(update, api)?;
            }
        }

        validator.key = key;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(validator),
        )?;

        Ok(())
    }

    pub fn update_fee<Y: SystemApi<RuntimeError>>(
        new_fee_factor: Decimal,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        // check if new fee is valid
        check_validator_fee_factor(new_fee_factor)?;

        // read the current epoch
        let consensus_manager_handle = api.actor_open_field(
            ACTOR_STATE_OUTER_OBJECT,
            ConsensusManagerField::State.into(),
            LockFlags::read_only(),
        )?;
        let consensus_manager = api
            .field_read_typed::<ConsensusManagerStateFieldPayload>(consensus_manager_handle)?
            .fully_update_and_into_latest_version();
        let current_epoch = consensus_manager.epoch;
        api.field_close(consensus_manager_handle)?;

        // read the configured fee increase epochs delay
        let config_handle = api.actor_open_field(
            ACTOR_STATE_OUTER_OBJECT,
            ConsensusManagerField::Configuration.into(),
            LockFlags::read_only(),
        )?;
        let config_substate = api
            .field_read_typed::<ConsensusManagerConfigurationFieldPayload>(config_handle)?
            .fully_update_and_into_latest_version();
        api.field_close(config_handle)?;

        // begin the read+modify+write of the validator substate...
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        // - promote any currently pending change if it became effective already
        if let Some(previous_request) = substate.validator_fee_change_request {
            if previous_request.epoch_effective <= current_epoch {
                substate.validator_fee_factor = previous_request.new_fee_factor;
            }
        }

        // - calculate the effective epoch of the requested change
        let epoch_effective = if new_fee_factor > substate.validator_fee_factor {
            current_epoch.after(config_substate.config.num_fee_increase_delay_epochs)
        } else {
            current_epoch.next() // make it effective on the *beginning* of next epoch
        }
        .ok_or(RuntimeError::ApplicationError(
            ApplicationError::ValidatorError(ValidatorError::EpochMathOverflow),
        ))?;

        // ...end the read+modify+write of the validator substate
        substate.validator_fee_change_request = Some(ValidatorFeeChangeRequest {
            epoch_effective,
            new_fee_factor,
        });
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(substate),
        )?;
        api.field_close(handle)?;

        Ok(())
    }

    pub fn accepts_delegated_stake<Y: SystemApi<RuntimeError>>(
        api: &mut Y,
    ) -> Result<bool, RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::read_only(),
        )?;

        let substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        api.field_close(handle)?;

        Ok(substate.accepts_delegated_stake)
    }

    pub fn total_stake_xrd_amount<Y: SystemApi<RuntimeError>>(
        api: &mut Y,
    ) -> Result<Decimal, RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::read_only(),
        )?;

        let substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        let stake_vault = Vault(substate.stake_xrd_vault_id);
        let stake_amount = stake_vault.amount(api)?;
        api.field_close(handle)?;

        Ok(stake_amount)
    }

    pub fn total_stake_unit_supply<Y: SystemApi<RuntimeError>>(
        api: &mut Y,
    ) -> Result<Decimal, RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::read_only(),
        )?;

        let substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        let stake_resource = ResourceManager(substate.stake_unit_resource);
        let total_stake_unit_supply = stake_resource.total_supply(api)?.unwrap();
        api.field_close(handle)?;

        Ok(total_stake_unit_supply)
    }

    pub fn get_redemption_value<Y: SystemApi<RuntimeError>>(
        amount_of_stake_units: Decimal,
        api: &mut Y,
    ) -> Result<Decimal, RuntimeError> {
        if amount_of_stake_units.is_negative() || amount_of_stake_units.is_zero() {
            return Err(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(ValidatorError::InvalidGetRedemptionAmount),
            ));
        }

        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::read_only(),
        )?;
        let validator = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        {
            let stake_unit_resman = ResourceManager(validator.stake_unit_resource);
            let total_stake_unit_supply = stake_unit_resman.total_supply(api)?.unwrap();
            if amount_of_stake_units > total_stake_unit_supply {
                return Err(RuntimeError::ApplicationError(
                    ApplicationError::ValidatorError(ValidatorError::InvalidGetRedemptionAmount),
                ));
            }
        }

        let redemption_value =
            Self::calculate_redemption_value(amount_of_stake_units, &validator, api)?;
        api.field_close(handle)?;

        Ok(redemption_value)
    }

    pub fn update_accept_delegated_stake<Y: SystemApi<RuntimeError>>(
        accept_delegated_stake: bool,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();
        substate.accepts_delegated_stake = accept_delegated_stake;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(substate),
        )?;
        api.field_close(handle)?;

        Runtime::emit_event(
            api,
            UpdateAcceptingStakeDelegationStateEvent {
                accepts_delegation: accept_delegated_stake,
            },
        )?;

        Ok(())
    }

    /// Locks the given stake units in an internal "delayed withdrawal" vault (which is the owner's
    /// way of showing their commitment to running this validator in an orderly fashion - see
    /// [`ValidatorSubstate.locked_owner_stake_unit_vault_id`]).
    pub fn lock_owner_stake_units<Y: SystemApi<RuntimeError>>(
        stake_unit_bucket: Bucket,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::read_only(),
        )?;
        let substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        Vault(substate.locked_owner_stake_unit_vault_id).put(stake_unit_bucket, api)?;

        api.field_close(handle)?;
        Ok(())
    }

    /// Starts the process of unlocking the owner's stake units stored in the internal vault.
    /// The requested amount of stake units (if available) will be ready for withdrawal after the
    /// network-configured [`ConsensusManagerConfigSubstate.num_owner_stake_units_unlock_epochs`] via a
    /// call to [`finish_unlock_owner_stake_units()`].
    pub fn start_unlock_owner_stake_units<Y: SystemApi<RuntimeError>>(
        requested_stake_unit_amount: Decimal,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        // read the current epoch (needed for a drive-by "finish unlocking" of available withdrawals)
        let consensus_manager_handle = api.actor_open_field(
            ACTOR_STATE_OUTER_OBJECT,
            ConsensusManagerField::State.into(),
            LockFlags::read_only(),
        )?;
        let consensus_manager = api
            .field_read_typed::<ConsensusManagerStateFieldPayload>(consensus_manager_handle)?
            .fully_update_and_into_latest_version();
        let current_epoch = consensus_manager.epoch;
        api.field_close(consensus_manager_handle)?;

        // read the configured unlock epochs delay
        let config_handle = api.actor_open_field(
            ACTOR_STATE_OUTER_OBJECT,
            ConsensusManagerField::Configuration.into(),
            LockFlags::read_only(),
        )?;
        let config_substate = api
            .field_read_typed::<ConsensusManagerConfigurationFieldPayload>(config_handle)?
            .fully_update_and_into_latest_version();
        api.field_close(config_handle)?;

        // begin the read+modify+write of the validator substate...
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        // - move the already-available withdrawals to a dedicated field
        Self::normalize_available_owner_stake_unit_withdrawals(&mut substate, current_epoch)?;

        // - insert the requested withdrawal as pending
        substate
            .pending_owner_stake_unit_withdrawals
            .entry(
                current_epoch
                    .after(config_substate.config.num_owner_stake_units_unlock_epochs)
                    .ok_or(RuntimeError::ApplicationError(
                        ApplicationError::ValidatorError(ValidatorError::EpochMathOverflow),
                    ))?,
            )
            .and_modify(|pending_amount| {
                *pending_amount = pending_amount
                    .checked_add(requested_stake_unit_amount)
                    .unwrap_or(Decimal::MAX)
            })
            .or_insert(requested_stake_unit_amount);

        // ...end the read+modify+write of the validator substate
        let mut locked_owner_stake_unit_vault = Vault(substate.locked_owner_stake_unit_vault_id);
        let mut pending_owner_stake_unit_unlock_vault =
            Vault(substate.pending_owner_stake_unit_unlock_vault_id);
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(substate),
        )?;

        // move the requested stake units from the "locked vault" to the "pending withdrawal vault"
        let pending_unlock_stake_unit_bucket =
            locked_owner_stake_unit_vault.take(requested_stake_unit_amount, api)?;
        pending_owner_stake_unit_unlock_vault.put(pending_unlock_stake_unit_bucket, api)?;

        api.field_close(handle)?;
        Ok(())
    }

    /// Finishes the process of unlocking the owner's stake units by withdrawing *all* the pending
    /// amounts which have reached their target epoch and thus are already available (potentially
    /// none).
    pub fn finish_unlock_owner_stake_units<Y: SystemApi<RuntimeError>>(
        api: &mut Y,
    ) -> Result<Bucket, RuntimeError> {
        // read the current epoch
        let consensus_manager_handle = api.actor_open_field(
            ACTOR_STATE_OUTER_OBJECT,
            ConsensusManagerField::State.into(),
            LockFlags::read_only(),
        )?;
        let consensus_manager = api
            .field_read_typed::<ConsensusManagerStateFieldPayload>(consensus_manager_handle)?
            .fully_update_and_into_latest_version();
        let current_epoch = consensus_manager.epoch;
        api.field_close(consensus_manager_handle)?;

        // drain the already-available withdrawals
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        Self::normalize_available_owner_stake_unit_withdrawals(&mut substate, current_epoch)?;
        let total_already_available_amount = mem::replace(
            &mut substate.already_unlocked_owner_stake_unit_amount,
            Decimal::zero(),
        );

        let mut pending_owner_stake_unit_unlock_vault =
            Vault(substate.pending_owner_stake_unit_unlock_vault_id);
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(substate),
        )?;

        // return the already-available withdrawals
        let already_available_stake_unit_bucket =
            pending_owner_stake_unit_unlock_vault.take(total_already_available_amount, api)?;

        api.field_close(handle)?;
        Ok(already_available_stake_unit_bucket)
    }

    /// Removes all no-longer-pending owner stake unit withdrawals (i.e. those which have already
    /// reached the given [`current_epoch`]) from [`pending_owner_stake_unit_withdrawals`] into
    /// [`already_unlocked_owner_stake_unit_amount`].
    /// Note: this house-keeping operation prevents the internal collection from growing to a size
    /// which would affect performance (or exceed the substate size limit).
    fn normalize_available_owner_stake_unit_withdrawals(
        substate: &mut ValidatorSubstate,
        current_epoch: Epoch,
    ) -> Result<(), RuntimeError> {
        let available_withdrawal_epochs = substate
            .pending_owner_stake_unit_withdrawals
            .range(..=current_epoch)
            .map(|(epoch, _available_amount)| *epoch)
            .collect::<Vec<_>>();
        for available_withdrawal_epoch in available_withdrawal_epochs {
            // no batch delete in a BTree
            let available_amount = substate
                .pending_owner_stake_unit_withdrawals
                .remove(&available_withdrawal_epoch)
                .expect("key was just returned by the iterator");
            substate.already_unlocked_owner_stake_unit_amount = substate
                .already_unlocked_owner_stake_unit_amount
                .checked_add(available_amount)
                .ok_or(RuntimeError::ApplicationError(
                    ApplicationError::ValidatorError(
                        ValidatorError::UnexpectedDecimalComputationError,
                    ),
                ))?;
        }
        Ok(())
    }

    /// Puts the given bucket into this validator's stake XRD vault, effectively increasing the
    /// value of all its stake units.
    /// Note: the validator's proposal statistics passed to this method are used only for creating
    /// an event (i.e. they are only informational and they do not drive any logic at this point).
    pub fn apply_emission<Y: SystemApi<RuntimeError>>(
        xrd_bucket: Bucket,
        concluded_epoch: Epoch,
        proposals_made: u64,
        proposals_missed: u64,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        // begin the read+modify+write of the validator substate...
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        // - resolve the effective validator fee factor
        let effective_validator_fee_factor = match &substate.validator_fee_change_request {
            Some(request) if request.epoch_effective <= concluded_epoch => request.new_fee_factor,
            _ => substate.validator_fee_factor,
        };

        // - calculate the validator fee and subtract it from the emission bucket
        let total_emission_xrd = xrd_bucket.amount(api)?;
        let validator_fee_xrd = effective_validator_fee_factor
            .checked_mul(total_emission_xrd)
            .ok_or(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(ValidatorError::UnexpectedDecimalComputationError),
            ))?;
        let fee_xrd_bucket = xrd_bucket.take(validator_fee_xrd, api)?;

        // - put the net emission XRDs into the stake pool
        let mut stake_xrd_vault = Vault(substate.stake_xrd_vault_id);
        let starting_stake_pool_xrd = stake_xrd_vault.amount(api)?;
        stake_xrd_vault.put(xrd_bucket, api)?;

        // - stake the validator fee XRDs (effectively same as regular staking)
        let mut stake_unit_resman = ResourceManager(substate.stake_unit_resource);
        let stake_pool_added_xrd = total_emission_xrd.checked_sub(validator_fee_xrd).ok_or(
            RuntimeError::ApplicationError(ApplicationError::ValidatorError(
                ValidatorError::UnexpectedDecimalComputationError,
            )),
        )?;
        let post_emission_stake_pool_xrd = starting_stake_pool_xrd
            .checked_add(stake_pool_added_xrd)
            .ok_or(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(ValidatorError::UnexpectedDecimalComputationError),
            ))?;
        let total_stake_unit_supply = stake_unit_resman.total_supply(api)?.unwrap();
        let stake_unit_mint_amount = Self::calculate_stake_unit_amount(
            validator_fee_xrd,
            post_emission_stake_pool_xrd,
            total_stake_unit_supply,
        )?;
        let fee_stake_unit_bucket = stake_unit_resman.mint_fungible(stake_unit_mint_amount, api)?;
        stake_xrd_vault.put(fee_xrd_bucket, api)?;

        // - immediately lock these new stake units in the internal owner's "public display" vault
        Vault(substate.locked_owner_stake_unit_vault_id).put(fee_stake_unit_bucket.into(), api)?;

        // - update the index, since the stake increased (because of net emission + staking of the validator fee)
        let new_stake_xrd = starting_stake_pool_xrd
            .checked_add(total_emission_xrd)
            .ok_or(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(ValidatorError::UnexpectedDecimalComputationError),
            ))?;
        let new_index_key =
            Self::index_update(&substate, substate.is_registered, new_stake_xrd, api)?;

        // ...end the read+modify+write of the validator substate (event can be emitted afterwards)
        substate.sorted_key = new_index_key;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(substate),
        )?;
        api.field_close(handle)?;

        Runtime::emit_event(
            api,
            ValidatorEmissionAppliedEvent {
                epoch: concluded_epoch,
                starting_stake_pool_xrd,
                stake_pool_added_xrd,
                total_stake_unit_supply,
                validator_fee_xrd,
                proposals_made,
                proposals_missed,
            },
        )?;

        Ok(())
    }

    pub fn apply_reward<Y: SystemApi<RuntimeError>>(
        xrd_bucket: Bucket,
        concluded_epoch: Epoch,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        // begin the read+modify+write of the validator substate...
        let handle = api.actor_open_field(
            ACTOR_STATE_SELF,
            ValidatorField::State.into(),
            LockFlags::MUTABLE,
        )?;
        let mut substate = api
            .field_read_typed::<ValidatorStateFieldPayload>(handle)?
            .fully_update_and_into_latest_version();

        // Get the total reward amount
        let total_reward_xrd = xrd_bucket.amount(api)?;

        // Stake it
        let mut stake_xrd_vault = Vault(substate.stake_xrd_vault_id);
        let starting_stake_pool_xrd = stake_xrd_vault.amount(api)?;
        let mut stake_unit_resman = ResourceManager(substate.stake_unit_resource);
        let total_stake_unit_supply = stake_unit_resman.total_supply(api)?.unwrap();
        let stake_unit_mint_amount = Self::calculate_stake_unit_amount(
            total_reward_xrd,
            starting_stake_pool_xrd,
            total_stake_unit_supply,
        )?;
        let new_stake_unit_bucket = stake_unit_resman.mint_fungible(stake_unit_mint_amount, api)?;
        stake_xrd_vault.put(xrd_bucket, api)?;

        // Lock these new stake units in the internal owner's "public display" vault
        Vault(substate.locked_owner_stake_unit_vault_id).put(new_stake_unit_bucket.into(), api)?;

        // Update the index, since the stake increased (because of staking of the reward)
        let new_stake_xrd = starting_stake_pool_xrd
            .checked_add(total_reward_xrd)
            .ok_or(RuntimeError::ApplicationError(
                ApplicationError::ValidatorError(ValidatorError::UnexpectedDecimalComputationError),
            ))?;
        let new_index_key =
            Self::index_update(&substate, substate.is_registered, new_stake_xrd, api)?;

        // Flush validator substate changes
        substate.sorted_key = new_index_key;
        api.field_write_typed(
            handle,
            &ValidatorStateFieldPayload::from_content_source(substate),
        )?;
        api.field_close(handle)?;

        Runtime::emit_event(
            api,
            ValidatorRewardAppliedEvent {
                epoch: concluded_epoch,
                amount: total_reward_xrd,
            },
        )?;

        Ok(())
    }

    fn to_sorted_key(
        registered: bool,
        stake: Decimal,
        address: ComponentAddress,
    ) -> Result<Option<SortedKey>, RuntimeError> {
        if !registered || stake.is_zero() {
            Ok(None)
        } else {
            Ok(Some((
                create_sort_prefix_from_stake(stake)?,
                scrypto_encode(&address).unwrap(),
            )))
        }
    }

    fn update_validator<Y: SystemApi<RuntimeError>>(
        update: UpdateSecondaryIndex,
        api: &mut Y,
    ) -> Result<(), RuntimeError> {
        match update {
            UpdateSecondaryIndex::Create {
                index_key,
                key,
                stake,
            } => {
                api.actor_sorted_index_insert_typed(
                    ACTOR_STATE_OUTER_OBJECT,
                    ConsensusManagerCollection::RegisteredValidatorByStakeSortedIndex
                        .collection_index(),
                    index_key,
                    ConsensusManagerRegisteredValidatorByStakeEntryPayload::from_content_source(
                        Validator { key, stake },
                    ),
                )?;
            }
            UpdateSecondaryIndex::UpdatePublicKey { index_key, key } => {
                let mut validator = api
                    .actor_sorted_index_remove_typed::<ConsensusManagerRegisteredValidatorByStakeEntryPayload>(
                        ACTOR_STATE_OUTER_OBJECT,
                        ConsensusManagerCollection::RegisteredValidatorByStakeSortedIndex.collection_index(),
                        &index_key,
                    )?
                    .unwrap().fully_update_and_into_latest_version();
                validator.key = key;
                api.actor_sorted_index_insert_typed(
                    ACTOR_STATE_OUTER_OBJECT,
                    ConsensusManagerCollection::RegisteredValidatorByStakeSortedIndex
                        .collection_index(),
                    index_key,
                    ConsensusManagerRegisteredValidatorByStakeEntryPayload::from_content_source(
                        validator,
                    ),
                )?;
            }
            UpdateSecondaryIndex::UpdateStake {
                index_key,
                new_index_key,
                new_stake_amount,
            } => {
                let mut validator = api
                    .actor_sorted_index_remove_typed::<ConsensusManagerRegisteredValidatorByStakeEntryPayload>(
                        ACTOR_STATE_OUTER_OBJECT,
                        ConsensusManagerCollection::RegisteredValidatorByStakeSortedIndex.collection_index(),
                        &index_key,
                    )?
                    .unwrap().fully_update_and_into_latest_version();
                validator.stake = new_stake_amount;
                api.actor_sorted_index_insert_typed(
                    ACTOR_STATE_OUTER_OBJECT,
                    ConsensusManagerCollection::RegisteredValidatorByStakeSortedIndex
                        .collection_index(),
                    new_index_key,
                    ConsensusManagerRegisteredValidatorByStakeEntryPayload::from_content_source(
                        validator,
                    ),
                )?;
            }
            UpdateSecondaryIndex::Remove { index_key } => {
                api.actor_sorted_index_remove(
                    ACTOR_STATE_OUTER_OBJECT,
                    ConsensusManagerCollection::RegisteredValidatorByStakeSortedIndex
                        .collection_index(),
                    &index_key,
                )?;
            }
        }

        Ok(())
    }

    fn calculate_redemption_value<Y: SystemApi<RuntimeError>>(
        amount_of_stake_units: Decimal,
        validator_substate: &ValidatorSubstate,
        api: &mut Y,
    ) -> Result<Decimal, RuntimeError> {
        let stake_vault = Vault(validator_substate.stake_xrd_vault_id);
        let stake_unit_resman = ResourceManager(validator_substate.stake_unit_resource);

        let active_stake_amount = stake_vault.amount(api)?;
        let total_stake_unit_supply = stake_unit_resman.total_supply(api)?.unwrap();
        let xrd_amount = if total_stake_unit_supply.is_zero() {
            Decimal::zero()
        } else {
            active_stake_amount
                .checked_div(total_stake_unit_supply)
                .and_then(|amount| amount_of_stake_units.checked_mul(amount))
                .ok_or(RuntimeError::ApplicationError(
                    ApplicationError::ValidatorError(
                        ValidatorError::UnexpectedDecimalComputationError,
                    ),
                ))?
        };

        Ok(xrd_amount)
    }

    /// Returns an amount of stake units to be minted when [`xrd_amount`] of XRDs is being staked.
    fn calculate_stake_unit_amount(
        xrd_amount: Decimal,
        total_stake_xrd_amount: Decimal,
        total_stake_unit_supply: Decimal,
    ) -> Result<Decimal, RuntimeError> {
        if total_stake_xrd_amount.is_zero() {
            Ok(xrd_amount)
        } else {
            total_stake_unit_supply
                .checked_div(total_stake_xrd_amount)
                .and_then(|amount| xrd_amount.checked_mul(amount))
                .ok_or(RuntimeError::ApplicationError(
                    ApplicationError::ValidatorError(
                        ValidatorError::UnexpectedDecimalComputationError,
                    ),
                ))
        }
    }
}

fn check_validator_fee_factor(fee_factor: Decimal) -> Result<(), RuntimeError> {
    // only allow a proper fraction
    if fee_factor.is_negative() || fee_factor > Decimal::one() {
        return Err(RuntimeError::ApplicationError(
            ApplicationError::ValidatorError(ValidatorError::InvalidValidatorFeeFactor),
        ));
    }
    Ok(())
}

fn create_sort_prefix_from_stake(stake: Decimal) -> Result<[u8; 2], RuntimeError> {
    // Note: XRD max supply is 24bn
    // 24bn / MAX::16 = 366210.9375 - so 100k as a divisor here is sensible.
    // If all available XRD was staked to one validator, they'd have 3.6 * u16::MAX * 100k stake
    // In reality, validators will have far less than u16::MAX * 100k stake, but let's handle that case just in case
    let stake_100k: Decimal = stake
        .checked_div(100000)
        .ok_or(RuntimeError::ApplicationError(
            ApplicationError::ValidatorError(ValidatorError::UnexpectedDecimalComputationError),
        ))?;

    let stake_100k_whole_units = dec!(10)
        .checked_powi(Decimal::SCALE.into())
        .and_then(|power| stake_100k.checked_div(power))
        .ok_or(RuntimeError::ApplicationError(
            ApplicationError::ValidatorError(ValidatorError::UnexpectedDecimalComputationError),
        ))?
        .attos();

    let stake_u16 = if stake_100k_whole_units > I192::from(u16::MAX) {
        u16::MAX
    } else {
        stake_100k_whole_units.try_into().unwrap()
    };
    // We invert the key because we need high stake to appear first and it's ordered ASC
    Ok((u16::MAX - stake_u16).to_be_bytes())
}

struct SecurifiedValidator;

impl SecurifiedRoleAssignment for SecurifiedValidator {
    type OwnerBadgeNonFungibleData = ValidatorOwnerBadgeData;
    const OWNER_BADGE: ResourceAddress = VALIDATOR_OWNER_BADGE;
    const SECURIFY_ROLE: Option<&'static str> = None;
}

pub(crate) struct ValidatorCreator;

impl ValidatorCreator {
    fn create_stake_unit_resource<Y: SystemApi<RuntimeError>>(
        validator_address: GlobalAddress,
        api: &mut Y,
    ) -> Result<ResourceAddress, RuntimeError> {
        let stake_unit_resman = ResourceManager::new_fungible(
            OwnerRole::Fixed(rule!(require(global_caller(validator_address)))),
            true,
            18,
            FungibleResourceRoles {
                mint_roles: mint_roles! {
                    minter => rule!(require(global_caller(validator_address)));
                    minter_updater => rule!(deny_all);
                },
                burn_roles: burn_roles! {
                    burner => rule!(require(global_caller(validator_address)));
                    burner_updater => rule!(deny_all);
                },
                ..Default::default()
            },
            metadata_init! {
                "name" => "Liquid Stake Units".to_owned(), locked;
                "description" => "Liquid Stake Unit tokens that represent a proportion of XRD stake delegated to a Radix Network validator.".to_owned(), locked;
                "icon_url" => UncheckedUrl::of("https://assets.radixdlt.com/icons/icon-liquid_stake_units.png"), locked;
                "validator" => validator_address, locked;
                "tags" => Vec::<String>::new(), locked;
            },
            None,
            api,
        )?;

        Ok(stake_unit_resman.0)
    }

    fn create_claim_nft<Y: SystemApi<RuntimeError>>(
        validator_address: GlobalAddress,
        api: &mut Y,
    ) -> Result<ResourceAddress, RuntimeError> {
        let unstake_resman = ResourceManager::new_non_fungible::<UnstakeData, Y, RuntimeError, _>(
            OwnerRole::Fixed(rule!(require(global_caller(validator_address)))),
            NonFungibleIdType::RUID,
            true,
            NonFungibleResourceRoles {
                mint_roles: mint_roles! {
                    minter => rule!(require(global_caller(validator_address)));
                    minter_updater => rule!(deny_all);
                },
                burn_roles: burn_roles! {
                    burner => rule!(require(global_caller(validator_address)));
                    burner_updater => rule!(deny_all);
                },
                ..Default::default()
            },
            metadata_init! {
                "name" => "Stake Claims NFTs".to_owned(), locked;
                "description" => "Unique Stake Claim tokens that represent a timed claimable amount of XRD stake from a Radix Network validator.".to_owned(), locked;
                "icon_url" => UncheckedUrl::of("https://assets.radixdlt.com/icons/icon-stake_claim_NFTs.png"), locked;
                "validator" => validator_address, locked;
                "tags" => Vec::<String>::new(), locked;
            },
            None,
            api,
        )?;

        Ok(unstake_resman.0)
    }

    pub fn create<Y: SystemApi<RuntimeError>>(
        key: Secp256k1PublicKey,
        is_registered: bool,
        fee_factor: Decimal,
        api: &mut Y,
    ) -> Result<(ComponentAddress, Bucket), RuntimeError> {
        // check if validator fee is valid
        check_validator_fee_factor(fee_factor)?;

        let (address_reservation, validator_address) =
            api.allocate_global_address(BlueprintId {
                package_address: CONSENSUS_MANAGER_PACKAGE,
                blueprint_name: VALIDATOR_BLUEPRINT.to_string(),
            })?;

        let stake_xrd_vault = Vault::create(XRD, api)?;
        let pending_xrd_withdraw_vault = Vault::create(XRD, api)?;
        let claim_nft = Self::create_claim_nft(validator_address, api)?;
        let stake_unit_resource = Self::create_stake_unit_resource(validator_address, api)?;
        let locked_owner_stake_unit_vault = Vault::create(stake_unit_resource, api)?;
        let pending_owner_stake_unit_unlock_vault = Vault::create(stake_unit_resource, api)?;
        let pending_owner_stake_unit_withdrawals = BTreeMap::new();

        let substate = ValidatorSubstate {
            sorted_key: None,
            key,
            is_registered,
            accepts_delegated_stake: false,
            validator_fee_factor: fee_factor,
            validator_fee_change_request: None,
            stake_unit_resource,
            claim_nft,
            stake_xrd_vault_id: stake_xrd_vault.0,
            pending_xrd_withdraw_vault_id: pending_xrd_withdraw_vault.0,
            locked_owner_stake_unit_vault_id: locked_owner_stake_unit_vault.0,
            pending_owner_stake_unit_unlock_vault_id: pending_owner_stake_unit_unlock_vault.0,
            pending_owner_stake_unit_withdrawals,
            already_unlocked_owner_stake_unit_amount: Decimal::zero(),
        };

        let protocol_update_readiness_signal = ValidatorProtocolUpdateReadinessSignalSubstate {
            protocol_version_name: None,
        };

        let validator_id = api.new_simple_object(
            VALIDATOR_BLUEPRINT,
            indexmap! {
                ValidatorField::State.field_index() => FieldValue::new(ValidatorStateFieldPayload::from_content_source(substate)),
                ValidatorField::ProtocolUpdateReadinessSignal.field_index() => FieldValue::new(ValidatorProtocolUpdateReadinessSignalFieldPayload::from_content_source(protocol_update_readiness_signal)),
            },
        )?;

        let (role_assignment, owner_token_bucket) = SecurifiedValidator::create_securified(
            ValidatorOwnerBadgeData {
                name: "Validator Owner Badge".to_owned(),
                validator: validator_address.try_into().expect("Impossible Case!"),
            },
            Some(NonFungibleLocalId::bytes(validator_address.as_node_id().0).unwrap()),
            api,
        )?;
        let owner_badge_local_id = owner_token_bucket
            .non_fungible_local_ids(api)?
            .first()
            .expect("Impossible Case")
            .clone();
        let metadata = Metadata::create_with_data(
            metadata_init! {
                "owner_badge" => owner_badge_local_id, locked;
                "pool_unit" => GlobalAddress::from(stake_unit_resource), locked;
                "claim_nft" => GlobalAddress::from(claim_nft), locked;
            },
            api,
        )?;

        api.globalize(
            validator_id,
            indexmap!(
                AttachedModuleId::RoleAssignment => role_assignment.0.0,
                AttachedModuleId::Metadata => metadata.0,
            ),
            Some(address_reservation),
        )?;

        Ok((
            ComponentAddress::new_or_panic(validator_address.into()),
            owner_token_bucket,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sort_key_is_calculated_correctly() {
        assert_eq!(
            create_sort_prefix_from_stake(Decimal::ZERO).unwrap(),
            u16::MAX.to_be_bytes()
        );
        assert_eq!(
            create_sort_prefix_from_stake(dec!(99_999)).unwrap(),
            u16::MAX.to_be_bytes()
        );
        assert_eq!(
            create_sort_prefix_from_stake(dec!(100_000)).unwrap(),
            (u16::MAX - 1).to_be_bytes()
        );
        assert_eq!(
            create_sort_prefix_from_stake(dec!(199_999)).unwrap(),
            (u16::MAX - 1).to_be_bytes()
        );
        assert_eq!(
            create_sort_prefix_from_stake(dec!(200_000)).unwrap(),
            (u16::MAX - 2).to_be_bytes()
        );
        // https://learn.radixdlt.com/article/start-here-radix-tokens-and-tokenomics
        let max_xrd_supply = dec!(24)
            .checked_mul(dec!(10).checked_powi(12).unwrap())
            .unwrap();
        assert_eq!(
            create_sort_prefix_from_stake(max_xrd_supply).unwrap(),
            0u16.to_be_bytes()
        );
    }
}

#[derive(ScryptoSbor)]
pub struct ValidatorOwnerBadgeData {
    pub name: String,
    pub validator: ComponentAddress,
}

impl NonFungibleData for ValidatorOwnerBadgeData {
    const MUTABLE_FIELDS: &'static [&'static str] = &[];
}