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
use anyhow::Result;
use chaincraft::{
    crypto::ecdsa::{ECDSASigner, ECDSAVerifier},
    network::PeerId,
    shared::{MessageType, SharedMessage},
    storage::MemoryStorage,
    ChaincraftNode,
};
use chrono::Utc;
use serde_json::json;
use std::sync::Arc;
use tokio::time::{sleep, Duration};

async fn create_consensus_node() -> Result<ChaincraftNode> {
    let id = PeerId::new();
    let storage = Arc::new(MemoryStorage::new());
    let mut node = ChaincraftNode::new(id, storage);
    node.set_port(0); // Use ephemeral port
    node.start().await?;
    Ok(node)
}

#[tokio::test]
async fn test_tendermint_initialization() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Test that Tendermint consensus can be initialized
    assert!(node.is_running_async().await);

    node.close().await?;
    Ok(())
}

#[tokio::test]
async fn test_validator_setup() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Simulate validator setup
    let validator_msg = json!({
        "type": "VALIDATOR_SETUP",
        "validator_id": "validator_1",
        "public_key": "validator_public_key",
        "stake": 1000
    });

    let msg = SharedMessage::custom("VALIDATOR_SETUP", validator_msg)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;
    Ok(())
}

#[tokio::test]
async fn test_block_proposal() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Create a block proposal
    let proposal = json!({
        "type": "PROPOSAL",
        "height": 1,
        "round": 0,
        "block_hash": "proposed_block_hash",
        "proposer": "validator_1",
        "timestamp": Utc::now().to_rfc3339()
    });

    let msg = SharedMessage::custom("PROPOSAL", proposal)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;
    Ok(())
}

#[tokio::test]
async fn test_prevote_phase() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Simulate prevote phase
    let prevote = json!({
        "type": "PREVOTE",
        "height": 1,
        "round": 0,
        "block_hash": "voted_block_hash",
        "validator": "validator_1"
    });

    let msg = SharedMessage::custom("PREVOTE", prevote)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;
    Ok(())
}

#[tokio::test]
async fn test_precommit_phase() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Simulate precommit phase
    let precommit = json!({
        "type": "PRECOMMIT",
        "height": 1,
        "round": 0,
        "block_hash": "committed_block_hash",
        "validator": "validator_1"
    });

    let msg = SharedMessage::custom("PRECOMMIT", precommit)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;
    Ok(())
}

#[tokio::test]
async fn test_consensus_round_completion() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Simulate a complete consensus round
    let height = 1;
    let round = 0;

    // 1. Proposal
    let proposal = json!({
        "type": "PROPOSAL",
        "height": height,
        "round": round,
        "block_hash": "test_block_hash",
        "proposer": "validator_1",
        "timestamp": Utc::now().to_rfc3339()
    });

    let msg = SharedMessage::custom("PROPOSAL", proposal)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(50)).await;

    // 2. Prevote
    let prevote = json!({
        "type": "PREVOTE",
        "height": height,
        "round": round,
        "block_hash": "test_block_hash",
        "validator": "validator_1"
    });

    let msg = SharedMessage::custom("PREVOTE", prevote)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(50)).await;

    // 3. Precommit
    let precommit = json!({
        "type": "PRECOMMIT",
        "height": height,
        "round": round,
        "block_hash": "test_block_hash",
        "validator": "validator_1"
    });

    let msg = SharedMessage::custom("PRECOMMIT", precommit)?;
    node.create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;
    Ok(())
}

#[tokio::test]
async fn test_byzantine_fault_tolerance() -> Result<()> {
    let mut nodes = Vec::new();

    // Create 4 nodes (can tolerate 1 Byzantine fault)
    for _ in 0..4 {
        let node = create_consensus_node().await?;
        nodes.push(node);
    }

    // Simulate Byzantine behavior from one node
    let byzantine_msg = json!({
        "type": "PREVOTE",
        "height": 1,
        "round": 0,
        "block_hash": "block_hash_1",
        "validator": "byzantine_validator"
    });

    let msg = SharedMessage::custom("PREVOTE", byzantine_msg)?;
    nodes[0].create_shared_message(msg.to_json()?).await?;

    // Send conflicting vote from same validator
    let conflicting_msg = json!({
        "type": "PREVOTE",
        "height": 1,
        "round": 0,
        "block_hash": "block_hash_2",
        "validator": "byzantine_validator"
    });

    let msg = SharedMessage::custom("PREVOTE", conflicting_msg)?;
    nodes[0].create_shared_message(msg.to_json()?).await?;
    sleep(Duration::from_millis(100)).await;

    // Honest nodes continue with consensus
    for (i, node) in nodes.iter_mut().enumerate().skip(1) {
        let honest_msg = json!({
            "type": "PREVOTE",
            "height": 1,
            "round": 0,
            "block_hash": "legitimate_block_hash",
            "validator": format!("honest_validator_{}", i)
        });

        let msg = SharedMessage::custom("PREVOTE", honest_msg)?;
        node.create_shared_message(msg.to_json()?).await?;
    }

    // Close all nodes
    for mut node in nodes {
        node.close().await?;
    }

    Ok(())
}

#[tokio::test]
async fn test_validator_signature_verification() {
    let signer = ECDSASigner::new().unwrap();
    let verifier = ECDSAVerifier::new();

    // Create a signed consensus message
    let consensus_data = b"height:1,round:0,block_hash:test_hash";
    let signature = signer.sign(consensus_data).unwrap();
    let public_key_pem = signer.get_public_key_pem().unwrap();

    // Verify the consensus signature
    let is_valid = verifier
        .verify(consensus_data, &signature, &public_key_pem)
        .unwrap();
    assert!(is_valid);

    // Test with tampered data
    let tampered_data = b"height:1,round:0,block_hash:tampered_hash";
    let is_invalid = verifier
        .verify(tampered_data, &signature, &public_key_pem)
        .unwrap();
    assert!(!is_invalid);
}

#[tokio::test]
async fn test_network_partition_recovery() -> Result<()> {
    let mut node1 = create_consensus_node().await?;
    let mut node2 = create_consensus_node().await?;

    // Simulate network partition and recovery
    let partition_msg = json!({
        "message_type": "NETWORK_PARTITION",
        "node": "node1",
        "status": "isolated"
    });

    node1.create_shared_message_with_data(partition_msg).await?;
    sleep(Duration::from_millis(100)).await;

    // Simulate recovery
    let recovery_msg = json!({
        "message_type": "NETWORK_RECOVERY",
        "node": "node1",
        "status": "reconnected",
        "sync_height": 5
    });

    node1
        .create_shared_message_with_data(recovery_msg.clone())
        .await?;
    node2.create_shared_message_with_data(recovery_msg).await?;

    sleep(Duration::from_millis(200)).await;

    node1.close().await?;
    node2.close().await?;

    // Test passes if partition recovery simulated
    Ok(())
}

#[tokio::test]
async fn test_consensus_timeout_handling() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Simulate timeout scenarios
    let timeout_msg = json!({
        "message_type": "CONSENSUS_TIMEOUT",
        "height": 1,
        "round": 0,
        "timeout_type": "propose_timeout",
        "validator": "validator_1"
    });

    node.create_shared_message_with_data(timeout_msg).await?;
    sleep(Duration::from_millis(100)).await;

    // Simulate round increment due to timeout
    let round_increment = json!({
        "message_type": "ROUND_INCREMENT",
        "height": 1,
        "new_round": 1,
        "reason": "propose_timeout"
    });

    node.create_shared_message_with_data(round_increment)
        .await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;

    // Test passes if timeout handling works
    Ok(())
}

#[tokio::test]
async fn test_validator_set_changes() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Add new validator
    let add_validator = json!({
        "message_type": "ADD_VALIDATOR",
        "validator_id": "new_validator",
        "public_key": "new_validator_pubkey",
        "stake": 500
    });

    node.create_shared_message_with_data(add_validator).await?;
    sleep(Duration::from_millis(100)).await;

    // Remove validator
    let remove_validator = json!({
        "message_type": "REMOVE_VALIDATOR",
        "validator_id": "old_validator",
        "reason": "insufficient_stake"
    });

    node.create_shared_message_with_data(remove_validator)
        .await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;

    // Test passes if validator set changes handled
    Ok(())
}

#[tokio::test]
async fn test_block_finality() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Create finalized block
    let finalized_block = json!({
        "message_type": "BLOCK_FINALIZED",
        "height": 1,
        "block_hash": "finalized_block_hash",
        "commit_signatures": ["sig1", "sig2", "sig3"],
        "timestamp": chrono::Utc::now().to_rfc3339()
    });

    node.create_shared_message_with_data(finalized_block)
        .await?;
    sleep(Duration::from_millis(100)).await;

    // Verify finality cannot be reverted
    let revert_attempt = json!({
        "message_type": "REVERT_BLOCK",
        "height": 1,
        "new_block_hash": "different_block_hash"
    });

    // This should be rejected/ignored
    let _result = node.create_shared_message_with_data(revert_attempt).await;

    node.close().await?;

    // Test passes if finality maintained
    Ok(())
}

#[tokio::test]
async fn test_state_machine_replication() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Apply state machine transactions
    for i in 0..5 {
        let tx = json!({
            "message_type": "STATE_MACHINE_TX",
            "height": 1,
            "tx_id": i,
            "operation": "transfer",
            "from": "account_a",
            "to": "account_b",
            "amount": 100
        });

        node.create_shared_message_with_data(tx).await?;
        sleep(Duration::from_millis(20)).await;
    }

    // Verify state consistency
    let state_query = json!({
        "message_type": "QUERY_STATE",
        "height": 1,
        "account": "account_a"
    });

    node.create_shared_message_with_data(state_query).await?;
    sleep(Duration::from_millis(100)).await;

    node.close().await?;

    // Test passes if state machine replication works
    Ok(())
}

#[tokio::test]
async fn test_message_type_handling() -> Result<()> {
    let mut node = create_consensus_node().await?;

    // Test different message types
    let message_types = vec![
        MessageType::PeerDiscovery,
        MessageType::RequestLocalPeers,
        MessageType::LocalPeers,
        MessageType::Custom("test".to_string()),
    ];

    for msg_type in message_types {
        let data = json!({ "type": msg_type.to_string() });
        let hash = node.create_shared_message_with_data(data.clone()).await?;
        let stored = node.get_object(&hash).await?;
        let value: serde_json::Value = serde_json::from_str(&stored)?;
        let msg_type_json = &value["message_type"];
        match msg_type {
            MessageType::Custom(ref s) => {
                // Should be an object like {"Custom": "test"}
                assert!(msg_type_json.is_object());
                assert_eq!(msg_type_json.get("Custom").and_then(|v| v.as_str()), Some(s.as_str()));
            },
            _ => {
                // Should be a string
                assert_eq!(msg_type_json, &serde_json::Value::String(msg_type.to_string()));
            },
        }
    }

    node.close().await?;
    Ok(())
}