Merge pull request #26 from andrei-21/feature/configure-network
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Thu, 9 Feb 2023 00:08:30 +0000 (00:08 +0000)
committerGitHub <noreply@github.com>
Thu, 9 Feb 2023 00:08:30 +0000 (00:08 +0000)
README.md
src/config.rs
src/lib.rs

index 683efd747d9e04adc0801ec89a74c8e5a7251ed5..13f46c6a561dbad062c9f0496ebeedaa8c45899f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -18,6 +18,7 @@ can be made by setting environment variables, whose usage is as follows:
 | RAPID_GOSSIP_SYNC_SERVER_DB_USER     | alice               | Username to access Postgres                                                                                |
 | RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD | _None_              | Password to access Postgres                                                                                |
 | RAPID_GOSSIP_SYNC_SERVER_DB_NAME     | ln_graph_sync       | Name of the database to be used for gossip storage                                                         |
+| RAPID_GOSSIP_SYNC_SERVER_NETWORK     | mainnet             | Network to operate in. Possible values are mainnet, testnet, signet, regtest                               |
 | BITCOIN_REST_DOMAIN                  | 127.0.0.1           | Domain of the [bitcoind REST server](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md) |
 | BITCOIN_REST_PORT                    | 8332                | HTTP port of the bitcoind REST server                                                                      |
 | BITCOIN_REST_PATH                    | /rest/              | Path infix to access the bitcoind REST endpoints                                                           |
index 444d54cfd10d2a18c5cf4faa65be4a0bac3ce50c..1f968d6418760b880cd1db6862ecf892b9446dc7 100644 (file)
@@ -5,6 +5,7 @@ use std::env;
 use std::io::Cursor;
 use std::net::{SocketAddr, ToSocketAddrs};
 
+use bitcoin::Network;
 use bitcoin::hashes::hex::FromHex;
 use bitcoin::secp256k1::PublicKey;
 use futures::stream::{FuturesUnordered, StreamExt};
@@ -17,6 +18,18 @@ pub(crate) const SCHEMA_VERSION: i32 = 8;
 pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
 pub(crate) const DOWNLOAD_NEW_GOSSIP: bool = true;
 
+pub(crate) fn network() -> Network {
+       let network = env::var("RAPID_GOSSIP_SYNC_SERVER_NETWORK").unwrap_or("bitcoin".to_string()).to_lowercase();
+       match network.as_str() {
+               "mainnet" => Network::Bitcoin,
+               "bitcoin" => Network::Bitcoin,
+               "testnet" => Network::Testnet,
+               "signet" => Network::Signet,
+               "regtest" => Network::Regtest,
+               _ => panic!("Invalid network"),
+       }
+}
+
 pub(crate) fn network_graph_cache_path() -> &'static str {
        "./res/network_graph.bin"
 }
index cc5a350847e2c4c42322d2bf20545d5de900059a..25ce1a2f5015c2a23dd76479fd12f831a854903d 100644 (file)
@@ -16,7 +16,6 @@ use std::io::BufReader;
 use std::sync::Arc;
 
 use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::Network;
 use bitcoin::secp256k1::PublicKey;
 use lightning::routing::gossip::NetworkGraph;
 use lightning::util::ser::{ReadableArgs, Writeable};
@@ -54,6 +53,7 @@ pub struct SerializedResponse {
 
 impl RapidSyncProcessor {
        pub fn new() -> Self {
+               let network = config::network();
                let logger = TestLogger::new();
                let network_graph = if let Ok(file) = File::open(&config::network_graph_cache_path()) {
                        println!("Initializing from cached network graph…");
@@ -65,10 +65,10 @@ impl RapidSyncProcessor {
                                network_graph
                        } else {
                                println!("Initialization from cached network graph failed: {}", network_graph_result.err().unwrap());
-                               NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash(), logger)
+                               NetworkGraph::new(genesis_block(network).header.block_hash(), logger)
                        }
                } else {
-                       NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash(), logger)
+                       NetworkGraph::new(genesis_block(network).header.block_hash(), logger)
                };
                let arc_network_graph = Arc::new(network_graph);
                Self {