moloch-chain 0.1.0

Chain operations for Moloch audit chain
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
//! Validator registry for PoA consensus.
//!
//! Manages the set of authorized block producers:
//! - Ordered list of validators
//! - Round-robin leader selection
//! - Validator rotation (add/remove)

use moloch_core::{block::SealerId, Hash};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashSet;

/// A set of validators authorized to produce blocks.
#[derive(Debug, Clone, Serialize)]
pub struct ValidatorSet {
    /// Ordered list of validators.
    validators: Vec<SealerId>,
    /// Set for O(1) lookup (rebuilt on deserialize).
    #[serde(skip)]
    lookup: HashSet<SealerId>,
}

// Custom Deserialize to rebuild lookup HashSet
impl<'de> Deserialize<'de> for ValidatorSet {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // Deserialize just the validators vec
        #[derive(Deserialize)]
        struct ValidatorSetData {
            validators: Vec<SealerId>,
        }

        let data = ValidatorSetData::deserialize(deserializer)?;
        let lookup = data.validators.iter().cloned().collect();

        Ok(ValidatorSet {
            validators: data.validators,
            lookup,
        })
    }
}

impl ValidatorSet {
    /// Create a new validator set.
    pub fn new(validators: Vec<SealerId>) -> Self {
        let lookup = validators.iter().cloned().collect();
        Self { validators, lookup }
    }

    /// Create an empty validator set.
    pub fn empty() -> Self {
        Self {
            validators: Vec::new(),
            lookup: HashSet::new(),
        }
    }

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

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

    /// Check if a sealer is a valid validator.
    pub fn contains(&self, sealer: &SealerId) -> bool {
        self.lookup.contains(sealer)
    }

    /// Get the leader for a given round (round-robin).
    ///
    /// Returns None if the set is empty.
    pub fn leader_for_round(&self, round: u64) -> Option<&SealerId> {
        if self.validators.is_empty() {
            return None;
        }
        let idx = (round as usize) % self.validators.len();
        Some(&self.validators[idx])
    }

    /// Get the leader for a given block height.
    ///
    /// This is the same as `leader_for_round` but more semantically clear
    /// when used in block production context.
    pub fn leader_for_height(&self, height: u64) -> Option<&SealerId> {
        self.leader_for_round(height)
    }

    /// Get validator at index.
    pub fn get(&self, index: usize) -> Option<&SealerId> {
        self.validators.get(index)
    }

    /// Get all validators.
    pub fn validators(&self) -> &[SealerId] {
        &self.validators
    }

    /// Iterator over validators.
    pub fn iter(&self) -> impl Iterator<Item = &SealerId> {
        self.validators.iter()
    }

    /// Add a validator to the set.
    ///
    /// Returns true if the validator was added, false if already present.
    pub fn add(&mut self, validator: SealerId) -> bool {
        if self.lookup.contains(&validator) {
            return false;
        }
        self.lookup.insert(validator.clone());
        self.validators.push(validator);
        true
    }

    /// Remove a validator from the set.
    ///
    /// Returns true if the validator was removed, false if not present.
    pub fn remove(&mut self, validator: &SealerId) -> bool {
        if !self.lookup.remove(validator) {
            return false;
        }
        self.validators.retain(|v| v != validator);
        true
    }

    /// Compute a commitment hash of the validator set.
    ///
    /// This can be included in block headers for validator set verification.
    pub fn commitment(&self) -> Hash {
        let mut data = Vec::new();
        for v in &self.validators {
            data.extend_from_slice(&v.as_pubkey().as_bytes());
        }
        moloch_core::hash(&data)
    }

    /// Check if this validator set has supermajority (2/3+) of another set.
    ///
    /// Used for validator set transitions.
    pub fn has_supermajority_of(&self, other: &ValidatorSet) -> bool {
        if other.is_empty() {
            return true;
        }
        let common = self.validators.iter().filter(|v| other.contains(v)).count();
        // 2/3 majority
        common * 3 > other.len() * 2
    }

    /// Get the threshold for supermajority (2/3 + 1).
    pub fn supermajority_threshold(&self) -> usize {
        if self.validators.is_empty() {
            return 0;
        }
        (self.validators.len() * 2 / 3) + 1
    }

    /// Get the threshold for simple majority (1/2 + 1).
    pub fn majority_threshold(&self) -> usize {
        if self.validators.is_empty() {
            return 0;
        }
        (self.validators.len() / 2) + 1
    }
}

impl PartialEq for ValidatorSet {
    fn eq(&self, other: &Self) -> bool {
        self.validators == other.validators
    }
}

impl Eq for ValidatorSet {}

impl std::hash::Hash for ValidatorSet {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.validators.hash(state);
    }
}

/// A pending validator set change.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidatorChange {
    /// The change type.
    pub kind: ValidatorChangeKind,
    /// The validator being added or removed.
    pub validator: SealerId,
    /// Height at which this change takes effect.
    pub effective_height: u64,
}

/// Type of validator set change.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ValidatorChangeKind {
    /// Add a new validator.
    Add,
    /// Remove an existing validator.
    Remove,
}

/// Evidence of validator misbehavior (for slashing).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlashingEvidence {
    /// The misbehaving validator.
    pub validator: SealerId,
    /// The type of misbehavior.
    pub kind: MisbehaviorKind,
    /// Height at which misbehavior occurred.
    pub height: u64,
    /// First conflicting block/vote.
    pub evidence_a: Vec<u8>,
    /// Second conflicting block/vote (for double-sign).
    pub evidence_b: Option<Vec<u8>>,
}

/// Types of validator misbehavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MisbehaviorKind {
    /// Signed two different blocks at the same height.
    DoubleSign,
    /// Produced block out of turn.
    OutOfTurn,
    /// Included invalid transaction.
    InvalidBlock,
}

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

    fn make_sealer() -> SealerId {
        let key = SecretKey::generate();
        SealerId::new(key.public_key())
    }

    #[test]
    fn test_validator_set_new() {
        let v1 = make_sealer();
        let v2 = make_sealer();
        let set = ValidatorSet::new(vec![v1.clone(), v2.clone()]);

        assert_eq!(set.len(), 2);
        assert!(set.contains(&v1));
        assert!(set.contains(&v2));
    }

    #[test]
    fn test_validator_set_empty() {
        let set = ValidatorSet::empty();
        assert!(set.is_empty());
        assert_eq!(set.len(), 0);
    }

    #[test]
    fn test_round_robin_leader() {
        let v1 = make_sealer();
        let v2 = make_sealer();
        let v3 = make_sealer();
        let set = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);

        assert_eq!(set.leader_for_round(0), Some(&v1));
        assert_eq!(set.leader_for_round(1), Some(&v2));
        assert_eq!(set.leader_for_round(2), Some(&v3));
        assert_eq!(set.leader_for_round(3), Some(&v1)); // Wraps around
        assert_eq!(set.leader_for_round(4), Some(&v2));
    }

    #[test]
    fn test_leader_for_height() {
        let v1 = make_sealer();
        let v2 = make_sealer();
        let set = ValidatorSet::new(vec![v1.clone(), v2.clone()]);

        assert_eq!(set.leader_for_height(0), Some(&v1));
        assert_eq!(set.leader_for_height(1), Some(&v2));
        assert_eq!(set.leader_for_height(100), Some(&v1));
        assert_eq!(set.leader_for_height(101), Some(&v2));
    }

    #[test]
    fn test_add_validator() {
        let v1 = make_sealer();
        let v2 = make_sealer();
        let mut set = ValidatorSet::new(vec![v1.clone()]);

        assert!(!set.contains(&v2));
        assert!(set.add(v2.clone()));
        assert!(set.contains(&v2));
        assert_eq!(set.len(), 2);

        // Adding again returns false
        assert!(!set.add(v2));
    }

    #[test]
    fn test_remove_validator() {
        let v1 = make_sealer();
        let v2 = make_sealer();
        let mut set = ValidatorSet::new(vec![v1.clone(), v2.clone()]);

        assert!(set.remove(&v1));
        assert!(!set.contains(&v1));
        assert_eq!(set.len(), 1);

        // Removing again returns false
        assert!(!set.remove(&v1));
    }

    #[test]
    fn test_commitment_deterministic() {
        let v1 = make_sealer();
        let v2 = make_sealer();

        let set1 = ValidatorSet::new(vec![v1.clone(), v2.clone()]);
        let set2 = ValidatorSet::new(vec![v1.clone(), v2.clone()]);

        assert_eq!(set1.commitment(), set2.commitment());
    }

    #[test]
    fn test_commitment_order_matters() {
        let v1 = make_sealer();
        let v2 = make_sealer();

        let set1 = ValidatorSet::new(vec![v1.clone(), v2.clone()]);
        let set2 = ValidatorSet::new(vec![v2, v1]);

        assert_ne!(set1.commitment(), set2.commitment());
    }

    #[test]
    fn test_supermajority() {
        let validators: Vec<_> = (0..9).map(|_| make_sealer()).collect();
        let full_set = ValidatorSet::new(validators.clone());

        // 7 of 9 = 77% > 66%
        let partial = ValidatorSet::new(validators[0..7].to_vec());
        assert!(partial.has_supermajority_of(&full_set));

        // 6 of 9 = 66% = 66% (not strictly greater)
        let partial = ValidatorSet::new(validators[0..6].to_vec());
        assert!(!partial.has_supermajority_of(&full_set));

        // 5 of 9 = 55% < 66%
        let partial = ValidatorSet::new(validators[0..5].to_vec());
        assert!(!partial.has_supermajority_of(&full_set));
    }

    #[test]
    fn test_thresholds() {
        let validators: Vec<_> = (0..9).map(|_| make_sealer()).collect();
        let set = ValidatorSet::new(validators);

        // 9 validators: 2/3 + 1 = 6 + 1 = 7
        assert_eq!(set.supermajority_threshold(), 7);
        // 9 validators: 1/2 + 1 = 4 + 1 = 5
        assert_eq!(set.majority_threshold(), 5);
    }

    #[test]
    fn test_thresholds_small_set() {
        let v1 = make_sealer();
        let set = ValidatorSet::new(vec![v1]);

        assert_eq!(set.supermajority_threshold(), 1);
        assert_eq!(set.majority_threshold(), 1);
    }

    #[test]
    fn test_empty_set_no_leader() {
        let set = ValidatorSet::empty();
        assert!(set.leader_for_round(0).is_none());
        assert!(set.leader_for_height(100).is_none());
    }

    #[test]
    fn test_serde_roundtrip_rebuilds_lookup() {
        let v1 = make_sealer();
        let v2 = make_sealer();
        let v3 = make_sealer();
        let set = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);

        // Serialize
        let bytes = bincode::serialize(&set).expect("serialize should work");

        // Deserialize
        let restored: ValidatorSet = bincode::deserialize(&bytes).expect("deserialize should work");

        // Verify lookup was rebuilt (contains() uses lookup)
        assert!(restored.contains(&v1));
        assert!(restored.contains(&v2));
        assert!(restored.contains(&v3));
        assert_eq!(restored.len(), 3);

        // Verify order preserved
        assert_eq!(restored.validators(), set.validators());
    }
}