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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Per-validator safety state management
//!
//! Tracks signing history to prevent slashable signatures.
//! Supports multiple blockchain networks: Ethereum, Cosmos.

use crate::policy::types::ChainType;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::BTreeMap;

/// Serialize a fixed-size byte array as hex
fn serialize_bytes<S, const N: usize>(bytes: &[u8; N], serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_str(&hex::encode(bytes))
}

/// Deserialize a fixed-size byte array from hex
fn deserialize_bytes<'de, D, const N: usize>(deserializer: D) -> Result<[u8; N], D::Error>
where
    D: Deserializer<'de>,
{
    let s: String = Deserialize::deserialize(deserializer)?;
    let s = s.strip_prefix("0x").unwrap_or(&s);
    let bytes = hex::decode(s).map_err(serde::de::Error::custom)?;
    if bytes.len() != N {
        return Err(serde::de::Error::custom(format!(
            "expected {} bytes, got {}",
            N,
            bytes.len()
        )));
    }
    let mut arr = [0u8; N];
    arr.copy_from_slice(&bytes);
    Ok(arr)
}

/// Safety state for a single validator
///
/// Supports multiple chain types (Ethereum, Cosmos) with chain-specific state.
/// For backward compatibility, checkpoints without chain_state are assumed to be Ethereum.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidatorState {
    /// Validator's public key (48 bytes for BLS, 32 bytes for Ed25519)
    /// For Ethereum: 48-byte BLS public key
    /// For Cosmos: 32-byte Ed25519 public key (padded to 48 bytes for compatibility)
    #[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
    pub pubkey: [u8; 48],

    // Legacy fields for backward compatibility with existing checkpoints
    // These are used when chain_state is None (legacy Ethereum-only format)
    /// Highest slot for which a block was signed (Ethereum legacy)
    #[serde(default)]
    pub last_signed_block_slot: Option<u64>,

    /// Map of slot -> signing_root for block proposals (Ethereum legacy)
    #[serde(default)]
    pub block_signing_roots: BTreeMap<u64, [u8; 32]>,

    /// Highest source epoch in any signed attestation (Ethereum legacy)
    #[serde(default)]
    pub highest_source_epoch: u64,

    /// Highest target epoch in any signed attestation (Ethereum legacy)
    #[serde(default)]
    pub highest_target_epoch: u64,

    /// Attestation history for surround vote detection (Ethereum legacy)
    #[serde(default)]
    pub attestation_history: AttestationHistory,

    // New multi-chain field
    /// Chain-specific state (new format)
    /// If None, uses legacy Ethereum fields above
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chain_state: Option<ChainState>,
}

impl ValidatorState {
    /// Create a new Ethereum validator state with the given public key
    pub fn new(pubkey: [u8; 48]) -> Self {
        Self {
            pubkey,
            last_signed_block_slot: None,
            block_signing_roots: BTreeMap::new(),
            highest_source_epoch: 0,
            highest_target_epoch: 0,
            attestation_history: AttestationHistory::new(),
            chain_state: None, // Use legacy format for Ethereum by default
        }
    }

    /// Create a new Ethereum validator state (explicit)
    pub fn new_ethereum(pubkey: [u8; 48]) -> Self {
        Self {
            pubkey,
            last_signed_block_slot: None,
            block_signing_roots: BTreeMap::new(),
            highest_source_epoch: 0,
            highest_target_epoch: 0,
            attestation_history: AttestationHistory::new(),
            chain_state: Some(ChainState::Ethereum(EthereumState::new())),
        }
    }

    /// Create a new Cosmos validator state
    ///
    /// Note: Ed25519 pubkeys are 32 bytes, so they are zero-padded to 48 bytes
    pub fn new_cosmos(pubkey: [u8; 32]) -> Self {
        let mut full_pubkey = [0u8; 48];
        full_pubkey[..32].copy_from_slice(&pubkey);

        Self {
            pubkey: full_pubkey,
            // Legacy fields not used for Cosmos
            last_signed_block_slot: None,
            block_signing_roots: BTreeMap::new(),
            highest_source_epoch: 0,
            highest_target_epoch: 0,
            attestation_history: AttestationHistory::new(),
            chain_state: Some(ChainState::Cosmos(CosmosState::new())),
        }
    }

    /// Get the chain type for this validator
    pub fn chain_type(&self) -> ChainType {
        match &self.chain_state {
            Some(cs) => cs.chain_type(),
            None => ChainType::Ethereum, // Legacy format is Ethereum
        }
    }

    /// Get the Ethereum state for this validator
    ///
    /// Returns the chain_state if it's Ethereum, otherwise uses legacy fields
    pub fn ethereum_state(&self) -> Option<EthereumState> {
        match &self.chain_state {
            Some(ChainState::Ethereum(state)) => Some(state.clone()),
            Some(ChainState::Cosmos(_)) => None,
            None => {
                // Legacy format - construct EthereumState from legacy fields
                Some(EthereumState {
                    last_signed_block_slot: self.last_signed_block_slot,
                    block_signing_roots: self.block_signing_roots.clone(),
                    highest_source_epoch: self.highest_source_epoch,
                    highest_target_epoch: self.highest_target_epoch,
                    attestation_history: self.attestation_history.clone(),
                })
            }
        }
    }

    /// Get the Cosmos state for this validator
    pub fn cosmos_state(&self) -> Option<&CosmosState> {
        match &self.chain_state {
            Some(ChainState::Cosmos(state)) => Some(state),
            _ => None,
        }
    }

    /// Get the Cosmos state mutably
    pub fn cosmos_state_mut(&mut self) -> Option<&mut CosmosState> {
        match &mut self.chain_state {
            Some(ChainState::Cosmos(state)) => Some(state),
            _ => None,
        }
    }

    // ========================================================================
    // Ethereum-specific methods (for backward compatibility)
    // ========================================================================

    /// Get the signing root for a block at the given slot, if any
    pub fn get_block_signing_root(&self, slot: u64) -> Option<&[u8; 32]> {
        match &self.chain_state {
            Some(ChainState::Ethereum(state)) => state.block_signing_roots.get(&slot),
            Some(ChainState::Cosmos(_)) => None,
            None => self.block_signing_roots.get(&slot),
        }
    }

    /// Record that a block was signed for the given slot
    pub fn record_block_signing(&mut self, slot: u64, signing_root: [u8; 32]) {
        match &mut self.chain_state {
            Some(ChainState::Ethereum(state)) => {
                state.record_block_signing(slot, signing_root);
            }
            Some(ChainState::Cosmos(_)) => {
                // No-op for Cosmos validators
            }
            None => {
                // Legacy format
                self.block_signing_roots.insert(slot, signing_root);
                if self.last_signed_block_slot.is_none_or(|s| slot > s) {
                    self.last_signed_block_slot = Some(slot);
                }
            }
        }
    }

    /// Get the signing root for an attestation with the given source/target, if any
    pub fn get_attestation_signing_root(
        &self,
        source_epoch: u64,
        target_epoch: u64,
    ) -> Option<&[u8; 32]> {
        match &self.chain_state {
            Some(ChainState::Ethereum(state)) => {
                state.attestation_history.get_signing_root(source_epoch, target_epoch)
            }
            Some(ChainState::Cosmos(_)) => None,
            None => self.attestation_history.get_signing_root(source_epoch, target_epoch),
        }
    }

    /// Record that an attestation was signed
    pub fn record_attestation_signing(
        &mut self,
        source_epoch: u64,
        target_epoch: u64,
        signing_root: [u8; 32],
    ) {
        match &mut self.chain_state {
            Some(ChainState::Ethereum(state)) => {
                state.record_attestation_signing(source_epoch, target_epoch, signing_root);
            }
            Some(ChainState::Cosmos(_)) => {
                // No-op for Cosmos validators
            }
            None => {
                // Legacy format
                self.attestation_history.record(source_epoch, target_epoch, signing_root);
                if source_epoch > self.highest_source_epoch {
                    self.highest_source_epoch = source_epoch;
                }
                if target_epoch > self.highest_target_epoch {
                    self.highest_target_epoch = target_epoch;
                }
            }
        }
    }

    /// Prune old entries to limit memory usage
    /// Keeps entries within the weak subjectivity period
    pub fn prune(&mut self, min_slot: u64, min_epoch: u64) {
        match &mut self.chain_state {
            Some(ChainState::Ethereum(state)) => {
                state.prune(min_slot, min_epoch);
            }
            Some(ChainState::Cosmos(state)) => {
                // For Cosmos, use min_slot as min_height (they're similar concepts)
                state.prune(min_slot as i64);
            }
            None => {
                // Legacy format
                self.block_signing_roots = self.block_signing_roots.split_off(&min_slot);
                self.attestation_history.prune(min_epoch);
            }
        }
    }
}

/// History of signed attestations for surround vote detection
///
/// Uses min-span and max-span data structures for efficient detection
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AttestationHistory {
    /// Map of (source_epoch, target_epoch) -> signing_root
    /// Used for double vote detection and idempotent re-signing
    signed_attestations: BTreeMap<(u64, u64), [u8; 32]>,

    /// For each source epoch, track the minimum target epoch seen
    /// Used to detect if a new attestation surrounds an existing one
    min_target_by_source: BTreeMap<u64, u64>,

    /// For each source epoch, track the maximum target epoch seen
    /// Used to detect if a new attestation is surrounded by an existing one
    max_target_by_source: BTreeMap<u64, u64>,
}

impl AttestationHistory {
    /// Create a new empty attestation history
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the signing root for an attestation, if it exists
    pub fn get_signing_root(&self, source_epoch: u64, target_epoch: u64) -> Option<&[u8; 32]> {
        self.signed_attestations.get(&(source_epoch, target_epoch))
    }

    /// Iterate over all signed attestations
    pub fn iter(&self) -> impl Iterator<Item = ((u64, u64), &[u8; 32])> {
        self.signed_attestations.iter().map(|(&k, v)| (k, v))
    }

    /// Record a new signed attestation
    pub fn record(&mut self, source_epoch: u64, target_epoch: u64, signing_root: [u8; 32]) {
        self.signed_attestations
            .insert((source_epoch, target_epoch), signing_root);

        // Update min target for this source
        self.min_target_by_source
            .entry(source_epoch)
            .and_modify(|t| {
                if target_epoch < *t {
                    *t = target_epoch;
                }
            })
            .or_insert(target_epoch);

        // Update max target for this source
        self.max_target_by_source
            .entry(source_epoch)
            .and_modify(|t| {
                if target_epoch > *t {
                    *t = target_epoch;
                }
            })
            .or_insert(target_epoch);
    }

    /// Get the minimum target epoch for any attestation with source > given source
    /// Used to detect surrounding votes
    pub fn get_min_target_for_source_gt(&self, source_epoch: u64) -> Option<u64> {
        // Find all sources greater than the given source
        // and return the minimum target among them
        self.min_target_by_source
            .range((source_epoch + 1)..)
            .map(|(_, &target)| target)
            .min()
    }

    /// Get the maximum target epoch for any attestation with source < given source
    /// Used to detect surrounded votes
    pub fn get_max_target_for_source_lt(&self, source_epoch: u64) -> Option<u64> {
        // Find all sources less than the given source
        // and return the maximum target among them
        self.max_target_by_source
            .range(..source_epoch)
            .map(|(_, &target)| target)
            .max()
    }

    /// Prune entries older than the given epoch
    pub fn prune(&mut self, min_epoch: u64) {
        self.signed_attestations
            .retain(|(source, _), _| *source >= min_epoch);
        self.min_target_by_source
            .retain(|source, _| *source >= min_epoch);
        self.max_target_by_source
            .retain(|source, _| *source >= min_epoch);
    }
}

// ============================================================================
// Cosmos/CometBFT State
// ============================================================================

/// Signed message type in Tendermint consensus
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum CosmosSignedMsgType {
    /// Prevote (0x01)
    Prevote,
    /// Precommit (0x02)
    Precommit,
    /// Proposal (0x20)
    Proposal,
}

/// Information about a signed vote in Cosmos
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CosmosSignedVote {
    /// Hash of the block that was signed (None = nil vote)
    pub block_hash: Option<[u8; 32]>,
    /// When the signing occurred (unix timestamp)
    pub signed_at: u64,
}

/// Cosmos/CometBFT-specific validator state
///
/// Tracks signed votes to prevent double-signing at the same (height, round, type).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CosmosState {
    /// Chain ID this validator is bound to
    pub chain_id: Option<String>,

    /// Map of (height, round, type) -> signed vote info
    /// Only stores the signature for each (height, round, type) combination
    signed_votes: BTreeMap<(i64, i32, CosmosSignedMsgType), CosmosSignedVote>,

    /// Highest height we've signed at
    pub highest_height: i64,
}

impl CosmosState {
    /// Create a new empty Cosmos state
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the signed vote at a specific (height, round, type), if any
    pub fn get_signed_vote(
        &self,
        height: i64,
        round: i32,
        msg_type: CosmosSignedMsgType,
    ) -> Option<&CosmosSignedVote> {
        self.signed_votes.get(&(height, round, msg_type))
    }

    /// Record a new signed vote
    pub fn record_vote(
        &mut self,
        height: i64,
        round: i32,
        msg_type: CosmosSignedMsgType,
        block_hash: Option<[u8; 32]>,
    ) {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        self.signed_votes.insert(
            (height, round, msg_type),
            CosmosSignedVote {
                block_hash,
                signed_at: now,
            },
        );

        if height > self.highest_height {
            self.highest_height = height;
        }
    }

    /// Prune old entries to limit memory usage
    ///
    /// Keeps entries within the specified height range
    pub fn prune(&mut self, min_height: i64) {
        self.signed_votes.retain(|(height, _, _), _| *height >= min_height);
    }

    /// Get the number of tracked votes
    pub fn len(&self) -> usize {
        self.signed_votes.len()
    }

    /// Check if the state is empty
    pub fn is_empty(&self) -> bool {
        self.signed_votes.is_empty()
    }
}

// ============================================================================
// Chain-agnostic state wrapper
// ============================================================================

/// Chain-specific state variants
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "chain_type")]
pub enum ChainState {
    /// Ethereum Beacon Chain state
    Ethereum(EthereumState),
    /// Cosmos/CometBFT state
    Cosmos(CosmosState),
}

impl ChainState {
    /// Get the chain type for this state
    pub fn chain_type(&self) -> ChainType {
        match self {
            ChainState::Ethereum(_) => ChainType::Ethereum,
            ChainState::Cosmos(_) => ChainType::Cosmos,
        }
    }

    /// Get the Ethereum state if this is an Ethereum validator
    pub fn as_ethereum(&self) -> Option<&EthereumState> {
        match self {
            ChainState::Ethereum(state) => Some(state),
            _ => None,
        }
    }

    /// Get the Ethereum state mutably if this is an Ethereum validator
    pub fn as_ethereum_mut(&mut self) -> Option<&mut EthereumState> {
        match self {
            ChainState::Ethereum(state) => Some(state),
            _ => None,
        }
    }

    /// Get the Cosmos state if this is a Cosmos validator
    pub fn as_cosmos(&self) -> Option<&CosmosState> {
        match self {
            ChainState::Cosmos(state) => Some(state),
            _ => None,
        }
    }

    /// Get the Cosmos state mutably if this is a Cosmos validator
    pub fn as_cosmos_mut(&mut self) -> Option<&mut CosmosState> {
        match self {
            ChainState::Cosmos(state) => Some(state),
            _ => None,
        }
    }
}

/// Ethereum-specific validator state
///
/// This is the existing ValidatorState fields extracted for multi-chain support.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EthereumState {
    /// Highest slot for which a block was signed
    pub last_signed_block_slot: Option<u64>,

    /// Map of slot -> signing_root for block proposals
    /// Used to allow idempotent re-signing of the same block
    pub block_signing_roots: BTreeMap<u64, [u8; 32]>,

    /// Highest source epoch in any signed attestation
    pub highest_source_epoch: u64,

    /// Highest target epoch in any signed attestation
    pub highest_target_epoch: u64,

    /// Attestation history for surround vote detection
    pub attestation_history: AttestationHistory,
}

impl Default for EthereumState {
    fn default() -> Self {
        Self::new()
    }
}

impl EthereumState {
    /// Create a new empty Ethereum state
    pub fn new() -> Self {
        Self {
            last_signed_block_slot: None,
            block_signing_roots: BTreeMap::new(),
            highest_source_epoch: 0,
            highest_target_epoch: 0,
            attestation_history: AttestationHistory::new(),
        }
    }

    /// Get the signing root for a block at the given slot, if any
    pub fn get_block_signing_root(&self, slot: u64) -> Option<&[u8; 32]> {
        self.block_signing_roots.get(&slot)
    }

    /// Record that a block was signed for the given slot
    pub fn record_block_signing(&mut self, slot: u64, signing_root: [u8; 32]) {
        self.block_signing_roots.insert(slot, signing_root);
        if self.last_signed_block_slot.is_none_or(|s| slot > s) {
            self.last_signed_block_slot = Some(slot);
        }
    }

    /// Get the signing root for an attestation with the given source/target, if any
    pub fn get_attestation_signing_root(
        &self,
        source_epoch: u64,
        target_epoch: u64,
    ) -> Option<&[u8; 32]> {
        self.attestation_history
            .get_signing_root(source_epoch, target_epoch)
    }

    /// Record that an attestation was signed
    pub fn record_attestation_signing(
        &mut self,
        source_epoch: u64,
        target_epoch: u64,
        signing_root: [u8; 32],
    ) {
        self.attestation_history
            .record(source_epoch, target_epoch, signing_root);

        if source_epoch > self.highest_source_epoch {
            self.highest_source_epoch = source_epoch;
        }
        if target_epoch > self.highest_target_epoch {
            self.highest_target_epoch = target_epoch;
        }
    }

    /// Prune old entries to limit memory usage
    pub fn prune(&mut self, min_slot: u64, min_epoch: u64) {
        // Prune block signing roots older than min_slot
        self.block_signing_roots = self.block_signing_roots.split_off(&min_slot);

        // Prune attestation history
        self.attestation_history.prune(min_epoch);
    }
}

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

    fn make_root(val: u8) -> [u8; 32] {
        let mut root = [0u8; 32];
        root[0] = val;
        root
    }

    #[test]
    fn test_validator_state_new() {
        let pubkey = [1u8; 48];
        let state = ValidatorState::new(pubkey);

        assert_eq!(state.pubkey, pubkey);
        assert_eq!(state.last_signed_block_slot, None);
        assert!(state.block_signing_roots.is_empty());
        assert_eq!(state.highest_source_epoch, 0);
        assert_eq!(state.highest_target_epoch, 0);
    }

    #[test]
    fn test_block_signing() {
        let mut state = ValidatorState::new([0u8; 48]);
        let root = make_root(1);

        assert!(state.get_block_signing_root(100).is_none());

        state.record_block_signing(100, root);

        assert_eq!(state.get_block_signing_root(100), Some(&root));
        assert_eq!(state.last_signed_block_slot, Some(100));
    }

    #[test]
    fn test_attestation_signing() {
        let mut state = ValidatorState::new([0u8; 48]);
        let root = make_root(1);

        assert!(state.get_attestation_signing_root(10, 11).is_none());

        state.record_attestation_signing(10, 11, root);

        assert_eq!(state.get_attestation_signing_root(10, 11), Some(&root));
        assert_eq!(state.highest_source_epoch, 10);
        assert_eq!(state.highest_target_epoch, 11);
    }

    #[test]
    fn test_surround_detection_spans() {
        let mut history = AttestationHistory::new();

        // Record attestation (5, 10)
        history.record(5, 10, make_root(1));

        // Check spans
        // For source > 5, min target should be 10
        assert_eq!(history.get_min_target_for_source_gt(4), Some(10));
        assert_eq!(history.get_min_target_for_source_gt(5), None);

        // For source < 5, max target should be None (nothing recorded)
        assert_eq!(history.get_max_target_for_source_lt(5), None);

        // Record another attestation (3, 12)
        history.record(3, 12, make_root(2));

        // Now for source < 5, max target should be 12
        assert_eq!(history.get_max_target_for_source_lt(5), Some(12));
    }
}