asupersync 0.3.6

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Tests for Byzantine consensus protocols.

use crate::cx::Cx;
use crate::error::Result;
use crate::types::Time;
use std::sync::{Arc, Mutex};

use super::pbft::{PbftConfig, PbftConsensus, PbftMessage, PbftNode, PbftTransport};
use super::types::{
    ConsensusBatch, ConsensusRequest, MessageDigest, ReplicaId, SequenceNumber, ViewNumber,
};

/// Deterministic transport for testing PBFT.
#[derive(Debug)]
pub struct MockTransport {
    /// Messages sent by this replica.
    pub sent_messages: Arc<Mutex<Vec<PbftMessage>>>,
    /// Messages to be received by this replica.
    pub received_messages: Arc<Mutex<Vec<PbftMessage>>>,
    /// Whether to inject network failures.
    pub fail_sends: bool,
}

impl MockTransport {
    pub fn new() -> Self {
        Self {
            sent_messages: Arc::new(Mutex::new(Vec::new())),
            received_messages: Arc::new(Mutex::new(Vec::new())),
            fail_sends: false,
        }
    }

    pub fn add_received_message(&self, message: PbftMessage) {
        let mut messages = self.received_messages.lock().unwrap();
        messages.push(message);
    }

    pub fn get_sent_messages(&self) -> Vec<PbftMessage> {
        let messages = self.sent_messages.lock().unwrap();
        messages.clone()
    }

    pub fn clear_sent_messages(&self) {
        let mut messages = self.sent_messages.lock().unwrap();
        messages.clear();
    }
}

impl PbftTransport for MockTransport {
    fn send_to_replica(
        &self,
        _replica_id: &ReplicaId,
        message: PbftMessage,
    ) -> impl std::future::Future<Output = Result<()>> + Send {
        let fail_sends = self.fail_sends;
        let sent_messages = Arc::clone(&self.sent_messages);

        async move {
            if fail_sends {
                return Err(crate::error::Error::new(
                    crate::error::ErrorKind::ConnectionLost,
                ));
            }

            let mut messages = sent_messages.lock().unwrap();
            messages.push(message);
            Ok(())
        }
    }

    fn broadcast(
        &self,
        message: PbftMessage,
    ) -> impl std::future::Future<Output = Result<()>> + Send {
        let fail_sends = self.fail_sends;
        let sent_messages = Arc::clone(&self.sent_messages);

        async move {
            if fail_sends {
                return Err(crate::error::Error::new(
                    crate::error::ErrorKind::ConnectionLost,
                ));
            }

            let mut messages = sent_messages.lock().unwrap();
            messages.push(message);
            Ok(())
        }
    }

    fn receive(&self) -> impl std::future::Future<Output = Result<PbftMessage>> + Send {
        let received_messages = Arc::clone(&self.received_messages);

        async move {
            let mut messages = received_messages.lock().unwrap();
            if let Some(message) = messages.pop() {
                Ok(message)
            } else {
                // No message is currently available.
                Err(crate::error::Error::new(
                    crate::error::ErrorKind::ChannelEmpty,
                ))
            }
        }
    }
}

#[test]
fn test_pbft_config_validation() {
    // Valid configuration: 4 replicas, 1 fault
    let config = PbftConfig::new(4, 1).unwrap();
    assert!(config.is_valid());
    assert_eq!(config.quorum_size(), 3); // 2f + 1 = 2*1 + 1 = 3

    // Invalid configuration: insufficient replicas
    let result = PbftConfig::new(2, 1);
    assert!(result.is_err());

    // Valid configuration: 7 replicas, 2 faults
    let config = PbftConfig::new(7, 2).unwrap();
    assert!(config.is_valid());
    assert_eq!(config.quorum_size(), 5); // 2f + 1 = 2*2 + 1 = 5
}

#[test]
fn test_pbft_node_creation() {
    let replica_id = ReplicaId::new("0".to_string());
    let config = PbftConfig::new(4, 1).unwrap();
    let transport = MockTransport::new();

    let node = PbftNode::new(replica_id.clone(), config, transport).unwrap();

    // Node should be primary for view 0 (replica 0 % 4 = 0)
    assert!(node.is_primary());
}

#[test]
fn pbft_node_rejects_non_numeric_or_out_of_set_replica_id() {
    let config = PbftConfig::new(4, 1).unwrap();

    let non_numeric = PbftNode::new(
        ReplicaId::new("node-a".to_string()),
        config.clone(),
        MockTransport::new(),
    );
    assert!(non_numeric.is_err());

    let out_of_set = PbftNode::new(
        ReplicaId::new("4".to_string()),
        config,
        MockTransport::new(),
    );
    assert!(out_of_set.is_err());
}

#[test]
fn test_pbft_consensus_creation() {
    let replica_id = ReplicaId::new("1".to_string());
    let config = PbftConfig::new(4, 1).unwrap();
    let transport = MockTransport::new();

    let _consensus = PbftConsensus::new(replica_id, config, transport).unwrap();

    // Should be able to create consensus instance
    assert!(true); // Just verify creation doesn't panic
}

#[test]
fn test_request_submission() {
    let replica_id = ReplicaId::new("0".to_string());
    let config = PbftConfig::new(4, 1).unwrap();
    let transport = MockTransport::new();

    let _consensus = PbftConsensus::new(replica_id, config, transport).unwrap();

    let _request = ConsensusRequest::new(
        "client-1".to_string(),
        Time::from_millis(0),
        b"test operation".to_vec(),
    );

    // Create a simple context for testing
    let _cx = Cx::for_testing();

    // Just test creation for now, since we don't have async runtime
    assert!(true);
}

/// Drive a future that resolves without yielding (the view-change/new-view
/// handlers return immediately).
fn poll_ready<F: std::future::Future>(fut: F) -> F::Output {
    let mut fut = std::pin::pin!(fut);
    let waker = std::task::Waker::noop();
    let mut cx = std::task::Context::from_waker(waker);
    match fut.as_mut().poll(&mut cx) {
        std::task::Poll::Ready(value) => value,
        std::task::Poll::Pending => panic!("handler unexpectedly pended"),
    }
}

#[test]
fn view_change_and_new_view_fail_closed_until_implemented() {
    // PBFT view-change/new-view are not implemented (asupersync-v8mszr). They
    // must FAIL rather than silently return Ok, so a caller never believes
    // primary-failure recovery happened when it did not.
    let node = PbftNode::new(
        ReplicaId::new("0".to_string()),
        PbftConfig::new(4, 1).unwrap(),
        MockTransport::new(),
    )
    .unwrap();
    let cx = Cx::for_testing();

    let view_change = PbftMessage::ViewChange {
        new_view: ViewNumber::new(1),
        replica_id: ReplicaId::new("1".to_string()),
        certificates: Vec::new(),
    };
    assert!(
        poll_ready(node.process_message(&cx, view_change)).is_err(),
        "view-change must fail closed, not silently succeed"
    );

    let new_view = PbftMessage::NewView {
        view: ViewNumber::new(1),
        view_change_msgs: Vec::new(),
        preprepare_msgs: Vec::new(),
    };
    assert!(
        poll_ready(node.process_message(&cx, new_view)).is_err(),
        "new-view must fail closed, not silently succeed"
    );
}

#[test]
fn test_mock_transport_message_tracking_helpers() {
    let transport = MockTransport::new();
    let replica_id = ReplicaId::new("1".to_string());
    let request = ConsensusRequest::new(
        "client-1".to_string(),
        Time::from_millis(0),
        b"tracked operation".to_vec(),
    );
    let message = PbftMessage::Request(request);

    futures_lite::future::block_on(transport.send_to_replica(&replica_id, message.clone()))
        .expect("deterministic send should record message");
    assert_eq!(transport.get_sent_messages().len(), 1);

    transport.clear_sent_messages();
    assert!(transport.get_sent_messages().is_empty());

    transport.add_received_message(message);
    let received = futures_lite::future::block_on(transport.receive())
        .expect("deterministic receive should return queued message");
    assert!(matches!(received, PbftMessage::Request(_)));
}

#[test]
fn test_primary_election() {
    // Test primary selection for different views
    assert_eq!(ViewNumber::new(0).primary(4), 0);
    assert_eq!(ViewNumber::new(1).primary(4), 1);
    assert_eq!(ViewNumber::new(2).primary(4), 2);
    assert_eq!(ViewNumber::new(3).primary(4), 3);
    assert_eq!(ViewNumber::new(4).primary(4), 0); // Wraps around
    assert_eq!(ViewNumber::new(4).primary(0), 0); // Fails closed instead of panicking
}

fn test_batch(payload: &'static [u8]) -> ConsensusBatch {
    ConsensusBatch::new(vec![ConsensusRequest::new(
        "client-1".to_string(),
        Time::from_millis(0),
        payload.to_vec(),
    )])
}

fn preprepare_for(
    view: ViewNumber,
    sequence: SequenceNumber,
    batch: ConsensusBatch,
    replica_id: ReplicaId,
) -> PbftMessage {
    let digest = MessageDigest::of(&batch).expect("batch digest");
    PbftMessage::PrePrepare {
        view,
        sequence,
        digest,
        batch,
        replica_id,
    }
}

/// Drive backup replica `node` to a full prepare + commit certificate for one
/// sequence: a pre-prepare from primary "0", then corroborating prepares and
/// commits from replicas "2" and "3" (quorum = 2f + 1 = 3, including the node's
/// own implicit vote). Whether the sequence then *executes* depends only on the
/// execution watermark, which is what the reordering regression exercises.
fn certify_sequence(
    node: &PbftNode<MockTransport>,
    cx: &Cx,
    view: ViewNumber,
    sequence: SequenceNumber,
    batch: ConsensusBatch,
    digest: &MessageDigest,
) {
    let deliver = |message: PbftMessage| poll_ready(node.process_message(cx, message));

    deliver(PbftMessage::PrePrepare {
        view,
        sequence,
        digest: digest.clone(),
        batch,
        replica_id: ReplicaId::new("0".to_string()),
    })
    .expect("pre-prepare accepted");

    for replica in ["2", "3"] {
        deliver(PbftMessage::Prepare {
            view,
            sequence,
            digest: digest.clone(),
            replica_id: ReplicaId::new(replica.to_string()),
        })
        .expect("prepare accepted");
    }

    for replica in ["2", "3"] {
        deliver(PbftMessage::Commit {
            view,
            sequence,
            digest: digest.clone(),
            replica_id: ReplicaId::new(replica.to_string()),
        })
        .expect("commit accepted");
    }
}

#[test]
fn out_of_order_commit_quorum_drains_without_wedging_execution() {
    // Regression: a higher sequence whose commit certificate completes BEFORE
    // the lower sequence executes must still execute once the gap fills. Before
    // the drain loop, execution only fired on the arrival of a NEW commit for
    // exactly `last_executed.next()`, so seq 2's already-complete certificate
    // stalled permanently behind seq 1 — a liveness break under ordinary network
    // reordering, not just adversarial input.
    let node = PbftNode::new(
        ReplicaId::new("1".to_string()),
        PbftConfig::new(4, 1).unwrap(),
        MockTransport::new(),
    )
    .unwrap();
    let cx = Cx::for_testing();
    let view = ViewNumber::new(0);

    let batch1 = test_batch(b"op-1");
    let batch2 = test_batch(b"op-2");
    let digest1 = MessageDigest::of(&batch1).expect("digest 1");
    let digest2 = MessageDigest::of(&batch2).expect("digest 2");

    // Seq 2's commit quorum completes first, out of order.
    certify_sequence(&node, &cx, view, SequenceNumber::new(2), batch2, &digest2);
    assert_eq!(
        node.last_executed(),
        SequenceNumber::new(0),
        "seq 2 must wait for seq 1 (gap-free execution), not execute early",
    );

    // Seq 1 completes: it executes, then the drain loop picks up the already
    // committed seq 2.
    certify_sequence(&node, &cx, view, SequenceNumber::new(1), batch1, &digest1);
    assert_eq!(
        node.last_executed(),
        SequenceNumber::new(2),
        "executing seq 1 must drain the already-committed seq 2 (no permanent stall)",
    );
}

#[test]
fn preprepare_from_non_primary_fails_closed() {
    let transport = MockTransport::new();
    let sent_messages = Arc::clone(&transport.sent_messages);
    let node = PbftNode::new(
        ReplicaId::new("1".to_string()),
        PbftConfig::new(4, 1).unwrap(),
        transport,
    )
    .unwrap();
    let cx = Cx::for_testing();

    let message = preprepare_for(
        ViewNumber::new(0),
        SequenceNumber::new(1),
        test_batch(b"op-1"),
        ReplicaId::new("2".to_string()),
    );
    assert!(poll_ready(node.process_message(&cx, message)).is_err());
    assert!(
        sent_messages.lock().unwrap().is_empty(),
        "rejecting a non-primary pre-prepare must not broadcast prepare"
    );
}

#[test]
fn preprepare_equivocation_fails_closed_without_overwrite() {
    let node = PbftNode::new(
        ReplicaId::new("1".to_string()),
        PbftConfig::new(4, 1).unwrap(),
        MockTransport::new(),
    )
    .unwrap();
    let cx = Cx::for_testing();

    let first = preprepare_for(
        ViewNumber::new(0),
        SequenceNumber::new(1),
        test_batch(b"op-1"),
        ReplicaId::new("0".to_string()),
    );
    poll_ready(node.process_message(&cx, first)).expect("first pre-prepare accepted");

    let equivocated = preprepare_for(
        ViewNumber::new(0),
        SequenceNumber::new(1),
        test_batch(b"op-2"),
        ReplicaId::new("0".to_string()),
    );
    assert!(
        poll_ready(node.process_message(&cx, equivocated)).is_err(),
        "different digest for the same view/sequence must fail closed"
    );
}

#[test]
fn prepare_and_commit_reject_self_and_out_of_set_senders() {
    let node = PbftNode::new(
        ReplicaId::new("1".to_string()),
        PbftConfig::new(4, 1).unwrap(),
        MockTransport::new(),
    )
    .unwrap();
    let cx = Cx::for_testing();
    let view = ViewNumber::new(0);
    let sequence = SequenceNumber::new(1);
    let batch = test_batch(b"op-1");
    let digest = MessageDigest::of(&batch).expect("batch digest");

    let preprepare = PbftMessage::PrePrepare {
        view,
        sequence,
        digest: digest.clone(),
        batch,
        replica_id: ReplicaId::new("0".to_string()),
    };
    poll_ready(node.process_message(&cx, preprepare)).expect("pre-prepare accepted");

    let self_prepare = PbftMessage::Prepare {
        view,
        sequence,
        digest: digest.clone(),
        replica_id: ReplicaId::new("1".to_string()),
    };
    assert!(poll_ready(node.process_message(&cx, self_prepare)).is_err());

    let out_of_set_commit = PbftMessage::Commit {
        view,
        sequence,
        digest,
        replica_id: ReplicaId::new("4".to_string()),
    };
    assert!(poll_ready(node.process_message(&cx, out_of_set_commit)).is_err());
}

#[test]
fn test_sequence_number_ordering() {
    let seq1 = SequenceNumber::new(1);
    let seq2 = SequenceNumber::new(2);
    let seq3 = seq1.next();

    assert!(seq1 < seq2);
    assert_eq!(seq3, seq2);
}

#[test]
fn test_message_digest() {
    use super::types::{ConsensusBatch, MessageDigest};

    let request1 = ConsensusRequest::new(
        "client-1".to_string(),
        Time::from_millis(0),
        b"operation-1".to_vec(),
    );

    let request2 = ConsensusRequest::new(
        "client-1".to_string(),
        Time::from_millis(0),
        b"operation-2".to_vec(),
    );

    let batch1 = ConsensusBatch::new(vec![request1.clone()]);
    let batch2 = ConsensusBatch::new(vec![request2]);
    let batch3 = ConsensusBatch::new(vec![request1]);

    let digest1 = MessageDigest::of(&batch1).unwrap();
    let digest2 = MessageDigest::of(&batch2).unwrap();
    let digest3 = MessageDigest::of(&batch3).unwrap();

    // Different batches should have different digests
    assert_ne!(digest1, digest2);

    // Same batch content should have same digest
    assert_eq!(digest1, digest3);
}

#[cfg(test)]
mod integration_tests {
    use super::*;

    /// Test basic 3-node PBFT with no faults.
    #[test]
    fn test_basic_consensus() {
        let config = PbftConfig::new(4, 1).unwrap();

        // Create replicas
        for i in 0..4 {
            let replica_id = ReplicaId::new(i.to_string());
            let transport = MockTransport::new();
            let node = PbftNode::new(replica_id, config.clone(), transport);

            // Just verify creation succeeded
            assert!(node.is_ok());
        }
    }
}