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
use std::fmt::Arguments;
use std::path::PathBuf;
use std::process::exit;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;
#[cfg(feature = "json-rpc")]
use std::sync::OnceLock;

use async_std::sync::RwLock;
use async_std::task;
use async_std::task::block_on;
pub use bitcoin::Network;
use fern::colors::Color;
use fern::colors::ColoredLevelConfig;
use fern::FormatCallback;
use floresta_chain::pruned_utreexo::BlockchainInterface;
use floresta_chain::AssumeValidArg;
use floresta_chain::BlockchainError;
use floresta_chain::ChainState;
use floresta_chain::KvChainStore;
use floresta_compact_filters::flat_filters_store::FlatFiltersStore;
use floresta_compact_filters::network_filters::NetworkFilters;
use floresta_electrum::electrum_protocol::client_accept_loop;
use floresta_electrum::electrum_protocol::ElectrumServer;
use floresta_watch_only::kv_database::KvDatabase;
use floresta_watch_only::AddressCache;
use floresta_watch_only::AddressCacheDatabase;
use floresta_wire::address_man::LocalAddress;
use floresta_wire::mempool::Mempool;
use floresta_wire::node::UtreexoNode;
use floresta_wire::UtreexoNodeConfig;
use futures::channel::oneshot;
use log::debug;
use log::error;
use log::info;
use log::Record;

use crate::config_file::ConfigFile;
#[cfg(feature = "json-rpc")]
use crate::json_rpc;
use crate::wallet_input::InitialWalletSetup;
#[cfg(feature = "zmq-server")]
use crate::zmq::ZMQServer;

#[derive(Default, Clone)]
/// General configuration for the floresta daemon.
///
/// Those configs should be passed in by anyone that wants to start a floresta instance. Some of
/// these are also exposed through the config file.
pub struct Config {
    /// Where we should place our data
    ///
    /// This directory must be readable and writable by our proccess. We'll use this dir to store
    /// both chain and wallet data, so this should be kept in a non-volatile medium. We are not
    /// particurly aggressive in disk usage, so we don't need a fast disk to work.
    ///
    /// If not set, it defaults to $HOME/.floresta
    pub data_dir: Option<String>,
    /// We consider blocks prior to this one to have a valid signature
    ///
    /// This is a optimization mirrowed from Core, where blocks before this one are considered to
    /// have valid signatures. The idea here is that if a block is burried under a lot of PoW, it's
    /// very unlikely that it is invalid. We still validate everything else and build the
    /// accumulator until this point (unless running on PoW-fraud proof or assumeutreexo mode) so
    /// there's still some work to do.
    pub assume_valid: Option<String>,
    /// A vector of xpubs to cache
    ///
    /// This is a list of SLIP-132-encoded extended public key that we should add to our Watch-only
    /// wallet. A descriptor may be only passed one time, if you call florestad with an already
    /// cached address, that will be a no-op. After a xpub is cached, we derive multiple addresses
    /// from it and try to find transactions involving it.
    pub wallet_xpub: Option<Vec<String>>,
    /// A output descriptor to cache
    ///
    /// This should be a list of ouptut descriptors that we should add to our watch-only wallet.
    /// This works just like wallet_xpub, but with a descriptor.
    pub wallet_descriptor: Option<Vec<String>>,
    /// Whether we should rescan for wallet transactions
    ///
    /// If your wallet is missing some transaction (e.g. you've just added a new address), you can
    /// set this value to some height, and we'll rescan from this block to the tip.
    pub rescan: Option<u32>,
    /// Where should we read from a config file
    ///
    /// This is a toml-encoded file with floresta's configs. For a sample of how this file looks
    /// like, see config.toml.sample inside floresta's codebase.
    ///
    /// If a setting is modified by the config file and this config struct, the following logic is
    /// used:
    ///     - For vectors, we use the combination of both vectors
    ///     - for mutually exclusive options, this struct has precedense over the config file
    pub config_file: Option<String>,
    /// A proxy that we should use to connect with others
    ///
    /// This should be a socks5 proxy, like Tor's socks. If provided, all our outgoing connections
    /// will be made through this one, except dns seed connections.
    pub proxy: Option<String>,
    /// The network we are running in, it may be one of: bitcoin, signet, regtest or testnet.
    pub network: crate::Network,
    /// Whther we should build and store compact block filters
    ///
    /// Those filters are used for rescanning our wallet for historical transactions. If you don't
    /// have this on, the only way to find historical transactions is to download all blocks, which
    /// is very inefficient and resource/time consuming. But keep in mind that filters will take
    /// up disk space.
    pub cfilters: bool,
    /// If we are using block filters, we may not need to download the whole chain of filters, as
    /// our wallets may not have been created at the beginning of the chain. With this option, we
    /// can make a rough estimate of the block height we need to start downloading filters.
    ///
    /// If the value is negative, it's relative to the current tip. For example, if the current tip
    /// is at height 1000, and we set this value to -100, we will start downloading filters from
    /// height 900.
    pub filters_start_height: Option<i32>,
    #[cfg(feature = "zmq-server")]
    /// The address to listen to for our ZMQ server
    ///
    /// We have an (optional) ZMQ server, that pushes new blocks over a PUSH/PULL ZMQ queue, this
    /// is the address that we'll listen for incomming connections.
    pub zmq_address: Option<String>,
    pub connect: Option<String>,
    #[cfg(feature = "json-rpc")]
    /// The address our json-rpc should listen to
    pub json_rpc_address: Option<String>,
    /// The address our electrum server should listen to
    pub electrum_address: Option<String>,
    /// Whether we should write logs to the stdio
    pub log_to_stdout: bool,
    //// Whether we should log to a fs file
    pub log_to_file: bool,
    /// Whether we should use assume utreexo
    pub assume_utreexo: bool,
    /// Whether we should post debug information to the console
    pub debug: bool,
    /// The user agent that we will advertise to our peers
    pub user_agent: String,
}

pub struct Florestad {
    /// The config used by this node, see [Config] for more details
    config: Config,
    /// A channel that tells others to stop what they are doing because we
    /// are about to die
    stop_signal: Arc<RwLock<bool>>,
    /// A channel that notifies we are done, and it's safe to die now
    stop_notify: Arc<Mutex<Option<oneshot::Receiver<()>>>>,
    #[cfg(feature = "json-rpc")]
    /// A handle to our json-rpc server
    json_rpc: OnceLock<jsonrpc_http_server::Server>,
}

impl Florestad {
    /// Kills a running florestad, this will return as soon as the main node stops.
    ///
    /// It's not safe to stop you program before this thread returns because some
    /// information may not be fully flushed to disk yet, and killing the process
    /// before flushing everything is equivalent to an unclean shutdown.
    #[allow(unused)]
    pub fn stop(&self) {
        async_std::task::block_on(async move {
            *self.stop_signal.write().await = true;
            let chan = {
                let mut guard = self.stop_notify.lock().unwrap();
                std::mem::take(&mut *guard)
            };
            if let Some(chan) = chan {
                if let Err(e) = chan.await {
                    error!("POSSIBLE BUG: unexpected error while shutting down {e:?}");
                }
            }
        });
    }

    pub fn get_stop_signal(&self) -> Arc<RwLock<bool>> {
        self.stop_signal.clone()
    }

    pub async fn wait_shutdown(&self) {
        let chan = {
            let mut guard = self.stop_notify.lock().unwrap();
            std::mem::take(&mut *guard)
        };
        if let Some(chan) = chan {
            if let Err(e) = chan.await {
                error!("POSSIBLE BUG: unexpected error while shutting down {e:?}");
            }
        }
    }

    /// Actually runs florestad, spawning all modules and waiting util
    /// someone asks to stop.
    pub fn start(&self) {
        // Setup global logger
        let data_dir = self
            .config
            .data_dir
            .clone()
            .or_else(|| {
                dirs::home_dir().map(|x: PathBuf| {
                    format!(
                        "{}/{}/",
                        x.to_str().unwrap_or_default().to_owned(),
                        ".floresta",
                    )
                })
            })
            .unwrap_or("floresta".into());

        let data_dir = match self.config.network {
            crate::Network::Bitcoin => data_dir,
            crate::Network::Signet => data_dir + "/signet/",
            crate::Network::Testnet => data_dir + "/testnet3/",
            crate::Network::Regtest => data_dir + "/regtest/",
        };

        // create the data directory if it doesn't exist
        if !std::path::Path::new(&data_dir).exists() {
            std::fs::create_dir_all(&data_dir).expect("Could not create data directory");
        }

        if self.config.log_to_stdout || self.config.log_to_file {
            Self::setup_logger(
                &data_dir,
                self.config.log_to_file,
                self.config.log_to_stdout,
                self.config.debug,
            )
            .expect("failure to setup logger");
        }

        // The config file inside our datadir directory. Any datadir
        // passed as argument will be used instead
        let system_config_file = format!("{data_dir}/config.toml");
        let config_file = match &self.config.config_file {
            Some(path) => Self::get_config_file(path),
            None => Self::get_config_file(&system_config_file),
        };

        // Load the watch-only wallet
        debug!("Loading wallet");
        let mut wallet = Self::load_wallet(&data_dir);
        wallet.setup().expect("Could not initialize wallet");
        debug!("Done loading wallet");

        // Try to add more wallets to watch if needed
        let result = Self::setup_wallet(
            Self::get_both_vec(self.config.wallet_xpub.clone(), config_file.wallet.xpubs),
            Self::get_both_vec(
                self.config.wallet_descriptor.clone(),
                config_file.wallet.descriptors,
            ),
            Self::get_both_vec(config_file.wallet.addresses.clone(), None),
            &mut wallet,
            self.config.network,
        );

        if let Err(e) = result {
            log::error!("Something went wrong while setting wallet up: {e}");
            return;
        }

        info!("loading blockchain database");
        let datadir2 = data_dir.clone();
        let blockchain_state = Arc::new(Self::load_chain_state(
            datadir2,
            Self::get_net(&self.config.network),
            self.config
                .assume_valid
                .as_ref()
                .map(|value| value.parse().expect("invalid assumevalid")),
        ));

        if let Some(height) = self.config.rescan {
            blockchain_state
                .rescan(height)
                .expect("Fail while setting rescan");
        }

        #[cfg(feature = "compact-filters")]
        let cfilters = if self.config.cfilters {
            // Block Filters
            let filter_store = FlatFiltersStore::new((data_dir.clone() + "/cfilters").into());
            let cfilters = Arc::new(NetworkFilters::new(filter_store));
            info!(
                "loaded compact filters store at height: {:?}",
                cfilters.get_height()
            );
            Some(cfilters)
        } else {
            None
        };
        #[cfg(not(feature = "compact-filters"))]
        let cfilters = None;

        // Handle the `-connect` cli option
        let connect = match self
            .config
            .clone()
            .connect
            .map(|host| LocalAddress::from_str(&host))
        {
            Some(Ok(host)) => {
                debug!("Connecting to {:?}", host);
                Some(host)
            }
            Some(Err(e)) => {
                error!("Invalid host: {}", e);
                exit(-1);
            }
            None => None,
        };

        // For now we only have compatible bridges on signet
        let pow_fraud_proofs = match self.config.network {
            crate::Network::Bitcoin => false,
            crate::Network::Signet => true,
            crate::Network::Testnet => false,
            crate::Network::Regtest => false,
        };

        // If this network already allows pow fraud proofs, we should use it instead of assumeutreexo
        let assume_utreexo = match (pow_fraud_proofs, self.config.assume_utreexo) {
            (false, true) => Some(floresta_chain::ChainParams::get_assumeutreexo_value(
                Self::get_net(&self.config.network).into(),
            )),
            _ => None,
        };

        let config = UtreexoNodeConfig {
            network: Self::get_net(&self.config.network),
            pow_fraud_proofs,
            proxy: self
                .config
                .proxy
                .as_ref()
                .map(|address| address.parse().expect("Invalid address")),
            datadir: data_dir,
            fixed_peer: connect,
            max_banscore: 50,
            compact_filters: self.config.cfilters,
            max_outbound: 10,
            max_inflight: 20,
            assume_utreexo,
            backfill: false,
            filter_start_height: self.config.filters_start_height,
            user_agent: self.config.user_agent.clone(),
        };

        // Chain Provider (p2p)
        let chain_provider = UtreexoNode::new(
            config,
            blockchain_state.clone(),
            Arc::new(async_std::sync::RwLock::new(Mempool::new())),
            cfilters.clone(),
        );

        // ZMQ
        #[cfg(feature = "zmq-server")]
        {
            info!("Starting ZMQ server");
            if let Ok(zserver) = ZMQServer::new(
                &self
                    .config
                    .zmq_address
                    .as_ref()
                    .unwrap_or(&"tcp://127.0.0.1:5150".to_string()),
            ) {
                blockchain_state.subscribe(Arc::new(zserver));
                info!("Done!");
            } else {
                error!("Could not create zmq server, skipping");
            };
        }

        info!("Starting server");
        let wallet = Arc::new(RwLock::new(wallet));

        // JSON-RPC
        #[cfg(feature = "json-rpc")]
        {
            let server = json_rpc::server::RpcImpl::create(
                blockchain_state.clone(),
                wallet.clone(),
                chain_provider.get_handle(),
                self.stop_signal.clone(),
                Self::get_net(&self.config.network),
                cfilters.clone(),
                self.config
                    .json_rpc_address
                    .as_ref()
                    .map(|x| x.parse().expect("Invalid json rpc address")),
            );

            if self.json_rpc.set(server).is_err() {
                panic!("we should be the first one setting this");
            }
        }

        // Electrum
        let electrum_address = self
            .config
            .electrum_address
            .clone()
            .unwrap_or("0.0.0.0:50001".into());

        let electrum_server = block_on(ElectrumServer::new(
            electrum_address,
            wallet,
            blockchain_state,
            cfilters,
            chain_provider.get_handle(),
        ))
        .expect("Could not create an Electrum Server");

        // Spawn all services

        // Electrum accept loop
        task::spawn(client_accept_loop(
            electrum_server.tcp_listener.clone(),
            electrum_server.message_transmitter.clone(),
        ));
        // Electrum main loop
        task::spawn(electrum_server.main_loop());
        info!("Server running on: 0.0.0.0:50001");

        // Chain provider
        let kill_signal = self.stop_signal.clone();
        let (sender, receiver) = oneshot::channel();

        let mut recv = self.stop_notify.lock().unwrap();
        *recv = Some(receiver);

        task::spawn(chain_provider.run(kill_signal, sender));
    }

    fn setup_logger(
        data_dir: &String,
        log_file: bool,
        log_to_stdout: bool,
        debug: bool,
    ) -> Result<(), fern::InitError> {
        let colors = ColoredLevelConfig::new()
            .error(Color::Red)
            .warn(Color::Yellow)
            .info(Color::Green)
            .debug(Color::Blue)
            .trace(Color::BrightBlack);

        let formatter = |use_colors: bool| {
            move |out: FormatCallback, message: &Arguments, record: &Record| {
                out.finish(format_args!(
                    "[{} {} {}] {}",
                    chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
                    match use_colors {
                        true => colors.color(record.level()).to_string(),
                        false => record.level().to_string(),
                    },
                    record.target(),
                    message
                ))
            }
        };

        let mut dispatchers = fern::Dispatch::new();
        let stdout_dispatcher = fern::Dispatch::new()
            .format(formatter(true))
            .level(if debug {
                log::LevelFilter::Debug
            } else {
                log::LevelFilter::Info
            })
            .chain(std::io::stdout());

        let file_dispatcher = fern::Dispatch::new()
            .format(formatter(false))
            .level(log::LevelFilter::Info)
            .chain(fern::log_file(format!("{}/output.log", data_dir))?);

        if log_file {
            dispatchers = dispatchers.chain(file_dispatcher);
        }

        if log_to_stdout {
            dispatchers = dispatchers.chain(stdout_dispatcher);
        }

        dispatchers.apply()?;

        Ok(())
    }

    pub fn from_config(config: Config) -> Self {
        Self {
            config,
            stop_signal: Arc::new(RwLock::new(false)),
            stop_notify: Arc::new(Mutex::new(None)),
            #[cfg(feature = "json-rpc")]
            json_rpc: OnceLock::new(),
        }
    }

    pub fn new() -> Self {
        Self::from_config(Config::default())
    }

    /// Loads a config file from disk, returns default if some error happens
    fn get_config_file(path: &str) -> ConfigFile {
        let data = ConfigFile::from_file(path);

        if let Ok(data) = data {
            data
        } else {
            match data.unwrap_err() {
                crate::error::Error::TomlParsing(e) => {
                    error!("Error while parsing config file, ignoring it");
                    debug!("{e}");
                    ConfigFile::default()
                }
                crate::error::Error::Io(e) => {
                    error!("Error reading config file, ignoring it");
                    debug!("{e}");
                    ConfigFile::default()
                }
                // Shouldn't be any other error
                _ => unreachable!(),
            }
        }
    }

    fn get_key_from_env() -> Option<String> {
        let xpub = std::env::var("WALLET_XPUB");
        match xpub {
            Ok(key) => return Some(key),
            Err(e) => match e {
                std::env::VarError::NotPresent => {}
                std::env::VarError::NotUnicode(xpub) => error!("Invalid xpub {xpub:?}"),
            },
        }
        None
    }

    fn load_chain_state(
        data_dir: String,
        network: Network,
        assume_valid: Option<bitcoin::BlockHash>,
    ) -> ChainState<KvChainStore<'static>> {
        let db = KvChainStore::new(data_dir.to_string()).expect("Could not read db");
        let assume_valid =
            assume_valid.map_or(AssumeValidArg::Hardcoded, AssumeValidArg::UserInput);

        match ChainState::<KvChainStore>::load_chain_state(db, network.into(), assume_valid) {
            Ok(chainstate) => chainstate,
            Err(err) => match err {
                BlockchainError::ChainNotInitialized => {
                    let db = KvChainStore::new(data_dir.to_string()).expect("Could not read db");

                    ChainState::<KvChainStore>::new(db, network.into(), assume_valid)
                }
                _ => unreachable!(),
            },
        }
    }

    fn load_wallet(data_dir: &String) -> AddressCache<KvDatabase> {
        let database = KvDatabase::new(data_dir.to_owned()).expect("Could not create a database");
        AddressCache::new(database)
    }

    fn get_net(net: &crate::Network) -> bitcoin::Network {
        match net {
            crate::Network::Bitcoin => bitcoin::Network::Bitcoin,
            crate::Network::Signet => bitcoin::Network::Signet,
            crate::Network::Testnet => bitcoin::Network::Testnet,
            crate::Network::Regtest => bitcoin::Network::Regtest,
        }
    }

    fn setup_wallet<D: AddressCacheDatabase>(
        mut xpubs: Vec<String>,
        descriptors: Vec<String>,
        addresses: Vec<String>,
        wallet: &mut AddressCache<D>,
        network: crate::Network,
    ) -> anyhow::Result<()> {
        if let Some(key) = Self::get_key_from_env() {
            xpubs.push(key);
        }
        let setup = InitialWalletSetup::build(
            &xpubs,
            &descriptors,
            &addresses,
            Self::get_net(&network),
            100,
        )?;
        for descriptor in setup.descriptors {
            let descriptor = descriptor.to_string();
            if !wallet.is_cached(&descriptor)? {
                wallet.push_descriptor(&descriptor)?;
            }
        }
        for addresses in setup.addresses {
            wallet.cache_address(addresses.script_pubkey());
        }
        info!("Wallet setup completed!");
        anyhow::Ok(())
    }

    fn get_both_vec<T>(a: Option<Vec<T>>, b: Option<Vec<T>>) -> Vec<T> {
        let mut result: Vec<T> = Vec::new();
        if let Some(a) = a {
            result.extend(a);
        }
        if let Some(b) = b {
            result.extend(b);
        }
        result
    }
}

impl Default for Florestad {
    fn default() -> Self {
        Self::new()
    }
}

impl From<Config> for Florestad {
    fn from(config: Config) -> Self {
        Self {
            config,
            stop_signal: Arc::new(RwLock::new(false)),
            stop_notify: Arc::new(Mutex::new(None)),
            #[cfg(feature = "json-rpc")]
            json_rpc: OnceLock::new(),
        }
    }
}