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
mod snapshot;
/// wallet state utils
pub mod wallet_state;

use crate::cardano_node::{Block0, BlockBuilder, Settings};
use crate::db_sync::{InMemoryDbSync, SharedInMemoryDbSync};
use crate::{InMemoryNode, Ledger};
use jormungandr_lib::crypto::account::Identifier;
use std::collections::HashSet;

use crate::wallet_state::MainnetWalletState;
pub use snapshot::{Initials, Parameters};

/// Cardano Network state builder, responsible to create a given state of cardano network which will
/// be an input for snapshot
#[derive(Default)]
pub struct MainnetNetworkBuilder {
    states: Vec<MainnetWalletState>,
    settings: Settings,
}

impl MainnetNetworkBuilder {
    #[must_use]
    /// Pushes new wallet to collection
    pub fn with(mut self, state: MainnetWalletState) -> Self {
        self.states.push(state);
        self
    }

    /// Builds block0
    #[must_use]
    pub fn build_block0(&self) -> Block0 {
        let txs: Vec<_> = self
            .states
            .iter()
            .filter_map(|x| x.registration_tx.clone())
            .collect();

        Block0 {
            block: BlockBuilder::next_block(None, &txs),
            settings: self.settings.clone(),
        }
    }

    /// Builds dbsync instance and set or representatives identifiers
    ///
    /// # Panics
    ///
    /// On internal logic issue
    #[must_use]
    pub fn build(self) -> (InMemoryDbSync, Ledger, HashSet<Identifier>) {
        let block0 = self.build_block0();
        let db_sync = InMemoryDbSync::from_block0(&block0);
        let ledger = Ledger::new(block0);

        (
            db_sync,
            ledger,
            self.states
                .iter()
                .map(|x| x.rep.as_ref())
                .filter(Option::is_some)
                .map(|x| x.unwrap().clone())
                .collect(),
        )
    }

    /// Builds dbsync instance and set or representatives identifiers tailored for async usage
    ///
    /// # Panics
    ///
    /// On internal logic issue
    #[must_use]
    pub fn build_shared(self) -> (SharedInMemoryDbSync, InMemoryNode, HashSet<Identifier>) {
        let (db_sync, ledger, reps) = self.build();
        let mut node = InMemoryNode::start_from_ledger(ledger);
        (db_sync.connect_to_node(&mut node), node, reps)
    }
}