seda_sdk_rs/hashmap.rs
1//! A deterministic [`HashMap`] for tally oracle programs without random hashing.
2//!
3//! The standard [`std::collections::HashMap`] relies on randomized seeds, which are disallowed in tally
4//! oracle programs. This module provides [`DeterministicHasher`] and a [`HashMap`] alias
5//! that guarantees consistent, reproducible hashes without any randomness.
6
7use std::{
8 collections::HashMap as StdHashMap,
9 hash::{BuildHasher, DefaultHasher},
10};
11
12/// A hasher that always produces deterministic outputs by wrapping `DefaultHasher`.
13pub struct DeterministicHasher {}
14
15impl DeterministicHasher {
16 /// Creates a new `DeterministicHasher`.
17 pub fn new() -> Self {
18 Self {}
19 }
20}
21
22impl Default for DeterministicHasher {
23 /// Returns a default `DeterministicHasher`.
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl BuildHasher for DeterministicHasher {
30 type Hasher = DefaultHasher;
31
32 /// Builds a new `DefaultHasher` for deterministic hashing.
33 fn build_hasher(&self) -> Self::Hasher {
34 DefaultHasher::new()
35 }
36}
37
38/// A `HashMap` type alias using `DeterministicHasher` to ensure consistent hash values.
39pub type HashMap<K, V> = StdHashMap<K, V, DeterministicHasher>;