nklave-core 0.1.0

Core signing logic, BLS/Ed25519 keys, and slashing protection rules for Nklave
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Passive node state receiver
//!
//! The PassiveReceiver connects to the primary, receives decision records,
//! verifies the hash chain, and maintains shadow state ready for failover.

use super::heartbeat::{HeartbeatConfig, HeartbeatMonitor};
use super::protocol::{AckMessage, HelloMessage, ReplicationMessage, PROTOCOL_VERSION};
use super::tls::{ReplicationTlsConfig, ReplicationTlsStream};
use crate::state::integrity::{DecisionRecord, IntegrityError, StateIntegrity};
use crate::state::validator::ValidatorState;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::sync::watch;
use tokio_rustls::TlsConnector;
use tracing::{debug, error, info, warn};

/// Configuration for the passive receiver
#[derive(Debug, Clone)]
pub struct PassiveConfig {
    /// Address of the primary node
    pub primary_addr: String,

    /// Node identifier
    pub node_id: String,

    /// Reconnect delay on connection failure (milliseconds)
    pub reconnect_delay_ms: u64,

    /// Heartbeat configuration
    pub heartbeat_config: HeartbeatConfig,

    /// Optional TLS configuration for mTLS
    pub tls_config: Option<ReplicationTlsConfig>,

    /// Server name for TLS verification (e.g., "primary.nklave.local")
    pub tls_server_name: Option<String>,
}

impl Default for PassiveConfig {
    fn default() -> Self {
        Self {
            primary_addr: "127.0.0.1:26660".to_string(),
            node_id: "passive".to_string(),
            reconnect_delay_ms: 5000,
            heartbeat_config: HeartbeatConfig::default(),
            tls_config: None,
            tls_server_name: None,
        }
    }
}

/// Passive receiver that maintains shadow state
pub struct PassiveReceiver {
    config: PassiveConfig,

    /// Shadow copy of state integrity
    integrity: Arc<RwLock<StateIntegrity>>,

    /// Shadow copy of validator states
    _validator_states: Arc<RwLock<HashMap<[u8; 48], ValidatorState>>>,

    /// Heartbeat monitor
    heartbeat_monitor: Arc<HeartbeatMonitor>,

    /// Current fencing token from primary
    fencing_token: AtomicU64,

    /// Replication lag (sequences behind primary)
    replication_lag: AtomicU64,

    /// Whether we're connected to primary
    connected: AtomicBool,

    /// Shutdown flag
    _shutdown: AtomicBool,

    /// Optional TLS connector for secure connections
    tls_connector: Option<TlsConnector>,

    /// Server name for TLS verification
    tls_server_name: Option<String>,
}

/// Handle for controlling the passive receiver
pub struct PassiveHandle {
    /// Shutdown signal
    shutdown_tx: watch::Sender<bool>,

    /// Reference to integrity for promotion
    integrity: Arc<RwLock<StateIntegrity>>,

    /// Reference to validator states for promotion
    validator_states: Arc<RwLock<HashMap<[u8; 48], ValidatorState>>>,

    /// Heartbeat monitor for failover detection
    heartbeat_monitor: Arc<HeartbeatMonitor>,
}

impl PassiveHandle {
    /// Get the current state integrity (for promotion)
    pub fn integrity(&self) -> StateIntegrity {
        self.integrity.read().map(|i| i.clone()).unwrap_or_default()
    }

    /// Get the current validator states (for promotion)
    pub fn validator_states(&self) -> HashMap<[u8; 48], ValidatorState> {
        self.validator_states.read().map(|v| v.clone()).unwrap_or_default()
    }

    /// Subscribe to failover notifications
    pub fn subscribe_failover(&self) -> watch::Receiver<bool> {
        self.heartbeat_monitor.subscribe_failover()
    }

    /// Check if we should trigger failover
    pub fn should_failover(&self) -> bool {
        !self.heartbeat_monitor.is_primary_alive()
    }

    /// Get replication lag
    pub fn replication_lag(&self) -> u64 {
        // This would need to be tracked
        0
    }

    /// Signal shutdown
    pub fn shutdown(self) {
        let _ = self.shutdown_tx.send(true);
    }
}

/// Errors from passive receiver
#[derive(Debug, thiserror::Error)]
pub enum PassiveError {
    #[error("Connection failed: {0}")]
    ConnectionFailed(String),

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Serialization error: {0}")]
    Serialization(String),

    #[error("Protocol error: {0}")]
    Protocol(String),

    #[error("Hash chain verification failed: {0}")]
    HashChainError(#[from] IntegrityError),

    #[error("Genesis root mismatch")]
    GenesisRootMismatch,
}

impl PassiveReceiver {
    /// Create a new passive receiver
    pub fn new(
        config: PassiveConfig,
        initial_integrity: StateIntegrity,
        initial_validators: HashMap<[u8; 48], ValidatorState>,
    ) -> (Self, PassiveHandle) {
        let (shutdown_tx, _shutdown_rx) = watch::channel(false);

        let integrity = Arc::new(RwLock::new(initial_integrity));
        let validator_states = Arc::new(RwLock::new(initial_validators));
        let heartbeat_monitor = Arc::new(HeartbeatMonitor::new(config.heartbeat_config.clone()));

        // Build TLS connector if configured
        let tls_connector = if let Some(ref tls_config) = config.tls_config {
            match tls_config.build_connector() {
                Ok(connector) => {
                    info!("TLS connector configured for passive receiver");
                    Some(connector)
                }
                Err(e) => {
                    error!(error = %e, "Failed to build TLS connector, falling back to plaintext");
                    None
                }
            }
        } else {
            None
        };

        let tls_server_name = config.tls_server_name.clone();

        let receiver = Self {
            config: config.clone(),
            integrity: integrity.clone(),
            _validator_states: validator_states.clone(),
            heartbeat_monitor: heartbeat_monitor.clone(),
            fencing_token: AtomicU64::new(0),
            replication_lag: AtomicU64::new(0),
            connected: AtomicBool::new(false),
            _shutdown: AtomicBool::new(false),
            tls_connector,
            tls_server_name,
        };

        let handle = PassiveHandle {
            shutdown_tx,
            integrity,
            validator_states,
            heartbeat_monitor,
        };

        (receiver, handle)
    }

    /// Run the passive receiver (connects and stays connected to primary)
    pub async fn run(self, mut shutdown_rx: watch::Receiver<bool>) -> Result<(), PassiveError> {
        loop {
            // Check for shutdown
            if *shutdown_rx.borrow() {
                info!("Passive receiver shutting down");
                break;
            }

            // Try to connect
            match self.connect_and_receive(&mut shutdown_rx).await {
                Ok(()) => {
                    info!("Connection to primary closed normally");
                }
                Err(e) => {
                    warn!(error = %e, "Connection to primary failed");
                    self.connected.store(false, Ordering::Release);
                }
            }

            // Wait before reconnecting
            tokio::select! {
                _ = tokio::time::sleep(Duration::from_millis(self.config.reconnect_delay_ms)) => {}
                _ = shutdown_rx.changed() => {
                    if *shutdown_rx.borrow() {
                        break;
                    }
                }
            }
        }

        Ok(())
    }

    /// Connect to primary and receive messages
    async fn connect_and_receive(
        &self,
        shutdown_rx: &mut watch::Receiver<bool>,
    ) -> Result<(), PassiveError> {
        let tcp_stream = TcpStream::connect(&self.config.primary_addr).await?;
        info!(addr = %self.config.primary_addr, "TCP connected to primary");

        // Wrap with TLS if configured
        if let Some(ref connector) = self.tls_connector {
            let server_name = self
                .tls_server_name
                .as_deref()
                .unwrap_or("primary.nklave.local");

            match ReplicationTlsStream::connect(connector, server_name, tcp_stream).await {
                Ok(tls_stream) => {
                    info!("TLS handshake successful with primary");
                    self.connected.store(true, Ordering::Release);
                    self.receive_loop_generic(tls_stream, shutdown_rx).await
                }
                Err(e) => {
                    warn!(error = %e, "TLS handshake failed with primary");
                    Err(PassiveError::ConnectionFailed(format!(
                        "TLS handshake failed: {}",
                        e
                    )))
                }
            }
        } else {
            info!(addr = %self.config.primary_addr, "Connected to primary (plaintext - NOT RECOMMENDED FOR PRODUCTION)");
            self.connected.store(true, Ordering::Release);
            self.receive_loop_generic(ReplicationTlsStream::plain(tcp_stream), shutdown_rx)
                .await
        }
    }

    /// Main receive loop with generic stream type
    async fn receive_loop_generic<S>(
        &self,
        mut stream: S,
        shutdown_rx: &mut watch::Receiver<bool>,
    ) -> Result<(), PassiveError>
    where
        S: AsyncRead + AsyncWrite + Unpin,
    {
        // Receive hello from primary
        let primary_hello = match read_message_generic(&mut stream).await? {
            Some(ReplicationMessage::Hello(h)) => h,
            Some(other) => {
                return Err(PassiveError::Protocol(format!(
                    "Expected Hello, got {:?}",
                    std::mem::discriminant(&other)
                )));
            }
            None => {
                return Err(PassiveError::Protocol(
                    "Connection closed during handshake".to_string(),
                ));
            }
        };

        // Version check
        if primary_hello.version != PROTOCOL_VERSION {
            return Err(PassiveError::Protocol(format!(
                "Version mismatch: expected {}, got {}",
                PROTOCOL_VERSION, primary_hello.version
            )));
        }

        // Send our hello
        let (our_sequence, our_hash, our_genesis) = {
            let guard = self
                .integrity
                .read()
                .map_err(|_| PassiveError::Protocol("Lock poisoned".to_string()))?;
            (
                guard.sequence_number,
                guard.current_hash,
                guard.genesis_validators_root,
            )
        };

        let our_hello = HelloMessage {
            version: PROTOCOL_VERSION,
            node_id: self.config.node_id.clone(),
            role: "Passive".to_string(),
            sequence: our_sequence,
            state_hash: our_hash,
            genesis_root: our_genesis,
        };

        send_message_generic(&mut stream, &ReplicationMessage::Hello(our_hello)).await?;

        info!(
            primary_id = %primary_hello.node_id,
            primary_sequence = primary_hello.sequence,
            our_sequence = our_sequence,
            "Handshake complete with primary"
        );

        // Record initial heartbeat
        self.heartbeat_monitor
            .record_heartbeat(primary_hello.sequence, primary_hello.state_hash);

        // Check genesis root compatibility
        if let (Some(primary_genesis), Some(our_genesis)) =
            (primary_hello.genesis_root, our_genesis)
        {
            if primary_genesis != our_genesis {
                return Err(PassiveError::GenesisRootMismatch);
            }
        }

        // Main receive loop
        loop {
            tokio::select! {
                msg_result = read_message_generic(&mut stream) => {
                    match msg_result {
                        Ok(Some(msg)) => {
                            self.handle_message_generic(msg, &mut stream).await?;
                        }
                        Ok(None) => {
                            info!("Primary disconnected");
                            break;
                        }
                        Err(e) => {
                            return Err(e);
                        }
                    }
                }

                _ = shutdown_rx.changed() => {
                    if *shutdown_rx.borrow() {
                        break;
                    }
                }
            }
        }

        Ok(())
    }

    /// Handle a received message (legacy version for TcpStream)
    #[allow(dead_code)]
    async fn handle_message(
        &self,
        msg: ReplicationMessage,
        stream: &mut TcpStream,
    ) -> Result<(), PassiveError> {
        self.handle_message_generic(msg, stream).await
    }

    /// Handle a received message (generic version)
    async fn handle_message_generic<S>(
        &self,
        msg: ReplicationMessage,
        stream: &mut S,
    ) -> Result<(), PassiveError>
    where
        S: AsyncRead + AsyncWrite + Unpin,
    {
        match msg {
            ReplicationMessage::Heartbeat(hb) => {
                self.heartbeat_monitor
                    .record_heartbeat(hb.sequence, hb.state_hash);
                self.fencing_token.store(hb.fencing_token, Ordering::Release);

                // Calculate replication lag
                let our_seq = self
                    .integrity
                    .read()
                    .map(|i| i.sequence_number)
                    .unwrap_or(0);
                let lag = hb.sequence.saturating_sub(our_seq);
                self.replication_lag.store(lag, Ordering::Release);

                debug!(
                    primary_sequence = hb.sequence,
                    our_sequence = our_seq,
                    lag = lag,
                    "Heartbeat received"
                );
            }

            ReplicationMessage::Decision(record) => {
                self.apply_decision_record_generic(record, stream).await?;
            }

            ReplicationMessage::SyncResponse(response) => {
                info!(
                    record_count = response.records.len(),
                    has_more = response.has_more,
                    "Received sync response"
                );

                for record in response.records {
                    self.apply_decision_record_generic(record, stream).await?;
                }
            }

            ReplicationMessage::Error(err) => {
                error!(
                    code = ?err.code,
                    description = %err.description,
                    "Received error from primary"
                );
                return Err(PassiveError::Protocol(err.description));
            }

            _ => {
                debug!("Ignoring unexpected message type");
            }
        }

        Ok(())
    }

    /// Apply a decision record to our shadow state (legacy version for TcpStream)
    #[allow(dead_code)]
    async fn apply_decision_record(
        &self,
        record: DecisionRecord,
        stream: &mut TcpStream,
    ) -> Result<(), PassiveError> {
        self.apply_decision_record_generic(record, stream).await
    }

    /// Apply a decision record to our shadow state (generic version)
    async fn apply_decision_record_generic<S>(
        &self,
        record: DecisionRecord,
        stream: &mut S,
    ) -> Result<(), PassiveError>
    where
        S: AsyncWrite + Unpin,
    {
        let new_hash = {
            let mut integrity = self
                .integrity
                .write()
                .map_err(|_| PassiveError::Protocol("Lock poisoned".to_string()))?;

            // Verify and apply the record
            let new_hash = integrity.record_decision(&record)?;

            debug!(
                sequence = record.sequence,
                new_hash = hex::encode(new_hash),
                "Applied decision record"
            );

            new_hash
        };

        // Send acknowledgment
        let ack = AckMessage {
            sequence: record.sequence,
            state_hash: new_hash,
        };
        send_message_generic(stream, &ReplicationMessage::Ack(ack)).await?;

        // Update metrics
        crate::metrics::set_state_sequence(record.sequence);

        Ok(())
    }

    /// Get current replication lag
    pub fn replication_lag(&self) -> u64 {
        self.replication_lag.load(Ordering::Acquire)
    }

    /// Check if connected to primary
    pub fn is_connected(&self) -> bool {
        self.connected.load(Ordering::Acquire)
    }
}

/// Send a message with length prefix (legacy version for TcpStream)
#[allow(dead_code)]
async fn send_message(
    stream: &mut TcpStream,
    msg: &ReplicationMessage,
) -> Result<(), PassiveError> {
    send_message_generic(stream, msg).await
}

/// Send a message with length prefix (generic version)
async fn send_message_generic<S>(stream: &mut S, msg: &ReplicationMessage) -> Result<(), PassiveError>
where
    S: AsyncWrite + Unpin,
{
    let bytes =
        serde_json::to_vec(msg).map_err(|e| PassiveError::Serialization(e.to_string()))?;

    let len = bytes.len() as u32;
    stream.write_all(&len.to_be_bytes()).await?;
    stream.write_all(&bytes).await?;
    stream.flush().await?;

    Ok(())
}

/// Read a message with length prefix (legacy version for TcpStream)
#[allow(dead_code)]
async fn read_message(
    stream: &mut TcpStream,
) -> Result<Option<ReplicationMessage>, PassiveError> {
    read_message_generic(stream).await
}

/// Read a message with length prefix (generic version)
async fn read_message_generic<S>(stream: &mut S) -> Result<Option<ReplicationMessage>, PassiveError>
where
    S: AsyncRead + Unpin,
{
    let mut len_buf = [0u8; 4];
    match stream.read_exact(&mut len_buf).await {
        Ok(_) => {}
        Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
            return Ok(None);
        }
        Err(e) => return Err(e.into()),
    }

    let len = u32::from_be_bytes(len_buf) as usize;
    if len > super::protocol::MAX_MESSAGE_SIZE {
        return Err(PassiveError::Protocol(format!(
            "Message too large: {} bytes",
            len
        )));
    }

    let mut buf = vec![0u8; len];
    stream.read_exact(&mut buf).await?;

    let msg = serde_json::from_slice(&buf)
        .map_err(|e| PassiveError::Serialization(e.to_string()))?;

    Ok(Some(msg))
}

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

    #[test]
    fn test_passive_receiver_creation() {
        let config = PassiveConfig::default();
        let integrity = StateIntegrity::new();
        let validators = HashMap::new();

        let (receiver, _handle) = PassiveReceiver::new(config, integrity, validators);

        assert!(!receiver.is_connected());
        assert_eq!(receiver.replication_lag(), 0);
    }
}