Skip to content

Commit dc117ba

Browse files
committed
solo implementation with 5.2 Codex
Signed-off-by: dzdidi <dzdidi@protonmail.com>
1 parent 726a340 commit dc117ba

4 files changed

Lines changed: 77 additions & 18 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ libc = "0.2"
2727
chrono = { version = "0.4", default-features = false, features = ["clock"] }
2828
rand = "0.4"
2929
serde_json = { version = "1.0" }
30+
serde = { version = "1.0", features = ["derive"] }
3031
tokio = { version = "1", features = [ "io-util", "macros", "rt", "rt-multi-thread", "sync", "net", "time", "io-std" ] }
3132

3233
[profile.release]

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ cargo run <bitcoind-rpc-username>:<bitcoind-rpc-password>@<bitcoind-rpc-host>:<b
2121
`announced-listen-addr` can be set to an IPv4 or IPv6 address to announce that as a publicly-connectable address for this node.
2222
`announced-node-name` can be any string up to 32 bytes in length, representing this node's alias.
2323

24+
## Probe Config
25+
Targeted probing is disabled unless a config file exists.
26+
27+
```
28+
cp ./prober_config.json.example <ldk data dir>/prober_config.json
29+
```
30+
31+
`probe_peers` pubkey of peers you are probing.
32+
33+
`probe_interval_sec` how often to probe, in seconds.
34+
35+
`max_amount_msats` maximum amount to probe, in millisatoshis.
36+
2437
## License
2538

2639
Licensed under either:

prober_config.json.example

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"probe_interval_sec" : 10,
3+
"probe_peers" : [
4+
"03cc91150efc4bbe8d9f8ced06fbc81ad50b076b9759139e4c2d7a8380566da7e1",
5+
"037d8e050899fa0732fdd6fc1e0d5d8d685c2093932a7e10dc5e6fab8a34ed1c43"
6+
],
7+
"max_amount_msats": 500000000
8+
}

src/main.rs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use lightning_dns_resolver::OMDomainResolver;
5858
use lightning_net_tokio::SocketDescriptor;
5959
use lightning_persister::fs_store::FilesystemStore;
6060
use rand::{thread_rng, Rng};
61+
use serde::Deserialize;
6162
use std::collections::HashMap as StdHashMap;
6263
use std::convert::TryInto;
6364
use std::fmt;
@@ -77,6 +78,13 @@ pub(crate) enum HTLCStatus {
7778
Failed,
7879
}
7980

81+
#[derive(Deserialize)]
82+
struct ProbeConfig {
83+
probe_interval_sec: u64,
84+
probe_peers: Vec<String>,
85+
max_amount_msats: u64,
86+
}
87+
8088
impl_writeable_tlv_based_enum!(HTLCStatus,
8189
(0, Pending) => {},
8290
(1, Succeeded) => {},
@@ -213,21 +221,20 @@ pub(crate) type OutputSweeper = ldk_sweep::OutputSweeper<
213221
// Needed due to rust-lang/rust#63033.
214222
struct OutputSweeperWrapper(Arc<OutputSweeper>);
215223

216-
fn send_rand_probe(
224+
fn prepare_probe(
217225
channel_manager: &ChannelManager, graph: &NetworkGraph, logger: &disk::FilesystemLogger,
218226
scorer: &RwLock<ProbabilisticScorer<Arc<NetworkGraph>, Arc<disk::FilesystemLogger>>>,
227+
pub_key_hex: &str, probe_amount: u64,
219228
) {
220-
let rcpt = {
221-
let lck = graph.read_only();
222-
if lck.nodes().is_empty() {
223-
return;
224-
}
225-
let mut it =
226-
lck.nodes().unordered_iter().skip(::rand::random::<usize>() % lck.nodes().len());
227-
it.next().unwrap().0.clone()
229+
if probe_amount == 0 {
230+
return;
231+
}
232+
let amt = ::rand::random::<u64>() % probe_amount;
233+
let pub_key_bytes = match hex_utils::to_vec(pub_key_hex) {
234+
Some(bytes) => bytes,
235+
None => return,
228236
};
229-
let amt = ::rand::random::<u64>() % 500_000_000;
230-
if let Ok(pk) = bitcoin::secp256k1::PublicKey::from_slice(rcpt.as_slice()) {
237+
if let Ok(pk) = bitcoin::secp256k1::PublicKey::from_slice(&pub_key_bytes) {
231238
send_probe(channel_manager, pk, graph, logger, amt, scorer);
232239
}
233240
}
@@ -262,6 +269,14 @@ fn send_probe(
262269
}
263270
}
264271

272+
fn read_probe_file(ldk_data_dir: &str) -> Result<ProbeConfig, std::io::Error> {
273+
let prober_file = format!("{}/prober_config.json", ldk_data_dir);
274+
let file = fs::read_to_string(prober_file)?;
275+
let config: ProbeConfig = serde_json::from_str(&file)
276+
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
277+
Ok(config)
278+
}
279+
265280
fn handle_ldk_events<'a>(
266281
channel_manager: Arc<ChannelManager>, bitcoind_client: &'a BitcoindClient,
267282
network_graph: &'a NetworkGraph, keys_manager: &'a KeysManager,
@@ -1216,13 +1231,35 @@ async fn start_ldk() {
12161231
let probing_graph = Arc::clone(&network_graph);
12171232
let probing_logger = Arc::clone(&logger);
12181233
let probing_scorer = Arc::clone(&scorer);
1219-
tokio::spawn(async move {
1220-
let mut interval = tokio::time::interval(Duration::from_secs(1));
1221-
loop {
1222-
interval.tick().await;
1223-
send_rand_probe(&*probing_cm, &*probing_graph, &*probing_logger, &*probing_scorer);
1224-
}
1225-
});
1234+
match read_probe_file(&ldk_data_dir) {
1235+
Ok(probe_config) => {
1236+
if probe_config.probe_peers.is_empty() {
1237+
println!("WARNING: prober_config.json has no probe_peers. Probing disabled.");
1238+
} else {
1239+
let mut index = 0usize;
1240+
tokio::spawn(async move {
1241+
let mut interval =
1242+
tokio::time::interval(Duration::from_secs(probe_config.probe_interval_sec));
1243+
loop {
1244+
interval.tick().await;
1245+
if index >= probe_config.probe_peers.len() {
1246+
index = 0;
1247+
}
1248+
prepare_probe(
1249+
&*probing_cm,
1250+
&*probing_graph,
1251+
&*probing_logger,
1252+
&*probing_scorer,
1253+
&probe_config.probe_peers[index],
1254+
probe_config.max_amount_msats,
1255+
);
1256+
index += 1;
1257+
}
1258+
});
1259+
}
1260+
},
1261+
Err(_) => println!("WARNING: prober_config.json is missing. Probing disabled."),
1262+
};
12261263

12271264
// Start the CLI.
12281265
let cli_channel_manager = Arc::clone(&channel_manager);

0 commit comments

Comments
 (0)