spawn-lnd 0.2.0

Docker-backed Bitcoin Core and LND regtest clusters for Rust integration tests
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
use std::collections::{HashMap, HashSet};

use lnd_grpc_rust::{
    LndClient, LndNodeClients,
    lnrpc::{
        ChannelGraphRequest, Invoice, Payment, PaymentFailureReason, PaymentHash,
        invoice::InvoiceState, payment::PaymentStatus,
    },
    routerrpc::SendPaymentRequest,
};
use spawn_lnd::{DEFAULT_CHANNEL_CAPACITY_SAT, SpawnLnd};
use tokio::time::{Duration, sleep};

type TestResult<T> = Result<T, Box<dyn std::error::Error>>;

#[derive(Debug)]
struct LightningPaymentReport {
    from_alias: String,
    to_alias: String,
    amount_sat: i64,
    payment_hash: String,
    payment_preimage: String,
    settled_amount_sat: i64,
}

#[tokio::test]
async fn e2e_smoke_spawns_two_bitcoinds_and_opens_channel_ring() -> TestResult<()> {
    if std::env::var("RUN_DOCKER_TESTS").as_deref() != Ok("1") {
        eprintln!("skipping e2e smoke test; set RUN_DOCKER_TESTS=1 to run it");
        return Ok(());
    }

    let aliases = ["alice", "bob", "carol", "dave"];
    let ring = [
        ("alice", "bob"),
        ("bob", "carol"),
        ("carol", "dave"),
        ("dave", "alice"),
    ];
    eprintln!("e2e smoke: spawning cluster aliases={aliases:?}");
    let mut cluster = SpawnLnd::builder().nodes(aliases).spawn().await?;
    eprintln!(
        "e2e smoke: cluster_id={} bitcoinds={} aliases={:?}",
        cluster.cluster_id(),
        cluster.bitcoinds().len(),
        cluster.node_aliases().collect::<Vec<_>>()
    );

    assert_eq!(
        cluster.bitcoinds().len(),
        2,
        "four LND nodes should be split across two bitcoinds"
    );
    assert_eq!(cluster.node_aliases().collect::<Vec<_>>(), aliases);
    assert_eq!(cluster.node("alice").expect("alice").chain_group_index(), 0);
    assert_eq!(cluster.node("bob").expect("bob").chain_group_index(), 0);
    assert_eq!(cluster.node("carol").expect("carol").chain_group_index(), 0);
    assert_eq!(cluster.node("dave").expect("dave").chain_group_index(), 1);

    let result = async {
        eprintln!("e2e smoke: batch funding aliases={aliases:?}");
        let fundings = cluster.fund_nodes(aliases).await?;
        for funding in &fundings {
            eprintln!(
                "e2e smoke: funded {} txid={} address={} spendable_utxo_total_sat={}",
                funding.alias, funding.txid, funding.address, funding.spendable_utxo_total_sat
            );
        }

        let mut channels = Vec::new();
        for (from, to) in ring {
            eprintln!(
                "e2e smoke: opening channel {from} -> {to} capacity_sat={DEFAULT_CHANNEL_CAPACITY_SAT}"
            );
            let channel = cluster.open_channel(from, to).await?;
            eprintln!(
                "e2e smoke: channel {} -> {} point={} active_from={} active_to={} blocks={:?}",
                from,
                to,
                channel.channel_point,
                channel.from_channel.active,
                channel.to_channel.active,
                channel.confirmation_blocks
            );
            channels.push(channel);
        }

        eprintln!("e2e smoke: connecting raw gRPC clients");
        let mut clients = cluster.connect_nodes().await?;
        let mut infos = Vec::new();
        for alias in aliases {
            eprintln!("e2e smoke: querying GetInfo for {alias}");
            let info = clients
                .get_mut(alias)
                .expect("client")
                .lightning()
                .get_info(lnd_grpc_rust::lnrpc::GetInfoRequest {})
                .await?
                .into_inner();
            eprintln!(
                "e2e smoke: {alias} pubkey={} synced_to_chain={} block_height={} active_channels={}",
                info.identity_pubkey,
                info.synced_to_chain,
                info.block_height,
                info.num_active_channels
            );

            infos.push((
                alias.to_string(),
                info.identity_pubkey,
                info.synced_to_chain,
                info.num_active_channels,
            ));
        }

        let public_keys: HashMap<String, String> = infos
            .iter()
            .map(|(alias, public_key, _, _)| (alias.clone(), public_key.clone()))
            .collect();
        eprintln!("e2e smoke: waiting for public graph gossip for ring edges");
        wait_for_public_graph_edges(&mut clients, &public_keys, &ring).await?;
        eprintln!("e2e smoke: public graph has all ring edges");

        let payments = vec![
            pay_invoice(&mut clients, "alice", "carol", 1_000).await?,
            pay_invoice(&mut clients, "carol", "alice", 2_000).await?,
        ];

        let mut chain_tips = Vec::new();
        for bitcoind in cluster.bitcoinds() {
            let info = bitcoind.rpc.get_blockchain_info().await?;
            chain_tips.push((info.blocks, info.bestblockhash));
        }
        eprintln!("e2e smoke: chain tips: {chain_tips:?}");

        Ok::<_, Box<dyn std::error::Error>>((fundings, channels, infos, payments, chain_tips))
    }
    .await;
    eprintln!(
        "e2e smoke: shutting down cluster_id={}",
        cluster.cluster_id()
    );
    let cleanup = cluster.shutdown().await;

    let (fundings, channels, infos, payments, chain_tips) = result?;
    let cleanup = cleanup?;
    eprintln!(
        "e2e smoke: cleanup matched={} removed={} failures={}",
        cleanup.matched,
        cleanup.removed,
        cleanup.failures.len()
    );
    assert!(
        cleanup.removed >= 7,
        "expected two bitcoinds, four LND containers, and network to be removed"
    );

    assert_eq!(fundings.len(), aliases.len());
    let funding_txid = fundings[0].txid.clone();
    for funding in fundings {
        assert!(aliases.contains(&funding.alias.as_str()));
        assert_eq!(funding.txid, funding_txid);
        assert!(funding.spendable_utxo_total_sat >= DEFAULT_CHANNEL_CAPACITY_SAT);
    }

    assert_eq!(channels.len(), ring.len());
    for (channel, (from, to)) in channels.iter().zip(ring) {
        assert_eq!(channel.from_alias, from);
        assert_eq!(channel.to_alias, to);
        assert_eq!(
            channel.local_funding_amount_sat,
            DEFAULT_CHANNEL_CAPACITY_SAT
        );
        assert!(!channel.channel_point.is_empty());
        assert!(channel.from_channel.active);
        assert!(channel.to_channel.active);
        assert_eq!(channel.from_channel.capacity, DEFAULT_CHANNEL_CAPACITY_SAT);
        assert_eq!(channel.to_channel.capacity, DEFAULT_CHANNEL_CAPACITY_SAT);
    }

    assert_eq!(payments.len(), 2);
    assert_eq!(payments[0].from_alias, "alice");
    assert_eq!(payments[0].to_alias, "carol");
    assert_eq!(payments[0].amount_sat, 1_000);
    assert_eq!(payments[0].settled_amount_sat, 1_000);
    assert_eq!(payments[1].from_alias, "carol");
    assert_eq!(payments[1].to_alias, "alice");
    assert_eq!(payments[1].amount_sat, 2_000);
    assert_eq!(payments[1].settled_amount_sat, 2_000);
    for payment in payments {
        assert!(!payment.payment_hash.is_empty());
        assert!(!payment.payment_preimage.is_empty());
    }

    for (alias, public_key, synced_to_chain, active_channels) in infos {
        assert!(!public_key.is_empty());
        assert!(synced_to_chain, "{alias} must be synced");
        assert_eq!(
            active_channels, 2,
            "{alias} should have two active ring channels"
        );
    }

    assert_eq!(chain_tips.len(), 2);
    assert!(
        chain_tips
            .iter()
            .all(|tip| tip.0 == chain_tips[0].0 && tip.1 == chain_tips[0].1),
        "bitcoinds should end on the same tip: {chain_tips:?}"
    );
    assert!(
        chain_tips[0].0 <= 200,
        "batched funding should keep e2e block height low; tips: {chain_tips:?}"
    );

    Ok(())
}

async fn wait_for_public_graph_edges(
    clients: &mut LndNodeClients,
    public_keys: &HashMap<String, String>,
    expected_alias_edges: &[(&str, &str)],
) -> TestResult<()> {
    let mut expected_edges = HashSet::new();
    for (from_alias, to_alias) in expected_alias_edges {
        let from_key = public_keys
            .get(*from_alias)
            .ok_or_else(|| test_error(format!("missing public key for {from_alias}")))?;
        let to_key = public_keys
            .get(*to_alias)
            .ok_or_else(|| test_error(format!("missing public key for {to_alias}")))?;
        expected_edges.insert(edge_key(from_key, to_key));
    }

    let mut last_missing = Vec::new();
    for attempt in 1..=120 {
        let mut synced = true;
        last_missing.clear();

        for alias in public_keys.keys() {
            let client = clients
                .get_mut(alias)
                .ok_or_else(|| test_error(format!("missing client {alias}")))?;
            let graph = client
                .lightning()
                .describe_graph(ChannelGraphRequest {
                    include_unannounced: false,
                    include_auth_proof: false,
                })
                .await?
                .into_inner();
            let visible_edges: HashSet<_> = graph
                .edges
                .iter()
                .filter(|edge| edge.node1_policy.is_some() && edge.node2_policy.is_some())
                .map(|edge| edge_key(&edge.node1_pub, &edge.node2_pub))
                .collect();
            let missing: Vec<_> = expected_edges
                .difference(&visible_edges)
                .map(|(node_a, node_b)| format!("{node_a}<->{node_b}"))
                .collect();

            if !missing.is_empty() {
                synced = false;
                last_missing.push(format!("{alias}: {}", missing.join(", ")));
            }
        }

        if synced {
            eprintln!("e2e smoke: graph gossip synced after attempt {attempt}");
            return Ok(());
        }

        if attempt == 1 || attempt % 10 == 0 {
            eprintln!(
                "e2e smoke: waiting for graph gossip attempt={attempt} missing={}",
                last_missing.join("; ")
            );
        }
        sleep(Duration::from_millis(500)).await;
    }

    Err(test_error(format!(
        "public channel graph did not sync; missing edges: {}",
        last_missing.join("; ")
    )))
}

fn edge_key(node_a: &str, node_b: &str) -> (String, String) {
    if node_a <= node_b {
        (node_a.to_string(), node_b.to_string())
    } else {
        (node_b.to_string(), node_a.to_string())
    }
}

async fn pay_invoice(
    clients: &mut LndNodeClients,
    from_alias: &str,
    to_alias: &str,
    amount_sat: i64,
) -> TestResult<LightningPaymentReport> {
    eprintln!("e2e smoke: creating invoice {to_alias} amount_sat={amount_sat}");
    let invoice = {
        let recipient = clients
            .get_mut(to_alias)
            .ok_or_else(|| test_error(format!("missing recipient client {to_alias}")))?;

        recipient
            .lightning()
            .add_invoice(Invoice {
                memo: format!("spawn-lnd e2e {from_alias} to {to_alias}"),
                value: amount_sat,
                ..Default::default()
            })
            .await?
            .into_inner()
    };

    eprintln!(
        "e2e smoke: invoice created {to_alias} payment_hash={}",
        hex::encode(&invoice.r_hash)
    );
    assert!(
        !invoice.payment_request.is_empty(),
        "recipient should return a BOLT11 invoice"
    );

    eprintln!("e2e smoke: paying invoice {from_alias} -> {to_alias} amount_sat={amount_sat}");
    let response = {
        let payer = clients
            .get_mut(from_alias)
            .ok_or_else(|| test_error(format!("missing payer client {from_alias}")))?;
        send_payment_with_retry(payer, invoice.payment_request.clone()).await?
    };

    assert_eq!(
        response.payment_hash,
        hex::encode(&invoice.r_hash),
        "payment response hash should match the invoice hash"
    );
    assert!(
        !response.payment_preimage.is_empty(),
        "settled payment should return a preimage"
    );
    eprintln!(
        "e2e smoke: payment settled {from_alias} -> {to_alias} hash={} preimage={}",
        response.payment_hash, response.payment_preimage
    );

    let settled_invoice = {
        let recipient = clients
            .get_mut(to_alias)
            .ok_or_else(|| test_error(format!("missing recipient client {to_alias}")))?;

        recipient
            .lightning()
            .lookup_invoice(PaymentHash {
                r_hash: invoice.r_hash.clone(),
                ..Default::default()
            })
            .await?
            .into_inner()
    };

    assert_eq!(
        settled_invoice.state,
        InvoiceState::Settled as i32,
        "recipient invoice should be settled"
    );
    assert_eq!(settled_invoice.amt_paid_sat, amount_sat);
    eprintln!(
        "e2e smoke: invoice settled {to_alias} amt_paid_sat={}",
        settled_invoice.amt_paid_sat
    );

    Ok(LightningPaymentReport {
        from_alias: from_alias.to_string(),
        to_alias: to_alias.to_string(),
        amount_sat,
        payment_hash: response.payment_hash,
        payment_preimage: response.payment_preimage,
        settled_amount_sat: settled_invoice.amt_paid_sat,
    })
}

async fn send_payment_with_retry(
    payer: &mut LndClient,
    payment_request: String,
) -> TestResult<Payment> {
    let mut last_error = String::new();

    for attempt in 1..=6 {
        eprintln!("e2e smoke: SendPaymentV2 attempt={attempt}");
        let response = payer
            .router()
            .send_payment_v2(SendPaymentRequest {
                payment_request: payment_request.clone(),
                fee_limit_sat: 1_000,
                timeout_seconds: 10,
                no_inflight_updates: true,
                ..Default::default()
            })
            .await;

        match response {
            Ok(response) => {
                let mut stream = response.into_inner();
                loop {
                    match stream.message().await {
                        Ok(Some(payment)) => match payment.status {
                            status if status == PaymentStatus::Succeeded as i32 => {
                                eprintln!(
                                    "e2e smoke: SendPaymentV2 succeeded attempt={attempt} hash={}",
                                    payment.payment_hash
                                );
                                return Ok(payment);
                            }
                            status if status == PaymentStatus::Failed as i32 => {
                                last_error = format_payment_failure(&payment);
                                eprintln!(
                                    "e2e smoke: SendPaymentV2 failed attempt={attempt}: {last_error}"
                                );
                                break;
                            }
                            status => {
                                eprintln!(
                                    "e2e smoke: SendPaymentV2 update attempt={attempt} status={status}"
                                );
                            }
                        },
                        Ok(None) => {
                            last_error =
                                "payment stream ended before a terminal status".to_string();
                            eprintln!(
                                "e2e smoke: SendPaymentV2 stream ended attempt={attempt}: {last_error}"
                            );
                            break;
                        }
                        Err(err) => {
                            last_error = err.to_string();
                            eprintln!(
                                "e2e smoke: SendPaymentV2 stream error attempt={attempt}: {last_error}"
                            );
                            break;
                        }
                    }
                }
            }
            Err(err) => {
                last_error = err.to_string();
                eprintln!("e2e smoke: SendPaymentV2 RPC error attempt={attempt}: {last_error}");
            }
        }

        sleep(Duration::from_millis(500)).await;
    }

    Err(test_error(format!(
        "payment did not succeed after retries: {last_error}"
    )))
}

fn format_payment_failure(payment: &Payment) -> String {
    let reason = PaymentFailureReason::try_from(payment.failure_reason)
        .map(|reason| reason.as_str_name())
        .unwrap_or("UNKNOWN_FAILURE_REASON");

    format!("payment failed: {reason}")
}

fn test_error(message: impl Into<String>) -> Box<dyn std::error::Error> {
    Box::new(std::io::Error::other(message.into()))
}