Skip to content

Commit 32ac36a

Browse files
authored
Merge pull request lightningdevkit#24 from andrei-21/feature/configure-peers
Make ln peers configurable
2 parents f0ef173 + f395a2f commit 32ac36a

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@ These are the components it's comprised of.
1212
A config file where the Postgres credentials and Lightning peers can be adjusted. Most adjustments
1313
can be made by setting environment variables, whose usage is as follows:
1414

15-
| Name | Default | Description |
16-
|:-------------------------------------|:--------------|:-----------------------------------------------------------------------------------------------------------|
17-
| RAPID_GOSSIP_SYNC_SERVER_DB_HOST | localhost | Domain of the Postgres database |
18-
| RAPID_GOSSIP_SYNC_SERVER_DB_USER | alice | Username to access Postgres |
19-
| RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD | _None_ | Password to access Postgres |
20-
| RAPID_GOSSIP_SYNC_SERVER_DB_NAME | ln_graph_sync | Name of the database to be used for gossip storage |
21-
| BITCOIN_REST_DOMAIN | 127.0.0.1 | Domain of the [bitcoind REST server](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md) |
22-
| BITCOIN_REST_PORT | 8332 | HTTP port of the bitcoind REST server |
23-
| BITCOIN_REST_PATH | /rest/ | Path infix to access the bitcoind REST endpoints |
24-
25-
Notably, one property needs to be modified in code, namely the `ln_peers()` method. It specifies how
26-
many and which peers to use for retrieving gossip.
15+
| Name | Default | Description |
16+
|:-------------------------------------|:--------------------|:-----------------------------------------------------------------------------------------------------------|
17+
| RAPID_GOSSIP_SYNC_SERVER_DB_HOST | localhost | Domain of the Postgres database |
18+
| RAPID_GOSSIP_SYNC_SERVER_DB_USER | alice | Username to access Postgres |
19+
| RAPID_GOSSIP_SYNC_SERVER_DB_PASSWORD | _None_ | Password to access Postgres |
20+
| RAPID_GOSSIP_SYNC_SERVER_DB_NAME | ln_graph_sync | Name of the database to be used for gossip storage |
21+
| BITCOIN_REST_DOMAIN | 127.0.0.1 | Domain of the [bitcoind REST server](https://github.com/bitcoin/bitcoin/blob/master/doc/REST-interface.md) |
22+
| BITCOIN_REST_PORT | 8332 | HTTP port of the bitcoind REST server |
23+
| BITCOIN_REST_PATH | /rest/ | Path infix to access the bitcoind REST endpoints |
24+
| LN_PEERS | _Wallet of Satoshi_ | Comma separated list of LN peers to use for retrieving gossip |
2725

2826
### downloader
2927

src/config.rs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
use crate::hex_utils;
2+
13
use std::convert::TryInto;
24
use std::env;
3-
use std::net::SocketAddr;
45
use std::io::Cursor;
6+
use std::net::{SocketAddr, ToSocketAddrs};
7+
8+
use bitcoin::hashes::hex::FromHex;
59
use bitcoin::secp256k1::PublicKey;
10+
use futures::stream::{FuturesUnordered, StreamExt};
611
use lightning::ln::msgs::ChannelAnnouncement;
712
use lightning::util::ser::Readable;
813
use lightning_block_sync::http::HttpEndpoint;
914
use tokio_postgres::Config;
10-
use crate::hex_utils;
11-
12-
use futures::stream::{FuturesUnordered, StreamExt};
1315

1416
pub(crate) const SCHEMA_VERSION: i32 = 8;
1517
pub(crate) const SNAPSHOT_CALCULATION_INTERVAL: u32 = 3600 * 24; // every 24 hours, in seconds
@@ -209,19 +211,54 @@ pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client)
209211
let _ = client.execute("ALTER TABLE channel_announcements SET ( autovacuum_vacuum_insert_scale_factor = 0.005 );", &[]).await;
210212
}
211213

212-
/// EDIT ME
213214
pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> {
214-
vec![
215-
// Bitfinex
216-
// (hex_utils::to_compressed_pubkey("033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025").unwrap(), "34.65.85.39:9735".parse().unwrap()),
215+
const WALLET_OF_SATOSHI: &str = "035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735";
216+
let list = env::var("LN_PEERS").unwrap_or(WALLET_OF_SATOSHI.to_string());
217+
let mut peers = Vec::new();
218+
for peer_info in list.split(',') {
219+
peers.push(resolve_peer_info(peer_info).expect("Invalid peer info in LN_PEERS"));
220+
}
221+
peers
222+
}
223+
224+
fn resolve_peer_info(peer_info: &str) -> Result<(PublicKey, SocketAddr), &str> {
225+
let mut peer_info = peer_info.splitn(2, '@');
226+
227+
let pubkey = peer_info.next().ok_or("Invalid peer info. Should be formatted as: `pubkey@host:port`")?;
228+
let pubkey = Vec::from_hex(pubkey).map_err(|_| "Invalid node pubkey")?;
229+
let pubkey = PublicKey::from_slice(&pubkey).map_err(|_| "Invalid node pubkey")?;
230+
231+
let socket_address = peer_info.next().ok_or("Invalid peer info. Should be formatted as: `pubkey@host:port`")?;
232+
let socket_address = socket_address
233+
.to_socket_addrs()
234+
.map_err(|_| "Cannot resolve node address")?
235+
.next()
236+
.ok_or("Cannot resolve node address")?;
217237

218-
// Matt Corallo
219-
// (hex_utils::to_compressed_pubkey("03db10aa09ff04d3568b0621750794063df401e6853c79a21a83e1a3f3b5bfb0c8").unwrap(), "69.59.18.80:9735".parse().unwrap())
238+
Ok((pubkey, socket_address))
239+
}
240+
241+
#[cfg(test)]
242+
mod tests {
243+
use super::resolve_peer_info;
244+
use bitcoin::hashes::hex::ToHex;
220245

221-
// River Financial
222-
// (hex_utils::to_compressed_pubkey("03037dc08e9ac63b82581f79b662a4d0ceca8a8ca162b1af3551595b8f2d97b70a").unwrap(), "104.196.249.140:9735".parse().unwrap())
246+
#[test]
247+
fn test_resolve_peer_info() {
248+
let wallet_of_satoshi = "035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735";
249+
let (pubkey, socket_address) = resolve_peer_info(wallet_of_satoshi).unwrap();
250+
assert_eq!(pubkey.serialize().to_hex(), "035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226");
251+
assert_eq!(socket_address.to_string(), "170.75.163.209:9735");
223252

224-
// Wallet of Satoshi | 035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226@170.75.163.209:9735
225-
(hex_utils::to_compressed_pubkey("035e4ff418fc8b5554c5d9eea66396c227bd429a3251c8cbc711002ba215bfc226").unwrap(), "170.75.163.209:9735".parse().unwrap())
226-
]
253+
let ipv6 = "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025@[2001:db8::1]:80";
254+
let (pubkey, socket_address) = resolve_peer_info(ipv6).unwrap();
255+
assert_eq!(pubkey.serialize().to_hex(), "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025");
256+
assert_eq!(socket_address.to_string(), "[2001:db8::1]:80");
257+
258+
let localhost = "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025@localhost:9735";
259+
let (pubkey, socket_address) = resolve_peer_info(localhost).unwrap();
260+
assert_eq!(pubkey.serialize().to_hex(), "033d8656219478701227199cbd6f670335c8d408a92ae88b962c49d4dc0e83e025");
261+
let socket_address = socket_address.to_string();
262+
assert!(socket_address == "127.0.0.1:9735" || socket_address == "[::1]:9735");
263+
}
227264
}

src/hex_utils.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use bitcoin::secp256k1::PublicKey;
2-
31
pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
42
let mut out = Vec::with_capacity(hex.len() / 2);
53

@@ -20,14 +18,3 @@ pub fn to_vec(hex: &str) -> Option<Vec<u8>> {
2018

2119
Some(out)
2220
}
23-
24-
pub fn to_compressed_pubkey(hex: &str) -> Option<PublicKey> {
25-
let data = match to_vec(&hex[0..33 * 2]) {
26-
Some(bytes) => bytes,
27-
None => return None,
28-
};
29-
match PublicKey::from_slice(&data) {
30-
Ok(pk) => Some(pk),
31-
Err(_) => None,
32-
}
33-
}

0 commit comments

Comments
 (0)