Skip to content

Commit 0366f4c

Browse files
committed
Run tests on regtest
1 parent 840404a commit 0366f4c

File tree

21 files changed

+761
-522
lines changed

21 files changed

+761
-522
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Generate cache key
2323
run: echo "${{ matrix.rust }}" | tee .cache_key
2424
- name: cache
25-
uses: actions/cache@v2
25+
uses: actions/cache@v3
2626
with:
2727
path: |
2828
~/.cargo/registry

.github/workflows/test.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,33 @@ jobs:
2525
run: make cargo-test
2626
- name: Run wasm-pack test
2727
run: make wasm-test
28+
29+
regtest-test:
30+
name: Run regtest tests
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v3
35+
with:
36+
submodules: recursive
37+
- name: Set up Docker
38+
uses: docker/setup-buildx-action@v2
39+
- name: Set up Docker Compose
40+
run: |
41+
sudo apt-get update
42+
sudo apt-get install -y docker-compose
43+
- name: Set default toolchain
44+
run: rustup default nightly
45+
- name: Set profile
46+
run: rustup set profile minimal
47+
- name: Add wasm target
48+
run: rustup target add wasm32-unknown-unknown
49+
- name: Install wasm-pack
50+
run: cargo install wasm-pack
51+
- name: Start regtest environment
52+
working-directory: regtest
53+
run: sh start.sh
54+
- name: Run cargo regtest tests
55+
run: make cargo-regtest-test
56+
- name: Run WASM regtest tests
57+
run: make wasm-regtest-test

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "regtest/boltz"]
2+
path = regtest/boltz
3+
url = https://github.com/BoltzExchange/regtest

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ secp256k1-zkp = { git = "https://github.com/danielgranhao/rust-secp256k1-zkp.git
5757

5858
[dev-dependencies]
5959
futures-util = "0.3.31"
60+
serial_test = "3.2.0"
6061

6162
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dev-dependencies]
6263
bitcoind = { version = "0.36.0", features = ["25_0"] }
@@ -65,9 +66,14 @@ tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
6566

6667
[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies]
6768
wasm-bindgen-test = "0.3.50"
69+
wasm-bindgen = "0.2.100"
70+
gloo-timers = { version = "0.3.0", features = ["futures"] }
71+
futures = "0.3.31"
6872

6973
[features]
7074
default = ["esplora"]
7175
lnurl = ["dep:lnurl-rs"]
7276
esplora = []
7377
electrum = ["dep:electrum-client"]
78+
# Feature to enable tests that require previous initialization of a regtest environment
79+
regtest = []

Makefile

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ ifeq ($(UNAME), Darwin)
44
CLANG_PREFIX += AR=$(shell brew --prefix llvm)/bin/llvm-ar CC=$(shell brew --prefix llvm)/bin/clang
55
endif
66

7+
LND_MACAROON_HEX=$(shell xxd -p regtest/boltz/data/lnd1/data/chain/bitcoin/regtest/admin.macaroon | tr -d '\n')
8+
BITCOIND_COOKIE=$(shell cat regtest/boltz/data/bitcoind/regtest/.cookie)
9+
REGTEST_PREFIX = LND_MACAROON_HEX=$(LND_MACAROON_HEX) BITCOIND_COOKIE=$(BITCOIND_COOKIE)
10+
711
init:
812
cargo install wasm-pack
913

@@ -19,20 +23,36 @@ clippy: cargo-clippy wasm-clippy
1923

2024
test: cargo-test wasm-test
2125

26+
regtest-test: cargo-regtest-test wasm-regtest-test
27+
2228
cargo-clippy:
2329
cargo clippy --all-targets --all-features -- -D warnings
2430

2531
cargo-test:
26-
cargo test -- --nocapture
32+
cargo test --features "esplora, electrum, lnurl" -- --nocapture
33+
34+
cargo-regtest-test:
35+
$(REGTEST_PREFIX) cargo test regtest --features "electrum, regtest" -- --nocapture
2736

2837
wasm-clippy:
2938
$(CLANG_PREFIX) cargo clippy --target=wasm32-unknown-unknown --all-features -- -D warnings
3039

40+
BROWSER ?= firefox
41+
3142
wasm-test:
32-
$(CLANG_PREFIX) wasm-pack test --headless --firefox
43+
$(CLANG_PREFIX) wasm-pack test --headless --$(BROWSER)
3344

3445
wasm-test-chrome:
35-
$(CLANG_PREFIX) wasm-pack test --headless --chrome
46+
BROWSER=chrome $(MAKE) wasm-test
3647

3748
wasm-test-safari:
38-
$(CLANG_PREFIX) wasm-pack test --headless --safari
49+
BROWSER=safari $(MAKE) wasm-test
50+
51+
wasm-regtest-test:
52+
$(CLANG_PREFIX) $(REGTEST_PREFIX) WASM_BINDGEN_TEST_TIMEOUT=500 wasm-pack test --headless --$(BROWSER) --features regtest -- regtest
53+
54+
wasm-regtest-test-chrome:
55+
BROWSER=chrome $(MAKE) wasm-regtest-test
56+
57+
wasm-regtest-test-safari:
58+
BROWSER=safari $(MAKE) wasm-regtest-test

regtest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 0379ce3174dae022b6bd0b266b7286c6b83bffd3

set_regtest_env.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
export LND_MACAROON_HEX=$(xxd -p regtest/boltz/data/lnd1/data/chain/bitcoin/regtest/admin.macaroon | tr -d '\n')
3+
export BITCOIND_COOKIE=$(<regtest/boltz/data/bitcoind/regtest/.cookie)
4+
5+
echo "LND_MACAROON_HEX set to: $LND_MACAROON_HEX"
6+
echo "BITCOIND_COOKIE set to: $BITCOIND_COOKIE"

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub enum Error {
2626
Hash(bitcoin::hashes::FromSliceError),
2727
Locktime(String),
2828
Url(url::ParseError),
29-
WebSocket(tokio_tungstenite_wasm::Error),
29+
WebSocket(Box<tokio_tungstenite_wasm::Error>),
3030
Taproot(String),
3131
Musig2(String),
3232
Generic(String),
@@ -186,7 +186,7 @@ impl From<url::ParseError> for Error {
186186

187187
impl From<tokio_tungstenite_wasm::Error> for Error {
188188
fn from(value: tokio_tungstenite_wasm::Error) -> Self {
189-
Self::WebSocket(value)
189+
Self::WebSocket(value.into())
190190
}
191191
}
192192

src/network/electrum.rs

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ use electrum_client::{ElectrumApi, GetHistoryRes};
77
use elements::encode::{serialize, Decodable};
88
use std::collections::HashMap;
99

10-
pub const DEFAULT_TESTNET_NODE: &str = "electrum.blockstream.info:60002";
1110
pub const DEFAULT_MAINNET_NODE: &str = "wes.bullbitcoin.com:50002";
12-
pub const DEFAULT_LIQUID_TESTNET_NODE: &str = "blockstream.info:465";
11+
pub const DEFAULT_TESTNET_NODE: &str = "electrum.blockstream.info:60002";
12+
pub const DEFAULT_REGTEST_NODE: &str = "localhost:19001";
1313
pub const DEFAULT_LIQUID_MAINNET_NODE: &str = "blockstream.info:995";
14+
pub const DEFAULT_LIQUID_TESTNET_NODE: &str = "blockstream.info:465";
15+
pub const DEFAULT_LIQUID_REGTEST_NODE: &str = "localhost:19002";
16+
1417
pub const DEFAULT_ELECTRUM_TIMEOUT: u8 = 10;
1518

1619
#[derive(Debug, Clone)]
@@ -42,57 +45,50 @@ pub struct ElectrumConfig {
4245
}
4346

4447
impl ElectrumConfig {
45-
pub fn default(chain: Chain, regtest_url: Option<String>) -> Result<Self, Error> {
46-
if (chain == Chain::LiquidRegtest || chain == Chain::BitcoinRegtest)
47-
&& regtest_url.is_none()
48-
{
49-
return Err(Error::Electrum(electrum_client::Error::Message(
50-
"Regtest requires using a custom url".to_string(),
51-
)));
52-
}
48+
pub fn default(chain: Chain, regtest_url: Option<&str>) -> Self {
5349
match chain {
54-
Chain::Bitcoin => Ok(ElectrumConfig::new(
50+
Chain::Bitcoin => ElectrumConfig::new(
5551
Chain::Bitcoin,
5652
DEFAULT_MAINNET_NODE,
5753
true,
5854
true,
5955
DEFAULT_ELECTRUM_TIMEOUT,
60-
)),
61-
Chain::BitcoinTestnet => Ok(ElectrumConfig::new(
56+
),
57+
Chain::BitcoinTestnet => ElectrumConfig::new(
6258
Chain::BitcoinTestnet,
6359
DEFAULT_TESTNET_NODE,
6460
true,
6561
true,
6662
DEFAULT_ELECTRUM_TIMEOUT,
67-
)),
68-
Chain::BitcoinRegtest => Ok(ElectrumConfig::new(
69-
Chain::BitcoinTestnet,
70-
&regtest_url.unwrap(),
71-
true,
72-
true,
63+
),
64+
Chain::BitcoinRegtest => ElectrumConfig::new(
65+
Chain::BitcoinRegtest,
66+
regtest_url.unwrap_or(DEFAULT_REGTEST_NODE),
67+
false,
68+
false,
7369
DEFAULT_ELECTRUM_TIMEOUT,
74-
)),
75-
Chain::Liquid => Ok(ElectrumConfig::new(
70+
),
71+
Chain::Liquid => ElectrumConfig::new(
7672
Chain::Liquid,
7773
DEFAULT_LIQUID_MAINNET_NODE,
7874
true,
7975
true,
8076
DEFAULT_ELECTRUM_TIMEOUT,
81-
)),
82-
Chain::LiquidTestnet => Ok(ElectrumConfig::new(
77+
),
78+
Chain::LiquidTestnet => ElectrumConfig::new(
8379
Chain::LiquidTestnet,
8480
DEFAULT_LIQUID_TESTNET_NODE,
8581
true,
8682
true,
8783
DEFAULT_ELECTRUM_TIMEOUT,
88-
)),
89-
Chain::LiquidRegtest => Ok(ElectrumConfig::new(
90-
Chain::BitcoinTestnet,
91-
&regtest_url.unwrap(),
92-
true,
93-
true,
84+
),
85+
Chain::LiquidRegtest => ElectrumConfig::new(
86+
Chain::LiquidRegtest,
87+
regtest_url.unwrap_or(DEFAULT_LIQUID_REGTEST_NODE),
88+
false,
89+
false,
9490
DEFAULT_ELECTRUM_TIMEOUT,
95-
)),
91+
),
9692
}
9793
}
9894

@@ -307,11 +303,11 @@ mod tests {
307303
#[test]
308304
fn test_electrum_default_clients() {
309305
// let network_config = ElectrumConfig::default(Chain::Bitcoin, None).unwrap();
310-
let network_config = ElectrumConfig::default(Chain::Bitcoin, None).unwrap();
306+
let network_config = ElectrumConfig::default(Chain::Bitcoin, None);
311307
let electrum_client = network_config.build_bitcoin_client().unwrap();
312308
assert!(electrum_client.inner.ping().is_ok());
313309

314-
let network_config = ElectrumConfig::default(Chain::Liquid, None).unwrap();
310+
let network_config = ElectrumConfig::default(Chain::Liquid, None);
315311
let electrum_client = network_config.build_liquid_client().unwrap();
316312
assert!(electrum_client.inner.ping().is_ok());
317313
}
@@ -323,7 +319,6 @@ mod tests {
323319
let network_config = ElectrumConfig::default_bitcoin();
324320

325321
let electrum_client = network_config.build_bitcoin_client().unwrap();
326-
print!("{:?}", electrum_client.inner.block_header(1).unwrap());
327322
assert!(electrum_client.inner.ping().is_ok());
328323

329324
let network_config = ElectrumConfig::default_liquid();
@@ -333,7 +328,7 @@ mod tests {
333328
#[test]
334329
#[ignore]
335330
fn test_raw_electrum_calls() {
336-
let network_config = ElectrumConfig::default(Chain::Liquid, None).unwrap();
331+
let network_config = ElectrumConfig::default(Chain::Liquid, None);
337332
let electrum_client = network_config.build_liquid_client().unwrap();
338333
let numblocks = "blockchain.numblocks.subscribe";
339334
let blockheight = electrum_client.inner.raw_call(numblocks, []).unwrap();

src/network/esplora.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ use std::fmt::format;
1212
use std::str::FromStr;
1313
use std::time::Duration;
1414

15-
pub const DEFAULT_TESTNET_NODE: &str = "https://blockstream.info/testnet/api";
1615
pub const DEFAULT_MAINNET_NODE: &str = "https://blockstream.info/api";
17-
pub const DEFAULT_LIQUID_TESTNET_NODE: &str = "https://blockstream.info/liquidtestnet/api";
16+
pub const DEFAULT_TESTNET_NODE: &str = "https://blockstream.info/testnet/api";
17+
pub const DEFAULT_REGTEST_NODE: &str = "http://localhost:4002/api";
1818
pub const DEFAULT_LIQUID_MAINNET_NODE: &str = "https://blockstream.info/liquid/api";
19+
pub const DEFAULT_LIQUID_TESTNET_NODE: &str = "https://blockstream.info/liquidtestnet/api";
20+
pub const DEFAULT_LIQUID_REGTEST_NODE: &str = "http://localhost:4003/api";
1921

2022
pub const DEFAULT_ESPLORA_TIMEOUT_SECS: u64 = 30;
2123

@@ -34,45 +36,38 @@ impl EsploraConfig {
3436
timeout,
3537
}
3638
}
37-
pub fn default(chain: Chain, regtest_url: Option<String>) -> Result<Self, Error> {
38-
if (chain == Chain::LiquidRegtest || chain == Chain::BitcoinRegtest)
39-
&& regtest_url.is_none()
40-
{
41-
return Err(Error::Esplora(
42-
"Regtest requires using a custom url".to_string(),
43-
));
44-
}
39+
pub fn default(chain: Chain, regtest_url: Option<&str>) -> Self {
4540
match chain {
46-
Chain::Bitcoin => Ok(Self::new(
41+
Chain::Bitcoin => Self::new(
4742
Chain::Bitcoin,
4843
DEFAULT_MAINNET_NODE,
4944
DEFAULT_ESPLORA_TIMEOUT_SECS,
50-
)),
51-
Chain::BitcoinTestnet => Ok(Self::new(
45+
),
46+
Chain::BitcoinTestnet => Self::new(
5247
Chain::BitcoinTestnet,
5348
DEFAULT_TESTNET_NODE,
5449
DEFAULT_ESPLORA_TIMEOUT_SECS,
55-
)),
56-
Chain::BitcoinRegtest => Ok(Self::new(
57-
Chain::BitcoinTestnet,
58-
&regtest_url.unwrap(),
50+
),
51+
Chain::BitcoinRegtest => Self::new(
52+
Chain::BitcoinRegtest,
53+
regtest_url.unwrap_or(DEFAULT_REGTEST_NODE),
5954
DEFAULT_ESPLORA_TIMEOUT_SECS,
60-
)),
61-
Chain::Liquid => Ok(Self::new(
55+
),
56+
Chain::Liquid => Self::new(
6257
Chain::Liquid,
6358
DEFAULT_LIQUID_MAINNET_NODE,
6459
DEFAULT_ESPLORA_TIMEOUT_SECS,
65-
)),
66-
Chain::LiquidTestnet => Ok(Self::new(
60+
),
61+
Chain::LiquidTestnet => Self::new(
6762
Chain::LiquidTestnet,
6863
DEFAULT_LIQUID_TESTNET_NODE,
6964
DEFAULT_ESPLORA_TIMEOUT_SECS,
70-
)),
71-
Chain::LiquidRegtest => Ok(Self::new(
72-
Chain::BitcoinTestnet,
73-
&regtest_url.unwrap(),
65+
),
66+
Chain::LiquidRegtest => Self::new(
67+
Chain::LiquidRegtest,
68+
regtest_url.unwrap_or(DEFAULT_LIQUID_REGTEST_NODE),
7469
DEFAULT_ESPLORA_TIMEOUT_SECS,
75-
)),
70+
),
7671
}
7772
}
7873

@@ -434,7 +429,7 @@ mod tests {
434429

435430
#[macros::async_test_all]
436431
async fn test_esplora_default_clients() {
437-
let network_config = EsploraConfig::default(Chain::Bitcoin, None).unwrap();
432+
let network_config = EsploraConfig::default(Chain::Bitcoin, None);
438433
let esplora_client = network_config.build_bitcoin_client().unwrap();
439434
assert!(esplora_client
440435
.get_address_balance(
@@ -445,7 +440,7 @@ mod tests {
445440
.await
446441
.is_ok());
447442

448-
let network_config = EsploraConfig::default(Chain::Liquid, None).unwrap();
443+
let network_config = EsploraConfig::default(Chain::Liquid, None);
449444
let esplora_client = network_config.build_liquid_client().unwrap();
450445
assert_eq!(
451446
esplora_client.get_genesis_hash().await.unwrap().to_hex(),

0 commit comments

Comments
 (0)