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
//! <!-- markdown-link-check-disable -->
//! # X-DNS Pallet
//! </pre></p></details>

// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use codec::Encode;

use frame_support::sp_runtime::traits::Zero;
use sp_std::{collections::btree_set::BTreeSet, prelude::*};

pub use t3rn_types::{
    gateway::GatewayABIConfig,
    sfx::{EventSignature, SideEffectId, SideEffectName},
};

use frame_support::sp_runtime::traits::Saturating;
use t3rn_primitives::reexport_currency_types;
pub use t3rn_primitives::{ChainId, GatewayGenesisConfig, GatewayType, GatewayVendor};

// Re-export pallet items so that they can be accessed from the crate namespace.
pub use crate::pallet::*;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub mod weights;

use weights::WeightInfo;
reexport_currency_types!();

// Definition of the pallet logic, to be aggregated at runtime definition through
// `construct_runtime`.
#[frame_support::pallet]
pub mod pallet {
    // Import various types used to declare pallet in scope.
    use super::*;
    use crate::WeightInfo;
    use circuit_runtime_types::AssetId;
    use frame_support::{
        pallet_prelude::*,
        traits::{
            fungible::{Inspect, Mutate},
            Currency, Time,
        },
    };
    use frame_system::pallet_prelude::*;
    use sp_core::H256;
    use sp_runtime::{traits::CheckedDiv, SaturatedConversion};
    use sp_std::convert::TryInto;
    use t3rn_abi::{sfx_abi::SFXAbi, Codec};
    use t3rn_primitives::{
        attesters::AttestersReadApi,
        circuit::{AdaptiveTimeout, CircuitDLQ},
        light_client::{LightClientAsyncAPI, LightClientHeartbeat},
        portal::Portal,
        xdns::{
            EpochEstimate, FullGatewayRecord, GatewayRecord, PalletAssetsOverlay, TokenRecord, Xdns,
        },
        Bytes, ChainId, ExecutionVendor, FinalityVerifierActivity, GatewayActivity, GatewayVendor,
        SpeedMode, TokenInfo, TreasuryAccount, TreasuryAccountProvider, XDNSTopology,
    };
    use t3rn_types::{fsx::TargetId, sfx::Sfx4bId};

    use t3rn_types::sfx::SecurityLvl;

    pub const MAX_GATEWAY_OVERVIEW_RECORDS: u32 = 1000;

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

        /// Type representing the weight of this pallet
        type WeightInfo: weights::WeightInfo;

        /// A type that provides inspection and mutation to some fungible assets
        type Balances: Inspect<Self::AccountId> + Mutate<Self::AccountId>;

        type Currency: Currency<Self::AccountId>;

        type AssetsOverlay: PalletAssetsOverlay<Self, BalanceOf<Self>>;

        type Portal: Portal<Self>;

        type CircuitDLQ: CircuitDLQ<Self>;

        type AttestersRead: AttestersReadApi<Self::AccountId, BalanceOf<Self>, BlockNumberFor<Self>>;

        type TreasuryAccounts: TreasuryAccountProvider<Self::AccountId>;

        type SelfTokenId: Get<AssetId>;

        type SelfGatewayId: Get<ChainId>;

        type Time: Time;
    }

    // Simple declaration of the `Pallet` type. It is placeholder we use to implement traits and
    // method.
    #[pallet::pallet]
    #[pallet::without_storage_info]
    pub struct Pallet<T>(_);

    // Pallet implements [`Hooks`] trait to define some logic to execute in some context.
    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
        // `on_initialize` is executed at the beginning of the block before any extrinsic are
        // dispatched.
        //
        // This function must return the weight consumed by `on_initialize` and `on_finalize`.
        fn on_initialize(_n: frame_system::pallet_prelude::BlockNumberFor<T>) -> Weight {
            // Anything that needs to be done at the start of the block.
            // We don't do anything here.
            Zero::zero()
        }

        // `on_finalize` is executed at the end of block after all extrinsic are dispatched.
        fn on_finalize(_n: frame_system::pallet_prelude::BlockNumberFor<T>) {
            // Perform necessary data/state clean up here.
        }

        // A runtime code run after every block and have access to extended set of APIs.
        //
        // For instance you can generate extrinsics for the upcoming produced block.
        fn offchain_worker(_n: frame_system::pallet_prelude::BlockNumberFor<T>) {
            // We don't do anything here.
            // but we could dispatch extrinsic (transaction/unsigned/inherent) using
            // sp_io::submit_extrinsic.
            // To see example on offchain worker, please refer to example-offchain-worker pallet
            // accompanied in this repository.
        }

        fn on_runtime_upgrade() -> Weight {
            // Define the maximum weight of this migration.
            let max_weight = T::DbWeight::get().reads_writes(10, 10);
            // Define the current storage migration version.
            const CURRENT_STORAGE_VERSION: u32 = 2;
            // Migrate the storage entries.
            StorageMigrations::<T>::try_mutate(|current_version| {
                match *current_version {
                    0 => {
                        // Storage Migration: StandardSideEffects -> StandardSFXABIs
                        // Storage Migration Details: 16-03-2023; v1.4.0-rc -> v1.5.0-rc
                        // Iterate through the old storage entries and migrate them.
                        for (key, _value) in StandardSideEffects::<T>::drain() {
                            let sfx4b_id = key;
                            match SFXAbi::get_standard_interface(sfx4b_id) {
                                Some(sfx_abi) => {
                                    StandardSFXABIs::<T>::insert(sfx4b_id, sfx_abi);
                                }
                                None => {
                                    log::error!(
                                "Failed to migrate StandardSideEffects to StandardSFXABIs for sfx4b_id: {:?}",
                                sfx4b_id
                            );
                                }
                            }
                        }

                        // Set migrations_done to true
                        *current_version = CURRENT_STORAGE_VERSION;

                        // Return the weight consumed by the migration.
                        Ok::<Weight, DispatchError>(max_weight)
                    }
                    // Storage Migration: Raw XDNS storage entry kill
                    // Storage Migration Details: 27-07-2023; v1.4.43-rc -> v1.4.44-rc
                    //     Many Collators on t0rn hit: frame_support::storage: (key, value) failed to decode at [225, 205, 72, 162, 242, 43, 101, 142, 192, 157, 178, 168, 200, 143, 21, 13, 175, 239, 182, 147, 135, 79, 226, 105, 210, 52, 22, 179, 228, 93, 185, 249, 114, 111, 99, 111]
                    1 => {
                        // Manually kill the old XDNS storage entry (XDNSRegistry is now replaced by Gateways)
                        frame_support::storage::unhashed::kill(&[225, 205, 72, 162, 242, 43, 101, 142, 192, 157, 178, 168, 200, 143, 21, 13, 175, 239, 182, 147, 135, 79, 226, 105, 210, 52, 22, 179, 228, 93, 185, 249, 114, 111, 99, 111]);
                        // Set migrations_done to true
                        *current_version = CURRENT_STORAGE_VERSION;
                        // Return the weight consumed by the migration.
                        Ok::<Weight, DispatchError>(T::DbWeight::get().writes(1))
                    }
                    // Storage Migration: Another Raw XDNS storage entry kill
                    // Storage Migration Details: 27-07-2023; v1.4.44-rc -> v1.4.45-rc
                    //     Many Collators on t0rn hit: frame_support::storage: (key, value) failed to decode at [84, 10, 79, 135, 84, 170, 82, 152, 163, 214, 233, 170, 9, 233, 63, 151, 78, 11,
                    //      18, 119, 80, 58, 19, 112, 111, 133, 165, 20, 116, 96, 124, 88, 24, 172, 250, 191, 195, 140, 91, 41, 106, 32, 177, 28, 37, 248, 177, 35, 27, 230, 169, 204, 8, 192, 121, 163, 226, 24, 100, 166, 207, 36, 66, 173, 219, 150, 184, 250, 101, 171, 135, 85,]
                    2 => {
                        // Manually kill the old XDNS storage entry (XDNSRegistry is now replaced by Gateways)
                        frame_support::storage::unhashed::kill(&[84, 10, 79, 135, 84, 170, 82, 152, 163, 214, 233, 170, 9, 233, 63, 151, 78, 11,
                            18, 119, 80, 58, 19, 112, 111, 133, 165, 20, 116, 96, 124, 88, 24, 172, 250,
                            191, 195, 140, 91, 41, 106, 32, 177, 28, 37, 248, 177, 35, 27, 230, 169, 204,
                            8, 192, 121, 163, 226, 24, 100, 166, 207, 36, 66, 173, 219, 150, 184, 250, 101,
                            171, 135, 85,]);
                        // Set migrations_done to true
                        *current_version = CURRENT_STORAGE_VERSION;
                        // Return the weight consumed by the migration.
                        Ok::<Weight, DispatchError>(T::DbWeight::get().writes(1))
                    }
                    // Add more migration cases here, if needed in the future
                    _ => {
                        // No migration needed.
                        Ok::<Weight, DispatchError>(Default::default())
                    }
                }
            })
                .unwrap_or_default()
        }
    }

    impl<T: Config> Pallet<T> {
        pub fn check_for_manual_verifier_overview_process(
            n: frame_system::pallet_prelude::BlockNumberFor<T>,
        ) -> Weight {
            let mut total_weight: Weight = Zero::zero();

            let latest_overview = <VerifierOverviewStore<T>>::get();
            total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));

            GatewayVendor::iterator().for_each(|verifier| {
                let verifier = verifier.clone();
                let latest_vendor_overview = latest_overview
                    .iter()
                    .find(|x| x.verifier == verifier)
                    .cloned()
                    .unwrap_or_default();

                let estimated_epoch_length = EpochHistory::<T>::get(&verifier)
                    .and_then(|epochs| epochs.last().cloned())
                    .map(|epoch| epoch.local)
                    .unwrap_or_else(|| {
                        frame_system::pallet_prelude::BlockNumberFor::<T>::from(50u8)
                    });

                if latest_vendor_overview.reported_at + estimated_epoch_length < n {
                    let mut latest_heartbeat =
                        T::Portal::get_latest_heartbeat_by_vendor(verifier.clone());
                    let epoch = latest_heartbeat.last_finalized_height;
                    if verifier == GatewayVendor::XBI {
                        let current_block_here = frame_system::Pallet::<T>::block_number();
                        latest_heartbeat.last_finalized_height = current_block_here;
                        latest_heartbeat.last_rational_height = current_block_here;
                        latest_heartbeat.last_fast_height = current_block_here;
                        latest_heartbeat.last_heartbeat = current_block_here;
                        latest_heartbeat.ever_initialized = true;
                    }
                    let weight = Self::process_single_verifier_overview(
                        n,
                        verifier,
                        epoch,
                        latest_heartbeat,
                    );
                    total_weight = total_weight.saturating_add(weight);
                }
            });

            total_weight
        }

        pub fn process_all_verifier_overviews(
            n: frame_system::pallet_prelude::BlockNumberFor<T>,
        ) -> Weight {
            Self::check_for_manual_verifier_overview_process(n)
        }

        pub fn process_single_verifier_overview(
            n: frame_system::pallet_prelude::BlockNumberFor<T>,
            verifier: GatewayVendor,
            new_epoch: frame_system::pallet_prelude::BlockNumberFor<T>,
            latest_heartbeat: LightClientHeartbeat<T>,
        ) -> Weight {
            let mut total_weight: Weight = Zero::zero();

            let (justified_height, finalized_height, updated_height, is_active) = (
                latest_heartbeat.last_rational_height,
                latest_heartbeat.last_finalized_height,
                latest_heartbeat.last_fast_height,
                !latest_heartbeat.is_halted,
            );

            let historic_overview = VerifierOverviewStoreHistory::<T>::get(&verifier);
            let mut last_record = historic_overview.last().cloned().unwrap_or_default();
            total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));

            let new_activity =
                FinalityVerifierActivity::new_for_finalized_compare(n, finalized_height);

            let mut is_moving = false;
            let (mut local_height_increase, mut target_finalized_height_increase) =
                (Zero::zero(), Zero::zero());

            if let Some(last_record) = historic_overview.last() {
                if let Some((target_increase, local_increase)) =
                    FinalityVerifierActivity::determine_finalized_reports_increase(&[
                        last_record.clone(),
                        new_activity.clone(),
                    ])
                {
                    if local_increase > Zero::zero() {
                        local_height_increase = local_increase;
                    }
                    if target_increase > Zero::zero() {
                        is_moving = true;
                        target_finalized_height_increase = target_increase;
                    }
                }
            }

            if is_moving {
                Self::update_epoch_history(
                    &verifier,
                    target_finalized_height_increase,
                    local_height_increase,
                );
            }

            if !is_moving {
                if let Some(previous) = historic_overview.iter().rev().nth(1).cloned() {
                    if let Some((target_finality_height_increase, _)) =
                        FinalityVerifierActivity::determine_finalized_reports_increase(&[
                            previous,
                            last_record.clone(),
                            new_activity,
                        ])
                    {
                        if target_finality_height_increase > Zero::zero() {
                            is_moving = true;
                        }
                    }
                }
            }
            let was_active = last_record.is_active;
            let activity = if is_active {
                FinalityVerifierActivity {
                    verifier: verifier.clone(),
                    reported_at: n,
                    justified_height,
                    finalized_height,
                    updated_height,
                    epoch: new_epoch,
                    is_active: is_active && is_moving,
                }
            } else {
                last_record.reported_at = n;
                last_record.is_active = false;
                last_record
            };

            Self::update_historic_overview(verifier.clone(), activity.clone());
            Self::update_overview_store(verifier.clone(), activity.clone());
            total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(3, 2));

            if !was_active && activity.is_active {
                let weight = T::CircuitDLQ::process_dlq(n);
                total_weight = total_weight.saturating_add(weight);
            }

            let weight = T::CircuitDLQ::process_adaptive_xtx_timeout_queue(n, &verifier);
            total_weight = total_weight.saturating_add(weight);

            total_weight
        }

        pub fn update_historic_overview(
            verifier: GatewayVendor,
            activity: FinalityVerifierActivity<BlockNumberFor<T>>,
        ) {
            let mut historic_overview = VerifierOverviewStoreHistory::<T>::get(&verifier);
            if historic_overview.len() == MAX_GATEWAY_OVERVIEW_RECORDS as usize {
                let _ = historic_overview.remove(0);
            }
            historic_overview.push(activity);
            VerifierOverviewStoreHistory::<T>::insert(&verifier, historic_overview);
        }

        pub fn update_overview_store(
            verifier: GatewayVendor,
            activity: FinalityVerifierActivity<BlockNumberFor<T>>,
        ) {
            VerifierOverviewStore::<T>::mutate(|all_overviews| {
                if let Some(overview) = all_overviews.iter_mut().find(|o| o.verifier == verifier) {
                    *overview = activity.clone();
                } else {
                    all_overviews.push(activity.clone());
                }
            });
        }

        pub fn process_overview(n: frame_system::pallet_prelude::BlockNumberFor<T>) {
            let mut all_overviews: Vec<GatewayActivity<BlockNumberFor<T>>> = Vec::new();

            for gateway in Self::fetch_full_gateway_records() {
                let gateway_id = gateway.gateway_record.gateway_id;
                let last_finality_verifier_update = VerifierOverviewStoreHistory::<T>::get(
                    &gateway.gateway_record.verification_vendor,
                )
                .last()
                .cloned()
                .unwrap_or_else(|| FinalityVerifierActivity {
                    verifier: gateway.gateway_record.verification_vendor.clone(),
                    reported_at: Zero::zero(),
                    justified_height: Zero::zero(),
                    finalized_height: Zero::zero(),
                    updated_height: Zero::zero(),
                    epoch: Zero::zero(),
                    is_active: false,
                });

                let security_lvl = match gateway.gateway_record.escrow_account {
                    Some(_) => SecurityLvl::Escrow,
                    None => SecurityLvl::Optimistic,
                };

                let (justified_height, finalized_height, updated_height, is_active) = (
                    last_finality_verifier_update.justified_height,
                    last_finality_verifier_update.finalized_height,
                    last_finality_verifier_update.updated_height,
                    last_finality_verifier_update.is_active,
                );

                let attestation_latency = T::AttestersRead::read_attestation_latency(&gateway_id);

                let activity = GatewayActivity {
                    gateway_id,
                    reported_at: n,
                    justified_height,
                    finalized_height,
                    updated_height,
                    attestation_latency,
                    security_lvl,
                    is_active,
                };

                // Add the new activity to the historic overview of the gateway
                let mut historic_overview = GatewaysOverviewStoreHistory::<T>::get(gateway_id);
                if historic_overview.len() == MAX_GATEWAY_OVERVIEW_RECORDS as usize {
                    let _ = historic_overview.remove(0);
                }
                historic_overview.push(activity.clone());
                GatewaysOverviewStoreHistory::<T>::insert(gateway_id, historic_overview);

                // Add the new activity to the general overview
                all_overviews.push(activity);
            }

            // Update the general overview
            GatewaysOverviewStore::<T>::put(all_overviews);
        }

        // XDNS Topology Zip / Unzip
        pub fn do_zip_topology() -> XDNSTopology<T::AccountId> {
            let gateways = Self::fetch_full_gateway_records();

            // Collect all asset information
            let assets = AllTokenIds::<T>::get()
                .iter()
                .filter_map(|asset_id| Tokens::<T>::get(asset_id, T::SelfGatewayId::get()))
                .collect::<Vec<TokenRecord>>();

            // Assemble the topology structure
            XDNSTopology { gateways, assets }
        }

        pub fn do_unzip_topology(
            origin: &OriginFor<T>,
            topology: XDNSTopology<T::AccountId>,
        ) -> DispatchResult {
            // Update the gateways
            for gateway in topology.gateways {
                // Register Gateway
                let (
                    gateway_id,
                    verification_vendor,
                    execution_vendor,
                    codec,
                    registrant,
                    escrow_account,
                    allowed_side_effects,
                ) = (
                    gateway.gateway_record.gateway_id,
                    gateway.gateway_record.verification_vendor,
                    gateway.gateway_record.execution_vendor,
                    gateway.gateway_record.codec,
                    gateway.gateway_record.registrant,
                    gateway.gateway_record.escrow_account,
                    gateway.gateway_record.allowed_side_effects,
                );
                log::info!("topology unzip -- gateway_id: {:?}", gateway_id);
                Self::override_gateway(
                    gateway_id,
                    verification_vendor,
                    execution_vendor,
                    codec,
                    registrant,
                    escrow_account,
                    allowed_side_effects,
                )?;
            }

            // Update the assets
            for asset in topology.assets {
                log::info!("topology unzip -- asset_id: {:?}", asset.token_id);
                // Register Asset if not present
                if !AllTokenIds::<T>::get().contains(&asset.token_id) {
                    Self::register_new_token(origin, asset.token_id, asset.token_props)?;
                } else {
                    // Link the asset to the gateway
                    Self::link_token_to_gateway(
                        asset.token_id,
                        asset.gateway_id,
                        asset.token_props,
                    )?;
                }
            }

            Ok(())
        }
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Re-adds the self-gateway if was present before. Inserts if wasn't. Root only access.
        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn reboot_self_gateway(
            origin: OriginFor<T>,
            vendor: GatewayVendor,
        ) -> DispatchResultWithPostInfo {
            Self::do_reboot_self_gateway(origin, vendor)?;

            Ok(().into())
        }

        /// Re-adds the self-gateway if was present before. Inserts if wasn't. Root only access.
        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn add_supported_bridging_asset(
            origin: OriginFor<T>,
            asset_id: AssetId,
            target_id: TargetId,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;

            if !<AuthorizedMintAssets<T>>::get().contains(&(asset_id, target_id)) {
                <AuthorizedMintAssets<T>>::append((&asset_id, &target_id));
            }

            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn enroll_bridge_asset(
            origin: OriginFor<T>,
            asset_id: AssetId,
            target_id: TargetId,
            token_info: TokenInfo,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin.clone())?;

            assert!(!Self::check_asset_is_mintable(target_id, asset_id));

            if !<AllTokenIds<T>>::get().contains(&asset_id) {
                Self::register_new_token(&origin, asset_id, token_info.clone())?;
            }
            // Check that the asset is not already added to the gateway
            if !<Tokens<T>>::contains_key(&asset_id, &target_id) {
                Self::link_token_to_gateway(asset_id, target_id, token_info)?;
            }

            <AuthorizedMintAssets<T>>::append((&asset_id, &target_id));

            log::info!(
                "Enrolled asset {:?} for bridging to {:?}",
                asset_id,
                target_id
            );
            // Check that the asset is mintable
            assert!(Self::check_asset_is_mintable(target_id, asset_id));

            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn enroll_new_abi_to_selected_gateway(
            origin: OriginFor<T>,
            target_id: ChainId,
            sfx_4b_id: Sfx4bId,
            sfx_expected_abi: Option<SFXAbi>,
            maybe_pallet_id: Option<u8>,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin.clone())?;

            if let Some(abi) = sfx_expected_abi {
                <SFXABIRegistry<T>>::insert(target_id, sfx_4b_id, abi);
            } else {
                let mut assume_known_abi = <StandardSFXABIs<T>>::get(sfx_4b_id)
                    .ok_or(Error::<T>::SideEffectABINotFound)?;
                assume_known_abi.maybe_prefix_memo = maybe_pallet_id;
                <SFXABIRegistry<T>>::insert(target_id, sfx_4b_id, assume_known_abi);
            }

            let updated_abi_list = <SFXABIRegistry<T>>::iter_prefix(target_id).collect();

            Self::override_sfx_abi(target_id, updated_abi_list)?;

            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn unroll_abi_of_selected_gateway(
            origin: OriginFor<T>,
            target_id: ChainId,
            sfx_4b_id: Sfx4bId,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin.clone())?;

            <SFXABIRegistry<T>>::remove(target_id, sfx_4b_id);

            let updated_abi_list = <SFXABIRegistry<T>>::iter_prefix(target_id).collect();

            Self::override_sfx_abi(target_id, updated_abi_list)?;

            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn add_remote_order_address(
            origin: OriginFor<T>,
            target_id: TargetId,
            remote_address: H256,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin.clone())?;
            <RemoteOrderAddresses<T>>::insert(target_id, remote_address);
            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn add_remote_bidding_address(
            origin: OriginFor<T>,
            target_id: TargetId,
            remote_address: H256,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin.clone())?;
            <RemoteBiddingAddresses<T>>::insert(target_id, remote_address);
            Ok(().into())
        }

        /// Re-adds the self-gateway if was present before. Inserts if wasn't. Root only access.
        #[pallet::weight(< T as Config >::WeightInfo::reboot_self_gateway())]
        pub fn purge_supported_bridging_asset(
            origin: OriginFor<T>,
            asset_id: AssetId,
            target_id: TargetId,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;

            if <AuthorizedMintAssets<T>>::get().contains(&(asset_id, target_id)) {
                <AuthorizedMintAssets<T>>::mutate(|all_token_ids| {
                    all_token_ids.retain(|&(a_id, t_id)| a_id != asset_id && t_id != target_id);
                });
            }

            Ok(().into())
        }

        /// Removes a gateway from the onchain registry. Root only access.
        #[pallet::weight(< T as Config >::WeightInfo::purge_gateway())]
        pub fn purge_gateway_record(
            origin: OriginFor<T>,
            requester: T::AccountId,
            gateway_id: TargetId,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            if !<Gateways<T>>::contains_key(gateway_id) {
                Err(Error::<T>::XdnsRecordNotFound.into())
            } else {
                // Get associated finality verifier
                let verifier = <Gateways<T>>::get(gateway_id)
                    .expect("Ensured by Gateways::contains_key() above during purge_gateway_record")
                    .verification_vendor;

                <Gateways<T>>::remove(gateway_id);

                let token_ids = GatewayTokens::<T>::get(gateway_id);

                token_ids.iter().for_each(|token_id| {
                    <Tokens<T>>::remove(token_id, gateway_id);
                    if gateway_id == T::SelfGatewayId::get() {
                        <AllTokenIds<T>>::mutate(|all_token_ids| {
                            all_token_ids.retain(|id| id != token_id);
                        });
                    }
                });

                <GatewayTokens<T>>::remove(gateway_id);

                <AllGatewayIds<T>>::mutate(|all_gateway_ids| {
                    all_gateway_ids.retain(|&id| id != gateway_id);
                });

                let current_block = <frame_system::Pallet<T>>::block_number();

                let latest_heartbeat = T::Portal::get_latest_heartbeat_by_vendor(verifier.clone());
                let epoch = latest_heartbeat.last_finalized_height;
                let _weight = Self::process_single_verifier_overview(
                    current_block,
                    verifier,
                    epoch,
                    latest_heartbeat,
                );

                Self::deposit_event(Event::<T>::GatewayRecordPurged(requester, gateway_id));
                Ok(().into())
            }
        }

        #[pallet::weight(< T as Config >::WeightInfo::purge_gateway())]
        pub fn unlink_token(
            origin: OriginFor<T>,
            gateway_id: TargetId,
            token_id: AssetId,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;

            <Tokens<T>>::remove(token_id, gateway_id);

            <GatewayTokens<T>>::mutate(gateway_id, |token_ids| {
                token_ids.retain(|&x_token_id| x_token_id != token_id);
            });

            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::purge_gateway())]
        pub fn link_token(
            origin: OriginFor<T>,
            gateway_id: TargetId,
            token_id: AssetId,
            token_props: TokenInfo,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;

            Self::link_token_to_gateway(token_id, gateway_id, token_props)?;

            Ok(().into())
        }

        /// Removes from all of the registered destinations + the onchain registry. Root only access.
        #[pallet::weight(< T as Config >::WeightInfo::purge_gateway())]
        pub fn purge_token_record(
            origin: OriginFor<T>,
            token_id: AssetId,
        ) -> DispatchResultWithPostInfo {
            // Try destroying assets with associated to sudo origin of Escrow or admin / ownership rights
            let is_root = ensure_signed_or_root(origin.clone())?.is_none();
            let origin_admin: T::RuntimeOrigin = match is_root {
                true => T::RuntimeOrigin::from(frame_system::RawOrigin::Signed(
                    T::TreasuryAccounts::get_treasury_account(TreasuryAccount::Escrow),
                )),
                false => origin,
            };

            // Try destroying assets with
            T::AssetsOverlay::destroy(origin_admin, &token_id)?;

            // Remove from all destinations
            let destinations = <Tokens<T>>::iter_prefix(token_id)
                .map(|(dest, _)| dest)
                .collect::<Vec<_>>();
            destinations.iter().for_each(|dest| {
                <Tokens<T>>::remove(token_id, *dest);
            });

            <AllTokenIds<T>>::mutate(|all_token_ids| {
                all_token_ids.retain(|&id| id != token_id);
            });

            Ok(().into())
        }

        #[pallet::weight(< T as Config >::WeightInfo::purge_gateway())]
        pub fn zip_topology(origin: OriginFor<T>) -> DispatchResult {
            let _ = ensure_signed(origin)?;
            let topology = Self::do_zip_topology();
            Self::deposit_event(Event::<T>::XDNSTopologyZip(topology.clone()));
            Ok(())
        }

        #[pallet::weight(< T as Config >::WeightInfo::purge_gateway())]
        pub fn unzip_topology(
            origin: OriginFor<T>,
            topology_decoded: Option<XDNSTopology<T::AccountId>>,
            topology_encoded: Option<Vec<u8>>,
        ) -> DispatchResult {
            let _ = ensure_root(origin.clone())?;
            let topology = if let Some(topology) = topology_decoded {
                topology
            } else if let Some(topology) = topology_encoded {
                let topology = XDNSTopology::<T::AccountId>::decode(&mut topology.as_slice())
                    .map_err(|_| Error::<T>::TopologyDecodeError)?;
                topology
            } else {
                return Err(Error::<T>::EmptyTopologySubmitted.into())
            };

            Self::do_unzip_topology(&origin, topology)?;

            Ok(())
        }
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub (super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// \[gateway_4b_id\]
        GatewayRecordStored(TargetId),
        /// \[asset_id, gateway_4b_id\]
        NewTokenLinkedToGateway(AssetId, TargetId),
        /// \[asset_id, gateway_4b_id\]
        NewTokenAssetRegistered(AssetId, TargetId),
        /// \[requester, gateway_record_id\]
        GatewayRecordPurged(T::AccountId, TargetId),
        /// \[requester, xdns_record_id\]
        XdnsRecordPurged(T::AccountId, TargetId),
        /// \[xdns_record_id\]
        XdnsRecordUpdated(TargetId),
        /// \[xdns_topology\]
        XDNSTopologyZip(XDNSTopology<T::AccountId>),
    }

    // Errors inform users that something went wrong.
    #[pallet::error]
    pub enum Error<T> {
        /// Stored gateway has already been added before
        GatewayRecordAlreadyExists,
        /// XDNS Record not found
        XdnsRecordNotFound,
        /// Escrow account not found
        EscrowAccountNotFound,
        /// Escrow account not found
        RemoteOrderAddressNotFound,
        /// Stored token has already been added before
        TokenRecordAlreadyExists,
        /// XDNS Token not found in assets overlay
        TokenRecordNotFoundInAssetsOverlay,
        /// XDNS Token not found in on that gateway
        TokenRecordNotFoundInGateway,
        /// Gateway Record not found
        GatewayRecordNotFound,
        /// SideEffectABI already exists
        SideEffectABIAlreadyExists,
        /// SideEffectABI not found
        SideEffectABINotFound,
        /// the xdns entry does not contain parachain information
        NoParachainInfoFound,
        /// A token is not compatible with the gateways execution layer
        TokenExecutionVendorMismatch,
        /// Gateway verified as inactive
        GatewayNotActive,
        /// Failed to decode XDNS topology at Unzip
        TopologyDecodeError,
        /// Empty topology submitted at Unzip
        EmptyTopologySubmitted,
    }

    // Deprecated storage entry -- StandardSideEffects
    // Storage Migration: StandardSideEffects -> StandardSFXABIs
    // Storage Migration Details: 16-03-2023; v1.4.0-rc -> v1.5.0-rc
    #[pallet::storage]
    pub type StandardSideEffects<T: Config> = StorageMap<_, Identity, TargetId, Vec<u8>>; // SideEffectInterface

    // Deprecated storage entry -- CustomSideEffects
    // Storage Migration: CustomSideEffects -> !dropped and replaced by SFXABIRegistry
    // Storage Migration Details: 16-03-2023; v1.4.0-rc -> v1.5.0-rc
    #[pallet::storage]
    pub type CustomSideEffects<T: Config> = StorageMap<_, Identity, SideEffectId<T>, Vec<u8>>;

    #[pallet::storage]
    #[pallet::getter(fn storage_migrations_done)]
    pub type StorageMigrations<T: Config> = StorageValue<_, u32, ValueQuery>;

    #[pallet::storage]
    pub type StandardSFXABIs<T: Config> = StorageMap<_, Identity, Sfx4bId, SFXAbi>;

    #[pallet::storage]
    pub type SFXABIRegistry<T: Config> =
        StorageDoubleMap<_, Identity, TargetId, Identity, Sfx4bId, SFXAbi>;

    #[pallet::storage]
    #[pallet::getter(fn gateways)]
    pub type Gateways<T: Config> =
        StorageMap<_, Identity, TargetId, GatewayRecord<T::AccountId>, OptionQuery>;

    // Token can be stored in multiple gateways and on each Gateway be mapped to a different TokenRecord (Substrate, Eth etc.)
    #[pallet::storage]
    #[pallet::getter(fn tokens)]
    pub type Tokens<T: Config> =
        StorageDoubleMap<_, Identity, AssetId, Identity, TargetId, TokenRecord, OptionQuery>;

    // Recover TokenRecords stored per gateway, to be able to iterate over all tokens stored on a gateway
    #[pallet::storage]
    #[pallet::getter(fn gateway_tokens)]
    pub type GatewayTokens<T: Config> = StorageMap<_, Identity, TargetId, Vec<AssetId>, ValueQuery>;

    // All known TokenIds to t3rn
    #[pallet::storage]
    #[pallet::getter(fn all_token_ids)]
    pub type AllTokenIds<T: Config> = StorageValue<_, Vec<AssetId>, ValueQuery>;

    // All known TokenIds to t3rn
    #[pallet::storage]
    #[pallet::getter(fn supported_bridging_assets)]
    pub type AuthorizedMintAssets<T: Config> =
        StorageValue<_, Vec<(AssetId, TargetId)>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn all_gateway_ids)]
    pub type AllGatewayIds<T: Config> = StorageValue<_, Vec<TargetId>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn remote_order_addresses)]
    pub type RemoteOrderAddresses<T: Config> = StorageMap<_, Identity, TargetId, H256, OptionQuery>;

    #[pallet::storage]
    #[pallet::getter(fn remote_bidding_addresses)]
    pub type RemoteBiddingAddresses<T: Config> =
        StorageMap<_, Identity, TargetId, H256, OptionQuery>;

    #[pallet::storage]
    #[pallet::getter(fn per_target_asset_estimates)]
    pub type PerTargetAssetEstimates<T: Config> = StorageDoubleMap<
        _,
        Identity,
        TargetId,
        Identity,
        (AssetId, AssetId),
        BalanceOf<T>,
        ValueQuery,
    >;

    #[pallet::storage]
    #[pallet::getter(fn asset_estimates)]
    pub type AssetEstimates<T: Config> =
        StorageMap<_, Identity, (AssetId, AssetId), BalanceOf<T>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn asset_estimates_in_native)]
    pub type AssetEstimatesInNative<T: Config> =
        StorageMap<_, Identity, AssetId, BalanceOf<T>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn asset_cost_estimates_in_native)]
    pub type AssetCostEstimatesInNative<T: Config> =
        StorageMap<_, Identity, AssetId, BalanceOf<T>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn gateways_overview)]
    pub type GatewaysOverviewStore<T: Config> =
        StorageValue<_, Vec<GatewayActivity<BlockNumberFor<T>>>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn verifier_overview)]
    pub type VerifierOverviewStore<T: Config> =
        StorageValue<_, Vec<FinalityVerifierActivity<BlockNumberFor<T>>>, ValueQuery>;

    #[pallet::storage]
    #[pallet::getter(fn gateways_overview_history)]
    pub type GatewaysOverviewStoreHistory<T: Config> = StorageMap<
        _,
        Twox64Concat,
        TargetId,                                // Gateway Id
        Vec<GatewayActivity<BlockNumberFor<T>>>, // Activity
        ValueQuery,
    >;

    #[pallet::storage]
    #[pallet::getter(fn verifier_overview_history)]
    pub type VerifierOverviewStoreHistory<T: Config> = StorageMap<
        _,
        Twox64Concat,
        GatewayVendor,                                    // Gateway Id
        Vec<FinalityVerifierActivity<BlockNumberFor<T>>>, // Activity
        ValueQuery,
    >;

    // Keep last 10 epoch estimates
    #[pallet::storage]
    #[pallet::getter(fn epoch_history)]
    pub type EpochHistory<T: Config> =
        StorageMap<_, Identity, GatewayVendor, Vec<EpochEstimate<BlockNumberFor<T>>>>;

    // The genesis config type.
    #[pallet::genesis_config]
    #[derive(frame_support::DefaultNoBound)]
    pub struct GenesisConfig<T: Config> {
        pub known_gateway_records: Vec<u8>,
        // GatewayRecord is not serializable with DefaultNoBound with current serde settings. Debug what changed after v1.0.0 update.
        // pub known_gateway_records: Vec<GatewayRecord<T::AccountId>>,
        // pub standard_sfx_abi: Vec<(Sfx4bId, SFXAbi)>,
        pub standard_sfx_abi: Vec<u8>,
        #[serde(skip)]
        pub _marker: PhantomData<T>,
    }

    /// The build of genesis for the pallet.
    /// Populates storage with the known XDNS Records
    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            let known_gateway_records: Vec<GatewayRecord<T::AccountId>> =
                Decode::decode(&mut &self.known_gateway_records[..]).unwrap_or_default();

            let standard_sfx_abi: Vec<(Sfx4bId, SFXAbi)> =
                Decode::decode(&mut &self.standard_sfx_abi[..]).unwrap_or_default();

            for (sfx_4b_id, sfx_abi) in standard_sfx_abi {
                let _sfx_4b_str = sp_std::str::from_utf8(sfx_4b_id.as_slice())
                    .unwrap_or("invalid utf8 4b sfx id format");
                <StandardSFXABIs<T>>::insert(sfx_4b_id, sfx_abi);
            }

            for gateway_record in known_gateway_records {
                Pallet::<T>::override_gateway(
                    gateway_record.gateway_id,
                    gateway_record.verification_vendor,
                    gateway_record.execution_vendor,
                    gateway_record.codec,
                    gateway_record.registrant,
                    gateway_record.escrow_account,
                    gateway_record.allowed_side_effects,
                )
                .map_err(|e| {
                    log::error!(
                        "XDNS -- on-genesis: failed to add gateway via override_gateway: {:?}",
                        e
                    );
                })
                .ok();
            }
        }
    }

    impl<T: Config> Pallet<T> {
        pub fn do_reboot_self_gateway(
            origin: OriginFor<T>,
            vendor: GatewayVendor,
        ) -> DispatchResult {
            let admin: T::AccountId = ensure_signed_or_root(origin)?.unwrap_or(
                T::TreasuryAccounts::get_treasury_account(TreasuryAccount::Escrow),
            );

            // Refresh the list of StandardABI based on latest implementation
            // Purge all standards first
            if <StandardSFXABIs<T>>::iter().count() > 0 {
                <StandardSFXABIs<T>>::remove_all(None);
            }
            // Re-add all standards
            t3rn_abi::standard::standard_sfx_abi()
                .iter()
                .for_each(|(sfx_4b_id, sfx_abi)| {
                    <StandardSFXABIs<T>>::insert(sfx_4b_id, sfx_abi.clone());
                });

            let target_id = T::SelfGatewayId::get();

            const BALANCES_INDEX: u8 = 10;
            const ASSETS_INDEX: u8 = 12;
            const EVM_INDEX: u8 = 120;
            const WASM_INDEX: u8 = 121;

            let mut allowed_side_effects = vec![];

            if <StandardSFXABIs<T>>::contains_key(*b"tran") {
                allowed_side_effects.push((*b"tran", Some(BALANCES_INDEX)));
            }
            if <StandardSFXABIs<T>>::contains_key(*b"tddd") {
                allowed_side_effects.push((*b"tran", Some(ASSETS_INDEX)));
            }
            if <StandardSFXABIs<T>>::contains_key(*b"tass") {
                allowed_side_effects.push((*b"tran", Some(ASSETS_INDEX)));
            }
            if <StandardSFXABIs<T>>::contains_key(*b"cevm") {
                allowed_side_effects.push((*b"cevm", Some(EVM_INDEX)));
            }
            if <StandardSFXABIs<T>>::contains_key(*b"wasm") {
                allowed_side_effects.push((*b"wasm", Some(WASM_INDEX)));
            }

            Pallet::<T>::override_gateway(
                target_id,
                vendor,
                ExecutionVendor::Substrate,
                Codec::Scale,
                Some(admin.clone()),
                Some(admin),
                allowed_side_effects,
            )?;

            // If standards are linked to Gateways, refresh them all at once
            t3rn_abi::standard::standard_sfx_abi().iter_mut().for_each(
                |(ref sfx_4b_id, ref mut sfx_abi)| {
                    <SFXABIRegistry<T>>::iter_keys().for_each(|(gateway_id, on_dest_sfx_4b_id)| {
                        if *sfx_4b_id == on_dest_sfx_4b_id {
                            <SFXABIRegistry<T>>::mutate(gateway_id, sfx_4b_id, |target_sfx_abi| {
                                sfx_abi.maybe_prefix_memo = match target_sfx_abi {
                                    Some(target_sfx_abi) => target_sfx_abi.maybe_prefix_memo,
                                    None => None,
                                };
                                *target_sfx_abi = Some(sfx_abi.clone());
                            });
                        }
                    })
                },
            );

            Ok(())
        }

        pub fn update_epoch_history(
            verifier: &GatewayVendor,
            epoch_duration_in_remote_blocks: frame_system::pallet_prelude::BlockNumberFor<T>,
            epoch_duration_in_local_blocks: frame_system::pallet_prelude::BlockNumberFor<T>,
        ) {
            const N: usize = 10; // Number of epochs to consider for the moving average

            let mut history = EpochHistory::<T>::get(verifier).unwrap_or_default();

            // Remove the oldest elements until length is no longer than N
            while history.len() > N {
                history.remove(0);
            }

            // Calculate moving averages
            let moving_average_local = history
                .iter()
                .map(|e| e.local.saturated_into::<u32>())
                .sum::<u32>()
                .checked_div(history.len() as u32)
                .unwrap_or(Zero::zero())
                .into();

            let moving_average_remote = history
                .iter()
                .map(|e| e.remote.saturated_into::<u32>())
                .sum::<u32>()
                .checked_div(history.len() as u32)
                .unwrap_or(Zero::zero())
                .into();

            let epoch_duration_estimate_update = EpochEstimate::<BlockNumberFor<T>> {
                remote: epoch_duration_in_remote_blocks,
                local: epoch_duration_in_local_blocks,
                moving_average_local,
                moving_average_remote,
            };

            history.push(epoch_duration_estimate_update);

            EpochHistory::<T>::insert(verifier, history);
        }
    }

    impl<T: Config> LightClientAsyncAPI<T> for Pallet<T> {
        fn on_new_epoch(
            verifier: GatewayVendor,
            new_epoch: frame_system::pallet_prelude::BlockNumberFor<T>,
            current_hearbeat: LightClientHeartbeat<T>,
        ) {
            Self::process_single_verifier_overview(
                <frame_system::Pallet<T>>::block_number(),
                verifier,
                new_epoch,
                current_hearbeat,
            );
        }
    }

    impl<T: Config> Xdns<T, BalanceOf<T>> for Pallet<T> {
        /// Fetches all known Gateway records
        fn fetch_gateways() -> Vec<GatewayRecord<T::AccountId>> {
            Gateways::<T>::iter_values().collect()
        }

        /// Register new token assuming self::SelfGatewayIdOptimistic as a base chain
        fn register_new_token(
            origin: &OriginFor<T>,
            token_id: AssetId,
            token_props: TokenInfo,
        ) -> DispatchResult {
            if T::AssetsOverlay::contains_asset(&token_id) {
                return Err(Error::<T>::TokenRecordAlreadyExists.into())
            }

            if <AllTokenIds<T>>::get().contains(&token_id) {
                return Err(Error::<T>::TokenRecordAlreadyExists.into())
            }

            let admin: T::AccountId = ensure_signed_or_root(origin.clone())?.unwrap_or(
                T::TreasuryAccounts::get_treasury_account(TreasuryAccount::Escrow),
            );

            T::AssetsOverlay::force_create_asset(
                origin.clone(),
                token_id,
                admin,
                true,
                T::Currency::minimum_balance(),
            )?;

            let gateway_id = T::SelfGatewayId::get();

            Self::link_token_to_gateway(token_id, gateway_id, token_props)?;

            <AllTokenIds<T>>::append(token_id);

            Self::deposit_event(Event::<T>::NewTokenAssetRegistered(token_id, gateway_id));

            Ok(())
        }

        // Link existing token to a gateway. Assume that the token is already registered in the assets overlay via register_new_token
        fn link_token_to_gateway(
            token_id: AssetId,
            gateway_id: TargetId,
            token_props: TokenInfo,
        ) -> DispatchResult {
            // fetch record and ensure it exists
            let _record =
                <Gateways<T>>::get(gateway_id).ok_or(Error::<T>::GatewayRecordNotFound)?;

            // early exit if record already exists in storage
            if <Tokens<T>>::contains_key(token_id, gateway_id) {
                return Err(Error::<T>::TokenRecordAlreadyExists.into())
            }

            ensure!(
                T::AssetsOverlay::contains_asset(&token_id),
                Error::<T>::TokenRecordNotFoundInAssetsOverlay
            );

            Self::override_token(token_id, gateway_id, token_props)
        }

        fn override_token(
            token_id: AssetId,
            gateway_id: TargetId,
            token_props: TokenInfo,
        ) -> DispatchResult {
            <Tokens<T>>::insert(
                token_id,
                gateway_id,
                TokenRecord {
                    token_id,
                    gateway_id,
                    token_props,
                },
            );

            <GatewayTokens<T>>::mutate(gateway_id, |tokens| {
                if !tokens.contains(&token_id) {
                    tokens.push(token_id);
                }
            });

            // Make sure that the token is added to the list of all tokens
            if !<AllTokenIds<T>>::get().contains(&token_id) {
                <AllTokenIds<T>>::append(token_id);
            }

            Self::deposit_event(Event::<T>::NewTokenLinkedToGateway(token_id, gateway_id));
            Ok(())
        }

        fn list_available_mint_assets(gateway_id: TargetId) -> Vec<TokenRecord> {
            let mut available_assets = Vec::new();
            for (asset, target) in <AuthorizedMintAssets<T>>::get() {
                if let Some(linked_asset) = <Tokens<T>>::get(asset, gateway_id) {
                    if target == gateway_id {
                        available_assets.push(linked_asset);
                    }
                }
            }
            available_assets
        }

        fn check_asset_is_mintable(gateway_id: TargetId, asset_id: AssetId) -> bool {
            Self::list_available_mint_assets(gateway_id)
                .iter()
                .any(|token| token.token_id == asset_id)
        }

        fn get_token_by_eth_address(
            gateway_id: TargetId,
            eth_address: sp_core::H160,
        ) -> Result<TokenRecord, DispatchError> {
            for token in <GatewayTokens<T>>::get(gateway_id) {
                let token_record = <Tokens<T>>::get(token, gateway_id)
                    .ok_or(Error::<T>::TokenRecordNotFoundInGateway)?;
                match token_record.token_props {
                    TokenInfo::Ethereum(ref eth_token) =>
                        if eth_token.address == Some(eth_address.into()) {
                            return Ok(token_record.clone())
                        },
                    TokenInfo::Substrate(_) =>
                        return Err(Error::<T>::TokenRecordNotFoundInGateway.into()),
                }
            }
            Err(Error::<T>::TokenRecordNotFoundInGateway.into())
        }

        fn add_new_gateway(
            gateway_id: TargetId,
            verification_vendor: GatewayVendor,
            execution_vendor: ExecutionVendor,
            codec: Codec,
            registrant: Option<T::AccountId>,
            escrow_account: Option<T::AccountId>,
            allowed_side_effects: Vec<(TargetId, Option<u8>)>,
        ) -> DispatchResult {
            // early exit if record already exists in storage
            if <Gateways<T>>::contains_key(gateway_id) {
                return Err(Error::<T>::GatewayRecordAlreadyExists.into())
            }

            Self::override_gateway(
                gateway_id,
                verification_vendor,
                execution_vendor,
                codec,
                registrant,
                escrow_account,
                allowed_side_effects,
            )
        }

        fn override_gateway(
            gateway_id: TargetId,
            verification_vendor: GatewayVendor,
            execution_vendor: ExecutionVendor,
            codec: Codec,
            registrant: Option<T::AccountId>,
            escrow_account: Option<T::AccountId>,
            allowed_side_effects: Vec<(TargetId, Option<u8>)>,
        ) -> DispatchResult {
            // Populate standard side effect ABI registry
            for (sfx_4b_id, maybe_event_memo_prefix) in allowed_side_effects.iter() {
                match <StandardSFXABIs<T>>::get(sfx_4b_id) {
                    Some(mut abi) => {
                        abi.maybe_prefix_memo = *maybe_event_memo_prefix;
                        <SFXABIRegistry<T>>::insert(gateway_id, sfx_4b_id, abi)
                    },
                    None => {
                        let _sfx_4b_str = sp_std::str::from_utf8(sfx_4b_id.as_slice())
                            .unwrap_or("invalid utf8 4b sfx id format");
                        log::error!(
                            "ABI not found for {:?}; override_gateway failed.",
                            sfx_4b_id
                        );
                        return Err(Error::<T>::SideEffectABINotFound.into())
                    },
                }
            }
            <Gateways<T>>::insert(
                gateway_id,
                GatewayRecord {
                    gateway_id,
                    verification_vendor,
                    execution_vendor,
                    codec,
                    registrant,
                    escrow_account,
                    allowed_side_effects,
                },
            );
            <AllGatewayIds<T>>::mutate(|ids| {
                ids.iter()
                    .position(|&id| id == gateway_id)
                    .map(|i| ids.remove(i));
                ids.push(gateway_id);
            });
            Self::deposit_event(Event::<T>::GatewayRecordStored(gateway_id));

            Ok(())
        }

        fn extend_sfx_abi(
            origin: OriginFor<T>,
            gateway_id: ChainId,
            sfx_4b_id: Sfx4bId,
            sfx_expected_abi: SFXAbi,
        ) -> DispatchResult {
            ensure_root(origin)?;
            if !<Gateways<T>>::contains_key(gateway_id) {
                return Err(Error::<T>::XdnsRecordNotFound.into())
            }

            <SFXABIRegistry<T>>::mutate(gateway_id, sfx_4b_id, |sfx_abi| match sfx_abi {
                Some(_) => Err(Error::<T>::SideEffectABIAlreadyExists),
                None => {
                    *sfx_abi = Some(sfx_expected_abi);
                    Ok(())
                },
            })?;

            Ok(())
        }

        fn override_sfx_abi(
            gateway_id: ChainId,
            new_sfx_abis: Vec<(Sfx4bId, SFXAbi)>,
        ) -> DispatchResult {
            if !<Gateways<T>>::contains_key(gateway_id) {
                return Err(Error::<T>::XdnsRecordNotFound.into())
            }
            // mutate allowed side effects field in gateway record
            let mut gateway_record =
                <Gateways<T>>::get(gateway_id).ok_or(Error::<T>::XdnsRecordNotFound)?;

            gateway_record.allowed_side_effects = new_sfx_abis
                .iter()
                .map(|(sfx_4b_id, abi)| (*sfx_4b_id, abi.maybe_prefix_memo))
                .collect();

            <Gateways<T>>::insert(gateway_id, gateway_record);

            for (sfx_4b_id, sfx_expected_abi) in new_sfx_abis {
                <SFXABIRegistry<T>>::mutate(gateway_id, sfx_4b_id, |sfx_abi| {
                    *sfx_abi = Some(sfx_expected_abi);
                });
            }

            Ok(())
        }

        fn get_all_sfx_abi(gateway_id: &ChainId) -> Vec<(Sfx4bId, SFXAbi)> {
            <SFXABIRegistry<T>>::iter_prefix(gateway_id)
                .map(|(sfx_4b_id, sfx_abi)| (sfx_4b_id, sfx_abi))
                .collect()
        }

        fn get_sfx_abi(gateway_id: &ChainId, sfx_4b_id: Sfx4bId) -> Option<SFXAbi> {
            <SFXABIRegistry<T>>::get(gateway_id, sfx_4b_id)
        }

        fn add_escrow_account(
            origin: OriginFor<T>,
            gateway_id: ChainId,
            escrow_account: T::AccountId,
        ) -> DispatchResult {
            ensure_root(origin)?;

            Gateways::<T>::mutate(gateway_id, |gateway| match gateway {
                None => Err(Error::<T>::GatewayRecordNotFound),
                Some(record) => {
                    record.escrow_account = Some(escrow_account);
                    Ok(())
                },
            })?;

            Ok(())
        }

        /// returns a mapping of all allowed side_effects of a gateway.
        fn allowed_side_effects(gateway_id: &ChainId) -> Vec<(Sfx4bId, Option<u8>)> {
            match <Gateways<T>>::get(gateway_id) {
                Some(gateway) => gateway.allowed_side_effects,
                None => Vec::new(),
            }
        }

        fn get_gateway_max_security_lvl(chain_id: &ChainId) -> SecurityLvl {
            if chain_id == &[3u8; 4] {
                return SecurityLvl::Escrow
            }

            match Self::get_escrow_account(chain_id) {
                Ok(_) => SecurityLvl::Escrow,
                Err(_) => SecurityLvl::Optimistic,
            }
        }

        /// returns the gateway vendor of a gateway if its available
        fn get_verification_vendor(chain_id: &ChainId) -> Result<GatewayVendor, DispatchError> {
            match <Gateways<T>>::get(chain_id) {
                Some(rec) => Ok(rec.verification_vendor),
                None => Err(Error::<T>::XdnsRecordNotFound.into()),
            }
        }

        fn get_target_codec(chain_id: &ChainId) -> Result<Codec, DispatchError> {
            match <Gateways<T>>::get(chain_id) {
                Some(rec) => Ok(rec.codec),
                None => Err(Error::<T>::XdnsRecordNotFound.into()),
            }
        }

        fn get_escrow_account(chain_id: &ChainId) -> Result<Bytes, DispatchError> {
            match <Gateways<T>>::get(chain_id) {
                Some(rec) => match rec.escrow_account {
                    Some(account) => Ok(account.encode()),
                    None => Err(Error::<T>::EscrowAccountNotFound.into()),
                },
                None => Err(Error::<T>::XdnsRecordNotFound.into()),
            }
        }

        fn fetch_full_gateway_records() -> Vec<FullGatewayRecord<T::AccountId>> {
            Gateways::<T>::iter_values()
                .map(|gateway| {
                    let tokens = Tokens::<T>::iter_values()
                        .filter(|token| token.gateway_id == gateway.gateway_id)
                        .collect();
                    FullGatewayRecord {
                        gateway_record: gateway,
                        tokens,
                    }
                })
                .collect()
        }

        fn read_last_activity(gateway_id: ChainId) -> Option<GatewayActivity<BlockNumberFor<T>>> {
            Self::read_last_activity_overview()
                .into_iter()
                .find(|activity| activity.gateway_id == gateway_id)
        }

        fn read_last_activity_overview() -> Vec<GatewayActivity<BlockNumberFor<T>>> {
            let mut overview = <GatewaysOverviewStore<T>>::get();
            // get the latest update
            let latest_update = overview
                .iter()
                .max_by_key(|activity| activity.reported_at)
                .map(|activity| activity.reported_at)
                .unwrap_or_default();

            if latest_update != <frame_system::Pallet<T>>::block_number() {
                Self::process_overview(<frame_system::Pallet<T>>::block_number());
                overview = <GatewaysOverviewStore<T>>::get();
            }

            overview
        }

        fn is_target_active(gateway_id: TargetId, security_lvl: &SecurityLvl) -> bool {
            match Self::read_last_activity(gateway_id) {
                Some(activity) => activity.security_lvl >= *security_lvl && activity.is_active,
                None => false,
            }
        }

        fn get_remote_order_contract_address(gateway_id: TargetId) -> Result<H256, DispatchError> {
            <RemoteOrderAddresses<T>>::get(gateway_id)
                .ok_or(Error::<T>::RemoteOrderAddressNotFound.into())
        }

        fn get_remote_bidding_contract_address(
            gateway_id: TargetId,
        ) -> Result<H256, DispatchError> {
            <RemoteBiddingAddresses<T>>::get(gateway_id)
                .ok_or(Error::<T>::RemoteOrderAddressNotFound.into())
        }

        fn get_self_token_id() -> AssetId {
            T::SelfTokenId::get()
        }

        fn mint(asset_id: AssetId, user: T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
            assert!(
                Self::check_asset_is_mintable(T::SelfGatewayId::get(), asset_id),
                "Asset is not mintable"
            );
            log::debug!(
                "attempt of minting asset: {:?} for user: {:?} with amount: {:?} on chain: {:?}",
                asset_id,
                user,
                amount,
                T::SelfGatewayId::get()
            );
            T::AssetsOverlay::mint(
                T::RuntimeOrigin::from(frame_system::RawOrigin::Signed(
                    T::TreasuryAccounts::get_treasury_account(TreasuryAccount::Escrow),
                )),
                asset_id,
                user,
                amount,
            )
        }

        fn burn(asset_id: AssetId, user: T::AccountId, amount: BalanceOf<T>) -> DispatchResult {
            assert!(
                Self::check_asset_is_mintable(T::SelfGatewayId::get(), asset_id),
                "Asset is not mintable"
            );
            T::AssetsOverlay::burn(
                T::RuntimeOrigin::from(frame_system::RawOrigin::Signed(
                    T::TreasuryAccounts::get_treasury_account(TreasuryAccount::Escrow),
                )),
                asset_id,
                user,
                amount,
            )
        }

        fn verify_active(
            gateway_id: &ChainId,
            max_acceptable_heartbeat_offset: frame_system::pallet_prelude::BlockNumberFor<T>,
            security_lvl: &SecurityLvl,
        ) -> Result<LightClientHeartbeat<T>, DispatchError> {
            let heartbeat = T::Portal::get_latest_heartbeat(gateway_id)
                .map_err(|_| Error::<T>::GatewayNotActive)?;

            if heartbeat.is_halted
                || !heartbeat.ever_initialized
                || max_acceptable_heartbeat_offset
                    > frame_system::Pallet::<T>::block_number()
                        .saturating_sub(heartbeat.last_heartbeat)
            {
                return Err(Error::<T>::GatewayNotActive.into())
            }

            if security_lvl == &SecurityLvl::Escrow {
                T::AttestersRead::get_activated_targets()
                    .iter()
                    .find(|target| target == &gateway_id)
                    .ok_or(Error::<T>::GatewayNotActive)?;
            }

            Ok(heartbeat)
        }

        fn get_slowest_verifier_target(
            all_targets: Vec<TargetId>,
            speed_mode: &SpeedMode,
            emergency_offset: frame_system::pallet_prelude::BlockNumberFor<T>,
        ) -> Option<(
            GatewayVendor,
            TargetId,
            frame_system::pallet_prelude::BlockNumberFor<T>,
            frame_system::pallet_prelude::BlockNumberFor<T>,
        )> {
            // map all targets to their respective vendors, and collect them into a BTreeSet to eliminate duplicates
            let all_distinct_verifiers: BTreeSet<_> = all_targets
                .iter()
                .filter_map(|target| {
                    let vendor = Self::get_verification_vendor(target);
                    vendor.ok().map(|vendor| (vendor, *target))
                })
                .collect();

            all_distinct_verifiers
                .iter()
                .map(|(vendor, target)| {
                    let epoch_history = <EpochHistory<T>>::get(vendor);
                    let (local_offset, remote_offset) =
                        vendor.calculate_offsets(speed_mode, emergency_offset, epoch_history);
                    (vendor.clone(), *target, local_offset, remote_offset)
                })
                .max_by_key(|(_, _, submit_by_local_offset, _)| *submit_by_local_offset)
        }

        fn estimate_adaptive_timeout_on_slowest_target(
            all_targets: Vec<TargetId>,
            speed_mode: &SpeedMode,
            emergency_offset: frame_system::pallet_prelude::BlockNumberFor<T>,
        ) -> AdaptiveTimeout<frame_system::pallet_prelude::BlockNumberFor<T>, TargetId> {
            let current_block = <frame_system::Pallet<T>>::block_number();

            let (slowest_verifier, target, submit_by_local_offset, submit_by_remote_offset) =
                match Self::get_slowest_verifier_target(all_targets, speed_mode, emergency_offset) {
                    Some(values) => values,
                    None => return AdaptiveTimeout::default_401(),
                };

            let latest_overview_of_verifier =
                match <VerifierOverviewStoreHistory<T>>::get(slowest_verifier).last() {
                    Some(overview) => overview.clone(),
                    None => return AdaptiveTimeout::default_401(),
                };

            let submit_by_height_here = current_block.saturating_add(submit_by_local_offset);
            let submit_by_height_there = latest_overview_of_verifier
                .finalized_height
                .saturating_add(submit_by_remote_offset);
            let estimated_height_here =
                submit_by_height_here.saturating_add(submit_by_local_offset);
            let estimated_height_there =
                submit_by_height_there.saturating_add(submit_by_remote_offset);

            AdaptiveTimeout {
                estimated_height_here,
                estimated_height_there,
                submit_by_height_here,
                submit_by_height_there,
                there: target,
                emergency_timeout_here: emergency_offset.saturating_add(current_block),
                dlq: None,
            }
        }
    }
}