e7ba89c329c605ab9a8b333307a9e58d7285080f
[rapid-gossip-sync-server] / src / config.rs
1 use std::env;
2 use std::net::SocketAddr;
3 use bitcoin::secp256k1::PublicKey;
4 use lightning_block_sync::http::HttpEndpoint;
5 use tokio_postgres::Config;
6 use crate::hex_utils;
7
8 pub(crate) const SCHEMA_VERSION: i32 = 1;
9 pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
10 pub(crate) const DOWNLOAD_NEW_GOSSIP: bool = true;
11
12 pub(crate) fn network_graph_cache_path() -> &'static str {
13         "./res/network_graph.bin"
14 }
15
16 pub(crate) fn db_connection_config() -> Config {
17         let mut config = Config::new();
18         let host = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_HOST").unwrap_or("localhost".to_string());
19         let user = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_USER").unwrap_or("alice".to_string());
20         let db = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_NAME").unwrap_or("ln_graph_sync".to_string());
21         config.host(&host);
22         config.user(&user);
23         config.dbname(&db);
24         if let Ok(password) = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD") {
25                 config.password(&password);
26         }
27         config
28 }
29
30 pub(crate) fn bitcoin_rest_endpoint() -> HttpEndpoint {
31         let host = env::var("BITCOIN_REST_DOMAIN").unwrap_or("127.0.0.1".to_string());
32         let port = env::var("BITCOIN_REST_PORT")
33                 .unwrap_or("8332".to_string())
34                 .parse::<u16>()
35                 .expect("BITCOIN_REST_PORT env variable must be a u16.");
36         let path = env::var("BITCOIN_REST_PATH").unwrap_or("/rest/".to_string());
37         HttpEndpoint::for_host(host).with_port(port).with_path(path)
38 }
39
40 pub(crate) fn db_config_table_creation_query() -> &'static str {
41         "CREATE TABLE IF NOT EXISTS config (
42                 id SERIAL PRIMARY KEY,
43                 db_schema integer
44         )"
45 }
46
47 pub(crate) fn db_announcement_table_creation_query() -> &'static str {
48         "CREATE TABLE IF NOT EXISTS channel_announcements (
49                 id SERIAL PRIMARY KEY,
50                 short_channel_id character varying(255) NOT NULL UNIQUE,
51                 block_height integer,
52                 chain_hash character varying(255),
53                 announcement_signed BYTEA,
54                 seen timestamp NOT NULL DEFAULT NOW()
55         )"
56 }
57
58 pub(crate) fn db_channel_update_table_creation_query() -> &'static str {
59         "CREATE TABLE IF NOT EXISTS channel_updates (
60                 id SERIAL PRIMARY KEY,
61                 composite_index character varying(255) UNIQUE,
62                 chain_hash character varying(255),
63                 short_channel_id character varying(255),
64                 timestamp bigint,
65                 channel_flags integer,
66                 direction integer,
67                 disable boolean,
68                 cltv_expiry_delta integer,
69                 htlc_minimum_msat bigint,
70                 fee_base_msat integer,
71                 fee_proportional_millionths integer,
72                 htlc_maximum_msat bigint,
73                 blob_signed BYTEA,
74                 seen timestamp NOT NULL DEFAULT NOW()
75         )"
76 }
77
78 pub(crate) fn db_index_creation_query() -> &'static str {
79         "
80         CREATE INDEX IF NOT EXISTS channels_seen ON channel_announcements(seen);
81         CREATE INDEX IF NOT EXISTS channel_updates_scid ON channel_updates(short_channel_id);
82         CREATE INDEX IF NOT EXISTS channel_updates_direction ON channel_updates(direction);
83         CREATE INDEX IF NOT EXISTS channel_updates_seen ON channel_updates(seen);
84         "
85 }
86
87 pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client) {
88         match schema {
89                 SCHEMA_VERSION => {},
90                 _ => panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION),
91         }
92 }
93
94 /// EDIT ME
95 pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> {
96         vec![
97                 // Bitfinex
98                 // (hex_utils::to_compressed_pubkey("033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025").unwrap(), "34.65.85.39:9735".parse().unwrap()),
99
100                 // Matt Corallo
101                 // (hex_utils::to_compressed_pubkey("03db10aa09ff04d3568b0621750794063df401e6853c79a21a83e1a3f3b5bfb0c8").unwrap(), "69.59.18.80:9735".parse().unwrap())
102
103                 // River Financial
104                 // (hex_utils::to_compressed_pubkey("03037dc08e9ac63b82581f79b662a4d0ceca8a8ca162b1af3551595b8f2d97b70a").unwrap(), "104.196.249.140:9735".parse().unwrap())
105
106                 // Wallet of Satoshi | 035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735
107                 (hex_utils::to_compressed_pubkey("035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226").unwrap(), "170.75.163.209:9735".parse().unwrap())
108         ]
109 }