Skip to main content

bintrie_demo/
bintrie_demo.rs

1use thru_base::bintrie::*;
2use thru_base::bintrie_types::{Hash, Pubkey};
3
4fn main() {
5    println!("Binary Trie Demo");
6    println!("================");
7
8    // Create a new empty trie
9    let mut trie = BinTrie::new();
10
11    // Create some test keys and values
12    let key1 = Pubkey::new([1u8; 32]);
13    let value1 = Hash::new([11u8; 32]);
14
15    let key2 = Pubkey::new([2u8; 32]);
16    let value2 = Hash::new([22u8; 32]);
17
18    let key3 = Pubkey::new([3u8; 32]);
19    let value3 = Hash::new([33u8; 32]);
20
21    // Insert keys into the trie
22    println!("\n1. Inserting keys into the trie:");
23    trie.insert(key1, value1).expect("Failed to insert key1");
24    println!("Inserted key1");
25
26    trie.insert(key2, value2).expect("Failed to insert key2");
27    println!("Inserted key2");
28
29    trie.insert(key3, value3).expect("Failed to insert key3");
30    println!("Inserted key3");
31
32    // Display the trie structure
33    println!("\n2. Trie structure:");
34    trie.print();
35
36    // Query for keys
37    println!("\n3. Querying keys:");
38    match trie.query(&key1) {
39        Some(pair) => println!("Found key1 with value: {}", pair.value_hash),
40        None => println!("Key1 not found"),
41    }
42
43    match trie.query(&key2) {
44        Some(pair) => println!("Found key2 with value: {}", pair.value_hash),
45        None => println!("Key2 not found"),
46    }
47
48    // Try to query a non-existent key
49    let missing_key = Pubkey::new([99u8; 32]);
50    match trie.query(&missing_key) {
51        Some(pair) => println!("Found missing key with value: {}", pair.value_hash),
52        None => println!("Missing key not found (as expected)"),
53    }
54
55    // Generate proofs
56    println!("\n4. Generating proofs:");
57
58    // Proof of existence
59    match trie.prove_existence(&key1) {
60        Ok((proof, value_hash)) => {
61            println!("Generated existence proof for key1:");
62            println!("  - Proof steps: {}", proof.proof_indices.len());
63            println!("  - Value hash: {}", value_hash);
64        }
65        Err(e) => println!("Failed to generate existence proof: {}", e),
66    }
67
68    // Proof of non-existence
69    match trie.prove_non_existence(&missing_key) {
70        Ok(non_existence_proof) => {
71            println!("Generated non-existence proof for missing key:");
72            println!(
73                "  - Proof steps: {}",
74                non_existence_proof.proof.proof_indices.len()
75            );
76            println!("  - Existing key: {}", non_existence_proof.existing_pubkey);
77        }
78        Err(e) => println!("Failed to generate non-existence proof: {}", e),
79    }
80
81    // Update a value
82    println!("\n5. Updating a value:");
83    let new_value1 = Hash::new([111u8; 32]);
84    trie.update_hash(&key1, new_value1)
85        .expect("Failed to update key1");
86    match trie.query(&key1) {
87        Some(pair) => println!("Updated key1 value: {}", pair.value_hash),
88        None => println!("Key1 not found after update"),
89    }
90
91    // Show final state root
92    println!("\n6. Final state root: {}", trie.state_root());
93
94    println!("\nDemo completed!");
95}