pub struct BinTrie { /* private fields */ }Expand description
Binary prefix trie with a fixed, 256-bit key
This is the main data structure that provides a binary prefix trie with SHA256-based hash caching. Unlike the C implementation that uses manual memory management, this Rust version uses standard collections and recursive data structures for memory safety and idiomatic design.
Implementations§
Source§impl BinTrie
impl BinTrie
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty binary trie
Examples found in repository?
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}Sourcepub fn state_root(&self) -> Hash
pub fn state_root(&self) -> Hash
Get the state root hash of the entire trie
Examples found in repository?
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}Sourcepub fn query(&self, pubkey: &Pubkey) -> Option<&BinTriePair>
pub fn query(&self, pubkey: &Pubkey) -> Option<&BinTriePair>
Query for a key in the trie
Examples found in repository?
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}Sourcepub fn insert(
&mut self,
pubkey: Pubkey,
value_hash: Hash,
) -> Result<(), BinTrieError>
pub fn insert( &mut self, pubkey: Pubkey, value_hash: Hash, ) -> Result<(), BinTrieError>
Insert a new key-value pair into the trie
Examples found in repository?
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}Sourcepub fn update_hash(
&mut self,
pubkey: &Pubkey,
new_value_hash: Hash,
) -> Result<(), BinTrieError>
pub fn update_hash( &mut self, pubkey: &Pubkey, new_value_hash: Hash, ) -> Result<(), BinTrieError>
Update the hash for an existing key
Examples found in repository?
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}Sourcepub fn prove_existence(
&self,
pubkey: &Pubkey,
) -> Result<(Proof, Hash), BinTrieError>
pub fn prove_existence( &self, pubkey: &Pubkey, ) -> Result<(Proof, Hash), BinTrieError>
Generate a proof of existence for a key
Examples found in repository?
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}Sourcepub fn prove_non_existence(
&self,
pubkey: &Pubkey,
) -> Result<NonExistenceProof, BinTrieError>
pub fn prove_non_existence( &self, pubkey: &Pubkey, ) -> Result<NonExistenceProof, BinTrieError>
Generate a proof of non-existence for a key
Examples found in repository?
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}Sourcepub fn insert_with_proof(
&mut self,
pubkey: Pubkey,
value_hash: Hash,
proof: &Proof,
) -> Result<(), BinTrieError>
pub fn insert_with_proof( &mut self, pubkey: Pubkey, value_hash: Hash, proof: &Proof, ) -> Result<(), BinTrieError>
Insert a key with a proof of existence
Sourcepub fn insert_with_creation_proof(
&mut self,
pubkey: Pubkey,
value_hash: Hash,
existing_pubkey: Pubkey,
existing_value_hash: Hash,
proof: &Proof,
) -> Result<(), BinTrieError>
pub fn insert_with_creation_proof( &mut self, pubkey: Pubkey, value_hash: Hash, existing_pubkey: Pubkey, existing_value_hash: Hash, proof: &Proof, ) -> Result<(), BinTrieError>
Insert a key with proof of creation (includes existing key proof)
Sourcepub fn print(&self)
pub fn print(&self)
Print a compact representation of the trie
Examples found in repository?
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}Sourcepub fn print_verbose(&self)
pub fn print_verbose(&self)
Print a verbose representation of the trie
Sourcepub fn print_log_verbose(&self)
pub fn print_log_verbose(&self)
Print a verbose representation of the trie