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
/// Generates types and typed-interfaces for native blueprints, their
/// state models, features, and schemas.
///
/// See the below structure for detail on how it should look - or check
/// out [../package/substates.rs](the package substates definition).
///
/// For each field, the following types will be created:
/// * `<BlueprintIdent><FieldIdent>FieldPayload` - a transparent new type for the field content
/// * `<BlueprintIdent><FieldIdent>FieldSubstate` - a type for the full system-wrapped substate
///
/// For each collection value, the following types will be created:
/// * `<BlueprintIdent><CollectionIdent>EntryPayload` - a transparent new type for the entry content
/// * `<BlueprintIdent><CollectionIdent>EntrySubstate` - a type for the full system-wrapped substate
///
/// For each collection key, the following types will be created:
/// * `<BlueprintIdent><CollectionIdent>KeyPayload` - a new type for the key payload (eg includes the u16 for a sorted index key)
///
/// The content of each of the above can take a number of forms. This is configured via specifying the type as one of the following.
/// Only Static is supported for keys at present. By default, you should choose StaticSingleVersioned for fields and collection values.
/// ```ignore
///     {
///         kind: StaticSingleVersioned,
///     }
///     {
///         kind: Static,
///         content_type: x,
///     },
///     {
///         kind: Generic,
///         ident: BlueprintGenericParameterIdent,
///     },
///     {
///         kind: StaticMultiVersioned,
///         previous_versions: {
///             1 => { updates_to: 2 },
///             2 => { updates_to: 3 },
///         },
///         latest_version: 3,
///     }
/// ```
///
/// Choosing `StaticSingleVersioned`, which will create a forward-compatible enum wrapper with a single version for the content.
///
/// For Fields, it will assume the existence of a type called
/// `<BlueprintIdent><FieldIdent>V1` and will generate the following types:
/// * `<BlueprintIdent><FieldIdent>` - a type alias for the latest version (V1).
/// * `Versioned<BlueprintIdent><FieldIdent>` - the enum wrapper with a single version. This will be the content of `<BlueprintIdent><FieldIdent>FieldPayload`.
///
/// For collection values, it will assume the existence of `<BlueprintIdent><CollectionIdent>V1` and generate the following types:
/// * `<BlueprintIdent><CollectionIdent>` - a type alias for the latest version (V1).
/// * `Versioned<BlueprintIdent><CollectionIdent>` - the enum wrapper with a single version. This will be the content of `<BlueprintIdent><CollectionIdent>EntryPayload`.
///
/// Choosing `StaticMultiVersioned` will create the same types, but the type alias will be for the latest version.
#[allow(unused)]
macro_rules! declare_native_blueprint_state {
    (
        blueprint_ident: $blueprint_ident:ident,
        blueprint_snake_case: $blueprint_property_name:ident,
        $(
            outer_blueprint: {
                ident: $outer_blueprint_ident:ident
                $(,)?
            },
        )?
        $(
            generics: {
                $(
                    $generic_property_name:ident: {
                        ident: $generic_ident:ident,
                        description: $generic_description:expr
                        $(,)?
                    }
                ),*
                $(,)?
            },
        )?
        $(
            features: {
                $(
                    $feature_property_name:ident: {
                        ident: $feature_ident:ident,
                        description: $feature_description:expr,
                    }
                ),*
                $(,)?
            },
        )?
        fields: {
            $(
                $field_property_name:ident: {
                    ident: $field_ident:ident,
                    field_type: $field_type:tt
                    $(, condition: $field_condition:expr)?
                    $(, transience: $field_transience:expr)?
                    $(,)? // Optional trailing comma
                }
            ),*
            $(,)? // Optional trailing comma
        },
        collections: {
            $(
                $collection_property_name:ident: $collection_type:ident {
                    entry_ident: $collection_ident:ident,
                    $(mapped_physical_partition: $mapped_physical_partition:expr,)?
                    key_type: $collection_key_type:tt,
                    // The full_key_content is required if it's a sorted index
                    $(full_key_content: $full_key_content:tt,)?
                    value_type: $collection_value_type:tt,
                    allow_ownership: $collection_allow_ownership:expr
                    $(,)? // Optional trailing comma
                }
            ),*
            $(,)? // Optional trailing comma
        }
        $(,)?
    ) => {
        paste::paste! {
            pub use [<$blueprint_property_name _models>]::*;

            #[allow(unused_imports, dead_code, unused_mut, unused_assignments, unused_variables, unreachable_code)]
            mod [<$blueprint_property_name _models>] {
                use super::*;
                use sbor::*;
                use $crate::internal_prelude::*;
                use $crate::track::interface::*;
                use $crate::errors::*;
                use $crate::system::system::*;
                use radix_engine_interface::api::*;
                //--------------------------------------------------------
                // MODELS
                //--------------------------------------------------------

                // Generate models for each field
                $(
                    // Value
                    // > Set up Versioned types (if relevant). Assumes __FieldV1 exists and then creates
                    //   - Versioned__Field
                    //   - __Field (alias for __FieldV1)
                    // > Set up the (transparent) _FieldPayload new type for the content of the field
                    // > Set up the FieldContent trait for anything which can be resolved into the field payload
                    generate_content_type!(
                        content_trait: FieldContentSource,
                        payload_trait: FieldPayload,
                        ident_core: [<$blueprint_ident $field_ident>],
                        #[derive(Debug, PartialEq, Eq, ScryptoSbor)]
                        struct [<$blueprint_ident $field_ident FieldPayload>] = $field_type
                    );

                    // > Set up the _FieldSubstate alias for the system-wrapped substate
                    generate_system_substate_type_alias!(
                        Field,
                        type [<$blueprint_ident $field_ident FieldSubstate>] = WRAPPED [<$blueprint_ident $field_ident FieldPayload>]
                    );
                )*

                // Generate models for each collection
                $(
                    // Key
                    generate_key_type!(
                        content_trait: [<$collection_type KeyContentSource>],
                        payload_trait: [<$collection_type KeyPayload>],
                        $(full_key_content: $full_key_content,)?
                        #[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, ScryptoSbor)]
                        struct [<$blueprint_ident $collection_ident KeyPayload>] = $collection_key_type
                    );
                    pub type [<$blueprint_ident $collection_ident KeyContent>] = <[<$blueprint_ident $collection_ident KeyPayload>] as [<$collection_type KeyPayload>]>::Content;

                    // Values
                    // > If relevant, set up Versioned types, which:
                    //   - Assumes [BlueprintCollection]V1 exists
                    //   - Creates Versioned[BlueprintCollection] enum
                    //   - Creates [BlueprintCollection] as a "latest" type alias for [BlueprintCollection]V1
                    // > Set up the [BlueprintCollection]EntryPayload transparent new type for the value content
                    // > Set up the [Collectiontype]EntryContent::<[BlueprintCollection]EntryPayload> trait for:
                    //   - The [BlueprintCollection] if it exists
                    //   - The Versioned[BlueprintCollection] if it exists
                    //   - The static content type, if it exists
                    generate_content_type!(
                        content_trait: [<$collection_type EntryContentSource>],
                        payload_trait: [<$collection_type EntryPayload>],
                        ident_core: [<$blueprint_ident $collection_ident>],
                        #[derive(Debug, PartialEq, Eq, ScryptoSbor)]
                        struct [<$blueprint_ident $collection_ident EntryPayload>] = $collection_value_type
                    );
                    // > Set up the _EntrySubstate alias for the system-wrapped substate
                    generate_system_substate_type_alias!(
                        $collection_type,
                        type [<$blueprint_ident $collection_ident EntrySubstate>] = WRAPPED [<$blueprint_ident $collection_ident EntryPayload>]
                    );
                )*

                //-------------------------------------
                // System - Generate schema definitions
                //-------------------------------------
                pub struct [<$blueprint_ident StateSchemaInit>];

                impl [<$blueprint_ident StateSchemaInit>] {
                    pub fn create_schema_init(
                        type_aggregator: &mut TypeAggregator<ScryptoCustomTypeKind>,
                    ) -> BlueprintStateSchemaInit {
                        let mut fields = vec![];
                        $(
                            fields.push(FieldSchema {
                                field: map_type_ref!(
                                    $blueprint_ident,
                                    type_aggregator,
                                    $field_type,
                                    [<$blueprint_ident $field_ident FieldPayload>],
                                ),
                                condition: optional_or_fallback!($({ $field_condition })?, { Condition::Always }),
                                transience: optional_or_fallback!($({ $field_transience})?, { FieldTransience::NotTransient }),
                            });
                        )*
                        let collections = vec![
                            $(
                                map_collection_schema!(
                                    $collection_type,
                                    $blueprint_ident,
                                    type_aggregator,
                                    $collection_key_type,
                                    [<$blueprint_ident $collection_ident KeyContent>],
                                    $collection_value_type,
                                    [<$blueprint_ident $collection_ident EntryPayload>],
                                    $collection_allow_ownership
                                )
                            ),*
                        ];
                        BlueprintStateSchemaInit {
                            fields,
                            collections,
                        }
                    }
                }

                //--------------------------------------------------------
                // System - Fields, Collections, Features and Generics
                //--------------------------------------------------------
                if_exists!(
                    TEST: [[$($field_ident)*]],
                    // Avoid https://doc.rust-lang.org/error_codes/E0084.html if no fields exist
                    [[
                        #[repr(u8)]
                        #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord, FromRepr)]
                        pub enum [<$blueprint_ident Field>] {
                            $($field_ident,)*
                        }

                        impl [<$blueprint_ident Field>] {
                            pub const fn field_index(&self) -> u8 {
                                *self as u8
                            }
                        }

                        impl From<[<$blueprint_ident Field>]> for SubstateKey {
                            fn from(value: [<$blueprint_ident Field>]) -> Self {
                                SubstateKey::Field(value as u8)
                            }
                        }

                        impl From<[<$blueprint_ident Field>]> for u8 {
                            fn from(value: [<$blueprint_ident Field>]) -> Self {
                                value as u8
                            }
                        }

                        impl TryFrom<&SubstateKey> for [<$blueprint_ident Field>] {
                            type Error = ();

                            fn try_from(key: &SubstateKey) -> Result<Self, Self::Error> {
                                match key {
                                    SubstateKey::Field(x) => Self::from_repr(*x).ok_or(()),
                                    _ => Err(()),
                                }
                            }
                        }

                        impl TryFrom<u8> for [<$blueprint_ident Field>] {
                            type Error = ();

                            fn try_from(offset: u8) -> Result<Self, Self::Error> {
                                Self::from_repr(offset).ok_or(())
                            }
                        }

                        impl FieldDescriptor for [<$blueprint_ident Field>] {
                            fn field_index(&self) -> FieldIndex {
                                *self as u8
                            }
                        }
                    ]],
                    [[
                        #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord)]
                        pub enum [<$blueprint_ident Field>] {}

                        impl FieldDescriptor for [<$blueprint_ident Field>] {
                            fn field_index(&self) -> FieldIndex {
                                unreachable!("No fields exist")
                            }
                        }
                    ]],
                );

                if_exists!(
                    TEST: [[$($collection_ident)*]],
                    [[
                        #[repr(u8)]
                        #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord, FromRepr)]
                        #[allow(clippy::enum_variant_names)]
                        pub enum [<$blueprint_ident Collection>] {
                            $([<$collection_ident $collection_type>],)*
                        }

                        impl TryFrom<u8> for [<$blueprint_ident Collection>] {
                            type Error = ();

                            fn try_from(offset: u8) -> Result<Self, Self::Error> {
                                Self::from_repr(offset).ok_or(())
                            }
                        }

                        impl CollectionDescriptor for [<$blueprint_ident Collection>] {
                            fn collection_index(&self) -> CollectionIndex {
                                *self as u8
                            }
                        }
                    ]],
                    [[
                        #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord)]
                        pub enum [<$blueprint_ident Collection>] {}

                        impl CollectionDescriptor for [<$blueprint_ident Collection>] {
                            fn collection_index(&self) -> CollectionIndex {
                                unreachable!("No collections exist")
                            }
                        }
                    ]]
                );

                $(
                    #[repr(u8)]
                    #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash, FromRepr)]
                    pub enum [<$blueprint_ident Generic>] {
                        $($generic_ident,)*
                    }

                    impl [<$blueprint_ident Generic>] {
                        pub const fn generic_index(&self) -> u8 {
                            *self as u8
                        }
                    }
                )?

                #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash)]
                pub enum [<$blueprint_ident Feature>] {
                    $($($feature_ident,)*)?
                }

                impl BlueprintFeature for [<$blueprint_ident Feature>] {
                    fn feature_name(&self) -> &'static str {
                        if_exists!(
                            TEST: [[$($($feature_ident)*)?]],
                            [[
                                match *self {
                                    $($(
                                        Self::$feature_ident => stringify!($feature_property_name),
                                    )*)?
                                }
                            ]],
                            [[
                                unreachable!("No features exist")
                            ]]
                        )
                    }
                }

                #[derive(Debug, Clone, Copy, Sbor, PartialEq, Eq, Hash, Default)]
                pub struct [<$blueprint_ident FeatureSet>] {
                    $($(pub [<$feature_property_name>]: bool,)*)?
                }

                impl [<$blueprint_ident FeatureSet>] {
                    pub fn all_features() -> IndexSet<String> {
                        let mut features = index_set_new();
                        $($(
                            features.insert(
                                [<$blueprint_ident Feature>]::$feature_ident.feature_name().to_string()
                            );
                        )*)?
                        features
                    }
                }

                impl HasFeatures for [<$blueprint_ident FeatureSet>] {
                    fn feature_names_str(&self) -> Vec<&'static str> {
                        let mut names = vec![];
                        $($(
                            if self.[<$feature_property_name>] {
                                names.push([<$blueprint_ident Feature>]::$feature_ident.feature_name());
                            }
                        )*)?
                        names
                    }
                }

                //---------------------------------
                // Typed - Substate Keys and Values
                //---------------------------------

                if_exists!(
                    TEST: [[$($field_ident)*]],
                    [[
                         enum_filter_out_ignored!(
                            /// All the SubstateKeys for all logical partitions for the $blueprint_ident blueprint.
                            /// Does not include mapped partitions, as these substates are mapped via their canonical partition.
                            #[derive(Debug, Clone)]
                            #[allow(clippy::large_enum_variant)]
                            pub enum [<$blueprint_ident TypedSubstateKey>]
                            {
                                [[
                                    Field([<$blueprint_ident Field>])
                                ]],
                                $(
                                    $(|IGNORE_ENTRY| { $mapped_physical_partition })?
                                    [[
                                        [<$collection_ident $collection_type Entry>]([<$blueprint_ident $collection_ident KeyPayload>])
                                    ]],
                                )*
                            }
                        );
                    ]],
                    [[
                        enum_filter_out_ignored!(
                            /// All the SubstateKeys for all logical partitions for the $blueprint_ident blueprint.
                            /// Does not include mapped partitions, as these substates are mapped via their canonical partition.
                            #[derive(Debug, Clone)]
                            #[allow(clippy::large_enum_variant)]
                            pub enum [<$blueprint_ident TypedSubstateKey>]
                            {
                                $(
                                    $(|IGNORE_ENTRY| { $mapped_physical_partition })?
                                    [[
                                        [<$collection_ident $collection_type Entry>]([<$blueprint_ident $collection_ident KeyPayload>])
                                    ]],
                                )*
                            }
                        );
                    ]]
                );



                impl [<$blueprint_ident TypedSubstateKey>] {
                    #[allow(clippy::result_unit_err)]
                    pub fn for_key_at_partition_offset(partition_offset: PartitionOffset, substate_key: &SubstateKey) -> Result<Self, ()> {
                        Self::for_key_in_partition(
                            &[<$blueprint_ident PartitionOffset>]::try_from(partition_offset)?,
                            substate_key,
                        )
                    }

                    if_exists!(
                        TEST: [[$($field_ident)*]],
                        [[
                            #[allow(clippy::result_unit_err)]
                            pub fn for_key_in_partition(partition: &[<$blueprint_ident PartitionOffset>], substate_key: &SubstateKey) -> Result<Self, ()> {
                                let key = match_filter_out_ignored!(match partition {
                                    [[
                                        [<$blueprint_ident PartitionOffset>]::Field => {
                                            [<$blueprint_ident TypedSubstateKey>]::Field(
                                                [<$blueprint_ident Field>]::try_from(substate_key)?
                                            )
                                        }
                                    ]],
                                    $(
                                        $(|IGNORE_ENTRY| { $mapped_physical_partition })?
                                        [[
                                            [<$blueprint_ident PartitionOffset>]::[<$collection_ident $collection_type>] => {
                                                [<$blueprint_ident TypedSubstateKey>]::[<$collection_ident $collection_type Entry>](
                                                    [<$blueprint_ident $collection_ident KeyPayload>]::try_from(substate_key)?,
                                                )
                                            }
                                        ]],
                                    )*
                                });
                                Ok(key)
                            }
                        ]],
                        [[
                            #[allow(clippy::result_unit_err)]
                            pub fn for_key_in_partition(partition: &[<$blueprint_ident PartitionOffset>], substate_key: &SubstateKey) -> Result<Self, ()> {
                                let key = match_filter_out_ignored!(match partition {
                                    $(
                                        $(|IGNORE_ENTRY| { $mapped_physical_partition })?
                                        [[
                                            [<$blueprint_ident PartitionOffset>]::[<$collection_ident $collection_type>] => {
                                                [<$blueprint_ident TypedSubstateKey>]::[<$collection_ident $collection_type Entry>](
                                                    [<$blueprint_ident $collection_ident KeyPayload>]::try_from(substate_key)?,
                                                )
                                            }
                                        ]],
                                    )*
                                });
                                Ok(key)
                            }
                        ]]
                    );
                }

                #[derive(Debug)]
                #[allow(clippy::large_enum_variant)]
                pub enum [<$blueprint_ident TypedFieldSubstateValue>] {
                    $($field_ident([<$blueprint_ident $field_ident FieldSubstate>]),)*
                }

                enum_filter_out_ignored!(
                    /// All the Substate values for all logical partitions for the $blueprint_ident blueprint.
                    /// Does not include mapped partitions, as these substates are mapped via their canonical partition.
                    #[derive(Debug)]
                    #[allow(clippy::large_enum_variant)]
                    pub enum [<$blueprint_ident TypedSubstateValue>]
                    {
                        [[
                            Field([<$blueprint_ident TypedFieldSubstateValue>])
                        ]],
                        $(
                            $(|IGNORE_ENTRY| { $mapped_physical_partition })?
                            [[
                                [<$collection_ident $collection_type>]([<$blueprint_ident $collection_ident EntrySubstate>])
                            ]],
                        )*
                    }
                );

                impl [<$blueprint_ident TypedSubstateValue>] {
                    pub fn from_key_and_data(key: &[<$blueprint_ident TypedSubstateKey>], data: &[u8]) -> Result<Self, DecodeError> {
                        let substate_value = match_filter_out_ignored!(match key {
                            $(
                                [[
                                    [<$blueprint_ident TypedSubstateKey>]::Field([<$blueprint_ident Field>]::$field_ident) => {
                                        [<$blueprint_ident TypedSubstateValue>]::Field(
                                            [<$blueprint_ident TypedFieldSubstateValue>]::$field_ident(scrypto_decode(data)?)
                                        )
                                    }
                                ]],
                            )*
                            $(
                                $(|IGNORE_ENTRY| { $mapped_physical_partition })?
                                [[
                                    [<$blueprint_ident TypedSubstateKey>]::[<$collection_ident $collection_type Entry>](_) => {
                                        [<$blueprint_ident TypedSubstateValue>]::[<$collection_ident $collection_type>](
                                            scrypto_decode(data)?
                                        )
                                    }
                                ]],
                            )*
                        });
                        Ok(substate_value)
                    }
                }
            }
        }
    }
}

#[allow(unused)]
pub(crate) use declare_native_blueprint_state;

pub(crate) use helper_macros::*;

#[allow(unused_macros)]
mod helper_macros {
    macro_rules! ignore_arg {
        ($($ignored:tt)*) => {};
    }
    #[allow(unused)]
    pub(crate) use ignore_arg;

    macro_rules! generate_content_type {
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            ident_core: $ident_core:ident,
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: StaticSingleVersioned
                $(,)?
            }$(,)?
        ) => {
            paste::paste! {
                sbor::define_single_versioned!(
                    $(#[$attributes])*
                    pub [<Versioned $ident_core>]([<$ident_core Versions>]) => $ident_core = [<$ident_core V1>]
                );
                declare_payload_new_type!(
                    content_trait: $content_trait,
                    payload_trait: $payload_trait,
                    ----
                    $(#[$attributes])*
                    pub struct $payload_type_name([<Versioned $ident_core>]);
                );

                impl $payload_type_name
                {
                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::fully_update`]."]
                    pub fn fully_update(self) -> Self {
                        Self::of(self.content.fully_update())
                    }

                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::fully_update_and_into_latest_version`]."]
                    pub fn fully_update_and_into_latest_version(self) -> $ident_core {
                        self.content.fully_update_and_into_latest_version()
                    }

                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::from_latest_version`]."]
                    pub fn from_latest_version(latest_version: $ident_core) -> Self {
                        [<Versioned $ident_core>]::from_latest_version(latest_version).into()
                    }

                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::into_unique_version`]."]
                    pub fn into_unique_version(self) -> $ident_core {
                        self.content.into_unique_version()
                    }

                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::from_unique_version`]."]
                    pub fn from_unique_version(unique_version: $ident_core) -> Self {
                        [<Versioned $ident_core>]::from_unique_version(unique_version).into()
                    }
                }

                // Now implement the Content trait for other relevant content types that don't already have it:
                // > The internal enum: [<$ident_core Versions>]
                // > The latest (and unique) version: $ident_core = [<$ident_core V $latest_version_num>]
                impl $content_trait<$payload_type_name> for [<$ident_core Versions>] {
                    fn into_content(self) -> [<Versioned $ident_core>] {
                        [<Versioned $ident_core>]::from(self).into()
                    }
                }

                impl $content_trait<$payload_type_name> for $ident_core {
                    fn into_content(self) -> [<Versioned $ident_core>] {
                        self.into()
                    }
                }
            }
        };
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            ident_core: $ident_core:ident,
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: StaticMultiVersioned,
                previous_versions: [
                    $($version_num:expr => { updates_to: $update_to_version_num:expr }),*
                    $(,)? // Optional trailing comma
                ],
                latest_version: $latest_version_num:expr
                $(,)?
            }$(,)?
        ) => {
            paste::paste! {
                sbor::define_versioned!(
                    $(#[$attributes])*
                    pub [<Versioned $ident_core>]([<$ident_core Versions>]) {
                        previous_versions: [
                            $($version_num => [<$ident_core V $version_num>]: { updates_to: $update_to_version_num }),*
                        ],
                        latest_version: {
                            $latest_version_num => $ident_core = [<$ident_core V $latest_version_num>]
                        }
                    }
                );
                declare_payload_new_type!(
                    content_trait: $content_trait,
                    payload_trait: $payload_trait,
                    ----
                    $(#[$attributes])*
                    pub struct $payload_type_name([<Versioned $ident_core>]);
                );

                // NOTE: If updating here, also update StaticSingleVersioned
                impl $payload_type_name
                {
                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::fully_update`]."]
                    pub fn fully_update(self) -> Self {
                        Self::of(self.content.fully_update())
                    }

                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::fully_update_and_into_latest_version`]."]
                    pub fn fully_update_and_into_latest_version(self) -> $ident_core {
                        self.content.fully_update_and_into_latest_version()
                    }

                    #[doc = "Delegates to [`"[<Versioned $ident_core>]"::from_latest_version`]."]
                    pub fn from_latest_version(latest_version: $ident_core) -> Self {
                        [<Versioned $ident_core>]::from_latest_version(latest_version).into()
                    }
                }

                // Now implement the Content trait for other relevant content types that don't already have it:
                // > The internal enum: [<$ident_core Versions>]
                // > Each previous version: [<$ident_core V $version_num>]
                // > The latest version: $ident_core = [<$ident_core V $latest_version_num>]
                impl $content_trait<$payload_type_name> for [<$ident_core Versions>] {
                    fn into_content(self) -> [<Versioned $ident_core>] {
                        [<Versioned $ident_core>]::from(self).into()
                    }
                }

                $(
                    impl $content_trait<$payload_type_name> for [<$ident_core V $version_num>] {
                        fn into_content(self) -> [<Versioned $ident_core>] {
                            self.into()
                        }
                    }
                )*

                impl $content_trait<$payload_type_name> for $ident_core {
                    fn into_content(self) -> [<Versioned $ident_core>] {
                        self.into()
                    }
                }
            }
        };
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            ident_core: $ident_core:ident,
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: Static,
                content_type: $static_type:ty
                $(,)?
            }$(,)?
        ) => {
            paste::paste! {
                declare_payload_new_type!(
                    content_trait: $content_trait,
                    payload_trait: $payload_trait,
                    ----
                    $(#[$attributes])*
                    pub struct $payload_type_name($static_type);
                );
            }
        };
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            ident_core: $ident_core:ident,
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: Generic,
                ident: $generic_ident:ident
                $(,)?
            }
        ) => {
            paste::paste! {
                declare_payload_new_type!(
                    content_trait: $content_trait,
                    payload_trait: $payload_trait,
                    ----
                    $(#[$attributes])*
                    pub struct $payload_type_name<$generic_ident: [<$ident_core ContentMarker>] = ScryptoValue>($generic_ident);
                );
                // We choose to create an explicit marker trait, as an alternative to a blanket impl
                // over ScryptoEncode + ScryptoDecode. Any explicit types can implement this trait.
                // This avoids every type getting implementations for every such generic type,
                // which would require disambiguation everywhere `to_substate()` is used.
                // Anyone needing a type implementing content can use the payload type itself
                pub trait [<$ident_core ContentMarker>]: ScryptoEncode + ScryptoDecode {}
                impl [<$ident_core ContentMarker>] for ScryptoValue {}
                impl [<$ident_core ContentMarker>] for ScryptoRawValue<'_> {}
            }
        };
    }

    #[allow(unused)]
    pub(crate) use generate_content_type;

    macro_rules! generate_key_type {
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            $(full_key_content: $full_key_content:tt,)?
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: StaticSingleVersioned
                $(,)?
            }$(,)?
        ) => {
            compile_error!(
                "A StaticSingleVersioned key is not supported, because keys cannot be lazily updated, because they need to be static"
            );
        };
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            $(full_key_content: $full_key_content:tt,)?
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: Static,
                content_type: $static_type:ty
                $(,)?
            }$(,)?
        ) => {
            paste::paste! {
                declare_key_new_type!(
                    content_trait: $content_trait,
                    payload_trait: $payload_trait,
                    $(full_key_content: $full_key_content,)?
                    ----
                    $(#[$attributes])*
                    pub struct $payload_type_name($static_type);
                );
            }
        };
        (
            content_trait: $content_trait:ident,
            payload_trait: $payload_trait:ident,
            $(full_key_content: $full_key_content:tt,)?
            $(#[$attributes:meta])*
            struct $payload_type_name:ident = {
                kind: Generic,
                ident: $generic_ident:ident
                $(,)?
            }
        ) => {
            paste::paste! {
                compile_error!(
                    "A Generic key is not currently supported by these macros"
                );
            }
        };
    }

    #[allow(unused)]
    pub(crate) use generate_key_type;

    macro_rules! generate_system_substate_type_alias {
        (SystemField, type $alias:ident = WRAPPED $content:ty$(,)?) => {
            // There is no system wrapper around SystemField substates
            pub type $alias = $content;
        };
        (Field, type $alias:ident = WRAPPED $content:ty$(,)?) => {
            pub type $alias = $crate::system::system_substates::FieldSubstate<$content>;
        };
        (KeyValue, type $alias:ident = WRAPPED $content:ty$(,)?) => {
            pub type $alias = KeyValueEntrySubstate<$content>;
        };
        (Index, type $alias:ident = WRAPPED $content:ty$(,)?) => {
            // There is no system wrapper around Index substates
            pub type $alias = IndexEntrySubstate<$content>;
        };
        (SortedIndex, type $alias:ident = WRAPPED $content:ty$(,)?) => {
            // There is no system wrapper around SortedIndex substates
            pub type $alias = SortedIndexEntrySubstate<$content>;
        };
        ($unknown_system_substate_type:ident, type $alias:ident = WRAPPED $content:ty$(,)?) => {
            compile_error!(concat!(
                "Unrecognized system substate type: `",
                stringify!($unknown_system_substate_type),
                "` - expected `Field`, `SystemField`, `KeyValue`, `Index` or `SortedIndex`"
            ));
        };
    }

    #[allow(unused)]
    pub(crate) use generate_system_substate_type_alias;

    macro_rules! map_collection_schema {
        (KeyValue, $blueprint_ident:ident, $aggregator:ident, $key_type:tt, $key_payload_alias:ident, $value_type:tt, $value_payload_alias:ident, $allow_ownership:expr$(,)?) => {
            BlueprintCollectionSchema::KeyValueStore(BlueprintKeyValueSchema {
                key: map_type_ref!($blueprint_ident, $aggregator, $key_type, $key_payload_alias),
                value: map_type_ref!(
                    $blueprint_ident,
                    $aggregator,
                    $value_type,
                    $value_payload_alias
                ),
                allow_ownership: $allow_ownership,
            })
        };
        (Index, $blueprint_ident:ident, $aggregator:ident, $key_type:tt, $key_payload_alias:ident, $value_type:tt, $value_payload_alias:ident, $allow_ownership:expr$(,)?) => {
            BlueprintCollectionSchema::Index(BlueprintKeyValueSchema {
                key: map_type_ref!($blueprint_ident, $aggregator, $key_type, $key_payload_alias),
                value: map_type_ref!(
                    $blueprint_ident,
                    $aggregator,
                    $value_type,
                    $value_payload_alias
                ),
                allow_ownership: $allow_ownership,
            })
        };
        (SortedIndex, $blueprint_ident:ident, $aggregator:ident, $key_type:tt, $key_payload_alias:ident, $value_type:tt, $value_payload_alias:ident, $allow_ownership:expr$(,)?) => {
            BlueprintCollectionSchema::SortedIndex(BlueprintKeyValueSchema {
                key: map_type_ref!($blueprint_ident, $aggregator, $key_type, $key_payload_alias),
                value: map_type_ref!(
                    $blueprint_ident,
                    $aggregator,
                    $value_type,
                    $value_payload_alias
                ),
                allow_ownership: $allow_ownership,
            })
        };
        ($unknown_system_substate_type:ident, $blueprint_ident:ident, $aggregator:ident, $key_type:tt, $key_payload_alias:ident, $value_type:tt, $value_payload_alias:ident, $allow_ownership:expr$(,)?) => {
            compile_error!(concat!(
                "Unrecognized system collection substate type: `",
                stringify!($unknown_system_substate_type),
                "` - expected `KeyValue`, `Index` or `SortedIndex`"
            ));
        };
    }

    #[allow(unused)]
    pub(crate) use map_collection_schema;

    macro_rules! map_type_ref {
        (
            $blueprint_ident:ident,
            $aggregator:ident,
            {
                kind: StaticSingleVersioned
                $(,)?
            },
            $payload_alias:ident$(,)?
        ) => {
            TypeRef::Static($aggregator.add_child_type_and_descendents::<$payload_alias>())
        };
        (
            $blueprint_ident:ident,
            $aggregator:ident,
            {
                kind: StaticMultiVersioned,
                previous_versions: [
                    $($version_num:expr => { updates_to: $update_to_version_num:expr }),*
                    $(,)? // Optional trailing comma
                ],
                latest_version: $latest_version_num:expr
                $(,)?
            },
            $payload_alias:ident$(,)?
        ) => {
            TypeRef::Static($aggregator.add_child_type_and_descendents::<$payload_alias>())
        };
        (
            $blueprint_ident:ident,
            $aggregator:ident,
            {
                kind: Static,
                content_type: $static_type:ty
                $(,)?
            },
            $payload_alias:ident$(,)?
        ) => {
            TypeRef::Static($aggregator.add_child_type_and_descendents::<$payload_alias>())
        };
        (
            $blueprint_ident:ident,
            $aggregator:ident,
            {
                kind: Generic,
                ident: $generic_ident:ident
                $(,)?
            },
            $payload_alias:ident$(,)?
        ) => {
            paste::paste! {
                TypeRef::Generic([<$blueprint_ident Generic>]::$generic_ident.generic_index())
            }
        };
    }

    #[allow(unused)]
    pub(crate) use map_type_ref;

    macro_rules! map_entry_substate_to_kv_entry {
        (KeyValue, $entry_substate:ident) => {
            paste::paste! {
                KVEntry {
                    value: $entry_substate.value.map(|v| scrypto_encode(&v).unwrap()),
                    locked: match $entry_substate.lock_status {
                        LockStatus::Locked => true,
                        LockStatus::Unlocked => false,
                    },
                }
            }
        };
        (Index, $entry_substate:ident) => {
            // This code still needs to compile, but it shouldn't be possible to execute
            panic!("Not possible to map an Index entry to a KVEntry")
        };
        (SortedIndex, $entry_substate:ident) => {
            // This code still needs to compile, but it shouldn't be possible to execute
            panic!("Not possible to map a SortedIndex entry to a KVEntry")
        };
        ($unknown_system_substate_type:ident, $entry_substate:ident) => {
            paste::paste! {
                compile_error!(concat!(
                    "Unrecognized system collection substate type: `",
                    stringify!($unknown_system_substate_type),
                    "` - expected `KeyValue`, `Index` or `SortedIndex`"
                ));
            }
        };
    }

    #[allow(unused)]
    pub(crate) use map_entry_substate_to_kv_entry;

    macro_rules! optional_or_fallback {
        ($value:tt, $fallback:tt$(,)?) => {
            $value
        };
        (, $fallback:tt$(,)?) => {
            $fallback
        };
    }

    #[allow(unused)]
    pub(crate) use optional_or_fallback;

    macro_rules! enum_filter_out_ignored {
        (
            $(#[$attributes:meta])*
            pub enum $enum_name:ident
            {$(
                $(|IGNORE_ENTRY| { $($misc:tt)* } [[ $($ignored:tt)* ]])?
                $([[ $($present:tt)* ]])?
                ,
            )*}
        ) => {
            $(#[$attributes])*
            pub enum $enum_name
            {$(
                $($($present)*,)?
            )*}
        };
    }
    #[allow(unused)]
    pub(crate) use enum_filter_out_ignored;

    macro_rules! match_filter_out_ignored {
        (
            match $value_to_match:ident {$(
                $(|IGNORE_ENTRY| { $($misc:tt)*} [[ $($ignored:tt)* ]])?
                $([[ $($present:tt)* ]])?
                ,
            )*}
        ) => {
            match $value_to_match {$(
                $($($present)*)?
            )*}
        };
    }
    #[allow(unused)]
    pub(crate) use match_filter_out_ignored;

    macro_rules! if_exists {
        (
            TEST: [[]],
            [[ $($present:tt)* ]],
            [[ $($not_present:tt)* ]]$(,)?
        ) => {
            $($not_present)*
        };
        (
            TEST: [[ $($exists:tt)* ]],
            [[ $($present:tt)* ]],
            [[ $($not_present:tt)* ]]$(,)?
        ) => {
            $($present)*
        };
    }
    #[allow(unused)]
    pub(crate) use if_exists;
}

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

    // Types and Impls required by the macro:
    #[derive(Debug, PartialEq, Eq, Sbor, Copy, Clone)]
    pub struct TestBlueprintRoyaltyV1;

    #[derive(Debug, PartialEq, Eq, Sbor)]
    pub struct TestBlueprintMyCoolKeyValueStoreV1;

    #[derive(Debug, PartialEq, Eq, Sbor)]
    pub struct TestBlueprintMyCoolIndexV1;

    #[derive(Debug, PartialEq, Eq, Sbor)]
    pub struct TestBlueprintMyCoolSortedIndexV1;

    use radix_engine_interface::blueprints::package::*;

    #[allow(dead_code)]
    pub enum TestBlueprintPartitionOffset {
        Field,
        MyCoolKeyValueStoreKeyValue,
        MyCoolIndexIndex,
        MyCoolSortedIndexSortedIndex,
    }

    impl TryFrom<PartitionOffset> for TestBlueprintPartitionOffset {
        type Error = ();

        fn try_from(_value: PartitionOffset) -> Result<Self, Self::Error> {
            Err(())
        }
    }

    pub struct ExampleSortedIndexKey(u16, BlueprintVersion);

    impl SortedIndexKeyFullContent<TestBlueprintMyCoolSortedIndexKeyPayload> for ExampleSortedIndexKey {
        fn from_sort_key_and_content(sort_key: u16, content: BlueprintVersion) -> Self {
            ExampleSortedIndexKey(sort_key, content)
        }

        fn as_content(&self) -> &BlueprintVersion {
            &self.1
        }
    }

    impl SortedIndexKeyContentSource<TestBlueprintMyCoolSortedIndexKeyPayload>
        for ExampleSortedIndexKey
    {
        fn sort_key(&self) -> u16 {
            self.0
        }

        fn into_content(self) -> BlueprintVersion {
            self.1
        }
    }

    // Macro itself
    declare_native_blueprint_state! {
        blueprint_ident: TestBlueprint,
        blueprint_snake_case: test_blueprint_v1,
        generics: {
            abc: {
                ident: Abc,
                description: "Some generic parameter called Abc",
            }
        },
        features: {
            some_feature: {
                ident: Feature,
                description: "Some feature",
            }
        },
        fields: {
            royalty:  {
                ident: Royalty,
                field_type: {
                    kind: StaticSingleVersioned,
                },
                condition: Condition::Always,
            },
            some_generic_field:  {
                ident: GenericField,
                field_type: {
                    kind: Generic,
                    ident: Abc,
                },
            }
        },
        collections: {
            some_key_value_store: KeyValue {
                entry_ident: MyCoolKeyValueStore,
                key_type: {
                    kind: Static,
                    content_type: BlueprintVersion,
                },
                value_type: {
                    kind: StaticSingleVersioned,
                },
                allow_ownership: true,
            },
            abc: Index {
                entry_ident: MyCoolIndex,
                key_type: {
                    kind: Static,
                    content_type: BlueprintVersion,
                },
                value_type: {
                    kind: StaticSingleVersioned,
                },
                allow_ownership: true,
            },
            def: SortedIndex {
                entry_ident: MyCoolSortedIndex,
                key_type: {
                    kind: Static,
                    content_type: BlueprintVersion,
                },
                full_key_content: {
                    full_content_type: ExampleSortedIndexKey,
                    sort_prefix_property_name: sort_prefix,
                },
                value_type: {
                    kind: StaticSingleVersioned,
                },
                allow_ownership: true,
            },
        }
    }

    #[test]
    fn validate_declare_sorted_index_key_new_type_macro() {
        let mut bv = BlueprintVersion::default();
        let mut idx_key = TestBlueprintMyCoolSortedIndexKeyPayload::new(1, bv);

        assert_eq!(&bv, idx_key.as_ref());
        assert_eq!(&mut bv, idx_key.as_mut());
        assert_eq!((1, &bv), idx_key.as_sort_key_and_content());
        assert_eq!((1, bv), idx_key.into_sort_key_and_content());
    }

    #[test]
    fn validate_declare_index_key_new_type_macro() {
        let mut bv = BlueprintVersion::default();
        let mut payload = TestBlueprintMyCoolIndexKeyPayload::from(bv);

        assert_eq!(&bv, payload.as_ref());
        assert_eq!(&mut bv, payload.as_mut());
        assert_eq!(bv, payload.into_content());
    }

    #[test]
    fn validate_royalty_field_payload_mutability() {
        let mut content = VersionedTestBlueprintRoyalty::from(TestBlueprintRoyaltyV1);
        let mut payload = TestBlueprintRoyaltyFieldPayload::from(
            VersionedTestBlueprintRoyalty::from(TestBlueprintRoyaltyV1),
        );
        assert_eq!(&content, payload.as_ref());
        assert_eq!(&mut content, payload.as_mut());
        let payload = TestBlueprintRoyaltyFieldPayload::from(VersionedTestBlueprintRoyalty::from(
            TestBlueprintRoyaltyV1,
        ));
        assert_eq!(
            LockStatus::Locked,
            payload.into_locked_substate().lock_status()
        );
        let payload = TestBlueprintRoyaltyFieldPayload::from(VersionedTestBlueprintRoyalty::from(
            TestBlueprintRoyaltyV1,
        ));
        assert_eq!(
            LockStatus::Unlocked,
            payload.into_unlocked_substate().lock_status()
        );
    }

    #[test]
    fn validate_key_value_store_entry_payload_mutability() {
        fn create_payload() -> TestBlueprintMyCoolKeyValueStoreEntryPayload {
            TestBlueprintMyCoolKeyValueStoreEntryPayload::of(
                TestBlueprintMyCoolKeyValueStoreV1.into(),
            )
        }

        assert_eq!(
            LockStatus::Locked,
            create_payload().into_locked_substate().lock_status()
        );
        assert_eq!(
            LockStatus::Unlocked,
            create_payload().into_unlocked_substate().lock_status()
        );

        assert_eq!(
            LockStatus::Locked,
            create_payload()
                .into_content()
                .into_locked_substate()
                .lock_status()
        );
        assert_eq!(
            LockStatus::Unlocked,
            create_payload()
                .into_content()
                .into_unlocked_substate()
                .lock_status()
        );

        assert!(create_payload().as_latest_version().is_some());
    }

    #[test]
    fn validate_index_entry_payload() {
        let payload = TestBlueprintMyCoolIndexEntryPayload::of(TestBlueprintMyCoolIndexV1.into());
        assert_eq!(
            payload.into_substate().into_value().into_content(),
            VersionedTestBlueprintMyCoolIndex::from(TestBlueprintMyCoolIndexV1)
        );

        let content =
            TestBlueprintMyCoolIndexVersions::V1(TestBlueprintMyCoolIndexV1).into_versioned();
        assert_eq!(
            VersionedTestBlueprintMyCoolIndex::from(TestBlueprintMyCoolIndexV1),
            content.into_substate().into_value().into_content()
        );
    }

    #[test]
    fn validate_sorted_index_entry_payload() {
        let payload =
            TestBlueprintMyCoolSortedIndexEntryPayload::of(TestBlueprintMyCoolSortedIndexV1.into());
        assert_eq!(
            payload.into_substate().into_value().into_content(),
            TestBlueprintMyCoolSortedIndexVersions::V1(TestBlueprintMyCoolSortedIndexV1)
                .into_versioned()
        );

        let content = TestBlueprintMyCoolSortedIndexVersions::V1(TestBlueprintMyCoolSortedIndexV1)
            .into_versioned();
        assert_eq!(
            TestBlueprintMyCoolSortedIndexVersions::V1(TestBlueprintMyCoolSortedIndexV1)
                .into_versioned(),
            content.into_substate().into_value().into_content()
        );
    }

    #[test]
    fn test_blueprint_field_try_from() {
        assert!(TestBlueprintField::try_from(&SubstateKey::Field(0)).is_ok());
        assert!(TestBlueprintField::try_from(&SubstateKey::Map(Vec::new())).is_err());
    }

    #[test]
    fn validate_blueprint_field_index() {
        let field = TestBlueprintField::Royalty;
        assert_eq!(0, FieldDescriptor::field_index(&field));

        let field = TestBlueprintField::GenericField;
        assert_eq!(1, FieldDescriptor::field_index(&field));
    }

    #[test]
    fn test_substate_key_partition() {
        assert!(TestBlueprintTypedSubstateKey::for_key_at_partition_offset(
            PartitionOffset(0),
            &SubstateKey::Field(0)
        )
        .is_err());

        assert!(TestBlueprintTypedSubstateKey::for_key_in_partition(
            &TestBlueprintPartitionOffset::Field,
            &SubstateKey::Field(0)
        )
        .is_ok());

        assert!(TestBlueprintTypedSubstateKey::for_key_in_partition(
            &TestBlueprintPartitionOffset::MyCoolIndexIndex,
            &SubstateKey::Map(vec![92, 0])
        )
        .is_err());
    }

    // And now imagine we have a V2 blueprint needing types:

    pub type TestBlueprintV2RoyaltyV1 = TestBlueprintRoyaltyV1;
    pub type TestBlueprintV2MyCoolKeyValueStoreV1 = TestBlueprintMyCoolKeyValueStoreV1;
    pub type TestBlueprintV2MyCoolIndexV1 = TestBlueprintMyCoolIndexV1;
    pub type TestBlueprintV2MyCoolSortedIndexV1 = TestBlueprintMyCoolSortedIndexV1;

    pub type TestBlueprintV2PartitionOffset = TestBlueprintPartitionOffset;

    #[derive(Debug, PartialEq, Eq, Sbor, Clone)]
    pub struct TestBlueprintV2RoyaltyV2 {
        my_new_value: String,
    }

    impl From<TestBlueprintV2RoyaltyV1> for TestBlueprintV2RoyaltyV2 {
        fn from(_value: TestBlueprintV2RoyaltyV1) -> Self {
            Self {
                my_new_value: "created during upgrade".to_string(),
            }
        }
    }

    impl SortedIndexKeyFullContent<TestBlueprintV2MyCoolSortedIndexKeyPayload>
        for ExampleSortedIndexKey
    {
        fn from_sort_key_and_content(sort_key: u16, content: BlueprintVersion) -> Self {
            ExampleSortedIndexKey(sort_key, content)
        }

        fn as_content(&self) -> &BlueprintVersion {
            &self.1
        }
    }

    impl SortedIndexKeyContentSource<TestBlueprintV2MyCoolSortedIndexKeyPayload>
        for ExampleSortedIndexKey
    {
        fn sort_key(&self) -> u16 {
            self.0
        }

        fn into_content(self) -> BlueprintVersion {
            self.1
        }
    }

    declare_native_blueprint_state! {
        blueprint_ident: TestBlueprintV2,
        blueprint_snake_case: test_blueprint_v2,
        generics: {
            abc: {
                ident: Abc,
                description: "Some generic parameter called Abc",
            }
        },
        features: {
            some_feature: {
                ident: Feature,
                description: "Some feature",
            }
        },
        fields: {
            royalty:  {
                ident: Royalty,
                field_type: {
                    kind: StaticMultiVersioned,
                    previous_versions: [
                        1 => { updates_to: 2 }
                    ],
                    latest_version: 2,
                },
                condition: Condition::Always,
            },
            some_generic_field:  {
                ident: GenericField,
                field_type: {
                    kind: Generic,
                    ident: Abc,
                },
            }
        },
        collections: {
            some_key_value_store: KeyValue {
                entry_ident: MyCoolKeyValueStore,
                key_type: {
                    kind: Static,
                    content_type: BlueprintVersion,
                },
                value_type: {
                    kind: StaticSingleVersioned,
                },
                allow_ownership: true,
            },
            abc: Index {
                entry_ident: MyCoolIndex,
                key_type: {
                    kind: Static,
                    content_type: BlueprintVersion,
                },
                value_type: {
                    kind: StaticSingleVersioned,
                },
                allow_ownership: true,
            },
            def: SortedIndex {
                entry_ident: MyCoolSortedIndex,
                key_type: {
                    kind: Static,
                    content_type: BlueprintVersion,
                },
                full_key_content: {
                    full_content_type: ExampleSortedIndexKey,
                    sort_prefix_property_name: sort_prefix,
                },
                value_type: {
                    kind: StaticSingleVersioned,
                },
                allow_ownership: true,
            },
        }
    }

    #[test]
    fn validate_v1_data_can_be_loaded_as_v1_or_v2() {
        // ----------
        // V1 Content
        // ----------
        // Content v1 is used by both TestBlueprint and TestBlueprintV2 - it's actually the same type
        let content_v1 = TestBlueprintRoyaltyV1;
        let v1_versioned_content_v1 = VersionedTestBlueprintRoyalty::from(content_v1);
        let v1_payload_v1 = TestBlueprintRoyaltyFieldPayload::of(content_v1.into());
        let v2_versioned_content_v1 = VersionedTestBlueprintV2Royalty::from(content_v1);
        let v2_payload_v1 = TestBlueprintV2RoyaltyFieldPayload::of(content_v1.into());

        // These should all be the same:
        let encoded_v1_versioned_content_v1 = scrypto_encode(&v1_versioned_content_v1).unwrap();
        let encoded_v1_payload_v1 = scrypto_encode(&v1_payload_v1).unwrap();
        let encoded_v2_versioned_content_v1 = scrypto_encode(&v2_versioned_content_v1).unwrap();
        let encoded_v2_payload_v1 = scrypto_encode(&v2_payload_v1).unwrap();

        assert_eq!(encoded_v1_versioned_content_v1, encoded_v1_payload_v1);
        assert_eq!(
            encoded_v1_versioned_content_v1,
            encoded_v2_versioned_content_v1
        );
        assert_eq!(encoded_v1_versioned_content_v1, encoded_v2_payload_v1);

        let v1_payload_v1_decoded_as_v2 =
            scrypto_decode::<TestBlueprintV2RoyaltyFieldPayload>(&encoded_v1_payload_v1).unwrap();
        assert_eq!(&v1_payload_v1_decoded_as_v2, &v2_payload_v1);

        // ----------
        // V2 Content
        // ----------
        // Content v2 is used by only TestBlueprintV2:
        let content_v2 = TestBlueprintV2RoyaltyV2 {
            my_new_value: "created during upgrade".to_string(),
        };
        let v2_versioned_content_v2 = VersionedTestBlueprintV2Royalty::from(content_v2.clone());
        let v2_payload_v2 = TestBlueprintV2RoyaltyFieldPayload::of(content_v2.clone().into());

        // These should both be the same:
        let encoded_v2_versioned_content_v2 = scrypto_encode(&v2_versioned_content_v2).unwrap();
        let encoded_v2_payload_v2 = scrypto_encode(&v2_payload_v2).unwrap();
        assert_eq!(encoded_v2_versioned_content_v2, encoded_v2_payload_v2);

        // And we can check that upgrading it works:
        let v2_payload_v1_updated =
            TestBlueprintV2RoyaltyFieldPayload::of(content_v1.into()).fully_update();
        assert_eq!(&v2_payload_v1_updated, &v2_payload_v2);

        // And deserializing into a v2_payload works:
        let decoded_v2_payload_v2 =
            scrypto_decode::<TestBlueprintV2RoyaltyFieldPayload>(&encoded_v2_payload_v2).unwrap();
        assert_eq!(&decoded_v2_payload_v2, &v2_payload_v2);

        // But deserializing as a v1_payload (which doesn't understand v2) breaks:
        let decode_result_v1_payload_v2 =
            scrypto_decode::<TestBlueprintRoyaltyFieldPayload>(&encoded_v2_payload_v2);
        assert!(decode_result_v1_payload_v2.is_err());
    }
}