From 8f3ee1c7753620065c6495f4c88622300b036bce Mon Sep 17 00:00:00 2001 From: Orbital Date: Sun, 9 Apr 2023 00:06:07 -0500 Subject: [PATCH] tests: setup lnd for integration tests --- Cargo.lock | 402 ++++++++++++++++++++++++++++++++++--- Cargo.toml | 6 + tests/common/mod.rs | 193 ++++++++++++++++++ tests/integration_tests.rs | 7 + 4 files changed, 581 insertions(+), 27 deletions(-) create mode 100644 tests/common/mod.rs create mode 100644 tests/integration_tests.rs diff --git a/Cargo.lock b/Cargo.lock index cef60edb..8868506e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "1.0.1" @@ -120,9 +126,9 @@ checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" [[package]] name = "base64" -version = "0.21.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" +checksum = "3f1e31e207a6b8fb791a38ea3105e6cb541f55e4d029902d3039a4ad07cc4105" [[package]] name = "bech32" @@ -137,16 +143,64 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0694ea59225b0c5f3cb405ff3f670e4828358ed26aec49dc352f730f0cb1a8a3" dependencies = [ "bech32", - "bitcoin_hashes", - "secp256k1", + "bitcoin_hashes 0.11.0", + "secp256k1 0.24.3", +] + +[[package]] +name = "bitcoin" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b36f4c848f6bd9ff208128f08751135846cc23ae57d66ab10a22efff1c675f3c" +dependencies = [ + "bech32", + "bitcoin-private", + "bitcoin_hashes 0.12.0", + "hex_lit", + "secp256k1 0.27.0", + "serde", ] +[[package]] +name = "bitcoin-private" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57" + [[package]] name = "bitcoin_hashes" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90064b8dee6815a6470d60bad07bbbaee885c0e12d04177138fa3291a01b7bc4" +[[package]] +name = "bitcoin_hashes" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d7066118b13d4b20b23645932dfb3a81ce7e29f95726c2036fa33cd7b092501" +dependencies = [ + "bitcoin-private", + "serde", +] + +[[package]] +name = "bitcoind" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e4a98e66bb7c1f81abf4f8d992e1839fc0f9cbc627effbff122aae7d5e493a0" +dependencies = [ + "anyhow", + "bitcoin_hashes 0.12.0", + "core-rpc", + "flate2", + "log", + "minreq", + "tar", + "tempfile", + "which", + "zip", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -155,9 +209,15 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" -version = "3.12.2" +version = "3.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" + +[[package]] +name = "byteorder" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c6ed94e98ecff0c12dd1b04c15ec0d7d9458ca8fe806cea6f12954efe74c63b" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" @@ -165,6 +225,27 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +[[package]] +name = "bzip2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdb116a6ef3f6c3698828873ad02c3014b3c85cadb88496095628e3ef1e347f8" +dependencies = [ + "bzip2-sys", + "libc", +] + +[[package]] +name = "bzip2-sys" +version = "0.1.11+1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "736a955f3fa7875102d57c82b8cac37ec45224a07fd32d58f9f7a186b6cd4cdc" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "cargo_toml" version = "0.12.4" @@ -226,6 +307,41 @@ dependencies = [ "void", ] +[[package]] +name = "core-rpc" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d77079e1b71c2778d6e1daf191adadcd4ff5ec3ccad8298a79061d865b235b" +dependencies = [ + "bitcoin-private", + "core-rpc-json", + "jsonrpc", + "log", + "serde", + "serde_json", +] + +[[package]] +name = "core-rpc-json" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581898ed9a83f31c64731b1d8ca2dfffcfec14edf1635afacd5234cddbde3a41" +dependencies = [ + "bitcoin 0.30.0", + "bitcoin-private", + "serde", + "serde_json", +] + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "difflib" version = "0.4.0" @@ -274,12 +390,34 @@ dependencies = [ "instant", ] +[[package]] +name = "filetime" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cbc844cecaee9d4443931972e1289c8ff485cb4cc2767cb03ca139ed6885153" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.2.16", + "windows-sys 0.48.0", +] + [[package]] name = "fixedbitset" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37ab347416e802de484e4d03c7316c48f1ecb56574dfd4a46a80f173ce1de04d" +[[package]] +name = "flate2" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b9429470923de8e8cbd4d2dc513535400b4b3fef0319fb5c4e1f520a7bef743" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "float-cmp" version = "0.9.0" @@ -404,7 +542,7 @@ checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -471,6 +609,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex_lit" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" + [[package]] name = "http" version = "0.2.9" @@ -595,6 +739,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "jsonrpc" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd8d6b3f301ba426b30feca834a2a18d48d5b54e5065496b5c1b05537bee3639" +dependencies = [ + "base64 0.13.1", + "serde", + "serde_json", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -613,32 +768,38 @@ version = "0.0.114" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "800ec68a160529ba3ca12c5db629867c4a8de2df272792c1246602966a5b789b" dependencies = [ - "bitcoin", + "bitcoin 0.29.2", ] [[package]] name = "linux-raw-sys" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "lndk" version = "0.0.1" dependencies = [ "async-trait", - "bitcoin", + "bitcoin 0.29.2", + "bitcoind", "bytes", "configure_me", "configure_me_codegen", + "core-rpc", + "flate2", "futures", "hex", "lightning", "log", + "minreq", "mockall", "rand_chacha", "rand_core", "simple_logger", + "tar", + "tempfile", "tokio", "tonic 0.8.3", "tonic_lnd", @@ -680,6 +841,28 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + +[[package]] +name = "minreq" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3de406eeb24aba36ed3829532fa01649129677186b44a49debec0ec574ca7da7" +dependencies = [ + "log", + "once_cell", + "rustls 0.20.8", + "webpki 0.22.0", + "webpki-roots", +] + [[package]] name = "mio" version = "0.8.6" @@ -688,7 +871,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.45.0", ] @@ -819,6 +1002,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1000,6 +1189,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "redox_syscall" version = "0.3.5" @@ -1011,9 +1209,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.8.1" +version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" +checksum = "d1a59b5d8e97dee33696bf13c5ba8ab85341c002922fba050069326b9c498974" dependencies = [ "aho-corasick", "memchr", @@ -1022,9 +1220,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" +checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" [[package]] name = "ring" @@ -1070,8 +1268,20 @@ dependencies = [ "base64 0.13.1", "log", "ring", - "sct", - "webpki", + "sct 0.6.1", + "webpki 0.21.4", +] + +[[package]] +name = "rustls" +version = "0.20.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f" +dependencies = [ + "log", + "ring", + "sct 0.7.0", + "webpki 0.22.0", ] [[package]] @@ -1080,7 +1290,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d194b56d58803a43635bdc398cd17e383d6f71f9182b9a192c127ca42494a59b" dependencies = [ - "base64 0.21.0", + "base64 0.21.1", ] [[package]] @@ -1089,6 +1299,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06" +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + [[package]] name = "sct" version = "0.6.1" @@ -1099,15 +1315,37 @@ dependencies = [ "untrusted", ] +[[package]] +name = "sct" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "secp256k1" version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b1629c9c557ef9b293568b338dddfc8208c98a18c59d722a9d53f859d9c9b62" dependencies = [ - "bitcoin_hashes", + "bitcoin_hashes 0.11.0", + "rand", + "secp256k1-sys 0.6.1", +] + +[[package]] +name = "secp256k1" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" +dependencies = [ + "bitcoin_hashes 0.12.0", "rand", - "secp256k1-sys", + "secp256k1-sys 0.8.1", + "serde", ] [[package]] @@ -1119,6 +1357,15 @@ dependencies = [ "cc", ] +[[package]] +name = "secp256k1-sys" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" +dependencies = [ + "cc", +] + [[package]] name = "serde" version = "1.0.163" @@ -1139,6 +1386,17 @@ dependencies = [ "syn 2.0.16", ] +[[package]] +name = "serde_json" +version = "1.0.96" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "simple_logger" version = "4.1.0" @@ -1148,7 +1406,7 @@ dependencies = [ "atty", "colored", "log", - "time", + "time 0.3.21", "windows-sys 0.42.0", ] @@ -1205,6 +1463,17 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "tar" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b55807c0344e1e6c04d7c965f5289c39a8d94ae23ed5c0b57aabac549f871c6" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "tempfile" version = "3.5.0" @@ -1213,7 +1482,7 @@ checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" dependencies = [ "cfg-if", "fastrand", - "redox_syscall", + "redox_syscall 0.3.5", "rustix", "windows-sys 0.45.0", ] @@ -1224,6 +1493,37 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +[[package]] +name = "thiserror" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.16", +] + +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.21" @@ -1297,9 +1597,9 @@ version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc6844de72e57df1980054b38be3a9f4702aba4858be64dd700181a8a6d0e1b6" dependencies = [ - "rustls", + "rustls 0.19.1", "tokio", - "webpki", + "webpki 0.21.4", ] [[package]] @@ -1434,12 +1734,12 @@ checksum = "a207832efa21cc12bd0d520ce36554af91f5bbcc8873273bc1bab238b3365dbb" dependencies = [ "hex", "prost 0.9.0", - "rustls", + "rustls 0.19.1", "rustls-pemfile", "tokio", "tonic 0.6.2", "tonic-build", - "webpki", + "webpki 0.21.4", ] [[package]] @@ -1557,6 +1857,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -1637,6 +1943,25 @@ dependencies = [ "untrusted", ] +[[package]] +name = "webpki" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd" +dependencies = [ + "ring", + "untrusted", +] + +[[package]] +name = "webpki-roots" +version = "0.22.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87" +dependencies = [ + "webpki 0.22.0", +] + [[package]] name = "which" version = "4.4.0" @@ -1816,3 +2141,26 @@ name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + +[[package]] +name = "xattr" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d1526bbe5aaeb5eb06885f4d987bcdfa5e23187055de9b83fe00156a821fabc" +dependencies = [ + "libc", +] + +[[package]] +name = "zip" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93ab48844d61251bb3835145c521d88aa4031d7139e8485990f60ca911fa0815" +dependencies = [ + "byteorder", + "bzip2", + "crc32fast", + "flate2", + "thiserror", + "time 0.1.45", +] diff --git a/Cargo.toml b/Cargo.toml index 3d137636..ef816aef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,13 @@ configure_me = "0.4.0" bytes = "1.4.0" [dev-dependencies] +bitcoincore-rpc = { package="core-rpc", version = "0.17.0" } +bitcoind = { version = "0.30.0", features = [ "22_0" ] } +flate2 = "1.0.25" +minreq = { version = "2.7.0", features = [ "https" ] } mockall = "0.11.3" +tar = "0.4.38" +tempfile = "3.5.0" [build-dependencies] configure_me_codegen = "0.4.4" diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 00000000..3ab9c8ec --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,193 @@ +use bitcoincore_rpc::{bitcoin::Network, json, RpcApi}; +use bitcoind::{BitcoinD, Conf, ConnectParams}; +use flate2::read::GzDecoder; +use std::env; +use std::process::{Child, Command, Stdio}; +use std::thread; +use tar::Archive; +use tempfile::{tempdir, Builder, TempDir}; +use tokio::time::Duration; +use tonic_lnd::Client; + +const LND_VERSION: &str = "0.16.2"; + +// setup_test_infrastructure spins up all of the infrastructure we need to test LNDK, including a bitcoind node and a +// LND node. LNDK can then use this test environment to run. +pub async fn setup_test_infrastructure() -> (BitcoinD, LndNode, TempDir) { + let (bitcoind, bitcoind_dir) = setup_bitcoind().await; + let lnd_exe_dir = download_lnd().await; + + let mut lnd_node = LndNode::new(bitcoind.params.clone(), lnd_exe_dir); + lnd_node.setup_client().await; + + return (bitcoind, lnd_node, bitcoind_dir); +} + +pub async fn setup_bitcoind() -> (BitcoinD, TempDir) { + let bitcoind_dir = tempdir().unwrap(); + let bitcoind_dir_path = bitcoind_dir.path().clone().to_path_buf(); + let mut conf = Conf::default(); + conf.tmpdir = Some(bitcoind_dir_path); + conf.args = vec![ + "-regtest", + "-zmqpubrawblock=tcp://127.0.0.1:28332", + "-zmqpubrawtx=tcp://127.0.0.1:28333", + ]; + let bitcoind = BitcoinD::from_downloaded_with_conf(&conf).unwrap(); + + // Mine 101 blocks in our little regtest network so that the funds are spendable. + // (See https://bitcoin.stackexchange.com/questions/1991/what-is-the-block-maturation-time) + let address = bitcoind + .client + .get_new_address(None, Some(json::AddressType::Bech32)) + .unwrap(); + let address = address.require_network(Network::Regtest).unwrap(); + bitcoind.client.generate_to_address(101, &address).unwrap(); + + (bitcoind, bitcoind_dir) +} + +#[cfg(all(target_os = "linux", target_arch = "x86_64"))] +fn lnd_download_filename() -> String { + format!("lnd-linux-amd64-v{}-beta.tar.gz", &LND_VERSION) +} + +#[cfg(all(target_os = "macos", target_arch = "x86_64"))] +fn lnd_download_filename() -> String { + format!("lnd-darwin-arm64-v{}-beta.tar.gz", &LND_VERSION) +} + +#[cfg(all(target_os = "windows"))] +fn lnd_download_filename() -> String { + panic!("Running integration tests on Windows os is not currently supported"); +} + +// download_lnd downloads the lnd binary to tmp if it doesn't exist yet. Currently it only downloads binaries +// compatible with the linux os. +pub async fn download_lnd() -> TempDir { + let temp_dir = tempdir().unwrap(); + let lnd_dir = &temp_dir.path().to_path_buf(); + let lnd_releases_endpoint = + "https://github.com/lightningnetwork/lnd/releases/download/v0.16.2-beta"; + let lnd_download_endpoint = format!("{lnd_releases_endpoint}/{}", lnd_download_filename()); + + let resp = minreq::get(&lnd_download_endpoint).send().unwrap(); + assert_eq!( + resp.status_code, 200, + "url {lnd_download_endpoint} didn't return 200" + ); + let content = resp.as_bytes(); + + let decoder = GzDecoder::new(&content[..]); + let mut archive = Archive::new(decoder); + archive.unpack(lnd_dir).expect("unable to unpack lnd files"); + + temp_dir +} + +// LndNode holds the tools we need to interact with a Lightning node. +pub struct LndNode { + address: String, + _lnd_exe_dir: TempDir, + _lnd_dir_tmp: TempDir, + cert_path: String, + macaroon_path: String, + _handle: Child, + client: Option, +} + +impl LndNode { + fn new(bitcoind_connect_params: ConnectParams, lnd_exe_dir: TempDir) -> LndNode { + env::set_current_dir(lnd_exe_dir.path().join("lnd-linux-amd64-v0.16.2-beta")) + .expect("couldn't set current directory"); + + let lnd_dir_binding = Builder::new().prefix("lnd").tempdir().unwrap(); + let lnd_dir = lnd_dir_binding.path(); + + let cert_path = lnd_dir.join("tls.cert").to_str().unwrap().to_string(); + let macaroon_path = lnd_dir + .join("data/chain/bitcoin/regtest/admin.macaroon") + .to_str() + .unwrap() + .to_string(); + + // Have node run on a randomly assigned grpc port. That way, if we run more than one lnd node, they won't + // clash. + let port = bitcoind::get_available_port().unwrap(); + let rpc_addr = format!("localhost:{}", port); + let args = [ + format!("--rpclisten={}", rpc_addr), + format!("--norest"), + // With this flag, we don't have to unlock the wallet on startup. + format!("--noseedbackup"), + format!("--bitcoin.active"), + format!("--bitcoin.node=bitcoind"), + format!("--bitcoin.regtest"), + format!("--lnddir={}", lnd_dir.display()), + format!( + "--bitcoind.rpccookie={}", + bitcoind_connect_params.cookie_file.display() + ), + format!("--bitcoind.zmqpubrawblock=tcp://127.0.0.1:28332"), + format!("--bitcoind.zmqpubrawtx=tcp://127.0.0.1:28333"), + format!( + "--bitcoind.rpchost={:?}", + bitcoind_connect_params.rpc_socket + ), + ]; + + let cmd = Command::new("./lnd") + .args(args) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("Failed to execute lnd command"); + + LndNode { + address: format!("https://{}", rpc_addr), + _lnd_exe_dir: lnd_exe_dir, + _lnd_dir_tmp: lnd_dir_binding, + cert_path: cert_path, + macaroon_path: macaroon_path, + _handle: cmd, + client: None, + } + } + + // Setup the client we need to interact with the LND node. + async fn setup_client(&mut self) { + // We need to give lnd some time to start up before we'll be able to interact with it via the client. + let mut retry = false; + let mut retry_num = 0; + while retry_num == 0 || retry { + thread::sleep(Duration::from_secs(2)); + + let client_result = tonic_lnd::connect( + self.address.clone(), + self.cert_path.clone(), + self.macaroon_path.clone(), + ) + .await; + + match client_result { + Ok(client) => { + self.client = Some(client); + + retry = false; + retry_num += 1; + } + Err(err) => { + println!( + "getting client error {err}, retrying call {} time", + retry_num + ); + if retry_num == 3 { + panic!("could not set up client: {err}") + } + retry = true; + retry_num += 1; + } + } + } + } +} diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs new file mode 100644 index 00000000..9be08667 --- /dev/null +++ b/tests/integration_tests.rs @@ -0,0 +1,7 @@ +mod common; + +#[tokio::test] +async fn test_setup() { + // Spin up a bitcoind and lnd node, which are required for our tests. + let (_bitcoind, _lnd, _bitcoind_dir) = common::setup_test_infrastructure().await; +}