hotshot_query_service/merklized_state/
data_source.rs1use std::cmp::Ordering;
20
21use async_trait::async_trait;
22use derivative::Derivative;
23use derive_more::with_trait::Display;
24pub use hotshot_query_service_types::merklized_state::MerklizedState;
25use hotshot_types::traits::node_implementation::NodeType;
26use jf_merkle_tree_compat::prelude::MerkleProof;
27
28use crate::QueryResult;
29
30#[async_trait]
33pub trait MerklizedStateDataSource<Types, State, const ARITY: usize>
34where
35 Types: NodeType,
36 State: MerklizedState<Types, ARITY>,
37{
38 async fn get_path(
39 &self,
40 snapshot: Snapshot<Types, State, ARITY>,
41 key: State::Key,
42 ) -> QueryResult<MerkleProof<State::Entry, State::Key, State::T, ARITY>>;
43}
44
45#[async_trait]
47pub trait UpdateStateData<Types: NodeType, State: MerklizedState<Types, ARITY>, const ARITY: usize>:
48 Send + Sync
49{
50 async fn set_last_state_height(&mut self, height: usize) -> anyhow::Result<()>;
51 async fn insert_merkle_nodes(
52 &mut self,
53 path: MerkleProof<State::Entry, State::Key, State::T, ARITY>,
54 traversal_path: Vec<usize>,
55 block_number: u64,
56 ) -> anyhow::Result<()>;
57 async fn insert_merkle_nodes_batch(
58 &mut self,
59 proofs: Vec<(
60 MerkleProof<State::Entry, State::Key, State::T, ARITY>,
61 Vec<usize>,
62 )>,
63 block_number: u64,
64 ) -> anyhow::Result<()>;
65}
66
67#[async_trait]
68pub trait MerklizedStateHeightPersistence {
69 async fn get_last_state_height(&self) -> QueryResult<usize>;
70}
71
72type StateCommitment<Types, T, const ARITY: usize> = <T as MerklizedState<Types, ARITY>>::Commit;
73
74#[derive(Derivative, Display)]
76#[derivative(Ord = "feature_allow_slow_enum")]
77#[derivative(
78 Copy(bound = ""),
79 Debug(bound = ""),
80 PartialEq(bound = ""),
81 Eq(bound = ""),
82 Ord(bound = ""),
83 Hash(bound = "")
84)]
85pub enum Snapshot<Types: NodeType, T: MerklizedState<Types, ARITY>, const ARITY: usize> {
86 #[display("{_0}")]
87 Commit(StateCommitment<Types, T, ARITY>),
88 #[display("{_0}")]
89 Index(u64),
90}
91
92impl<T: MerklizedState<Types, ARITY>, Types: NodeType, const ARITY: usize> Clone
93 for Snapshot<Types, T, ARITY>
94{
95 fn clone(&self) -> Self {
96 *self
97 }
98}
99
100impl<T: MerklizedState<Types, ARITY>, Types: NodeType, const ARITY: usize> PartialOrd
101 for Snapshot<Types, T, ARITY>
102{
103 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
104 Some(self.cmp(other))
105 }
106}