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
// RGB standard library
// Written in 2020 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use clap::{ArgEnum, Clap};
use lnpbp::bp;
use std::path::PathBuf;

#[cfg(feature = "serde")]
use serde::Deserialize;

use crate::constants::*;
use crate::DataFormat;

#[derive(Clap)]
#[clap(
    name = "rgbd",
    version = "0.1.0",
    author = "Dr Maxim Orlovsky <orlovsky@pandoracore.com>",
    about = "RGB main daemon; part of RGB suite"
)]
pub struct Opts {
    /// Sets verbosity level; can be used multiple times to increase verbosity
    #[clap(short, long, global = true, parse(from_occurrences))]
    pub verbose: u8,

    /// Path to the directory containing daemon executables
    #[clap(short, long, default_value = RGB_BIN_DIR, env = "RGB_BIN_DIR")]
    pub bin_dir: String,

    /// Data directory path
    #[clap(short, long, default_value = RGB_DATA_DIR, env = "RGB_DATA_DIR")]
    pub data_dir: String,

    /// Contract daemons to launch
    #[clap(arg_enum, long = "contract", default_value = RGB_CONTRACTS, env = "RGB_CONTRACTS")]
    pub contracts: Vec<ContractName>,

    /// ZMQ socket address string for REQ/REP API of fungibled
    #[clap(
        long = "fungible-rpc",
        default_value = FUNGIBLED_RPC_ENDPOINT,
        env = "RGB_FUNGIBLED_RPC"
    )]
    pub fungible_rpc_endpoint: String,

    /// ZMQ socket address string for PUB/SUB API of fungibled
    #[clap(
        long = "fungible-pub",
        default_value = FUNGIBLED_PUB_ENDPOINT,
        env = "RGB_FUNGIBLED_PUB"
    )]
    pub fungible_pub_endpoint: String,

    /// ZMQ socket address string for REQ/REP API of stashd
    #[clap(
        long = "stash-rpc",
        default_value = STASHD_RPC_ENDPOINT,
        env = "RGB_STASHD_RPC"
    )]
    pub stash_rpc_endpoint: String,

    /// ZMQ socket address string for PUB/SUB API of stashd
    #[clap(
        long = "stash-pub",
        default_value = STASHD_PUB_ENDPOINT,
        env = "RGB_STASHD_PUB"
    )]
    pub stash_pub_endpoint: String,

    /// Connection string to fungibled cache (exact format depends on used
    /// storage engine)
    #[clap(
        short = 'c',
        long = "cache",
        default_value = FUNGIBLED_CACHE,
        env = "RGB_FUNGIBLED_CACHE"
    )]
    pub cache: String,

    /// Data format for fungibled cache storage (valid only if file storage is
    /// used)
    #[clap(short, long, default_value = "yaml", env = "RGB_FUNGIBLED_FORMAT")]
    pub format: DataFormat,

    /// Connection string to stashd stash (exact format depends on used storage
    /// engine)
    #[clap(
        short,
        long,
        default_value = STASHD_STASH,
        env = "RGB_STASHD_STASH"
    )]
    pub stash: String,

    /// Connection string to indexing service
    #[clap(
        short,
        long,
        default_value = STASHD_INDEX,
        env = "RGB_STASHD_INDEX"
    )]
    pub index: String,

    /// LNP socket address string for P2P API of stashd
    #[clap(
        long = "bind",
        default_value = STASHD_P2P_ENDPOINT,
        env = "RGB_STASHD_BIND"
    )]
    pub p2p_endpoint: String,

    /// Run services as threads instead of daemons
    #[clap(short, long)]
    pub threaded: bool,

    /// Bitcoin network to use
    #[clap(short, long, default_value = RGB_NETWORK, env = "RGB_NETWORK")]
    pub network: bp::Chain,

    /// Electrum server to use to fecth Bitcoin transactions
    #[clap(
        long = "electrum",
        default_value = DEFAULT_ELECTRUM_ENDPOINT,
        env = "RGB_ELECTRUM_SERVER"
    )]
    pub electrum_server: String,
}

#[derive(Clap, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]
#[display(Debug)]
#[cfg_attr(
    feature = "serde",
    derive(Serialize, Deserialize,),
    serde(crate = "serde_crate")
)]
#[repr(u8)]
#[non_exhaustive]
pub enum ContractName {
    Fungible,
    Collectible,
    Identity,
}

impl ContractName {
    pub fn daemon_name(&self) -> &str {
        match self {
            ContractName::Fungible => "fungibled",
            ContractName::Collectible => "collectibled",
            ContractName::Identity => "identityd",
        }
    }
}

#[derive(Clone, PartialEq, Eq, Debug, Display)]
#[display(Debug)]
pub struct Config {
    pub data_dir: PathBuf,
    pub bin_dir: PathBuf,
    pub threaded: bool,
    pub contracts: Vec<ContractName>,
    pub network: bp::Chain,
    pub verbose: u8,
    pub fungible_rpc_endpoint: String,
    pub fungible_pub_endpoint: String,
    pub stash_rpc_endpoint: String,
    pub stash_pub_endpoint: String,
    pub cache: String,
    pub format: DataFormat,
    pub stash: String,
    pub index: String,
    pub p2p_endpoint: String,
    pub electrum_server: String,
}

impl From<Opts> for Config {
    fn from(opts: Opts) -> Self {
        Self {
            data_dir: opts.data_dir.into(),
            bin_dir: opts.bin_dir.into(),
            threaded: opts.threaded,
            network: opts.network,
            contracts: opts.contracts,
            fungible_rpc_endpoint: opts.fungible_rpc_endpoint,
            fungible_pub_endpoint: opts.fungible_pub_endpoint,
            stash_rpc_endpoint: opts.stash_rpc_endpoint,
            stash_pub_endpoint: opts.stash_pub_endpoint,
            cache: opts.cache,
            format: opts.format,
            stash: opts.stash,
            index: opts.index,
            p2p_endpoint: opts.p2p_endpoint,
            verbose: opts.verbose,
            electrum_server: opts.electrum_server,
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            data_dir: RGB_DATA_DIR
                .parse()
                .expect("Error in RGB_DATA_DIR constant value"),
            bin_dir: RGB_BIN_DIR
                .parse()
                .expect("Error in RGB_BIN_DIR constant value"),
            threaded: false,
            contracts: vec![ContractName::from_str(RGB_CONTRACTS, false)
                .expect("Error in RGB_CONTRACTS constant value")],
            fungible_rpc_endpoint: FUNGIBLED_RPC_ENDPOINT.to_string(),
            fungible_pub_endpoint: FUNGIBLED_PUB_ENDPOINT.to_string(),
            stash_rpc_endpoint: STASHD_RPC_ENDPOINT.to_string(),
            stash_pub_endpoint: STASHD_PUB_ENDPOINT.to_string(),
            cache: FUNGIBLED_CACHE.to_string(),
            #[cfg(feature = "serde_yaml")]
            format: DataFormat::Yaml,
            #[cfg(not(feature = "serde_yaml"))]
            format: DataFormat::StrictEncode,
            stash: STASHD_STASH.to_string(),
            index: STASHD_INDEX.to_string(),
            p2p_endpoint: STASHD_P2P_ENDPOINT.to_string(),
            network: RGB_NETWORK
                .parse()
                .expect("Error in RGB_NETWORK constant value"),
            verbose: 0,
            electrum_server: DEFAULT_ELECTRUM_ENDPOINT
                .parse()
                .expect("Error in DEFAULT_ELECTRUM_ENDPOINT constant value"),
        }
    }
}

impl Default for Opts {
    fn default() -> Self {
        Self {
            data_dir: RGB_DATA_DIR
                .parse()
                .expect("Error in RGB_DATA_DIR constant value"),
            bin_dir: RGB_BIN_DIR
                .parse()
                .expect("Error in RGB_BIN_DIR constant value"),
            threaded: false,
            contracts: vec![ContractName::from_str(RGB_CONTRACTS, false)
                .expect("Error in RGB_CONTRACTS constant value")],
            fungible_rpc_endpoint: FUNGIBLED_RPC_ENDPOINT.to_string(),
            fungible_pub_endpoint: FUNGIBLED_PUB_ENDPOINT.to_string(),
            stash_rpc_endpoint: STASHD_RPC_ENDPOINT.to_string(),
            stash_pub_endpoint: STASHD_PUB_ENDPOINT.to_string(),
            cache: FUNGIBLED_CACHE.to_string(),
            #[cfg(feature = "serde_yaml")]
            format: DataFormat::Yaml,
            #[cfg(not(feature = "serde_yaml"))]
            format: DataFormat::StrictEncode,
            stash: STASHD_STASH.to_string(),
            index: STASHD_INDEX.to_string(),
            p2p_endpoint: STASHD_P2P_ENDPOINT.to_string(),
            network: RGB_NETWORK
                .parse()
                .expect("Error in RGB_NETWORK constant value"),
            verbose: 0,
            electrum_server: DEFAULT_ELECTRUM_ENDPOINT
                .parse()
                .expect("Error in DEFAULT_ELECTRUM_ENDPOINT constant value"),
        }
    }
}