hotshot_query_service/merklized_state/
data_source.rs1use std::{cmp::Ordering, fmt::Debug, str::FromStr};
20
21use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
22use async_trait::async_trait;
23use derivative::Derivative;
24use derive_more::with_trait::Display;
25use hotshot_types::traits::node_implementation::NodeType;
26use jf_merkle_tree_compat::{
27 prelude::MerkleProof, DigestAlgorithm, Element, ForgetableMerkleTreeScheme, Index,
28 MerkleCommitment, NodeValue, ToTraversalPath,
29};
30use serde::{de::DeserializeOwned, Serialize};
31use tagged_base64::TaggedBase64;
32
33use crate::QueryResult;
34
35#[async_trait]
38pub trait MerklizedStateDataSource<Types, State, const ARITY: usize>
39where
40 Types: NodeType,
41 State: MerklizedState<Types, ARITY>,
42{
43 async fn get_path(
44 &self,
45 snapshot: Snapshot<Types, State, ARITY>,
46 key: State::Key,
47 ) -> QueryResult<MerkleProof<State::Entry, State::Key, State::T, ARITY>>;
48}
49
50#[async_trait]
52pub trait UpdateStateData<Types: NodeType, State: MerklizedState<Types, ARITY>, const ARITY: usize>:
53 Send + Sync
54{
55 async fn set_last_state_height(&mut self, height: usize) -> anyhow::Result<()>;
56 async fn insert_merkle_nodes(
57 &mut self,
58 path: MerkleProof<State::Entry, State::Key, State::T, ARITY>,
59 traversal_path: Vec<usize>,
60 block_number: u64,
61 ) -> anyhow::Result<()>;
62 async fn insert_merkle_nodes_batch(
63 &mut self,
64 proofs: Vec<(
65 MerkleProof<State::Entry, State::Key, State::T, ARITY>,
66 Vec<usize>,
67 )>,
68 block_number: u64,
69 ) -> anyhow::Result<()>;
70}
71
72#[async_trait]
73pub trait MerklizedStateHeightPersistence {
74 async fn get_last_state_height(&self) -> QueryResult<usize>;
75}
76
77type StateCommitment<Types, T, const ARITY: usize> = <T as MerklizedState<Types, ARITY>>::Commit;
78
79#[derive(Derivative, Display)]
81#[derivative(Ord = "feature_allow_slow_enum")]
82#[derivative(
83 Copy(bound = ""),
84 Debug(bound = ""),
85 PartialEq(bound = ""),
86 Eq(bound = ""),
87 Ord(bound = ""),
88 Hash(bound = "")
89)]
90pub enum Snapshot<Types: NodeType, T: MerklizedState<Types, ARITY>, const ARITY: usize> {
91 #[display("{_0}")]
92 Commit(StateCommitment<Types, T, ARITY>),
93 #[display("{_0}")]
94 Index(u64),
95}
96
97impl<T: MerklizedState<Types, ARITY>, Types: NodeType, const ARITY: usize> Clone
98 for Snapshot<Types, T, ARITY>
99{
100 fn clone(&self) -> Self {
101 *self
102 }
103}
104
105impl<T: MerklizedState<Types, ARITY>, Types: NodeType, const ARITY: usize> PartialOrd
106 for Snapshot<Types, T, ARITY>
107{
108 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
109 Some(self.cmp(other))
110 }
111}
112
113pub trait MerklizedState<Types, const ARITY: usize>:
116 ForgetableMerkleTreeScheme<Commitment = Self::Commit> + Send + Sync + Clone + 'static
117where
118 Types: NodeType,
119{
120 type Key: Index
121 + Send
122 + Sync
123 + Serialize
124 + ToTraversalPath<ARITY>
125 + FromStr
126 + DeserializeOwned
127 + Display
128 + CanonicalSerialize
129 + CanonicalDeserialize;
130 type Entry: Element
131 + Send
132 + Sync
133 + Serialize
134 + DeserializeOwned
135 + CanonicalSerialize
136 + CanonicalDeserialize;
137 type T: NodeValue + Send;
138 type Commit: MerkleCommitment<Self::T>
139 + Send
140 + for<'a> TryFrom<&'a TaggedBase64>
141 + Display
142 + Debug
143 + Into<TaggedBase64>;
144 type Digest: DigestAlgorithm<Self::Entry, Self::Key, Self::T>;
145
146 fn state_type() -> &'static str;
148
149 fn header_state_commitment_field() -> &'static str;
152
153 fn tree_height() -> usize;
155
156 fn insert_path(
158 &mut self,
159 key: Self::Key,
160 proof: &MerkleProof<Self::Entry, Self::Key, Self::T, ARITY>,
161 ) -> anyhow::Result<()>;
162}