1 use std::convert::TryInto;
3 use std::net::SocketAddr;
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;
12 use futures::stream::{FuturesUnordered, StreamExt};
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;
18 pub(crate) fn network_graph_cache_path() -> &'static str {
19 "./res/network_graph.bin"
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());
30 if let Ok(password) = env::var("RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD") {
31 config.password(&password);
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())
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)
46 pub(crate) fn db_config_table_creation_query() -> &'static str {
47 "CREATE TABLE IF NOT EXISTS config (
48 id SERIAL PRIMARY KEY,
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()
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()
80 pub(crate) fn db_index_creation_query() -> &'static str {
82 CREATE INDEX IF NOT EXISTS channel_updates_seen ON channel_updates(seen, short_channel_id, direction) INCLUDE (id, blob_signed);
83 CREATE INDEX IF NOT EXISTS channel_updates_scid_seen ON channel_updates(short_channel_id, seen) INCLUDE (blob_signed);
84 CREATE INDEX IF NOT EXISTS channel_updates_seen_scid ON channel_updates(seen, short_channel_id);
85 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);
86 CREATE UNIQUE INDEX IF NOT EXISTS channel_updates_key ON channel_updates (short_channel_id, direction, timestamp);
90 pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client) {
92 let tx = client.transaction().await.unwrap();
93 tx.execute("ALTER TABLE channel_updates DROP COLUMN chain_hash", &[]).await.unwrap();
94 tx.execute("ALTER TABLE channel_announcements DROP COLUMN chain_hash", &[]).await.unwrap();
95 tx.execute("UPDATE config SET db_schema = 2 WHERE id = 1", &[]).await.unwrap();
96 tx.commit().await.unwrap();
98 if schema == 1 || schema == 2 {
99 let tx = client.transaction().await.unwrap();
100 tx.execute("ALTER TABLE channel_updates DROP COLUMN short_channel_id", &[]).await.unwrap();
101 tx.execute("ALTER TABLE channel_updates ADD COLUMN short_channel_id bigint DEFAULT null", &[]).await.unwrap();
102 tx.execute("ALTER TABLE channel_updates DROP COLUMN direction", &[]).await.unwrap();
103 tx.execute("ALTER TABLE channel_updates ADD COLUMN direction boolean DEFAULT null", &[]).await.unwrap();
105 let rows = tx.query("SELECT id, composite_index FROM channel_updates WHERE short_channel_id IS NULL LIMIT 50000", &[]).await.unwrap();
106 if rows.is_empty() { break; }
107 let mut updates = FuturesUnordered::new();
109 let id: i32 = row.get("id");
110 let index: String = row.get("composite_index");
112 updates.push(async move {
113 let mut index_iter = index.split(":");
114 let scid_hex = index_iter.next().unwrap();
115 index_iter.next().unwrap();
116 let direction_str = index_iter.next().unwrap();
117 assert!(direction_str == "1" || direction_str == "0");
118 let direction = direction_str == "1";
119 let scid_be_bytes = hex_utils::to_vec(scid_hex).unwrap();
120 let scid = i64::from_be_bytes(scid_be_bytes.try_into().unwrap());
121 assert!(scid > 0); // Will roll over in some 150 years or so
122 tx_ref.execute("UPDATE channel_updates SET short_channel_id = $1, direction = $2 WHERE id = $3", &[&scid, &direction, &id]).await.unwrap();
125 while let Some(_) = updates.next().await { }
127 tx.execute("ALTER TABLE channel_updates ALTER short_channel_id DROP DEFAULT", &[]).await.unwrap();
128 tx.execute("ALTER TABLE channel_updates ALTER short_channel_id SET NOT NULL", &[]).await.unwrap();
129 tx.execute("ALTER TABLE channel_updates ALTER direction DROP DEFAULT", &[]).await.unwrap();
130 tx.execute("ALTER TABLE channel_updates ALTER direction SET NOT NULL", &[]).await.unwrap();
131 tx.execute("UPDATE config SET db_schema = 3 WHERE id = 1", &[]).await.unwrap();
132 tx.commit().await.unwrap();
134 if schema >= 1 && schema <= 3 {
135 let tx = client.transaction().await.unwrap();
136 tx.execute("ALTER TABLE channel_announcements DROP COLUMN short_channel_id", &[]).await.unwrap();
137 tx.execute("ALTER TABLE channel_announcements ADD COLUMN short_channel_id bigint DEFAULT null", &[]).await.unwrap();
139 let rows = tx.query("SELECT id, announcement_signed FROM channel_announcements WHERE short_channel_id IS NULL LIMIT 10000", &[]).await.unwrap();
140 if rows.is_empty() { break; }
141 let mut updates = FuturesUnordered::new();
143 let id: i32 = row.get("id");
144 let announcement: Vec<u8> = row.get("announcement_signed");
146 updates.push(async move {
147 let scid = ChannelAnnouncement::read(&mut Cursor::new(announcement)).unwrap().contents.short_channel_id as i64;
148 assert!(scid > 0); // Will roll over in some 150 years or so
149 tx_ref.execute("UPDATE channel_announcements SET short_channel_id = $1 WHERE id = $2", &[&scid, &id]).await.unwrap();
152 while let Some(_) = updates.next().await { }
154 tx.execute("ALTER TABLE channel_announcements ADD CONSTRAINT channel_announcements_short_channel_id_key UNIQUE (short_channel_id)", &[]).await.unwrap();
155 tx.execute("ALTER TABLE channel_announcements ALTER short_channel_id DROP DEFAULT", &[]).await.unwrap();
156 tx.execute("ALTER TABLE channel_announcements ALTER short_channel_id SET NOT NULL", &[]).await.unwrap();
157 tx.execute("UPDATE config SET db_schema = 4 WHERE id = 1", &[]).await.unwrap();
158 tx.commit().await.unwrap();
160 if schema >= 1 && schema <= 4 {
161 let tx = client.transaction().await.unwrap();
162 tx.execute("ALTER TABLE channel_updates ALTER composite_index SET DATA TYPE character(29)", &[]).await.unwrap();
163 tx.execute("UPDATE config SET db_schema = 5 WHERE id = 1", &[]).await.unwrap();
164 tx.commit().await.unwrap();
166 if schema >= 1 && schema <= 5 {
167 let tx = client.transaction().await.unwrap();
168 tx.execute("ALTER TABLE channel_updates ALTER channel_flags SET DATA TYPE smallint", &[]).await.unwrap();
169 tx.execute("ALTER TABLE channel_announcements DROP COLUMN block_height", &[]).await.unwrap();
170 tx.execute("UPDATE config SET db_schema = 6 WHERE id = 1", &[]).await.unwrap();
171 tx.commit().await.unwrap();
173 if schema >= 1 && schema <= 6 {
174 let tx = client.transaction().await.unwrap();
175 tx.execute("ALTER TABLE channel_updates DROP COLUMN composite_index", &[]).await.unwrap();
176 tx.execute("ALTER TABLE channel_updates ALTER timestamp SET NOT NULL", &[]).await.unwrap();
177 tx.execute("ALTER TABLE channel_updates ALTER channel_flags SET NOT NULL", &[]).await.unwrap();
178 tx.execute("ALTER TABLE channel_updates ALTER disable SET NOT NULL", &[]).await.unwrap();
179 tx.execute("ALTER TABLE channel_updates ALTER cltv_expiry_delta SET NOT NULL", &[]).await.unwrap();
180 tx.execute("ALTER TABLE channel_updates ALTER htlc_minimum_msat SET NOT NULL", &[]).await.unwrap();
181 tx.execute("ALTER TABLE channel_updates ALTER fee_base_msat SET NOT NULL", &[]).await.unwrap();
182 tx.execute("ALTER TABLE channel_updates ALTER fee_proportional_millionths SET NOT NULL", &[]).await.unwrap();
183 tx.execute("ALTER TABLE channel_updates ALTER htlc_maximum_msat SET NOT NULL", &[]).await.unwrap();
184 tx.execute("ALTER TABLE channel_updates ALTER blob_signed SET NOT NULL", &[]).await.unwrap();
185 tx.execute("CREATE UNIQUE INDEX channel_updates_key ON channel_updates (short_channel_id, direction, timestamp)", &[]).await.unwrap();
186 tx.execute("UPDATE config SET db_schema = 7 WHERE id = 1", &[]).await.unwrap();
187 tx.commit().await.unwrap();
189 if schema >= 1 && schema <= 7 {
190 let tx = client.transaction().await.unwrap();
191 tx.execute("DROP INDEX channels_seen", &[]).await.unwrap();
192 tx.execute("DROP INDEX channel_updates_scid", &[]).await.unwrap();
193 tx.execute("DROP INDEX channel_updates_direction", &[]).await.unwrap();
194 tx.execute("DROP INDEX channel_updates_seen", &[]).await.unwrap();
195 tx.execute("DROP INDEX channel_updates_scid_seen", &[]).await.unwrap();
196 tx.execute("DROP INDEX channel_updates_scid_dir_seen", &[]).await.unwrap();
197 tx.execute("UPDATE config SET db_schema = 8 WHERE id = 1", &[]).await.unwrap();
198 tx.commit().await.unwrap();
200 if schema <= 1 || schema > SCHEMA_VERSION {
201 panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION);
206 pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> {
209 // (hex_utils::to_compressed_pubkey("033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025").unwrap(), "34.65.85.39:9735".parse().unwrap()),
212 // (hex_utils::to_compressed_pubkey("03db10aa09ff04d3568b0621750794063df401e6853c79a21a83e1a3f3b5bfb0c8").unwrap(), "69.59.18.80:9735".parse().unwrap())
215 // (hex_utils::to_compressed_pubkey("03037dc08e9ac63b82581f79b662a4d0ceca8a8ca162b1af3551595b8f2d97b70a").unwrap(), "104.196.249.140:9735".parse().unwrap())
217 // Wallet of Satoshi | 035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735
218 (hex_utils::to_compressed_pubkey("035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226").unwrap(), "170.75.163.209:9735".parse().unwrap())