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
use cid::multihash::MultihashDigest;
use std::collections::BTreeMap;
use std::result;
use std::sync::Arc;

use ex3_balance_vault_public_types::{
    AcceptedWithdrawalShardingSubReport, BalanceDataIntegrityShardingSubReport,
    CandidBalanceShardingSubReport, RejectedWithdrawalShardingSubReport,
};
use ex3_block_reports::BlockWithShardingReports;
use ex3_blockchain_public_types::{
    CandidConsensusReport, IdCursorReport, NewSnapshotVaultActivatedReport, SnapshotCursor,
    WithdrawalCounterReport,
};
use ex3_core_registry_public_types::{
    CoreRegistryDataIntegrityShardingSubReport, CoreRegistryUpdateTxShardingSubReport,
};
use ex3_crypto::sha256;
use ex3_deposit_detector_public_types::{
    ConsumedDepositShardingSubReport, DepositDetectorDataIntegrityShardingSubReport,
};
use ex3_node_error::Error;
use ex3_node_types::balance_changed::BalanceChanged;
use ex3_node_types::block::{Block, BlockBody, BlockBodyExt, BlockHeader};
use ex3_node_types::number::Ex3Uint;
use ex3_node_types::transaction::{EncodedTransaction, Transaction, TransactionType};
use ex3_node_types::{
    AssetId, BalanceVaultSeqId, BlockHash, BlockHeight, CanisterId, PackageId, SecretVaultSeqId,
    Version, WalletRegistrySeqId,
};
use ex3_node_types::{MerkleNode, WalletRegisterId};
use ex3_secret_vault_public_types::{
    SecretUpdateTxShardingSubReport, SecretVaultDataIntegrityShardingSubReport,
};
use ex3_serde::bincode;
use ex3_wallet_registry_public_types::{
    WalletRegistryDataIntegrityShardingSubReport, WalletTxShardingSubReport,
};
use num_traits::{One, ToPrimitive};
use rs_merkle::algorithms::Sha256;
use rs_merkle::MerkleTree;

pub type Result<T> = result::Result<T, Error>;

pub struct SnapshotVaultIndex {
    wallet_registry_count: u32,
    secret_vault_count: u32,
    balance_vault_count: u32,
}

pub struct BlockBuilder {
    core_registry_canister_id: CanisterId,
    deposit_detector_canister_id: CanisterId,
    max_core_registry_update_tx_per_sharding_sub_report: usize,
    max_wallet_register_txs_per_sharding_sub_report: usize,
    max_secret_update_txs_per_sharding_sub_report: usize,
    max_balance_changed_records_per_sharding_sub_report: usize,
    max_withdrawal_txs_per_sharding_sub_report: usize,
    max_consumed_deposit_per_sharding_sub_report: usize,
    max_wallets_per_balance_vault_canister: usize,
    max_wallets_per_registry_canister: usize,
    max_wallets_per_secret_vault_canister: usize,
    start_wallet_id: usize,
}

mod balance_vault;
mod core_registry;
mod deposit_detector;
mod secret_vault;
mod wallet_registry;

impl BlockBuilder {
    pub fn new(
        core_registry_canister_id: CanisterId,
        deposit_detector_canister_id: CanisterId,
        max_core_registry_update_tx_per_sharding_sub_report: usize,
        max_wallet_register_txs_per_sharding_sub_report: usize,
        max_secret_update_txs_per_sharding_sub_report: usize,
        max_balance_changed_records_per_sharding_sub_report: usize,
        max_withdrawal_txs_per_sharding_sub_report: usize,
        max_consumed_deposit_per_sharding_sub_report: usize,
        max_wallets_per_balance_vault_canister: usize,
        max_wallets_per_registry_canister: usize,
        max_wallets_per_secret_vault_canister: usize,
        start_wallet_id: usize,
    ) -> Self {
        Self {
            core_registry_canister_id,
            deposit_detector_canister_id,
            max_core_registry_update_tx_per_sharding_sub_report,
            max_wallet_register_txs_per_sharding_sub_report,
            max_secret_update_txs_per_sharding_sub_report,
            max_balance_changed_records_per_sharding_sub_report,
            max_withdrawal_txs_per_sharding_sub_report,
            max_consumed_deposit_per_sharding_sub_report,
            max_wallets_per_balance_vault_canister,
            max_wallets_per_registry_canister,
            max_wallets_per_secret_vault_canister,
            start_wallet_id,
        }
    }

    pub fn build(
        &self,
        pre_block_hash: BlockHash,
        block_height: BlockHeight,
        last_process_p_id: PackageId,
        snapshot_cursor_pre_height: SnapshotCursor,
        txs: Vec<Transaction>,
        rejected_tx_indexes: Vec<u32>,
        balances_changed: Vec<(WalletRegisterId, Vec<(AssetId, BalanceChanged)>)>,
    ) -> Result<BlockWithShardingReports> {
        let (
            core_registry_tx_sharding_sub_reports,
            wallet_registry_sharding_sub_reports,
            secret_sharding_sub_reports,
            balance_sharding_sub_reports,
            deposit_detector_sharding_sub_reports,
            encoded_txs,
            tx_merkle_tree,
            rejected_tx_merkle_tree,
            balances_changed_merkle_tree,
            new_asset_count,
            new_market_count,
            new_wallet_count,
            total_accepted_withdrawal_count,
            total_rejected_withdrawal_count,
        ) = self.generate_sharding_data_sub_reports(
            &snapshot_cursor_pre_height,
            &txs,
            &rejected_tx_indexes,
            &balances_changed,
        );

        let (
            new_snapshot_vault_activated_report,
            id_cursor_report,
            core_registry_data_integrity_sharding_sub_report,
            wallet_registry_data_integrity_sharding_sub_reports,
            secret_vault_data_integrity_sharding_sub_reports,
            balance_vault_data_integrity_sharding_sub_reports,
            deposit_detector_data_integrity_sharding_sub_report,
            data_integrity_merkle_tree,
        ) = self.generate_data_integrity_sharding_sub_reports(
            &block_height,
            &core_registry_tx_sharding_sub_reports,
            &wallet_registry_sharding_sub_reports,
            &secret_sharding_sub_reports,
            &balance_sharding_sub_reports,
            &deposit_detector_sharding_sub_reports,
            &snapshot_cursor_pre_height,
            new_asset_count,
            new_market_count,
            new_wallet_count,
        );

        let block_body = BlockBody {
            txs: encoded_txs,
            data_integrity: data_integrity_merkle_tree.leaves().unwrap(),
            rejected_tx_indexes,
        };

        let block_header = BlockHeader {
            version: Version::V0.encode(),
            height: block_height,
            p_id: last_process_p_id,
            pre_block_hash,
            tx_root: tx_merkle_tree.root().map_or([0u8; 32], |root| root),
            state_changed_root: balances_changed_merkle_tree
                .root()
                .map_or([0u8; 32], |root| root),
            data_integrity_root: data_integrity_merkle_tree
                .root()
                .expect("data integrity merkle tree should be correct"),
            rejected_tx_root: rejected_tx_merkle_tree
                .root()
                .map_or([0u8; 32], |root| root),
        };

        let withdrawal_counter_report = WithdrawalCounterReport {
            accepted_withdrawal_count: total_accepted_withdrawal_count.try_into().unwrap(),
            rejected_withdrawal_count: total_rejected_withdrawal_count.try_into().unwrap(),
        };

        let block = Block {
            head: block_header.clone(),
            body: Some(block_body),
        };
        let block_bytes = bincode::serialize(&block).unwrap();
        let hash = cid::multihash::Code::Sha2_256.digest(&block_bytes);
        let block_cid = cid::Cid::new_v1(0x55, hash);

        let block_with_reports = BlockWithShardingReports {
            block: Arc::new(block),
            block_body_ext: Arc::new(BlockBodyExt { balances_changed }),
            consensus_report: CandidConsensusReport {
                head: block_header.into(),
                cid: block_cid.to_bytes(),
                activated_vault_report: new_snapshot_vault_activated_report,
                id_cursor_report,
                withdrawal_counter_report,
            },
            core_registry_reports: core_registry_tx_sharding_sub_reports,
            core_registry_data_integrity_report: core_registry_data_integrity_sharding_sub_report,
            wallet_registry_sharding_sub_reports,
            wallet_registry_data_integrity_reports:
                wallet_registry_data_integrity_sharding_sub_reports,
            secret_sharding_sub_reports,
            secret_vault_data_integrity_reports: secret_vault_data_integrity_sharding_sub_reports,
            balance_sharding_sub_reports,
            balance_vault_data_integrity_reports: balance_vault_data_integrity_sharding_sub_reports,
            deposit_detector_sharding_sub_reports,
            deposit_detector_data_integrity_report:
                deposit_detector_data_integrity_sharding_sub_report,
        };

        Ok(block_with_reports)
    }

    fn generate_sharding_data_sub_reports(
        &self,
        snapshot_vault_cursor_of_pre_height: &SnapshotCursor,
        txs: &Vec<Transaction>,
        rejected_tx_indexes: &Vec<u32>,
        balances_changed: &Vec<(WalletRegisterId, Vec<(AssetId, BalanceChanged)>)>,
    ) -> (
        Vec<CoreRegistryUpdateTxShardingSubReport>,
        BTreeMap<WalletRegistrySeqId, Vec<WalletTxShardingSubReport>>,
        BTreeMap<SecretVaultSeqId, Vec<SecretUpdateTxShardingSubReport>>,
        BTreeMap<
            BalanceVaultSeqId,
            (
                Vec<CandidBalanceShardingSubReport>,
                Vec<AcceptedWithdrawalShardingSubReport>,
                Vec<RejectedWithdrawalShardingSubReport>,
            ),
        >,
        Vec<ConsumedDepositShardingSubReport>,
        Vec<EncodedTransaction>,
        MerkleTree<Sha256>, // tx merkle tree
        MerkleTree<Sha256>, // rejected tx merkle tree
        MerkleTree<Sha256>,
        usize, // new asset count
        usize, // new market count
        usize, // new wallet count
        usize, // total accepted withdrawal count
        usize, // total rejected withdrawal count
    ) {
        let mut encoded_txs = vec![];
        let mut encoded_rejected_txs = vec![];
        let mut core_registry_update_txs = vec![];
        let mut wallet_tx_map = BTreeMap::new();
        let mut secret_vault_tx_map = BTreeMap::new();
        let mut balances_changed_map = BTreeMap::new();
        let mut balance_vault_accepted_withdrawal_tx_map = BTreeMap::new();
        let mut balance_vault_rejected_withdrawal_tx_map = BTreeMap::new();
        let mut deposit_txs = vec![];
        let mut new_asset_count = 0usize;
        let mut new_market_count = 0usize;
        let mut new_wallet_count = 0usize;
        let mut total_accepted_withdrawal_count = 0usize;
        let mut total_rejected_withdrawal_count = 0usize;
        let mut next_wallet_id = snapshot_vault_cursor_of_pre_height
            .max_wallet_id
            .clone()
            .map_or(self.start_wallet_id.clone().into(), |max_wallet_id| {
                WalletRegisterId::from(max_wallet_id + Ex3Uint::one())
            });
        let rejected_tx_indexes_map = rejected_tx_indexes
            .iter()
            .map(|index| (index.clone() as usize, ()))
            .collect::<BTreeMap<usize, ()>>();

        for (index, tx) in txs.iter().enumerate() {
            match tx.r#type {
                TransactionType::WalletRegister => {
                    let wallet_registry_seq_id =
                        self.get_wallet_registry_seq_id_by_wallet_id(&next_wallet_id);
                    let encoded_tx = EncodedTransaction::from(tx);
                    encoded_txs.push(encoded_tx.clone());
                    wallet_tx_map
                        .entry(wallet_registry_seq_id)
                        .or_insert(vec![])
                        .push((encoded_tx, index));
                    next_wallet_id += 1u8;
                    new_wallet_count += 1;
                }
                TransactionType::AssetAccountBinding | TransactionType::AssetAccountUnbinding => {
                    let wallet_registry_seq_id =
                        self.get_wallet_registry_seq_id_by_wallet_id(&tx.from);
                    let encoded_tx = EncodedTransaction::from(tx);
                    encoded_txs.push(encoded_tx.clone());
                    wallet_tx_map
                        .entry(wallet_registry_seq_id)
                        .or_insert(vec![])
                        .push((encoded_tx, index));
                }
                TransactionType::CreateApiSecret
                | TransactionType::DestroyApiSecret
                | TransactionType::ResetMainSecret => {
                    let secret_vault_seq_id = self.get_secret_vault_seq_id_by_wallet_id(&tx.from);
                    let encoded_tx = EncodedTransaction::from(tx);
                    encoded_txs.push(encoded_tx.clone());
                    secret_vault_tx_map
                        .entry(secret_vault_seq_id)
                        .or_insert(vec![])
                        .push((encoded_tx, index));
                }
                TransactionType::Withdrawal | TransactionType::ForceWithdrawal => {
                    let balance_vault_seq_id = self.get_balance_vault_seq_id_by_wallet_id(&tx.from);
                    let encoded_tx = EncodedTransaction::from(tx);

                    if rejected_tx_indexes_map.contains_key(&index) {
                        encoded_rejected_txs.push(encoded_tx.clone());
                        balance_vault_rejected_withdrawal_tx_map
                            .entry(balance_vault_seq_id)
                            .or_insert(vec![])
                            .push((encoded_tx, index));
                        total_rejected_withdrawal_count += 1;
                    } else {
                        encoded_txs.push(encoded_tx.clone());
                        balance_vault_accepted_withdrawal_tx_map
                            .entry(balance_vault_seq_id)
                            .or_insert(vec![])
                            .push((encoded_tx, index));
                        total_accepted_withdrawal_count += 1;
                    }
                }
                TransactionType::RegisterAsset
                | TransactionType::UpdateAssetWithdrawalFeeTo
                | TransactionType::UpdateChainConfirmationTimes
                | TransactionType::UpdateGlobalWithdrawalFeeTo
                | TransactionType::RegisterSpotMarket
                | TransactionType::UpdateSpotMarketTradingSettings
                | TransactionType::UpdateSpotMarketInitialFeeTo
                | TransactionType::UpdateSpotMarketInitialTradingFee
                | TransactionType::UpdateSpotMarketFeeTo
                | TransactionType::UpdateSpotMarketTradingFee => {
                    if tx.r#type == TransactionType::RegisterAsset {
                        new_asset_count += 1;
                    } else if tx.r#type == TransactionType::RegisterSpotMarket {
                        new_market_count += 1;
                    }
                    let encoded_tx = EncodedTransaction::from(tx);
                    encoded_txs.push(encoded_tx.clone());
                    core_registry_update_txs.push((encoded_tx, index));
                }
                TransactionType::Deposit => {
                    let encoded_tx = EncodedTransaction::from(tx);
                    encoded_txs.push(encoded_tx.clone());
                    deposit_txs.push((encoded_tx, index));
                }
                _ => {
                    let encoded_tx = EncodedTransaction::from(tx);
                    encoded_txs.push(encoded_tx.clone());
                }
            }
        }

        let tx_merkle_tree = self.build_tx_merkle_tree(&encoded_txs);
        let rejected_txs_merkle_tree = self.build_rejected_txs_merkle_tree(&encoded_rejected_txs);
        let balances_changed_merkle_tree =
            self.build_balances_changed_merkle_tree(&balances_changed);

        for (index, (wallet_id, asset_changes)) in balances_changed.iter().enumerate() {
            let balance_vault_seq_id = self.get_balance_vault_seq_id_by_wallet_id(&wallet_id);
            balances_changed_map
                .entry(balance_vault_seq_id)
                .or_insert(vec![])
                .push(((wallet_id.clone(), asset_changes.clone()), index));
        }

        let core_registry_tx_sharding_sub_reports = self
            .generate_core_registry_update_tx_sharding_sub_reports(
                core_registry_update_txs,
                &tx_merkle_tree,
            );

        let wallet_register_tx_sharding_sub_reports = self.generate_wallet_tx_sharding_sub_reports(
            wallet_tx_map,
            &tx_merkle_tree,
            snapshot_vault_cursor_of_pre_height,
        );

        let secret_update_tx_sharding_sub_reports = self
            .generate_secret_update_tx_sharding_sub_reports(
                secret_vault_tx_map,
                &tx_merkle_tree,
                snapshot_vault_cursor_of_pre_height,
            );

        let consumed_deposit_sharding_sub_reports =
            self.generate_consumed_deposit_sharding_sub_reports(deposit_txs, &tx_merkle_tree);

        let balance_sharding_sub_reports = self.generate_balance_sharding_sub_reports(
            balances_changed_map,
            &balances_changed_merkle_tree,
            balance_vault_accepted_withdrawal_tx_map,
            &tx_merkle_tree,
            balance_vault_rejected_withdrawal_tx_map,
            &rejected_txs_merkle_tree,
            snapshot_vault_cursor_of_pre_height,
        );

        (
            core_registry_tx_sharding_sub_reports,
            wallet_register_tx_sharding_sub_reports,
            secret_update_tx_sharding_sub_reports,
            balance_sharding_sub_reports,
            consumed_deposit_sharding_sub_reports,
            encoded_txs,
            tx_merkle_tree,
            rejected_txs_merkle_tree,
            balances_changed_merkle_tree,
            new_asset_count,
            new_market_count,
            new_wallet_count,
            total_accepted_withdrawal_count,
            total_rejected_withdrawal_count,
        )
    }

    /// Generate data integrity sharding data integrity sub reports
    fn generate_data_integrity_sharding_sub_reports(
        &self,
        current_block_height: &BlockHeight,
        core_registry_tx_sharding_sub_reports: &Vec<CoreRegistryUpdateTxShardingSubReport>,
        wallet_register_tx_sharding_sub_reports: &BTreeMap<
            WalletRegistrySeqId,
            Vec<WalletTxShardingSubReport>,
        >,
        secret_update_tx_sharding_sub_reports: &BTreeMap<
            SecretVaultSeqId,
            Vec<SecretUpdateTxShardingSubReport>,
        >,
        balance_sharding_sub_reports: &BTreeMap<
            BalanceVaultSeqId,
            (
                Vec<CandidBalanceShardingSubReport>,
                Vec<AcceptedWithdrawalShardingSubReport>,
                Vec<RejectedWithdrawalShardingSubReport>,
            ),
        >,
        deposit_detector_sharding_sub_reports: &Vec<ConsumedDepositShardingSubReport>,
        snapshot_vault_cursor_of_pre_height: &SnapshotCursor,
        new_asset_count: usize,
        new_market_count: usize,
        new_wallet_count: usize,
    ) -> (
        NewSnapshotVaultActivatedReport,
        IdCursorReport,
        CoreRegistryDataIntegrityShardingSubReport,
        BTreeMap<WalletRegistrySeqId, WalletRegistryDataIntegrityShardingSubReport>,
        BTreeMap<SecretVaultSeqId, SecretVaultDataIntegrityShardingSubReport>,
        BTreeMap<BalanceVaultSeqId, BalanceDataIntegrityShardingSubReport>,
        DepositDetectorDataIntegrityShardingSubReport,
        MerkleTree<Sha256>,
    ) {
        let snapshot_vault_index = SnapshotVaultIndex::new(
            wallet_register_tx_sharding_sub_reports
                .len()
                .try_into()
                .unwrap(),
            secret_update_tx_sharding_sub_reports
                .len()
                .try_into()
                .unwrap(),
            balance_sharding_sub_reports.len().try_into().unwrap(),
        );

        let core_registry_data_integrity = self.generate_core_registry_data_integrity(
            current_block_height,
            core_registry_tx_sharding_sub_reports,
            &snapshot_vault_index,
        );

        let (wallet_registry_data_integrity, new_activated_wallet_registries) = self
            .generate_wallet_registry_data_integrity(
                current_block_height,
                &wallet_register_tx_sharding_sub_reports,
                snapshot_vault_cursor_of_pre_height,
                &snapshot_vault_index,
            );

        let (secret_vault_data_integrity, new_activated_secret_vaults) = self
            .generate_secret_vault_data_integrity(
                current_block_height,
                &secret_update_tx_sharding_sub_reports,
                snapshot_vault_cursor_of_pre_height,
                &snapshot_vault_index,
            );

        let (balance_vault_data_integrity, new_activated_balance_vaults) = self
            .generate_balance_vault_data_integrity(
                current_block_height,
                &balance_sharding_sub_reports,
                snapshot_vault_cursor_of_pre_height,
                &snapshot_vault_index,
            );

        let deposit_detector_data_integrity = self.generate_deposit_detector_data_integrity(
            current_block_height,
            deposit_detector_sharding_sub_reports,
            &snapshot_vault_index,
        );

        let id_cursor_report = IdCursorReport {
            wallet_id_cursor_increase: new_wallet_count.try_into().unwrap(),
            asset_id_cursor_increase: new_asset_count.try_into().unwrap(),
            market_id_cursor_increase: new_market_count.try_into().unwrap(),
        };

        let mut data_integrity = vec![];

        data_integrity.push(core_registry_data_integrity.clone());
        data_integrity.extend(wallet_registry_data_integrity.values());
        data_integrity.extend(secret_vault_data_integrity.values());
        data_integrity.extend(balance_vault_data_integrity.values());
        data_integrity.push(deposit_detector_data_integrity.clone());
        data_integrity.push(sha256(
            id_cursor_report.wallet_id_cursor_increase.to_le_bytes(),
        ));
        data_integrity.push(sha256(
            id_cursor_report.asset_id_cursor_increase.to_le_bytes(),
        ));
        data_integrity.push(sha256(
            id_cursor_report.market_id_cursor_increase.to_le_bytes(),
        ));

        let data_integrity_merkle_tree = self.build_data_integrity_merkle_tree(&data_integrity);

        let (
            core_registry_data_integrity_report,
            wallet_registry_data_integrity_reports,
            secret_vault_data_integrity_reports,
            balance_vault_data_integrity_reports,
            deposit_detector_data_integrity_report,
        ) = self.generate_data_integrity_reports(
            &snapshot_vault_index,
            &core_registry_data_integrity,
            &core_registry_tx_sharding_sub_reports,
            &wallet_registry_data_integrity,
            &wallet_register_tx_sharding_sub_reports,
            &secret_vault_data_integrity,
            &secret_update_tx_sharding_sub_reports,
            &balance_vault_data_integrity,
            &balance_sharding_sub_reports,
            &deposit_detector_data_integrity,
            &deposit_detector_sharding_sub_reports,
            &data_integrity_merkle_tree,
        );

        let new_snapshot_vault_activated_report = NewSnapshotVaultActivatedReport {
            wallet_registries: new_activated_wallet_registries,
            secret_vaults: new_activated_secret_vaults,
            balance_vaults: new_activated_balance_vaults,
        };

        (
            new_snapshot_vault_activated_report,
            id_cursor_report,
            core_registry_data_integrity_report,
            wallet_registry_data_integrity_reports,
            secret_vault_data_integrity_reports,
            balance_vault_data_integrity_reports,
            deposit_detector_data_integrity_report,
            data_integrity_merkle_tree,
        )
    }

    /// Generate data integrity reports
    fn generate_data_integrity_reports(
        &self,
        snapshot_vault_index: &SnapshotVaultIndex,
        core_registry_data_integrity: &MerkleNode,
        core_registry_sharding_sub_reports: &Vec<CoreRegistryUpdateTxShardingSubReport>,
        wallet_registry_data_integrity: &BTreeMap<WalletRegistrySeqId, MerkleNode>,
        wallet_registry_sharding_sub_reports: &BTreeMap<
            WalletRegistrySeqId,
            Vec<WalletTxShardingSubReport>,
        >,
        secret_vault_data_integrity: &BTreeMap<SecretVaultSeqId, MerkleNode>,
        secret_vault_sharding_sub_reports: &BTreeMap<
            SecretVaultSeqId,
            Vec<SecretUpdateTxShardingSubReport>,
        >,
        balance_vault_data_integrity: &BTreeMap<BalanceVaultSeqId, MerkleNode>,
        balance_sharding_sub_reports: &BTreeMap<
            BalanceVaultSeqId,
            (
                Vec<CandidBalanceShardingSubReport>,
                Vec<AcceptedWithdrawalShardingSubReport>,
                Vec<RejectedWithdrawalShardingSubReport>,
            ),
        >,
        deposit_detector_data_integrity: &MerkleNode,
        deposit_detector_sharding_sub_reports: &Vec<ConsumedDepositShardingSubReport>,
        data_integrity_merkle_tree: &MerkleTree<Sha256>,
    ) -> (
        CoreRegistryDataIntegrityShardingSubReport,
        BTreeMap<WalletRegistrySeqId, WalletRegistryDataIntegrityShardingSubReport>,
        BTreeMap<SecretVaultSeqId, SecretVaultDataIntegrityShardingSubReport>,
        BTreeMap<BalanceVaultSeqId, BalanceDataIntegrityShardingSubReport>,
        DepositDetectorDataIntegrityShardingSubReport,
    ) {
        let total_merkle_node_count = data_integrity_merkle_tree.leaves_len();

        /* core registry report generate */
        let core_registry_index = snapshot_vault_index.get_core_registry_index();
        let core_registry_merkle_proof =
            data_integrity_merkle_tree.proof(&[core_registry_index as usize]);
        let total_core_registry_update_tx_count = core_registry_sharding_sub_reports
            .iter()
            .fold(0, |acc, report| acc + report.txs.len());

        let core_registry_report = CoreRegistryDataIntegrityShardingSubReport {
            merkle_proof: core_registry_merkle_proof.to_bytes(),
            data_digest: core_registry_data_integrity.clone(),
            merkle_node_count: total_merkle_node_count.try_into().unwrap(),
            index: core_registry_index,
            total_update_tx_count: total_core_registry_update_tx_count.try_into().unwrap(),
        };

        /* wallet registry report generate */
        let wallet_registry_reports = wallet_registry_data_integrity
            .iter()
            .map(|(seq_id, data_integrity)| {
                let vault_index = snapshot_vault_index.get_wallet_registry_index(seq_id);
                let sub_reports = wallet_registry_sharding_sub_reports.get(seq_id);
                let merkle_proof = data_integrity_merkle_tree.proof(&[vault_index as usize]);
                let total_register_tx_count = sub_reports.map_or(0, |reports| {
                    reports.iter().map(|report| report.txs.len()).sum::<usize>()
                });
                let report = WalletRegistryDataIntegrityShardingSubReport {
                    merkle_proof: merkle_proof.to_bytes(),
                    data_digest: data_integrity.clone(),
                    index: vault_index,
                    merkle_node_count: total_merkle_node_count.try_into().unwrap(),
                    total_register_tx_count: total_register_tx_count.try_into().unwrap(),
                };
                (seq_id.clone(), report)
            })
            .collect();

        let secret_vault_reports = secret_vault_data_integrity
            .iter()
            .map(|(seq_id, data_integrity)| {
                let vault_index = snapshot_vault_index.get_secret_vault_index(seq_id);
                let merkle_proof = data_integrity_merkle_tree.proof(&[vault_index as usize]);
                let total_update_tx_count = secret_vault_sharding_sub_reports
                    .get(seq_id)
                    .map_or(0, |reports| {
                        reports.iter().map(|report| report.txs.len()).sum::<usize>()
                    });
                let report = SecretVaultDataIntegrityShardingSubReport {
                    merkle_proof: merkle_proof.to_bytes(),
                    data_digest: data_integrity.clone(),
                    merkle_node_count: total_merkle_node_count.try_into().unwrap(),
                    index: vault_index,
                    total_update_tx_count: total_update_tx_count.try_into().unwrap(),
                };
                (seq_id.clone(), report)
            })
            .collect();

        let balance_vault_reports = balance_vault_data_integrity
            .iter()
            .map(|(seq_id, data_integrity)| {
                let vault_index = snapshot_vault_index.get_balance_vault_index(seq_id);
                let merkle_proof = data_integrity_merkle_tree.proof(&[vault_index as usize]);

                let (
                    wallet_count_balance_changed,
                    accepted_withdrawal_count,
                    rejected_withdrawal_count,
                ) = balance_sharding_sub_reports
                    .get(seq_id)
                    .map_or((0, 0, 0), |reports| {
                        let wallet_count_balance_changed = reports
                            .0
                            .iter()
                            .map(|report| report.balances_changed.0.len())
                            .sum();
                        let accepted_withdrawal_count = reports
                            .1
                            .iter()
                            .map(|report| report.withdrawals.len())
                            .sum();
                        let rejected_withdrawal_count = reports
                            .2
                            .iter()
                            .map(|report| report.withdrawals.len())
                            .sum();
                        (
                            wallet_count_balance_changed,
                            accepted_withdrawal_count,
                            rejected_withdrawal_count,
                        )
                    });

                let report = BalanceDataIntegrityShardingSubReport {
                    merkle_proof: merkle_proof.to_bytes(),
                    data_digest: data_integrity.clone(),
                    wallet_count_balance_changed: wallet_count_balance_changed.try_into().unwrap(),
                    accepted_withdrawal_count: accepted_withdrawal_count.try_into().unwrap(),
                    rejected_withdrawal_count: rejected_withdrawal_count.try_into().unwrap(),
                    index: vault_index.try_into().unwrap(),
                    merkle_node_count: total_merkle_node_count.try_into().unwrap(),
                };
                (seq_id.clone(), report)
            })
            .collect();

        let deposit_detector_index = snapshot_vault_index.get_deposit_detector_index();
        let deposit_detector_merkle_proof =
            data_integrity_merkle_tree.proof(&[deposit_detector_index as usize]);
        let total_consumed_deposit_count = deposit_detector_sharding_sub_reports
            .iter()
            .fold(0, |acc, report| acc + report.deposit_txs.len());
        let deposit_detector_report = DepositDetectorDataIntegrityShardingSubReport {
            merkle_proof: deposit_detector_merkle_proof.to_bytes(),
            data_digest: deposit_detector_data_integrity.clone(),
            merkle_node_count: total_merkle_node_count.try_into().unwrap(),
            index: deposit_detector_index,
            total_deposit_tx_count: total_consumed_deposit_count.try_into().unwrap(),
        };

        (
            core_registry_report,
            wallet_registry_reports,
            secret_vault_reports,
            balance_vault_reports,
            deposit_detector_report,
        )
    }

    fn build_tx_merkle_tree(&self, txs: &Vec<EncodedTransaction>) -> MerkleTree<Sha256> {
        let nodes = txs
            .iter()
            .map(|tx| sha256(tx.as_ref()))
            .collect::<Vec<MerkleNode>>();
        MerkleTree::<Sha256>::from_leaves(&nodes)
    }

    fn build_rejected_txs_merkle_tree(
        &self,
        rejected_txs: &Vec<EncodedTransaction>,
    ) -> MerkleTree<Sha256> {
        let nodes = rejected_txs
            .iter()
            .map(|tx| sha256(tx.as_ref()))
            .collect::<Vec<MerkleNode>>();
        MerkleTree::<Sha256>::from_leaves(&nodes)
    }

    pub fn build_data_integrity_merkle_tree(
        &self,
        data_integrity: &Vec<MerkleNode>,
    ) -> MerkleTree<Sha256> {
        MerkleTree::<Sha256>::from_leaves(data_integrity)
    }
}

impl SnapshotVaultIndex {
    pub fn new(
        wallet_registry_count: u32,
        secret_vault_count: u32,
        balance_vault_count: u32,
    ) -> Self {
        Self {
            wallet_registry_count,
            secret_vault_count,
            balance_vault_count,
        }
    }

    pub fn get_total_vaults_count(&self) -> u32 {
        self.wallet_registry_count.clone()
            + self.secret_vault_count.clone()
            + self.balance_vault_count.clone()
            + 2u32
    }

    pub fn get_total_merkle_node_count(&self) -> u32 {
        // total count of the canisters, include
        //   core registry, wallet registry, balance vault, secret vault, deposit detector
        //   + 3 (last merkle node is the new wallet count,new asset count, new market count)
        self.get_total_vaults_count() + 3u32
    }

    pub fn get_core_registry_index(&self) -> u32 {
        0u32
    }

    pub fn get_wallet_registry_index(&self, wallet_registry_seq_id: &WalletRegistrySeqId) -> u32 {
        wallet_registry_seq_id.to_u32().unwrap() + 1u32
    }

    pub fn get_secret_vault_index(&self, secret_vault_seq_id: &SecretVaultSeqId) -> u32 {
        secret_vault_seq_id.to_u32().unwrap() + self.wallet_registry_count.clone() + 1u32
    }

    pub fn get_balance_vault_index(&self, balance_vault_seq_id: &BalanceVaultSeqId) -> u32 {
        balance_vault_seq_id.to_u32().unwrap()
            + self.wallet_registry_count.clone()
            + self.secret_vault_count.clone()
            + 1u32
    }

    pub fn get_deposit_detector_index(&self) -> u32 {
        self.wallet_registry_count.clone()
            + self.secret_vault_count.clone()
            + self.balance_vault_count.clone()
            + 1u32
    }
}

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

    #[test]
    fn test_generate_merkle_tree_with_empty_nodes() {
        let block_builder = BlockBuilder::new(
            CanisterId::from_text("a4tbr-q4aaa-aaaaa-qaafq-cai").unwrap(),
            CanisterId::from_text("a4tbr-q4aaa-aaaaa-qaafq-cai").unwrap(),
            100,
            100,
            100,
            100,
            100,
            100,
            100,
            100,
            100,
            100,
        );
        let merkle_tree = block_builder.build_rejected_txs_merkle_tree(&vec![]);
        assert_eq!(merkle_tree.leaves_len(), 0);
        assert_eq!(merkle_tree.root().is_none(), true);
    }
}