Drop useless indexes, add (very) useful indxes after benchmarking
[rapid-gossip-sync-server] / src / config.rs
1 use std::convert::TryInto;
2 use std::env;
3 use std::net::SocketAddr;
4 use std::io::Cursor;
5 use bitcoin::secp256k1::PublicKey;
6 use lightning::ln::msgs::ChannelAnnouncement;
7 use lightning::util::ser::Readable;
8 use lightning_block_sync::http::HttpEndpoint;
9 use tokio_postgres::Config;
10 use crate::hex_utils;
11
12 use futures::stream::{FuturesUnordered, StreamExt};
13
14 pub(crate) const SCHEMA_VERSION: i32 = 8;
15 pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
16 pub(crate) const DOWNLOAD_NEW_GOSSIP: bool = true;
17
18 pub(crate) fn network_graph_cache_path() -> &'static str {
19         "./res/network_graph.bin"
20 }
21
22 pub(crate) fn db_connection_config() -> Config {
23         let mut config = Config::new();
24         let host = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_HOST").unwrap_or("localhost".to_string());
25         let user = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_USER").unwrap_or("alice".to_string());
26         let db = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_NAME").unwrap_or("ln_graph_sync".to_string());
27         config.host(&host);
28         config.user(&user);
29         config.dbname(&db);
30         if let Ok(password) = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD") {
31                 config.password(&password);
32         }
33         config
34 }
35
36 pub(crate) fn bitcoin_rest_endpoint() -> HttpEndpoint {
37         let host = env::var("BITCOIN_REST_DOMAIN").unwrap_or("127.0.0.1".to_string());
38         let port = env::var("BITCOIN_REST_PORT")
39                 .unwrap_or("8332".to_string())
40                 .parse::<u16>()
41                 .expect("BITCOIN_REST_PORT env variable must be a u16.");
42         let path = env::var("BITCOIN_REST_PATH").unwrap_or("/rest/".to_string());
43         HttpEndpoint::for_host(host).with_port(port).with_path(path)
44 }
45
46 pub(crate) fn db_config_table_creation_query() -> &'static str {
47         "CREATE TABLE IF NOT EXISTS config (
48                 id SERIAL PRIMARY KEY,
49                 db_schema integer
50         )"
51 }
52
53 pub(crate) fn db_announcement_table_creation_query() -> &'static str {
54         "CREATE TABLE IF NOT EXISTS channel_announcements (
55                 id SERIAL PRIMARY KEY,
56                 short_channel_id bigint NOT NULL UNIQUE,
57                 announcement_signed BYTEA,
58                 seen timestamp NOT NULL DEFAULT NOW()
59         )"
60 }
61
62 pub(crate) fn db_channel_update_table_creation_query() -> &'static str {
63         "CREATE TABLE IF NOT EXISTS channel_updates (
64                 id SERIAL PRIMARY KEY,
65                 short_channel_id bigint NOT NULL,
66                 timestamp bigint NOT NULL,
67                 channel_flags smallint NOT NULL,
68                 direction boolean NOT NULL,
69                 disable boolean NOT NULL,
70                 cltv_expiry_delta integer NOT NULL,
71                 htlc_minimum_msat bigint NOT NULL,
72                 fee_base_msat integer NOT NULL,
73                 fee_proportional_millionths integer NOT NULL,
74                 htlc_maximum_msat bigint NOT NULL,
75                 blob_signed BYTEA NOT NULL,
76                 seen timestamp NOT NULL DEFAULT NOW()
77         )"
78 }
79
80 pub(crate) fn db_index_creation_query() -> &'static str {
81         "
82         CREATE INDEX IF NOT EXISTS channel_updates_scid_seen ON channel_updates(short_channel_id, seen) INCLUDE (blob_signed);
83         CREATE INDEX IF NOT EXISTS channel_updates_seen_scid ON channel_updates(seen, short_channel_id);
84         CREATE INDEX IF NOT EXISTS channel_updates_scid_dir_seen ON channel_updates(short_channel_id ASC, direction ASC, seen DESC) INCLUDE (id, blob_signed);
85         CREATE UNIQUE INDEX IF NOT EXISTS channel_updates_key ON channel_updates (short_channel_id, direction, timestamp);
86         "
87 }
88
89 pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client) {
90         if schema == 1 {
91                 let tx = client.transaction().await.unwrap();
92                 tx.execute("ALTER TABLE channel_updates DROP COLUMN chain_hash", &[]).await.unwrap();
93                 tx.execute("ALTER TABLE channel_announcements DROP COLUMN chain_hash", &[]).await.unwrap();
94                 tx.execute("UPDATE config SET db_schema = 2 WHERE id = 1", &[]).await.unwrap();
95                 tx.commit().await.unwrap();
96         }
97         if schema == 1 || schema == 2 {
98                 let tx = client.transaction().await.unwrap();
99                 tx.execute("ALTER TABLE channel_updates DROP COLUMN short_channel_id", &[]).await.unwrap();
100                 tx.execute("ALTER TABLE channel_updates ADD COLUMN short_channel_id bigint DEFAULT null", &[]).await.unwrap();
101                 tx.execute("ALTER TABLE channel_updates DROP COLUMN direction", &[]).await.unwrap();
102                 tx.execute("ALTER TABLE channel_updates ADD COLUMN direction boolean DEFAULT null", &[]).await.unwrap();
103                 loop {
104                         let rows = tx.query("SELECT id, composite_index FROM channel_updates WHERE short_channel_id IS NULL LIMIT 50000", &[]).await.unwrap();
105                         if rows.is_empty() { break; }
106                         let mut updates = FuturesUnordered::new();
107                         for row in rows {
108                                 let id: i32 = row.get("id");
109                                 let index: String = row.get("composite_index");
110                                 let tx_ref = &tx;
111                                 updates.push(async move {
112                                         let mut index_iter = index.split(":");
113                                         let scid_hex = index_iter.next().unwrap();
114                                         index_iter.next().unwrap();
115                                         let direction_str = index_iter.next().unwrap();
116                                         assert!(direction_str == "1" || direction_str == "0");
117                                         let direction = direction_str == "1";
118                                         let scid_be_bytes = hex_utils::to_vec(scid_hex).unwrap();
119                                         let scid = i64::from_be_bytes(scid_be_bytes.try_into().unwrap());
120                                         assert!(scid > 0); // Will roll over in some 150 years or so
121                                         tx_ref.execute("UPDATE channel_updates SET short_channel_id = $1, direction = $2 WHERE id = $3", &[&scid, &direction, &id]).await.unwrap();
122                                 });
123                         }
124                         while let Some(_) = updates.next().await { }
125                 }
126                 tx.execute("ALTER TABLE channel_updates ALTER short_channel_id DROP DEFAULT", &[]).await.unwrap();
127                 tx.execute("ALTER TABLE channel_updates ALTER short_channel_id SET NOT NULL", &[]).await.unwrap();
128                 tx.execute("ALTER TABLE channel_updates ALTER direction DROP DEFAULT", &[]).await.unwrap();
129                 tx.execute("ALTER TABLE channel_updates ALTER direction SET NOT NULL", &[]).await.unwrap();
130                 tx.execute("UPDATE config SET db_schema = 3 WHERE id = 1", &[]).await.unwrap();
131                 tx.commit().await.unwrap();
132         }
133         if schema >= 1 && schema <= 3 {
134                 let tx = client.transaction().await.unwrap();
135                 tx.execute("ALTER TABLE channel_announcements DROP COLUMN short_channel_id", &[]).await.unwrap();
136                 tx.execute("ALTER TABLE channel_announcements ADD COLUMN short_channel_id bigint DEFAULT null", &[]).await.unwrap();
137                 loop {
138                         let rows = tx.query("SELECT id, announcement_signed FROM channel_announcements WHERE short_channel_id IS NULL LIMIT 10000", &[]).await.unwrap();
139                         if rows.is_empty() { break; }
140                         let mut updates = FuturesUnordered::new();
141                         for row in rows {
142                                 let id: i32 = row.get("id");
143                                 let announcement: Vec<u8> = row.get("announcement_signed");
144                                 let tx_ref = &tx;
145                                 updates.push(async move {
146                                         let scid = ChannelAnnouncement::read(&mut Cursor::new(announcement)).unwrap().contents.short_channel_id as i64;
147                                         assert!(scid > 0); // Will roll over in some 150 years or so
148                                         tx_ref.execute("UPDATE channel_announcements SET short_channel_id = $1 WHERE id = $2", &[&scid, &id]).await.unwrap();
149                                 });
150                         }
151                         while let Some(_) = updates.next().await { }
152                 }
153                 tx.execute("ALTER TABLE channel_announcements ADD CONSTRAINT channel_announcements_short_channel_id_key UNIQUE (short_channel_id)", &[]).await.unwrap();
154                 tx.execute("ALTER TABLE channel_announcements ALTER short_channel_id DROP DEFAULT", &[]).await.unwrap();
155                 tx.execute("ALTER TABLE channel_announcements ALTER short_channel_id SET NOT NULL", &[]).await.unwrap();
156                 tx.execute("UPDATE config SET db_schema = 4 WHERE id = 1", &[]).await.unwrap();
157                 tx.commit().await.unwrap();
158         }
159         if schema >= 1 && schema <= 4 {
160                 let tx = client.transaction().await.unwrap();
161                 tx.execute("ALTER TABLE channel_updates ALTER composite_index SET DATA TYPE character(29)", &[]).await.unwrap();
162                 tx.execute("UPDATE config SET db_schema = 5 WHERE id = 1", &[]).await.unwrap();
163                 tx.commit().await.unwrap();
164         }
165         if schema >= 1 && schema <= 5 {
166                 let tx = client.transaction().await.unwrap();
167                 tx.execute("ALTER TABLE channel_updates ALTER channel_flags SET DATA TYPE smallint", &[]).await.unwrap();
168                 tx.execute("ALTER TABLE channel_announcements DROP COLUMN block_height", &[]).await.unwrap();
169                 tx.execute("UPDATE config SET db_schema = 6 WHERE id = 1", &[]).await.unwrap();
170                 tx.commit().await.unwrap();
171         }
172         if schema >= 1 && schema <= 6 {
173                 let tx = client.transaction().await.unwrap();
174                 tx.execute("ALTER TABLE channel_updates DROP COLUMN composite_index", &[]).await.unwrap();
175                 tx.execute("ALTER TABLE channel_updates ALTER timestamp SET NOT NULL", &[]).await.unwrap();
176                 tx.execute("ALTER TABLE channel_updates ALTER channel_flags SET NOT NULL", &[]).await.unwrap();
177                 tx.execute("ALTER TABLE channel_updates ALTER disable SET NOT NULL", &[]).await.unwrap();
178                 tx.execute("ALTER TABLE channel_updates ALTER cltv_expiry_delta SET NOT NULL", &[]).await.unwrap();
179                 tx.execute("ALTER TABLE channel_updates ALTER htlc_minimum_msat SET NOT NULL", &[]).await.unwrap();
180                 tx.execute("ALTER TABLE channel_updates ALTER fee_base_msat SET NOT NULL", &[]).await.unwrap();
181                 tx.execute("ALTER TABLE channel_updates ALTER fee_proportional_millionths SET NOT NULL", &[]).await.unwrap();
182                 tx.execute("ALTER TABLE channel_updates ALTER htlc_maximum_msat SET NOT NULL", &[]).await.unwrap();
183                 tx.execute("ALTER TABLE channel_updates ALTER blob_signed SET NOT NULL", &[]).await.unwrap();
184                 tx.execute("CREATE UNIQUE INDEX channel_updates_key ON channel_updates (short_channel_id, direction, timestamp)", &[]).await.unwrap();
185                 tx.execute("UPDATE config SET db_schema = 7 WHERE id = 1", &[]).await.unwrap();
186                 tx.commit().await.unwrap();
187         }
188         if schema >= 1 && schema <= 7 {
189                 let tx = client.transaction().await.unwrap();
190                 tx.execute("DROP INDEX channels_seen", &[]).await.unwrap();
191                 tx.execute("DROP INDEX channel_updates_scid", &[]).await.unwrap();
192                 tx.execute("DROP INDEX channel_updates_direction", &[]).await.unwrap();
193                 tx.execute("DROP INDEX channel_updates_seen", &[]).await.unwrap();
194                 tx.execute("DROP INDEX channel_updates_scid_seen", &[]).await.unwrap();
195                 tx.execute("DROP INDEX channel_updates_scid_dir_seen", &[]).await.unwrap();
196                 tx.execute("UPDATE config SET db_schema = 8 WHERE id = 1", &[]).await.unwrap();
197                 tx.commit().await.unwrap();
198         }
199         if schema <= 1 || schema > SCHEMA_VERSION {
200                 panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION);
201         }
202 }
203
204 /// EDIT ME
205 pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> {
206         vec![
207                 // Bitfinex
208                 // (hex_utils::to_compressed_pubkey("033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025").unwrap(), "34.65.85.39:9735".parse().unwrap()),
209
210                 // Matt Corallo
211                 // (hex_utils::to_compressed_pubkey("03db10aa09ff04d3568b0621750794063df401e6853c79a21a83e1a3f3b5bfb0c8").unwrap(), "69.59.18.80:9735".parse().unwrap())
212
213                 // River Financial
214                 // (hex_utils::to_compressed_pubkey("03037dc08e9ac63b82581f79b662a4d0ceca8a8ca162b1af3551595b8f2d97b70a").unwrap(), "104.196.249.140:9735".parse().unwrap())
215
216                 // Wallet of Satoshi | 035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735
217                 (hex_utils::to_compressed_pubkey("035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226").unwrap(), "170.75.163.209:9735".parse().unwrap())
218         ]
219 }