Drop the chain_hash column
[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 = 2;
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                 announcement_signed BYTEA,
53                 seen timestamp NOT NULL DEFAULT NOW()
54         )"
55 }
56
57 pub(crate) fn db_channel_update_table_creation_query() -> &'static str {
58         "CREATE TABLE IF NOT EXISTS channel_updates (
59                 id SERIAL PRIMARY KEY,
60                 composite_index character varying(255) UNIQUE,
61                 short_channel_id character varying(255),
62                 timestamp bigint,
63                 channel_flags integer,
64                 direction integer,
65                 disable boolean,
66                 cltv_expiry_delta integer,
67                 htlc_minimum_msat bigint,
68                 fee_base_msat integer,
69                 fee_proportional_millionths integer,
70                 htlc_maximum_msat bigint,
71                 blob_signed BYTEA,
72                 seen timestamp NOT NULL DEFAULT NOW()
73         )"
74 }
75
76 pub(crate) fn db_index_creation_query() -> &'static str {
77         "
78         CREATE INDEX IF NOT EXISTS channels_seen ON channel_announcements(seen);
79         CREATE INDEX IF NOT EXISTS channel_updates_scid ON channel_updates(short_channel_id);
80         CREATE INDEX IF NOT EXISTS channel_updates_direction ON channel_updates(direction);
81         CREATE INDEX IF NOT EXISTS channel_updates_seen ON channel_updates(seen);
82         "
83 }
84
85 pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client) {
86         if schema == 1 {
87                 let tx = client.transaction().await.unwrap();
88                 tx.execute("ALTER TABLE channel_updates DROP COLUMN chain_hash", &[]).await.unwrap();
89                 tx.execute("ALTER TABLE channel_announcements DROP COLUMN chain_hash", &[]).await.unwrap();
90                 tx.execute("UPDATE config SET db_schema = 2 WHERE id = 1", &[]).await.unwrap();
91                 tx.commit().await.unwrap();
92         }
93         if schema <= 1 || schema > SCHEMA_VERSION {
94                 panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION);
95         }
96 }
97
98 /// EDIT ME
99 pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> {
100         vec![
101                 // Bitfinex
102                 // (hex_utils::to_compressed_pubkey("033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025").unwrap(), "34.65.85.39:9735".parse().unwrap()),
103
104                 // Matt Corallo
105                 // (hex_utils::to_compressed_pubkey("03db10aa09ff04d3568b0621750794063df401e6853c79a21a83e1a3f3b5bfb0c8").unwrap(), "69.59.18.80:9735".parse().unwrap())
106
107                 // River Financial
108                 // (hex_utils::to_compressed_pubkey("03037dc08e9ac63b82581f79b662a4d0ceca8a8ca162b1af3551595b8f2d97b70a").unwrap(), "104.196.249.140:9735".parse().unwrap())
109
110                 // Wallet of Satoshi | 035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735
111                 (hex_utils::to_compressed_pubkey("035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226").unwrap(), "170.75.163.209:9735".parse().unwrap())
112         ]
113 }