Skip to main content

Hash

Struct Hash 

Source
pub struct Hash(pub [u8; 32]);
Expand description

32-byte hash value compatible with the C implementation

Tuple Fields§

§0: [u8; 32]

Implementations§

Source§

impl Hash

Source

pub fn new(bytes: [u8; 32]) -> Self

Examples found in repository?
examples/bintrie_demo.rs (line 13)
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}
Source

pub fn as_bytes(&self) -> &[u8; 32]

Source

pub fn from_slice(slice: &[u8]) -> Result<Self, BinTrieError>

Source

pub fn is_zero(&self) -> bool

Trait Implementations§

Source§

impl Clone for Hash

Source§

fn clone(&self) -> Hash

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Hash

Source§

impl Debug for Hash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Hash

Source§

fn default() -> Hash

Returns the “default value” for a type. Read more
Source§

impl Display for Hash

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Hash

Source§

impl Hash for Hash

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Hash

Source§

fn eq(&self, other: &Hash) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Hash

Auto Trait Implementations§

§

impl Freeze for Hash

§

impl RefUnwindSafe for Hash

§

impl Send for Hash

§

impl Sync for Hash

§

impl Unpin for Hash

§

impl UnsafeUnpin for Hash

§

impl UnwindSafe for Hash

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more