chaincraft 0.3.2

A high-performance Rust-based platform for blockchain education and prototyping
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//! Enhanced shared object implementation with application-specific logic

pub use crate::shared::SharedObjectId;
use crate::{
    error::{ChaincraftError, Result},
    shared::{MessageType, SharedMessage, SharedObject},
};
use async_trait::async_trait;
use chrono;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::any::Any;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;

/// Enhanced shared object trait with application-specific functionality
#[async_trait]
pub trait ApplicationObject: Send + Sync + std::fmt::Debug {
    /// Get the object's unique identifier
    fn id(&self) -> &SharedObjectId;

    /// Get the object's type name
    fn type_name(&self) -> &'static str;

    /// Validate if a message is valid for this object
    async fn is_valid(&self, message: &SharedMessage) -> Result<bool>;

    /// Add a validated message to the object
    async fn add_message(&mut self, message: SharedMessage) -> Result<()>;

    /// Check if this object supports merkleized synchronization
    fn is_merkleized(&self) -> bool;

    /// Get the latest state digest
    async fn get_latest_digest(&self) -> Result<String>;

    /// Check if object has a specific digest
    async fn has_digest(&self, digest: &str) -> Result<bool>;

    /// Validate if a digest is valid
    async fn is_valid_digest(&self, digest: &str) -> Result<bool>;

    /// Add a digest to the object
    async fn add_digest(&mut self, digest: String) -> Result<bool>;

    /// Get messages for gossip protocol
    async fn gossip_messages(&self, digest: Option<&str>) -> Result<Vec<SharedMessage>>;

    /// Get messages since a specific digest
    async fn get_messages_since_digest(&self, digest: &str) -> Result<Vec<SharedMessage>>;

    /// Get the current state as JSON
    async fn get_state(&self) -> Result<Value>;

    /// Reset the object to initial state
    async fn reset(&mut self) -> Result<()>;

    /// Clone the object
    fn clone_box(&self) -> Box<dyn ApplicationObject>;

    /// Get reference as Any for downcasting
    fn as_any(&self) -> &dyn Any;

    /// Get mutable reference as Any for downcasting
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// Simple shared number object for testing (equivalent to Python SimpleSharedNumber)
#[derive(Debug, Clone)]
pub struct SimpleSharedNumber {
    id: SharedObjectId,
    number: i64,
    created_at: chrono::DateTime<chrono::Utc>,
    updated_at: chrono::DateTime<chrono::Utc>,
    locked: bool,
    messages: Vec<SharedMessage>,
    seen_hashes: HashSet<String>,
    digests: Vec<String>,
}

impl SimpleSharedNumber {
    pub fn new() -> Self {
        Self {
            id: SharedObjectId::new(),
            number: 0,
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            locked: false,
            messages: Vec::new(),
            seen_hashes: HashSet::new(),
            digests: Vec::new(),
        }
    }

    pub fn get_number(&self) -> i64 {
        self.number
    }

    pub fn get_messages(&self) -> &[SharedMessage] {
        &self.messages
    }

    fn calculate_message_hash(data: &Value) -> String {
        let data_str = serde_json::to_string(&serde_json::json!({
            "content": data
        }))
        .unwrap_or_default();
        let mut hasher = Sha256::new();
        hasher.update(data_str.as_bytes());
        hex::encode(hasher.finalize())
    }
}

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

#[async_trait]
impl ApplicationObject for SimpleSharedNumber {
    fn id(&self) -> &SharedObjectId {
        &self.id
    }

    fn type_name(&self) -> &'static str {
        "SimpleSharedNumber"
    }

    async fn is_valid(&self, message: &SharedMessage) -> Result<bool> {
        // We only accept integer data
        Ok(message.data.is_i64())
    }

    async fn add_message(&mut self, message: SharedMessage) -> Result<()> {
        // Deduplicate by hashing the message's data field
        let msg_hash = Self::calculate_message_hash(&message.data);

        if self.seen_hashes.contains(&msg_hash) {
            // Already processed this data
            return Ok(());
        }

        self.seen_hashes.insert(msg_hash);

        // Extract the integer value and add to our number
        if let Some(value) = message.data.as_i64() {
            self.number += value;
            self.messages.push(message);
            tracing::info!("SimpleSharedNumber: Added message with data: {}", value);
        }

        Ok(())
    }

    fn is_merkleized(&self) -> bool {
        false
    }

    async fn get_latest_digest(&self) -> Result<String> {
        Ok(self.number.to_string())
    }

    async fn has_digest(&self, digest: &str) -> Result<bool> {
        Ok(self.digests.contains(&digest.to_string()))
    }

    async fn is_valid_digest(&self, _digest: &str) -> Result<bool> {
        Ok(true)
    }

    async fn add_digest(&mut self, digest: String) -> Result<bool> {
        self.digests.push(digest);
        Ok(true)
    }

    async fn gossip_messages(&self, _digest: Option<&str>) -> Result<Vec<SharedMessage>> {
        Ok(Vec::new())
    }

    async fn get_messages_since_digest(&self, _digest: &str) -> Result<Vec<SharedMessage>> {
        Ok(Vec::new())
    }

    async fn get_state(&self) -> Result<Value> {
        Ok(serde_json::json!({
            "number": self.number,
            "message_count": self.messages.len(),
            "seen_hashes_count": self.seen_hashes.len()
        }))
    }

    async fn reset(&mut self) -> Result<()> {
        self.number = 0;
        self.messages.clear();
        self.seen_hashes.clear();
        self.digests.clear();
        Ok(())
    }

    fn clone_box(&self) -> Box<dyn ApplicationObject> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Merkelized chain object (Rust analogue of Python SimpleChainObject)
///
/// Maintains an append-only chain of SHA256 hashes where each hash is derived from
/// the previous hash. Supports digest-based synchronization for efficient
/// state sync between nodes.
#[derive(Debug, Clone)]
pub struct MerkelizedChain {
    id: SharedObjectId,
    /// The chain of hashes (genesis is at index 0)
    chain: Vec<String>,
    /// Messages corresponding to each chain entry (optional payload)
    messages: Vec<SharedMessage>,
    /// Set of all hashes in the chain for O(1) lookup
    hash_set: HashSet<String>,
    created_at: chrono::DateTime<chrono::Utc>,
}

impl MerkelizedChain {
    /// Create a new MerkelizedChain with a genesis hash
    pub fn new() -> Self {
        let genesis = Self::calculate_hash("genesis");
        Self {
            id: SharedObjectId::new(),
            chain: vec![genesis.clone()],
            messages: vec![SharedMessage::new(
                MessageType::Custom("genesis".to_string()),
                serde_json::json!("genesis"),
            )],
            hash_set: {
                let mut set = HashSet::new();
                set.insert(genesis);
                set
            },
            created_at: chrono::Utc::now(),
        }
    }

    /// Calculate the SHA256 hash of data (consistent with Python)
    pub fn calculate_hash(data: &str) -> String {
        let mut hasher = Sha256::new();
        hasher.update(data.as_bytes());
        hex::encode(hasher.finalize())
    }

    /// Calculate what the next hash should be given a previous hash
    pub fn calculate_next_hash(prev_hash: &str) -> String {
        Self::calculate_hash(prev_hash)
    }

    /// Get the current chain length (including genesis)
    pub fn chain_length(&self) -> usize {
        self.chain.len()
    }

    /// Get the genesis hash
    pub fn genesis_hash(&self) -> &str {
        &self.chain[0]
    }

    /// Get the latest (tip) hash
    pub fn latest_hash(&self) -> &str {
        self.chain.last().expect("chain is never empty")
    }

    /// Get a specific hash by index
    pub fn hash_at(&self, index: usize) -> Option<&str> {
        self.chain.get(index).map(|s| s.as_str())
    }

    /// Check if a hash is valid as the next hash in the chain
    pub fn is_valid_next_hash(&self, hash: &str) -> bool {
        let expected = Self::calculate_next_hash(self.latest_hash());
        hash == expected
    }

    /// Add a next hash to the chain (returns the new hash)
    pub fn add_next_hash(&mut self) -> String {
        let next_hash = Self::calculate_next_hash(self.latest_hash());
        self.chain.push(next_hash.clone());
        self.hash_set.insert(next_hash.clone());

        // Create a message for this hash
        let msg = SharedMessage::new(
            MessageType::Custom("chain_update".to_string()),
            serde_json::json!(next_hash),
        );
        self.messages.push(msg);

        next_hash
    }

    /// Try to add an existing hash to the chain (for sync from peers)
    /// Returns true if the hash was added, false if invalid or duplicate
    pub fn try_add_hash(&mut self, hash: &str) -> bool {
        // Skip if already in chain
        if self.hash_set.contains(hash) {
            return false;
        }

        // Check if this hash follows from any hash in our chain
        for i in 0..self.chain.len() {
            let expected_next = Self::calculate_next_hash(&self.chain[i]);
            if hash == expected_next {
                self.chain.push(hash.to_string());
                self.hash_set.insert(hash.to_string());

                let msg = SharedMessage::new(
                    MessageType::Custom("chain_update".to_string()),
                    serde_json::json!(hash),
                );
                self.messages.push(msg);

                return true;
            }
        }

        false
    }

    /// Get the chain as a slice of hashes
    pub fn chain(&self) -> &[String] {
        &self.chain
    }

    /// Find the index of a hash in the chain
    pub fn find_hash_index(&self, hash: &str) -> Option<usize> {
        self.chain.iter().position(|h| h == hash)
    }
}

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

#[async_trait]
impl ApplicationObject for MerkelizedChain {
    fn id(&self) -> &SharedObjectId {
        &self.id
    }

    fn type_name(&self) -> &'static str {
        "MerkelizedChain"
    }

    async fn is_valid(&self, message: &SharedMessage) -> Result<bool> {
        // Accept string messages that are valid hashes
        let Some(hash) = message.data.as_str() else {
            return Ok(false);
        };

        // If already in chain, it's valid (for deduplication)
        if self.hash_set.contains(hash) {
            return Ok(true);
        }

        // Check if it's a valid next hash from any existing hash
        for i in 0..self.chain.len() {
            let expected_next = Self::calculate_next_hash(&self.chain[i]);
            if hash == expected_next {
                return Ok(true);
            }
        }

        Ok(false)
    }

    async fn add_message(&mut self, message: SharedMessage) -> Result<()> {
        let Some(hash) = message.data.as_str() else {
            return Ok(());
        };

        // Skip if already in chain
        if self.hash_set.contains(hash) {
            return Ok(());
        }

        // Try to add to chain
        if self.try_add_hash(hash) {
            tracing::info!(
                "MerkelizedChain: Added hash {} to chain (length: {})",
                &hash[..8.min(hash.len())],
                self.chain.len()
            );
        }

        Ok(())
    }

    fn is_merkleized(&self) -> bool {
        true
    }

    async fn get_latest_digest(&self) -> Result<String> {
        Ok(self.latest_hash().to_string())
    }

    async fn has_digest(&self, digest: &str) -> Result<bool> {
        Ok(self.hash_set.contains(digest))
    }

    async fn is_valid_digest(&self, digest: &str) -> Result<bool> {
        Ok(self.hash_set.contains(digest) || self.is_valid_next_hash(digest))
    }

    async fn add_digest(&mut self, digest: String) -> Result<bool> {
        if self.try_add_hash(&digest) {
            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn gossip_messages(&self, digest: Option<&str>) -> Result<Vec<SharedMessage>> {
        let start_index = match digest {
            Some(hash) => {
                match self.find_hash_index(hash) {
                    Some(idx) => idx + 1,          // Start after the given digest
                    None => return Ok(Vec::new()), // Unknown digest
                }
            },
            None => 1, // Skip genesis, return all subsequent
        };

        let mut result = Vec::new();
        for i in start_index..self.chain.len() {
            let msg = SharedMessage::new(
                MessageType::Custom("chain_update".to_string()),
                serde_json::json!(self.chain[i]),
            );
            result.push(msg);
        }

        Ok(result)
    }

    async fn get_messages_since_digest(&self, digest: &str) -> Result<Vec<SharedMessage>> {
        self.gossip_messages(Some(digest)).await
    }

    async fn get_state(&self) -> Result<Value> {
        Ok(serde_json::json!({
            "chain_length": self.chain.len(),
            "latest_hash": self.latest_hash(),
            "genesis_hash": self.genesis_hash(),
        }))
    }

    async fn reset(&mut self) -> Result<()> {
        let genesis = Self::calculate_hash("genesis");
        self.chain = vec![genesis.clone()];
        self.hash_set = {
            let mut set = HashSet::new();
            set.insert(genesis);
            set
        };
        self.messages = vec![SharedMessage::new(
            MessageType::Custom("genesis".to_string()),
            serde_json::json!("genesis"),
        )];
        Ok(())
    }

    fn clone_box(&self) -> Box<dyn ApplicationObject> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Message chain - append-only ordered log of messages.
/// Mirrors Python message chain concept: ordered sequence of messages
/// with digest-based sync support.
#[derive(Debug, Clone)]
pub struct MessageChain {
    id: SharedObjectId,
    messages: Vec<SharedMessage>,
    seen_hashes: HashSet<String>,
    digests: Vec<String>,
}

impl MessageChain {
    pub fn new() -> Self {
        Self {
            id: SharedObjectId::new(),
            messages: Vec::new(),
            seen_hashes: HashSet::new(),
            digests: Vec::new(),
        }
    }

    pub fn len(&self) -> usize {
        self.messages.len()
    }

    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }

    pub fn messages(&self) -> &[SharedMessage] {
        &self.messages
    }

    fn msg_hash(msg: &SharedMessage) -> String {
        msg.hash.clone()
    }
}

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

#[async_trait]
impl ApplicationObject for MessageChain {
    fn id(&self) -> &SharedObjectId {
        &self.id
    }

    fn type_name(&self) -> &'static str {
        "MessageChain"
    }

    async fn is_valid(&self, message: &SharedMessage) -> Result<bool> {
        Ok(!message.data.is_null())
    }

    async fn add_message(&mut self, message: SharedMessage) -> Result<()> {
        let h = Self::msg_hash(&message);
        if self.seen_hashes.contains(&h) {
            return Ok(());
        }
        self.seen_hashes.insert(h);
        self.messages.push(message);
        Ok(())
    }

    fn is_merkleized(&self) -> bool {
        true
    }

    async fn get_latest_digest(&self) -> Result<String> {
        Ok(self
            .messages
            .last()
            .map(|m| m.hash.clone())
            .unwrap_or_else(|| "genesis".to_string()))
    }

    async fn has_digest(&self, digest: &str) -> Result<bool> {
        Ok(self.digests.contains(&digest.to_string())
            || self.messages.iter().any(|m| m.hash == digest))
    }

    async fn is_valid_digest(&self, digest: &str) -> Result<bool> {
        Ok(self.has_digest(digest).await?
            || digest == "genesis"
            || self.seen_hashes.contains(digest))
    }

    async fn add_digest(&mut self, _digest: String) -> Result<bool> {
        Ok(false)
    }

    async fn gossip_messages(&self, digest: Option<&str>) -> Result<Vec<SharedMessage>> {
        let start = match digest {
            Some(d) if d != "genesis" => self
                .messages
                .iter()
                .position(|m| m.hash == d)
                .map(|i| i + 1)
                .unwrap_or(0),
            _ => 0,
        };
        Ok(self.messages[start..].to_vec())
    }

    async fn get_messages_since_digest(&self, digest: &str) -> Result<Vec<SharedMessage>> {
        self.gossip_messages(Some(digest)).await
    }

    async fn get_state(&self) -> Result<Value> {
        Ok(serde_json::json!({
            "length": self.messages.len(),
            "message_count": self.messages.len()
        }))
    }

    async fn reset(&mut self) -> Result<()> {
        self.messages.clear();
        self.seen_hashes.clear();
        self.digests.clear();
        Ok(())
    }

    fn clone_box(&self) -> Box<dyn ApplicationObject> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Registry for managing application objects
#[derive(Debug)]
pub struct ApplicationObjectRegistry {
    pub objects: HashMap<SharedObjectId, Box<dyn ApplicationObject>>,
    objects_by_type: HashMap<String, Vec<SharedObjectId>>,
}

impl ApplicationObjectRegistry {
    pub fn new() -> Self {
        Self {
            objects: HashMap::new(),
            objects_by_type: HashMap::new(),
        }
    }

    /// Register a new application object
    pub fn register(&mut self, object: Box<dyn ApplicationObject>) -> SharedObjectId {
        let id = object.id().clone();
        let type_name = object.type_name().to_string();

        self.objects_by_type
            .entry(type_name)
            .or_default()
            .push(id.clone());

        self.objects.insert(id.clone(), object);
        id
    }

    /// Get an object by ID
    pub fn get(&self, id: &SharedObjectId) -> Option<&dyn ApplicationObject> {
        self.objects.get(id).map(|obj| obj.as_ref())
    }

    /// Get all objects of a specific type (returning owned clones for safety)
    pub fn get_by_type(&self, type_name: &str) -> Vec<Box<dyn ApplicationObject>> {
        self.objects_by_type
            .get(type_name)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.objects.get(id))
                    .map(|obj| obj.clone_box())
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Remove an object
    pub fn remove(&mut self, id: &SharedObjectId) -> Option<Box<dyn ApplicationObject>> {
        if let Some(object) = self.objects.remove(id) {
            let type_name = object.type_name().to_string();
            if let Some(type_list) = self.objects_by_type.get_mut(&type_name) {
                type_list.retain(|obj_id| obj_id != id);
                if type_list.is_empty() {
                    self.objects_by_type.remove(&type_name);
                }
            }
            Some(object)
        } else {
            None
        }
    }

    /// Get all object IDs
    pub fn ids(&self) -> Vec<SharedObjectId> {
        self.objects.keys().cloned().collect()
    }

    /// Get count of objects
    pub fn len(&self) -> usize {
        self.objects.len()
    }

    /// Check if registry is empty
    pub fn is_empty(&self) -> bool {
        self.objects.is_empty()
    }

    /// Clear all objects
    pub fn clear(&mut self) {
        self.objects.clear();
        self.objects_by_type.clear();
    }

    /// Process a message against all appropriate objects
    pub async fn process_message(&mut self, message: SharedMessage) -> Result<Vec<SharedObjectId>> {
        let mut processed_objects = Vec::new();

        // Get all object IDs first to avoid borrow checker issues
        let ids: Vec<SharedObjectId> = self.objects.keys().cloned().collect();

        // Process each object sequentially
        for id in ids {
            // Check validity first
            let is_valid = if let Some(object) = self.objects.get(&id) {
                object.is_valid(&message).await?
            } else {
                false
            };

            // If valid, add the message
            if is_valid {
                if let Some(object) = self.objects.get_mut(&id) {
                    object.add_message(message.clone()).await?;
                    processed_objects.push(id);
                }
            }
        }

        Ok(processed_objects)
    }
}

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