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
crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/chainparamsbase.h]

/**
  | BaseChainParams defines the base
  | parameters (shared between bitcoin-cli
  | and bitcoind) of a given instance of
  | the Bitcoin system.
  |
  */
#[derive(Default)]
pub struct BaseChainParams {
    pub rpc_port:                  u16,
    pub onion_service_target_port: u16,
    pub str_data_dir:              String,
}

pub mod base_chain_params {

    /**
      | Chain name strings
      |
      */
    pub const MAIN:    &'static str = "main";
    pub const TESTNET: &'static str = "test";
    pub const SIGNET:  &'static str = "signet";
    pub const REGTEST: &'static str = "regtest";
}

impl BaseChainParams {

    pub fn data_dir(&self) -> &String {
        
        &self.str_data_dir
    }
    
    pub fn rpc_port(&self) -> u16 {
        
       self.rpc_port
    }
    
    pub fn onion_service_target_port(&self) -> u16 {
        
        self.onion_service_target_port
    }
    
    pub fn new(
        data_dir:                  &str,
        rpc_port:                  u16,
        onion_service_target_port: u16) -> Self {
        Self {
            rpc_port:                  rpc_port,
            onion_service_target_port: onion_service_target_port,
            str_data_dir:              data_dir.to_string()
        }
    }
}

//-------------------------------------------[.cpp/bitcoin/src/chainparamsbase.cpp]

impl ArgsManagerInner {

    /**
      | Set the arguments for chainparams
      |
      */
    pub fn setup_chain_params_base_options(&mut self)  {

        self.add_arg(&base_options::ARG_SET_CHAIN);
        self.add_arg(&base_options::ARG_REGTEST);
        self.add_arg(&base_options::ARG_TESTACTIVATIONHEIGHT);
        self.add_arg(&base_options::ARG_TESTNET);
        self.add_arg(&base_options::ARG_SET_VBPARAMS);
        self.add_arg(&base_options::ARG_SIGNET);
        self.add_arg(&base_options::ARG_SIGNET_CHALLENGE);
        self.add_arg(&base_options::ARG_SIGNET_SEEDNODE);
    }
}

lazy_static!{
    static ref global_chain_base_params: Arc<Mutex<BaseChainParams>> = Arc::new(Mutex::new(BaseChainParams::default()));
}

/**
  | Return the currently selected parameters.
  | This won't change after app startup,
  | except for unit tests.
  |
  */
pub fn base_params() -> MutexGuard<'static, BaseChainParams> {
    
    global_chain_base_params.lock()
}

/**
  | Creates and returns
  | a std::unique_ptr<CBaseChainParams> of the
  | chosen chain.
  | 
  | -----------
  | @return
  | 
  | a CBaseChainParams* of the chosen chain.
  | @throws a std::runtime_error if the
  | chain is not supported.
  |
  -------------------------
  | Port numbers for incoming Tor connections
  | (8334, 18334, 38334, 18445) have been
  | chosen arbitrarily to keep ranges of
  | used ports tight.
  |
  */
pub fn create_base_chain_params(chain: &str) -> Result<Box<BaseChainParams>, StdException> {

    match chain {
        base_chain_params::MAIN => {
            Ok(Box::new(BaseChainParams::new("",8332,8334)))
        },
        base_chain_params::TESTNET => {
            Ok(Box::new(BaseChainParams::new("testnet3",18332,18334)))
        },
        base_chain_params::SIGNET => {
            Ok(Box::new(BaseChainParams::new("signet",38332,38334)))
        },
        base_chain_params::REGTEST => {
            Ok(Box::new(BaseChainParams::new("regtest",18443,18445)))
        },
        _ => {
            let msg = format!("{}: Unknown chain {}.",func![],chain);
            Err(runtime_error(&msg))
        }
    }
}

/**
  | Sets the params returned by Params()
  | to those for the given network.
  |
  */
pub fn select_base_params(chain: &str)  {
    
    *base_params() = *create_base_chain_params(chain).unwrap();

    let guard = G_ARGS.lock();

    let mut inner = (*guard).cs_args.lock();

    inner.select_config_network(chain);
}