dig-peer 0.10.0

The DIG Network peer client: DigPeer::connect(peer, tls) drives dig-nat's full direct→relay traversal ladder, exposes typed RPC over dig-rpc-protocol, seals directed calls end-to-end to the peer's captured BLS-G1 identity (§5.4) on top of mTLS, and disconnects cleanly. The client mirror of dig-rpc's server; distinct from ChiaPeer.
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! End-to-end loopback tests: a real [`DigPeer`] client connects to a real dig-tls mTLS server over
//! `dig-nat`'s Direct tier on `127.0.0.1`, and an RPC round-trips both unsealed (public-read) and
//! sealed (directed §5.4). These exercise the whole stack dig-peer owns — connect + peer_id pin +
//! mux stream + the RPC framing + the seal — against genuine mutual TLS, not a mock.

use std::sync::Arc;

use chia_protocol::Bytes32;
use chia_traits::Streamable as _;
use dig_message::envelope::{DigMessageEnvelope, InteractionShape};
use dig_message::{open_message, seal_message, ReplayGuard, SealParams};
use dig_nat::{BindingPolicy, PeerSession, PeerTarget, RangeFrame};
use dig_peer::{DigPeer, NodeCert, SealingIdentity};
use dig_rpc_protocol::envelope::{JsonRpcRequest, JsonRpcResponse};
use dig_rpc_protocol::types::{
    FetchModuleRangeParams, GetModuleInfoParams, Health, ModuleInfo, NetworkInfo, RelayStatus,
};
use dig_tls::bls::SecretKey;
use dig_tls::PeerId;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio_rustls::TlsAcceptor;

/// A deterministic BLS identity key from a label (test-only).
fn identity_key(label: &str) -> SecretKey {
    let mut seed = [0u8; 32];
    let bytes = label.as_bytes();
    seed[..bytes.len().min(32)].copy_from_slice(&bytes[..bytes.len().min(32)]);
    SecretKey::from_seed(&seed)
}

/// Read one `u32`-big-endian length-prefixed body from a stream.
async fn read_framed<R: AsyncReadExt + Unpin>(r: &mut R) -> std::io::Result<Vec<u8>> {
    let mut len = [0u8; 4];
    r.read_exact(&mut len).await?;
    let n = u32::from_be_bytes(len) as usize;
    let mut body = vec![0u8; n];
    r.read_exact(&mut body).await?;
    Ok(body)
}

/// Write one `u32`-big-endian length-prefixed body to a stream.
async fn write_framed<W: AsyncWriteExt + Unpin>(w: &mut W, body: &[u8]) -> std::io::Result<()> {
    w.write_all(&(body.len() as u32).to_be_bytes()).await?;
    w.write_all(body).await?;
    w.flush().await
}

/// A minimal serving node for the tests: it accepts one mTLS connection, then answers each inbound
/// mux stream — a `dig.health` unsealed request with a canned [`Health`], and a sealed
/// `dig.getNetworkInfo` request (opened with the server's key, re-sealed to the client) with a canned
/// [`NetworkInfo`]. It mirrors what a real dig-node peer-RPC server will do.
struct TestServer {
    addr: std::net::SocketAddr,
    peer_id: PeerId,
}

async fn spawn_test_server(server_key: SecretKey) -> TestServer {
    let server_node = Arc::new(NodeCert::generate_signed(&server_key).expect("server cert"));
    let peer_id = server_node.peer_id();
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");

    tokio::spawn(async move {
        let server_tls =
            dig_tls::server_config(&server_node, BindingPolicy::Opportunistic).expect("server cfg");
        let acceptor = TlsAcceptor::from(server_tls.config.clone());
        let (tcp, _) = listener.accept().await.expect("accept tcp");
        let tls = acceptor.accept(tcp).await.expect("accept tls");

        let client_peer_id = server_tls
            .captured_peer_id
            .get()
            .expect("client peer_id captured");
        let client_bls = server_tls.captured_bls.get().expect("client bls captured");

        let mut session = PeerSession::server(tls);
        let mut counter = 0u64;
        while let Some(mut stream) = session.accept_stream().await {
            let body = match read_framed(&mut stream).await {
                Ok(b) => b,
                Err(_) => break,
            };
            let response = handle_request(
                &body,
                &server_key,
                server_node.peer_id(),
                client_peer_id,
                &client_bls,
                &mut counter,
            );
            write_framed(&mut stream, &response)
                .await
                .expect("write response");
        }
    });

    TestServer { addr, peer_id }
}

/// Answer one request body — unsealed if it parses as JSON, sealed otherwise.
fn handle_request(
    body: &[u8],
    server_key: &SecretKey,
    server_peer_id: PeerId,
    client_peer_id: PeerId,
    client_bls: &[u8; 48],
    counter: &mut u64,
) -> Vec<u8> {
    // Unsealed (public-read) path: the body is JSON. Dispatch on the method name.
    if let Ok(req) = serde_json::from_slice::<JsonRpcRequest<serde_json::Value>>(body) {
        let result = public_result(&req.method);
        let response = JsonRpcResponse::success(req.id, result);
        return serde_json::to_vec(&response).unwrap();
    }

    // Sealed (directed) path: open with the server key, dispatch, re-seal the response to the client.
    let envelope = DigMessageEnvelope::from_bytes(body).expect("sealed envelope decodes");
    let mut guard = ReplayGuard::default();
    let resolver = |_did: Bytes32, _epoch: u32| Some(*client_bls);
    let opened = open_message(server_key, &envelope, resolver, &mut guard, now_ms())
        .expect("server opens the sealed request");

    let req: JsonRpcRequest<serde_json::Value> =
        serde_json::from_slice(&opened.payload).expect("inner request parses");
    let response = JsonRpcResponse::success(req.id, directed_result(&req.method));
    let response_json = serde_json::to_vec(&response).unwrap();

    // Re-seal the response to the client, echoing the request's correlation id (so the client's
    // open_response correlates it), authored by the server identity.
    *counter += 1;
    let params = SealParams {
        sender_sk: server_key,
        sender: Bytes32::new(*server_peer_id.as_bytes()),
        sender_epoch: 0,
        recipient: Bytes32::new(*client_peer_id.as_bytes()),
        recipient_pub: client_bls,
        message_type: dig_peer::seal::RPC_MESSAGE_TYPE,
        shape: InteractionShape::Response,
        correlation_id: opened.correlation_id,
        stream: None,
        counter: *counter,
        timestamp_ms: now_ms(),
        expires_at: 0,
        payload: &response_json,
    };
    seal_message(&params)
        .expect("server seals the response")
        .to_bytes()
        .expect("sealed response serializes")
}

/// The canned result for an unsealed (public-read) method.
fn public_result(method: &str) -> serde_json::Value {
    match method {
        "dig.methods" => serde_json::json!({ "methods": ["dig.health", "dig.methods"] }),
        _ => {
            let health = Health {
                status: "ok".into(),
                version: Some("test".into()),
                network_id: Some("DIG_TESTNET".into()),
                methods: vec!["dig.health".into()],
            };
            serde_json::to_value(health).unwrap()
        }
    }
}

/// The canned result for a directed (sealed) method.
fn directed_result(method: &str) -> serde_json::Value {
    match method {
        "dig.getPeers" => serde_json::json!({ "peers": [] }),
        "dig.announce" => serde_json::json!({ "accepted": true, "known_peers": 1 }),
        _ => {
            let info = NetworkInfo {
                peer_id: None,
                network_id: "DIG_TESTNET".into(),
                listen_addr: "127.0.0.1:1".into(),
                reflexive_addr: None,
                candidate_addresses: vec![],
                reachability: "direct".into(),
                relay: RelayStatus {
                    url: "off".into(),
                    reserved: false,
                },
            };
            serde_json::to_value(info).unwrap()
        }
    }
}

fn now_ms() -> u64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis() as u64
}

/// **Proves:** DigPeer::connect establishes a real mTLS connection to a dig-nat server and an
/// unsealed public-read RPC (`health`) round-trips end-to-end.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn health_round_trips_over_real_mtls() {
    let server = spawn_test_server(identity_key("srv/health")).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/health")).unwrap());

    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");
    assert_eq!(peer.peer_id(), server.peer_id);

    let health = peer.health().await.expect("health rpc");
    assert_eq!(health.status, "ok");

    peer.disconnect().await;
}

/// **Proves:** a directed RPC (`getNetworkInfo`) is sealed to the peer's captured BLS key over the
/// real connection and the sealed response round-trips — the §5.4 path works end-to-end.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn directed_rpc_is_sealed_and_round_trips() {
    let server = spawn_test_server(identity_key("srv/net")).await;
    let client_key = identity_key("cli/net");
    let client_node = Arc::new(NodeCert::generate_signed(&client_key).unwrap());

    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect")
        .with_sealing_identity(SealingIdentity::new(client_key, 0));

    assert!(
        peer.peer_bls_pub().is_some(),
        "peer BLS key must be captured for sealing"
    );
    assert!(peer.is_sealable());

    let info = peer
        .get_network_info()
        .await
        .expect("sealed getNetworkInfo rpc");
    assert_eq!(info.network_id, "DIG_TESTNET");

    peer.disconnect().await;
}

/// **Proves:** a directed RPC is REFUSED (fail-closed) when no sealing identity is configured —
/// dig-peer never downgrades a directed call to plaintext.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn directed_rpc_without_sealing_identity_is_refused() {
    let server = spawn_test_server(identity_key("srv/refuse")).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/refuse")).unwrap());

    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");

    let result = peer.get_network_info().await;
    assert!(
        matches!(result, Err(dig_peer::DigPeerError::NoSealingIdentity)),
        "a directed call without a sealing identity must fail closed, got {result:?}"
    );
}

/// **Proves:** the unsealed `methods` self-describe RPC round-trips.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn methods_round_trips() {
    let server = spawn_test_server(identity_key("srv/methods")).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/methods")).unwrap());
    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");
    let methods = peer.methods().await.expect("methods rpc");
    assert!(methods.methods.contains(&"dig.health".to_string()));
    peer.disconnect().await;
}

/// **Proves:** the directed `getPeers` and `announce` RPCs seal, round-trip, and decode their typed
/// results — exercising the directed path for the peer-exchange + announce methods.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_peers_and_announce_round_trip_sealed() {
    use dig_rpc_protocol::types::AnnounceParams;

    let server = spawn_test_server(identity_key("srv/px")).await;
    let client_key = identity_key("cli/px");
    let client_node = Arc::new(NodeCert::generate_signed(&client_key).unwrap());
    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect")
        .with_sealing_identity(SealingIdentity::new(client_key, 0));

    let peers = peer.get_peers().await.expect("sealed getPeers");
    assert!(peers.peers.is_empty());

    let ack = peer
        .announce(&AnnounceParams {
            peer_id: peer.peer_id().to_hex(),
            addresses: vec![],
        })
        .await
        .expect("sealed announce");
    assert!(ack.accepted);
    assert_eq!(ack.known_peers, 1);

    peer.disconnect().await;
}

/// **Proves:** connecting with the WRONG expected `peer_id` is rejected — chaining to the DigNetwork
/// CA does not authorize an arbitrary peer; the caller must pin the specific identity.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn wrong_expected_peer_id_is_rejected() {
    let server = spawn_test_server(identity_key("srv/pin")).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/pin")).unwrap());

    // Pin a peer_id the server does NOT have.
    let wrong_peer_id = PeerId::from_bytes([0xEE; 32]);
    let target = PeerTarget::with_addr(wrong_peer_id, server.addr, "DIG_TESTNET");
    let result = DigPeer::connect(&target, &client_node).await;
    assert!(
        result.is_err(),
        "connecting with a mismatched peer_id must fail, got Ok"
    );
}

/// **Proves:** `DigPeer::open_stream()` gives a caller a generic raw mux stream over the
/// authenticated mTLS connection that round-trips ARBITRARY caller-owned bytes byte-identically —
/// the unsealed escape hatch a consumer with its own wire framing (e.g. dig-dht's `DhtRequest`) uses.
/// The server here is a blind echo (it does no JSON/RPC parsing), proving the stream is opaque bytes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn open_stream_round_trips_arbitrary_caller_owned_bytes() {
    let server = spawn_echo_server(identity_key("srv/raw")).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/raw")).unwrap());

    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");

    // A caller-owned frame that is NOT valid JSON — it must survive as opaque bytes.
    let frame: &[u8] = &[0x00, 0x01, 0xFF, 0xFE, 0x42, 0x00, 0x99];
    let mut stream = peer.open_stream().await.expect("open raw stream");
    write_framed(&mut stream, frame).await.expect("write frame");
    let echoed = read_framed(&mut stream).await.expect("read echoed frame");
    assert_eq!(
        echoed, frame,
        "raw stream must round-trip bytes byte-identically"
    );

    peer.disconnect().await;
}

/// A minimal serving node that BLINDLY echoes each framed body back on the same mux stream — it does
/// no RPC/JSON parsing, so it proves `open_stream` carries opaque caller-owned bytes.
async fn spawn_echo_server(server_key: SecretKey) -> TestServer {
    let server_node = Arc::new(NodeCert::generate_signed(&server_key).expect("server cert"));
    let peer_id = server_node.peer_id();
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");

    tokio::spawn(async move {
        let server_tls =
            dig_tls::server_config(&server_node, BindingPolicy::Opportunistic).expect("server cfg");
        let acceptor = TlsAcceptor::from(server_tls.config.clone());
        let (tcp, _) = listener.accept().await.expect("accept tcp");
        let tls = acceptor.accept(tcp).await.expect("accept tls");
        let mut session = PeerSession::server(tls);
        while let Some(mut stream) = session.accept_stream().await {
            let body = match read_framed(&mut stream).await {
                Ok(b) => b,
                Err(_) => break,
            };
            write_framed(&mut stream, &body).await.expect("echo body");
        }
    });

    TestServer { addr, peer_id }
}

/// **Proves:** after `disconnect`, further RPCs fail with `InvalidState` — no use-after-close.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn rpc_after_disconnect_is_invalid_state() {
    let server = spawn_test_server(identity_key("srv/close")).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/close")).unwrap());
    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");

    // Keep the connection, flip to closed via a fresh client we then reuse is impossible (disconnect
    // consumes self); instead assert the state helper directly on a live peer, then that disconnect
    // is clean. Use-after-close at the type level is prevented because disconnect takes `self`.
    let peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");
    assert_eq!(peer.state(), dig_peer::PeerState::Connected);
    peer.disconnect().await;
}

// ===========================================================================
// The whole-`.dig`-module pull client leg (#1576) over REAL mTLS
// ===========================================================================
//
// `dig.getModuleInfo` is an ordinary unsealed request/response call; `dig.fetchModuleRange` STREAMS
// `RangeFrame`s, so it needs a server that keeps writing on the stream it was asked on. Both are
// exercised against genuine mutual TLS rather than a mock, because the read leg's wire bugs (an
// unbracketed IPv6 literal, a `serde_bytes`-vs-base64 skew) all survived symmetric mocks and only
// surfaced on a real socket (#836/#1593).

/// A serving node for the module methods: answers `dig.getModuleInfo` with `descriptor`, and
/// `dig.fetchModuleRange` by streaming the requested window of `blob` as `frame_size`-byte frames.
async fn spawn_module_server(
    server_key: SecretKey,
    descriptor: ModuleInfo,
    blob: Vec<u8>,
    frame_size: usize,
) -> TestServer {
    let server_node = Arc::new(NodeCert::generate_signed(&server_key).expect("server cert"));
    let peer_id = server_node.peer_id();
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("addr");

    tokio::spawn(async move {
        let server_tls =
            dig_tls::server_config(&server_node, BindingPolicy::Opportunistic).expect("server cfg");
        let acceptor = TlsAcceptor::from(server_tls.config.clone());
        let (tcp, _) = listener.accept().await.expect("accept tcp");
        let tls = acceptor.accept(tcp).await.expect("accept tls");

        let mut session = PeerSession::server(tls);
        while let Some(mut stream) = session.accept_stream().await {
            let Ok(body) = read_framed(&mut stream).await else {
                break;
            };
            let req: JsonRpcRequest<serde_json::Value> =
                serde_json::from_slice(&body).expect("module request parses");
            match req.method.as_str() {
                "dig.getModuleInfo" => {
                    let response = JsonRpcResponse::success(
                        req.id,
                        serde_json::to_value(&descriptor).unwrap(),
                    );
                    write_framed(&mut stream, &serde_json::to_vec(&response).unwrap())
                        .await
                        .expect("write descriptor");
                }
                "dig.fetchModuleRange" => {
                    let params = req.params.expect("module range params");
                    let offset = params["offset"].as_u64().unwrap_or(0) as usize;
                    let length = params["length"].as_u64().expect("length") as usize;
                    let start = offset.min(blob.len());
                    let window = &blob[start..(start + length).min(blob.len())];
                    let mut written = 0usize;
                    while written < window.len() {
                        let take = frame_size.min(window.len() - written);
                        // A MODULE frame carries no resource identity. `dig.fetchModuleRange` streams a
                        // module blob, which has no generation `root`, no chunk layout and hence no
                        // `chunk_count` — and dig-nat 0.13 reaches `total_length` only through
                        // `with_identity(root, total_length, chunk_count)`, which would mean fabricating
                        // a root that a reader's wrong-generation check would then compare against. The
                        // previous fixture set `total_length` on the first frame only, mirroring a
                        // "(first frame only)" rule that is no longer how identity works; nothing reads
                        // it on this path (`fetch_module_range` ignores it, no assertion covers it), so
                        // it is dropped rather than faked.
                        let frame = RangeFrame::data(
                            (start + written) as u64,
                            window[written..written + take].to_vec(),
                        )
                        .with_complete(written + take == window.len());
                        write_framed(&mut stream, &serde_json::to_vec(&frame).unwrap())
                            .await
                            .expect("write module frame");
                        written += take;
                    }
                }
                other => panic!("unexpected module method {other}"),
            }
        }
    });

    TestServer { addr, peer_id }
}

/// A descriptor + blob pair for the module tests: `chunks` chunks of `chunk_len` bytes each.
fn module_fixture(chunks: usize, chunk_len: usize) -> (ModuleInfo, Vec<u8>) {
    let blob: Vec<u8> = (0..chunks * chunk_len).map(|i| (i % 251) as u8).collect();
    let info = ModuleInfo {
        total_size: blob.len() as u64,
        module_hash: sha256_hex(&blob),
        chunk_hashes: blob.chunks(chunk_len).map(sha256_hex).collect(),
        chunk_lens: vec![chunk_len as u64; chunks],
    };
    (info, blob)
}

fn sha256_hex(bytes: &[u8]) -> String {
    use sha2::{Digest as _, Sha256};
    Sha256::digest(bytes)
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect()
}

/// **Proves:** `dig.getModuleInfo` round-trips over real mTLS and decodes into the
/// dig-rpc-protocol [`ModuleInfo`] the module puller plans from — the transfer descriptor crosses the
/// wire intact.
/// **Catches:** a `ModuleInfo` shape skew between the client's dig-rpc-protocol major and the
/// server's (the #836 `serde_bytes`-vs-base64 class) — a renamed or re-encoded field fails this decode
/// instead of silently planning a pull against zeroes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn get_module_info_round_trips_over_real_mtls() {
    let (descriptor, blob) = module_fixture(3, 64);
    let server =
        spawn_module_server(identity_key("srv/modinfo"), descriptor.clone(), blob, 64).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/modinfo")).unwrap());
    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");

    let info = peer
        .get_module_info(&GetModuleInfoParams {
            store_id: "aa".repeat(32),
            root: "bb".repeat(32),
        })
        .await
        .expect("getModuleInfo rpc");

    assert_eq!(info, descriptor, "the descriptor crossed the wire intact");
    peer.disconnect().await;
}

/// **Proves:** `dig.fetchModuleRange` opens a real stream whose frames reassemble to the EXACT
/// requested window of the module blob — including when the holder answers at a smaller frame
/// granularity than the requested range (the normal case for a chunk wider than one frame).
/// **Catches:** a client that stops after the first frame, silently truncating every multi-frame chunk
/// into a short range that then fails its chunk hash for no discoverable reason.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fetch_module_range_streams_the_exact_window() {
    let (descriptor, blob) = module_fixture(2, 256);
    // Frames deliberately narrower than the requested range, so a one-frame read is caught.
    let server =
        spawn_module_server(identity_key("srv/modrange"), descriptor, blob.clone(), 100).await;
    let client_node = Arc::new(NodeCert::generate_signed(&identity_key("cli/modrange")).unwrap());
    let target = PeerTarget::with_addr(server.peer_id, server.addr, "DIG_TESTNET");
    let mut peer = DigPeer::connect(&target, &client_node)
        .await
        .expect("connect");

    let mut stream = peer
        .fetch_module_range(&FetchModuleRangeParams {
            store_id: "aa".repeat(32),
            root: "bb".repeat(32),
            offset: Some(256),
            length: 256,
        })
        .await
        .expect("fetchModuleRange stream");

    let mut got = Vec::new();
    loop {
        let frame = RangeFrame::decode(&mut stream)
            .await
            .expect("frame decodes")
            .expect("the stream did not end mid-range");
        got.extend_from_slice(&frame.bytes);
        if frame.complete {
            break;
        }
    }
    assert_eq!(got, blob[256..512], "the exact requested window arrived");
    peer.disconnect().await;
}