lnd 0.1.6

Utility to run a regtest lnd process connected to a given bitcoind instance, useful in integration testing environment.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// #![warn(missing_docs)]

//!
//! LND
//!
//! Utility to run a regtest LND process, useful in integration testing environment
//!

mod error;
// mod ext;
pub mod versions;

use anyhow::Context;
use bitcoind::bitcoincore_rpc::jsonrpc::serde_json::Value;
use bitcoind::bitcoincore_rpc::RpcApi;
use bitcoind::tempfile::TempDir;
use bitcoind::{get_available_port, BitcoinD};
use log::{error, warn};
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::time::Duration;
use tonic_lnd::Client;

// re-export bitcoind
pub use bitcoind;
// re-export tonic_lnd
pub use tonic_lnd;

pub use error::Error;
pub use which;

/// Lnd configuration parameters, implements a convenient [Default] for most common use.
///
/// Default values:
/// ```
/// let mut conf = lnd::LndConf::default();
/// conf.view_stdout = false;
/// conf.network = "regtest";
/// conf.tmpdir = None;
/// conf.staticdir = None;
/// conf.minchansize = None;
/// conf.maxchansize = None;
/// assert_eq!(conf, lnd::LndConf::default());
/// ```
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub struct LndConf<'a> {
    /// Lnd command line arguments
    pub args: Vec<&'a str>,

    /// if `true` lnd log output will not be suppressed
    pub view_stdout: bool,

    /// if `true` lnd log error output will not be suppressed
    pub view_stderr: bool,

    /// Must match bitcoind network
    pub network: &'a str,

    /// Optionally specify a temporary or persistent working directory for the lnd.
    /// lnd index files will be stored in this path.
    /// The following two parameters can be configured to simulate desired working directory configuration.
    ///
    /// tmpdir is Some() && staticdir is Some() : Error. Cannot be enabled at same time.
    /// tmpdir is Some(temp_path) && staticdir is None : Create temporary directory at `tmpdir` path.
    /// tmpdir is None && staticdir is Some(work_path) : Create persistent directory at `staticdir` path.
    /// tmpdir is None && staticdir is None: Creates a temporary directory in OS default temporary directory (eg /tmp) or `TEMPDIR_ROOT` env variable path.
    ///
    /// Temporary directory path
    pub tmpdir: Option<PathBuf>,

    /// Persistent directory path
    pub staticdir: Option<PathBuf>,

    /// Try to spawn the process `attempt` time
    ///
    /// The OS is giving available ports to use, however, they aren't booked, so it could rarely
    /// happen they are used at the time the process is spawn. When retrying other available ports
    /// are returned reducing the probability of conflicts to negligible.
    attempts: u8,

    listen_port: u16,

    pub minchansize: Option<u64>,

    pub maxchansize: Option<u64>,
}

impl Default for LndConf<'_> {
    fn default() -> Self {
        // let args = if cfg!(feature = "lnd_0_9_1")
        //     || cfg!(feature = "lnd_0_8_10")
        //     || cfg!(feature = "esplora_a33e97e1")
        //     || cfg!(feature = "legacy")
        // {
        //     vec!["-vvv"]
        // } else {
        //     vec![]
        // };
        //
        let args = vec![];

        LndConf {
            args,
            view_stderr: false,
            view_stdout: false,
            network: "regtest",
            listen_port: 9735,
            tmpdir: None,
            staticdir: None,
            attempts: 3,
            minchansize: None,
            maxchansize: None,
        }
    }
}

/// Struct representing the lnd process with related information
pub struct Lnd {
    /// Process child handle, used to terminate the process when this struct is dropped
    process: Child,
    /// LND client connected to the lnd process
    pub client: Client,
    /// Work directory, where the lnd stores indexes and other stuffs.
    work_dir: DataDir,
    /// Url to connect to the gRPC server
    pub grpc_url: String,
    /// Url to connect to the REST server
    pub rest_url: String,
    /// Url to connect to p2p network
    pub listen_url: Option<String>,
    /// Admin macaroon hex
    pub admin_macaroon: String,
    /// TLS Cert hex
    pub tls_cert: String,
    /// network
    network: String,
}

/// The DataDir struct defining the kind of data directory lnd will use.
/// /// Data directory can be either persistent, or temporary.
pub enum DataDir {
    /// Persistent Data Directory
    Persistent(PathBuf),
    /// Temporary Data Directory
    Temporary(TempDir),
}

impl DataDir {
    /// Return the data directory path
    fn path(&self) -> PathBuf {
        match self {
            Self::Persistent(path) => path.to_owned(),
            Self::Temporary(tmp_dir) => tmp_dir.path().to_path_buf(),
        }
    }
}

impl Lnd {
    /// Create a new lnd process connected with the given bitcoind and default args.
    pub async fn new<S: AsRef<OsStr>>(
        exe: S,
        bitcoind_cookie: String,
        bitcoind_rpc_socket: String,
        #[cfg(feature = "bitcoind")] bitcoind: &BitcoinD,
    ) -> anyhow::Result<Lnd> {
        Lnd::with_conf(
            exe,
            &LndConf::default(),
            bitcoind_cookie,
            bitcoind_rpc_socket,
            #[cfg(feature = "bitcoind")]
            bitcoind,
        )
        .await
    }

    /// Create a new lnd process using given [Conf] connected with the given bitcoind
    #[async_recursion::async_recursion(?Send)]
    pub async fn with_conf<S: AsRef<OsStr>>(
        exe: S,
        conf: &LndConf<'_>,
        bitcoind_cookie: String,
        bitcoind_rpc_socket: String,
        #[cfg(feature = "bitcoind")] bitcoind: &BitcoinD,
    ) -> anyhow::Result<Lnd> {
        #[cfg(feature = "bitcoind")]
        {
            let response = bitcoind.client.call::<Value>("getblockchaininfo", &[])?;
            if response
                .get("initialblockdownload")
                .and_then(|v| v.as_bool())
                .unwrap_or(false)
            {
                // bitcoind will remain in IBD if doesn't see a block from a long time, thus adding a block
                let node_address = bitcoind.client.call::<Value>("getnewaddress", &[])?;
                bitcoind
                    .client
                    .call::<Value>("generatetoaddress", &[1.into(), node_address])
                    .unwrap();
            }
        }

        let mut args = conf.args.clone();

        let work_dir = match (&conf.tmpdir, &conf.staticdir) {
            (Some(_), Some(_)) => return Err(Error::BothDirsSpecified.into()),
            (Some(tmpdir), None) => DataDir::Temporary(TempDir::new_in(tmpdir)?),
            (None, Some(workdir)) => {
                std::fs::create_dir_all(workdir)?;
                DataDir::Persistent(workdir.to_owned())
            }
            (None, None) => match env::var("TEMPDIR_ROOT").map(PathBuf::from) {
                Ok(path) => DataDir::Temporary(TempDir::new_in(path)?),
                Err(_) => DataDir::Temporary(TempDir::new()?),
            },
        };

        let db_dir = format!("--lnddir={}", work_dir.path().display());
        args.push(&db_dir);

        let tls_path = format!("--tlscertpath={}/tls.cert", work_dir.path().display());
        args.push(&tls_path);

        let network = format!("--bitcoin.{}", conf.network);
        args.push(&network);

        args.push("--bitcoin.active");

        args.push("--bitcoin.node=bitcoind");

        let cookie = format!("--bitcoind.rpccookie={}", bitcoind_cookie);
        args.push(&cookie);

        let host = format!("--bitcoind.rpchost={}", bitcoind_rpc_socket);
        args.push(&host);

        let listen_port = get_available_port()?;
        let listen_url = format!("0.0.0.0:{}", listen_port);
        let listen_arg = format!("--listen={}", listen_url);
        args.push(&listen_arg);

        let grpc_port = get_available_port()?;
        let grpc_url = format!("0.0.0.0:{}", grpc_port);
        let grpc_arg = format!("--rpclisten={}", grpc_url);
        args.push(&grpc_arg);

        let rest_port = get_available_port()?;
        let rest_url = format!("0.0.0.0:{}", rest_port);
        let rest_arg = format!("--restlisten={}", rest_url);
        args.push(&rest_arg);

        args.push("--bitcoind.rpcpolling");
        args.push("--bitcoind.blockpollinginterval=1s");

        args.push("--noseedbackup");

        args.push("--accept-keysend");
        args.push("--accept-amp");

        args.push("--protocol.wumbo-channels");
        args.push("--protocol.option-scid-alias");

        args.push("--maxpendingchannels=10");
        args.push("--historicalsyncinterval=1s");
        args.push("--trickledelay=1000");

        let view_stderr = if conf.view_stdout {
            Stdio::inherit()
        } else {
            Stdio::null()
        };

        let view_stdout = if conf.view_stdout {
            Stdio::inherit()
        } else {
            Stdio::null()
        };

        let mut process = Command::new(&exe)
            .args(args)
            .stderr(view_stderr)
            .stdout(view_stdout)
            .spawn()
            .with_context(|| format!("Error while executing {:?}", exe.as_ref()))?;

        let cert_file = work_dir.path().join("tls.cert");
        let macaroon_file = work_dir.path().join(format!(
            "data/chain/bitcoin/{}/admin.macaroon",
            conf.network
        ));

        let client = loop {
            if let Some(status) = process.try_wait()? {
                if conf.attempts > 0 {
                    warn!("early exit with: {:?}. Trying to launch again ({} attempts remaining), maybe some other process used our available port", status, conf.attempts);
                    let mut conf = conf.clone();
                    conf.attempts -= 1;
                    return Self::with_conf(
                        exe,
                        &conf,
                        bitcoind_cookie,
                        bitcoind_rpc_socket,
                        #[cfg(feature = "bitcoind")]
                        bitcoind,
                    )
                    .await
                    .with_context(|| format!("Remaining attempts {}", conf.attempts));
                } else {
                    error!("early exit with: {:?}", status);
                    return Err(Error::EarlyExit(status).into());
                }
            }

            match tonic_lnd::connect(
                format!("https://localhost:{}", grpc_port.clone()),
                &cert_file,
                &macaroon_file,
            )
            .await
            {
                Ok(client) => break client,
                Err(e) => {
                    error!("Error creating client: {}", e);
                    std::thread::sleep(Duration::from_millis(500));
                }
            };
        };

        let cert = std::fs::read(cert_file)?;
        let tls_cert = hex::encode(&cert);

        let mac = std::fs::read(macaroon_file)?;
        let admin_macaroon = hex::encode(&mac);

        // Sleep for 5 seconds because the gRPC server needs to warm up.
        tokio::time::sleep(Duration::from_secs(5)).await;

        Ok(Lnd {
            process,
            client,
            work_dir,
            grpc_url: format!("https://127.0.0.1:{}", grpc_port),
            rest_url: format!("https://127.0.0.1:{}", rest_port),
            listen_url: Some(format!("127.0.0.1:{}", listen_port)),
            admin_macaroon,
            tls_cert,
            network: conf.network.to_string(),
        })
    }

    /// triggers lnd sync by sending the `SIGUSR1` signal, useful to call after a block for example
    pub fn trigger(&self) -> anyhow::Result<()> {
        Ok(nix::sys::signal::kill(
            nix::unistd::Pid::from_raw(self.process.id() as i32),
            nix::sys::signal::SIGUSR1,
        )?)
    }

    /// Return the current workdir path of the running lnd
    pub fn workdir(&self) -> PathBuf {
        self.work_dir.path()
    }

    /// Returns the path to the admin macaroon.
    pub fn admin_macaroon_path(&self) -> PathBuf {
        let mac_path = format!("data/chain/bitcoin/{}/admin.macaroon", self.network);
        self.workdir().join(mac_path)
    }

    /// Returns the path to the tls.cert
    pub fn tls_cert_path(&self) -> PathBuf {
        self.workdir().join("tls.cert")
    }

    /// terminate the lnd process
    pub fn kill(&mut self) -> anyhow::Result<()> {
        match self.work_dir {
            DataDir::Persistent(_) => {
                // Send SIGINT signal to lnd
                nix::sys::signal::kill(
                    nix::unistd::Pid::from_raw(self.process.id() as i32),
                    nix::sys::signal::SIGINT,
                )?;
                // Wait for the process to exit
                match self.process.wait() {
                    Ok(_) => Ok(()),
                    Err(e) => Err(e.into()),
                }
            }
            DataDir::Temporary(_) => Ok(self.process.kill()?),
        }
    }
}

impl Drop for Lnd {
    fn drop(&mut self) {
        let _ = self.kill();
    }
}

/// Provide the lnd executable path if a version feature has been specified
pub fn downloaded_exe_path() -> Option<String> {
    if versions::HAS_FEATURE {
        Some(format!(
            "{}/lnd/{}/lnd",
            env!("OUT_DIR"),
            versions::lnd_name(),
        ))
    } else {
        None
    }
}

/// Returns the daemon `lnd` executable with the following precedence:
///
/// 1) If it's specified in the `LND_EXEC` or in `LND_EXE` env var
/// (errors if both env vars are present)
/// 2) If there is no env var but an auto-download feature such as `lnd_0_9_11` is enabled, returns the
/// path of the downloaded executabled
/// 3) If neither of the precedent are available, the `lnd` executable is searched in the `PATH`
pub fn exe_path() -> anyhow::Result<String> {
    if let (Ok(_), Ok(_)) = (std::env::var("LND_EXEC"), std::env::var("LND_EXE")) {
        return Err(error::Error::BothEnvVars.into());
    }
    if let Ok(path) = std::env::var("LND_EXEC") {
        return Ok(path);
    }
    if let Ok(path) = std::env::var("LND_EXE") {
        return Ok(path);
    }
    if let Some(path) = downloaded_exe_path() {
        return Ok(path);
    }
    which::which("lnd")
        .map_err(|_| Error::NoLndExecutableFound.into())
        .map(|p| p.display().to_string())
}

#[cfg(all(test, feature = "bitcoind"))]
mod test {
    use crate::exe_path;
    use crate::Lnd;
    use bitcoind::bitcoincore_rpc::RpcApi;
    use bitcoind::BitcoinD;
    use log::debug;
    use std::env;
    use tonic_lnd::lnrpc::GetInfoRequest;

    #[test]
    fn test_both_env_vars() {
        env::set_var("LND_EXEC", "placeholder");
        env::set_var("LND_EXE", "placeholder");
        assert!(exe_path().is_err());
        // unsetting because this errors everything in mod test!
        env::remove_var("LND_EXEC");
        env::remove_var("LND_EXE");
    }

    #[tokio::test]
    async fn two_lnd_nodes() {
        let (lnd_exe, _, bitcoind) = setup_nodes().await;

        let cookie = bitcoind.params.cookie_file.to_str().unwrap();
        let rpc_socket = bitcoind.params.rpc_socket.to_string();

        let lnd = Lnd::new(&lnd_exe, cookie.to_string(), rpc_socket, &bitcoind).await;

        assert!(lnd.is_ok());
    }

    #[tokio::test]
    async fn test_with_gen_blocks() {
        let (_, _, bitcoind) = setup_nodes().await;

        let address = bitcoind
            .client
            .get_new_address(None, None)
            .unwrap()
            .assume_checked();

        bitcoind
            .client
            .generate_to_address(100, &address)
            .expect("Blocks not generated to address.");
    }

    #[tokio::test]
    async fn test_kill() {
        let (_, mut lnd, bitcoind) = setup_nodes().await;
        bitcoind.client.ping().unwrap(); // without using bitcoind, it is dropped and all the rest fails.
        let info = lnd.client.lightning().get_info(GetInfoRequest {}).await;
        assert!(info.is_ok());
        lnd.kill().unwrap();
        let info = lnd.client.lightning().get_info(GetInfoRequest {}).await;
        assert!(info.is_err());
    }

    pub(crate) async fn setup_nodes() -> (String, Lnd, BitcoinD) {
        let (bitcoind_exe, lnd_exe) = init();
        debug!("bitcoind: {}", &bitcoind_exe);
        debug!("lnd: {}", &lnd_exe);

        let bitcoin_conf = bitcoind::Conf::default();
        let bitcoind = BitcoinD::with_conf(bitcoind_exe, &bitcoin_conf).unwrap();

        let lnd_conf = super::LndConf::default();
        let cookie = bitcoind.params.cookie_file.to_str().unwrap();
        let rpc_socket = bitcoind.params.rpc_socket.to_string();
        let lnd = Lnd::with_conf(
            &lnd_exe,
            &lnd_conf,
            cookie.to_string(),
            rpc_socket,
            &bitcoind,
        )
        .await
        .unwrap();

        (lnd_exe, lnd, bitcoind)
    }

    #[tokio::test]
    async fn list_unspent() {
        let (_, mut lnd, _) = setup_nodes().await;

        let request = tonic_lnd::walletrpc::ListUnspentRequest::default();

        let unspent = lnd.client.wallet().list_unspent(request).await;
        println!("{:?}", unspent);

        assert!(unspent.is_ok())
    }

    fn init() -> (String, String) {
        let bitcoind_exe_path = bitcoind::exe_path().unwrap();
        let lnd_exe_path = exe_path().unwrap();
        (bitcoind_exe_path, lnd_exe_path)
    }
}