moloch-core 0.1.0

Core types and primitives 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
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
//! Block types for Moloch.
//!
//! Blocks batch audit events together and link to form the chain.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::crypto::{hash, hash_pair, Hash, PublicKey, SecretKey, Sig};
use crate::error::{Error, Result};
use crate::event::{AuditEvent, EventId};

/// Unique identifier for a block (hash of header).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct BlockHash(pub Hash);

impl BlockHash {
    /// The zero block hash (used for genesis parent).
    pub const ZERO: Self = Self(Hash::ZERO);

    /// Get the underlying hash.
    pub fn as_hash(&self) -> &Hash {
        &self.0
    }

    /// Create a block hash from a header.
    pub fn from_header(header: &BlockHeader) -> Self {
        header.hash()
    }
}

impl std::fmt::Display for BlockHash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Identifies a sealer (block producer).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SealerId {
    /// Sealer's public key.
    pub key: PublicKey,
    /// Human-readable name.
    pub name: Option<String>,
}

impl SealerId {
    /// Create a new sealer ID.
    pub fn new(key: PublicKey) -> Self {
        Self { key, name: None }
    }

    /// Add a display name.
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Get the unique hash ID for this sealer.
    pub fn id(&self) -> Hash {
        self.key.id()
    }

    /// Get the public key.
    pub fn as_pubkey(&self) -> &PublicKey {
        &self.key
    }
}

/// Block header containing metadata and proofs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockHeader {
    /// Block height (monotonically increasing).
    pub height: u64,

    /// Hash of parent block header.
    pub parent: BlockHash,

    /// Merkle root of events in this block.
    pub events_root: Hash,

    /// Number of events in this block.
    pub events_count: u32,

    /// MMR root after this block (cumulative chain state).
    pub mmr_root: Hash,

    /// Sealer's timestamp (must be >= parent timestamp), as Unix millis.
    #[serde(with = "chrono::serde::ts_milliseconds")]
    pub timestamp: DateTime<Utc>,

    /// Identity of the sealer who produced this block.
    pub sealer: SealerId,

    /// Sealer's signature over the header (excluding this field).
    pub seal: Sig,
}

impl BlockHeader {
    /// Get the bytes to be signed (everything except the seal).
    pub fn signing_bytes(&self) -> Vec<u8> {
        let signable = SignableHeader {
            height: self.height,
            parent: &self.parent,
            events_root: &self.events_root,
            events_count: self.events_count,
            mmr_root: &self.mmr_root,
            timestamp: self.timestamp,
            sealer: &self.sealer,
        };
        bincode::serialize(&signable).expect("serialization should not fail")
    }

    /// Compute the hash of this header.
    pub fn hash(&self) -> BlockHash {
        BlockHash(hash(&self.signing_bytes()))
    }

    /// Create a new header with the given seal.
    pub fn with_seal(mut self, seal: Sig) -> Self {
        self.seal = seal;
        self
    }

    /// Verify the seal signature.
    pub fn verify_seal(&self) -> Result<()> {
        self.sealer
            .key
            .verify(&self.signing_bytes(), &self.seal)
            .map_err(|_| Error::invalid_block("invalid seal signature"))
    }

    /// Validate this header against its parent.
    pub fn validate(&self, parent: Option<&BlockHeader>) -> Result<()> {
        // Verify seal
        self.verify_seal()?;

        match parent {
            Some(p) => {
                // Height must be parent + 1
                if self.height != p.height + 1 {
                    return Err(Error::invalid_block(format!(
                        "height {} should be {}",
                        self.height,
                        p.height + 1
                    )));
                }

                // Parent hash must match
                if self.parent != p.hash() {
                    return Err(Error::invalid_block("parent hash mismatch"));
                }

                // Timestamp must be >= parent
                if self.timestamp < p.timestamp {
                    return Err(Error::invalid_block("timestamp before parent"));
                }
            }
            None => {
                // Genesis block
                if self.height != 0 {
                    return Err(Error::invalid_block("genesis must have height 0"));
                }
                if self.parent != BlockHash::ZERO {
                    return Err(Error::invalid_block("genesis must have zero parent"));
                }
            }
        }

        Ok(())
    }
}

/// Helper for signing (excludes seal field).
#[derive(Serialize)]
struct SignableHeader<'a> {
    height: u64,
    parent: &'a BlockHash,
    events_root: &'a Hash,
    events_count: u32,
    mmr_root: &'a Hash,
    #[serde(with = "chrono::serde::ts_milliseconds")]
    timestamp: DateTime<Utc>,
    sealer: &'a SealerId,
}

/// A complete block with header and events.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Block {
    pub header: BlockHeader,
    pub events: Vec<AuditEvent>,
}

impl Block {
    /// Get the block hash.
    pub fn hash(&self) -> BlockHash {
        self.header.hash()
    }

    /// Validate the block (sequential event verification).
    ///
    /// For better performance with many events, use `validate_batch()` instead.
    pub fn validate(&self, parent: Option<&BlockHeader>) -> Result<()> {
        // Validate header
        self.header.validate(parent)?;

        // Validate events count matches
        if self.events.len() as u32 != self.header.events_count {
            return Err(Error::invalid_block(format!(
                "events count mismatch: {} vs {}",
                self.events.len(),
                self.header.events_count
            )));
        }

        // Validate events merkle root
        let computed_root = compute_events_root(&self.events);
        if computed_root != self.header.events_root {
            return Err(Error::invalid_block("events root mismatch"));
        }

        // Validate each event
        for event in &self.events {
            event.validate()?;
        }

        Ok(())
    }

    /// Validate the block using batch signature verification.
    ///
    /// This is 3-8x faster than `validate()` for blocks with many events,
    /// using Arcanum's optimized Ed25519 batch verifier.
    ///
    /// # Performance
    /// - 10 events: ~2x faster
    /// - 100 events: ~4x faster
    /// - 1000 events: ~6-8x faster
    pub fn validate_batch(&self, parent: Option<&BlockHeader>) -> Result<()> {
        // Validate header
        self.header.validate(parent)?;

        // Validate events count matches
        if self.events.len() as u32 != self.header.events_count {
            return Err(Error::invalid_block(format!(
                "events count mismatch: {} vs {}",
                self.events.len(),
                self.header.events_count
            )));
        }

        // Validate events merkle root
        let computed_root = compute_events_root(&self.events);
        if computed_root != self.header.events_root {
            return Err(Error::invalid_block("events root mismatch"));
        }

        // Batch verify all event signatures at once
        crate::batch_verify_events(&self.events)?;

        // Check event timestamps (can't be batched)
        let now = chrono::Utc::now();
        for event in &self.events {
            if event.event_time > now + chrono::Duration::minutes(5) {
                return Err(Error::invalid_event("event_time is in the future"));
            }
        }

        Ok(())
    }

    /// Validate the block using fully parallel processing.
    ///
    /// Uses batch signature verification (the main speedup) combined with
    /// parallel canonical bytes computation for large blocks (1000+ events).
    ///
    /// Note: Parallel merkle tree is not used because BLAKE3 is already
    /// SIMD-accelerated, making parallelization overhead counterproductive.
    ///
    /// # Performance
    /// - Uses all available CPU cores for signature verification
    /// - 100 events: ~2.5x faster than sequential
    /// - 500 events: ~2.5x faster than sequential
    /// - 1000+ events: ~2.7x faster than sequential (parallel canonical bytes kicks in)
    ///
    /// # When to Use
    /// - Blocks with many events where signature verification dominates
    /// - For bulk validation of historical blocks
    pub fn validate_parallel(&self, parent: Option<&BlockHeader>) -> Result<()> {
        // Validate header (single signature, not worth parallelizing)
        self.header.validate(parent)?;

        // Validate events count matches
        if self.events.len() as u32 != self.header.events_count {
            return Err(Error::invalid_block(format!(
                "events count mismatch: {} vs {}",
                self.events.len(),
                self.header.events_count
            )));
        }

        // Sequential merkle root (faster than parallel due to BLAKE3 SIMD)
        let computed_root = compute_events_root(&self.events);
        if computed_root != self.header.events_root {
            return Err(Error::invalid_block("events root mismatch"));
        }

        // Use parallel canonical bytes for large batches, otherwise batch only
        if self.events.len() >= 1000 {
            crate::batch_verify_events_parallel(&self.events)?;
        } else {
            crate::batch_verify_events(&self.events)?;
        }

        // Check event timestamps (fast, not worth parallelizing)
        let now = chrono::Utc::now();
        for event in &self.events {
            if event.event_time > now + chrono::Duration::minutes(5) {
                return Err(Error::invalid_event("event_time is in the future"));
            }
        }

        Ok(())
    }

    /// Get event IDs in this block.
    pub fn event_ids(&self) -> Vec<EventId> {
        self.events.iter().map(|e| e.id()).collect()
    }
}

/// Compute the merkle root of a list of events.
pub fn compute_events_root(events: &[AuditEvent]) -> Hash {
    if events.is_empty() {
        return Hash::ZERO;
    }

    // Get leaf hashes
    let mut hashes: Vec<Hash> = events.iter().map(|e| e.id().0).collect();

    // Pad to power of 2
    while hashes.len() & (hashes.len() - 1) != 0 {
        hashes.push(*hashes.last().unwrap());
    }

    // Build tree bottom-up
    while hashes.len() > 1 {
        let mut next_level = Vec::with_capacity(hashes.len() / 2);
        for pair in hashes.chunks(2) {
            next_level.push(hash_pair(pair[0], pair[1]));
        }
        hashes = next_level;
    }

    hashes[0]
}

/// Compute the merkle root using parallel hashing.
///
/// Uses Rayon to parallelize:
/// 1. Leaf hash computation (event ID extraction)
/// 2. Each level of the merkle tree (when level is large enough)
///
/// # Performance
/// - 100 events: ~1.2x faster than sequential
/// - 1000 events: ~2-3x faster than sequential
/// - Automatically falls back to sequential for small inputs
pub fn compute_events_root_parallel(events: &[AuditEvent]) -> Hash {
    use rayon::prelude::*;

    if events.is_empty() {
        return Hash::ZERO;
    }

    // Parallel leaf hash extraction
    let mut hashes: Vec<Hash> = events.par_iter().map(|e| e.id().0).collect();

    // Pad to power of 2
    while hashes.len() & (hashes.len() - 1) != 0 {
        hashes.push(*hashes.last().unwrap());
    }

    // Build tree bottom-up with parallel levels for large trees
    while hashes.len() > 1 {
        if hashes.len() >= 64 {
            // Parallel hash pairing for large levels
            let pairs: Vec<_> = hashes.chunks(2).collect();
            hashes = pairs
                .par_iter()
                .map(|pair| hash_pair(pair[0], pair[1]))
                .collect();
        } else {
            // Sequential for small levels (parallelization overhead not worth it)
            let mut next_level = Vec::with_capacity(hashes.len() / 2);
            for pair in hashes.chunks(2) {
                next_level.push(hash_pair(pair[0], pair[1]));
            }
            hashes = next_level;
        }
    }

    hashes[0]
}

/// Builder for creating blocks.
pub struct BlockBuilder {
    parent: Option<BlockHeader>,
    events: Vec<AuditEvent>,
    sealer: SealerId,
    mmr_root: Hash,
}

impl BlockBuilder {
    /// Create a new block builder.
    pub fn new(sealer: SealerId) -> Self {
        Self {
            parent: None,
            events: Vec::new(),
            sealer,
            mmr_root: Hash::ZERO,
        }
    }

    /// Set the parent block.
    pub fn parent(mut self, parent: BlockHeader) -> Self {
        self.parent = Some(parent);
        self
    }

    /// Add events.
    pub fn events(mut self, events: Vec<AuditEvent>) -> Self {
        self.events = events;
        self
    }

    /// Set the MMR root (computed externally).
    pub fn mmr_root(mut self, root: Hash) -> Self {
        self.mmr_root = root;
        self
    }

    /// Build and seal the block.
    pub fn seal(self, key: &SecretKey) -> Block {
        let (height, parent_hash) = match &self.parent {
            Some(p) => (p.height + 1, p.hash()),
            None => (0, BlockHash::ZERO),
        };

        let events_root = compute_events_root(&self.events);
        let events_count = self.events.len() as u32;

        let header = BlockHeader {
            height,
            parent: parent_hash,
            events_root,
            events_count,
            mmr_root: self.mmr_root,
            timestamp: Utc::now(),
            sealer: self.sealer,
            seal: Sig::empty(),
        };

        let seal = key.sign(&header.signing_bytes());
        let header = header.with_seal(seal);

        Block {
            header,
            events: self.events,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crypto::SecretKey;
    use crate::event::{ActorId, ActorKind, EventType, ResourceId, ResourceKind};

    fn test_sealer() -> (SecretKey, SealerId) {
        let key = SecretKey::generate();
        let sealer = SealerId::new(key.public_key()).with_name("test-sealer");
        (key, sealer)
    }

    fn test_event(key: &SecretKey) -> AuditEvent {
        let actor = ActorId::new(key.public_key(), ActorKind::User);
        let resource = ResourceId::new(ResourceKind::Repository, "test");

        AuditEvent::builder()
            .now()
            .event_type(EventType::Push {
                force: false,
                commits: 1,
            })
            .actor(actor)
            .resource(resource)
            .sign(key)
            .unwrap()
    }

    #[test]
    fn test_genesis_block() {
        let (key, sealer) = test_sealer();
        let event = test_event(&key);

        let block = BlockBuilder::new(sealer).events(vec![event]).seal(&key);

        assert_eq!(block.header.height, 0);
        assert_eq!(block.header.parent, BlockHash::ZERO);
        assert!(block.validate(None).is_ok());
    }

    #[test]
    fn test_block_chain() {
        let (key, sealer) = test_sealer();

        // Genesis
        let genesis = BlockBuilder::new(sealer.clone())
            .events(vec![test_event(&key)])
            .seal(&key);

        assert!(genesis.validate(None).is_ok());

        // Block 1
        let block1 = BlockBuilder::new(sealer.clone())
            .parent(genesis.header.clone())
            .events(vec![test_event(&key)])
            .seal(&key);

        assert!(block1.validate(Some(&genesis.header)).is_ok());
        assert_eq!(block1.header.height, 1);
        assert_eq!(block1.header.parent, genesis.hash());

        // Block 2
        let block2 = BlockBuilder::new(sealer)
            .parent(block1.header.clone())
            .events(vec![test_event(&key), test_event(&key)])
            .seal(&key);

        assert!(block2.validate(Some(&block1.header)).is_ok());
        assert_eq!(block2.header.height, 2);
    }

    #[test]
    fn test_empty_block() {
        let (key, sealer) = test_sealer();

        let block = BlockBuilder::new(sealer).events(vec![]).seal(&key);

        assert!(block.validate(None).is_ok());
        assert_eq!(block.header.events_count, 0);
        assert_eq!(block.header.events_root, Hash::ZERO);
    }

    #[test]
    fn test_events_root_deterministic() {
        let key = SecretKey::generate();
        let events = vec![test_event(&key), test_event(&key)];

        let root1 = compute_events_root(&events);
        let root2 = compute_events_root(&events);

        assert_eq!(root1, root2);
    }

    #[test]
    fn test_tampered_block_fails() {
        let (key, sealer) = test_sealer();

        let mut block = BlockBuilder::new(sealer)
            .events(vec![test_event(&key)])
            .seal(&key);

        // Tamper with height
        block.header.height = 999;

        // Validation should fail (signature doesn't match)
        assert!(block.validate(None).is_err());
    }
}