1pub mod cache;
2pub mod error;
3
4use arpa_core::{DKGStatus, DKGTask, Group, Member, Task};
5use async_trait::async_trait;
6use cache::BLSResultCache;
7pub use dkg_core::primitives::DKGOutput;
8use error::DataAccessResult;
9use ethers_core::types::Address;
10use std::collections::BTreeMap;
11use std::fmt::Debug;
12use threshold_bls::{group::Curve, sig::Share};
13
14pub trait BlockInfoHandler:
15 BlockInfoFetcher + BlockInfoUpdater + std::fmt::Debug + Sync + Send
16{
17}
18
19pub trait NodeInfoHandler<PC: Curve>:
20 NodeInfoFetcher<PC> + NodeInfoUpdater<PC> + ContextInfoUpdater + std::fmt::Debug + Sync + Send
21{
22}
23
24pub trait GroupInfoHandler<PC: Curve>:
25 GroupInfoFetcher<PC> + GroupInfoUpdater<PC> + ContextInfoUpdater + std::fmt::Debug + Sync + Send
26{
27}
28pub trait BLSTasksHandler<T: Task>:
29 BLSTasksFetcher<T> + BLSTasksUpdater<T> + std::fmt::Debug + Sync + Send
30{
31}
32pub trait SignatureResultCacheHandler<T: ResultCache>:
33 SignatureResultCacheFetcher<T> + SignatureResultCacheUpdater<T> + std::fmt::Debug + Sync + Send
34{
35}
36
37pub trait BlockInfoFetcher {
38 fn get_chain_id(&self) -> usize;
39
40 fn get_block_height(&self) -> usize;
41
42 fn get_block_time(&self) -> usize;
43}
44
45pub trait BlockInfoUpdater {
46 fn set_block_height(&mut self, block_height: usize);
47}
48
49pub trait ContextInfoUpdater: std::fmt::Debug {
50 fn refresh_context_entry(&self);
51}
52
53#[async_trait]
54pub trait NodeInfoUpdater<C: Curve> {
55 async fn set_node_rpc_endpoint(&mut self, node_rpc_endpoint: String) -> DataAccessResult<()>;
56
57 async fn set_dkg_key_pair(
58 &mut self,
59 dkg_private_key: C::Scalar,
60 dkg_public_key: C::Point,
61 ) -> DataAccessResult<()>;
62}
63
64pub trait NodeInfoFetcher<C: Curve>: std::fmt::Debug {
65 fn get_id_address(&self) -> DataAccessResult<Address>;
66
67 fn get_node_rpc_endpoint(&self) -> DataAccessResult<&str>;
68
69 fn get_dkg_private_key(&self) -> DataAccessResult<&C::Scalar>;
70
71 fn get_dkg_public_key(&self) -> DataAccessResult<&C::Point>;
72}
73
74#[async_trait]
75pub trait GroupInfoUpdater<C: Curve> {
76 async fn save_task_info(&mut self, self_index: usize, task: DKGTask) -> DataAccessResult<()>;
77
78 async fn save_successful_output(
79 &mut self,
80 index: usize,
81 epoch: usize,
82 output: DKGOutput<C>,
83 ) -> DataAccessResult<(C::Point, C::Point, Vec<Address>)>;
84
85 async fn save_failed_output(
86 &mut self,
87 index: usize,
88 epoch: usize,
89 disqualified_node_indices: Vec<u32>,
90 ) -> DataAccessResult<Vec<Address>>;
91
92 async fn update_dkg_status(
93 &mut self,
94 index: usize,
95 epoch: usize,
96 dkg_status: DKGStatus,
97 ) -> DataAccessResult<bool>;
98
99 async fn save_committers(
100 &mut self,
101 index: usize,
102 epoch: usize,
103 committer_indices: Vec<Address>,
104 ) -> DataAccessResult<()>;
105
106 async fn sync_up_members(
107 &mut self,
108 index: usize,
109 epoch: usize,
110 members: BTreeMap<Address, Member<C>>,
111 ) -> DataAccessResult<bool>;
112}
113
114pub trait GroupInfoFetcher<C: Curve>: std::fmt::Debug {
115 fn get_group(&self) -> DataAccessResult<&Group<C>>;
116
117 fn get_index(&self) -> DataAccessResult<usize>;
118
119 fn get_epoch(&self) -> DataAccessResult<usize>;
120
121 fn get_size(&self) -> DataAccessResult<usize>;
122
123 fn get_threshold(&self) -> DataAccessResult<usize>;
124
125 fn get_state(&self) -> DataAccessResult<bool>;
126
127 fn get_self_index(&self) -> DataAccessResult<usize>;
128
129 fn get_public_key(&self) -> DataAccessResult<&C::Point>;
130
131 fn get_secret_share(&self) -> DataAccessResult<&Share<C::Scalar>>;
132
133 fn get_members(&self) -> DataAccessResult<&BTreeMap<Address, Member<C>>>;
134
135 fn get_member(&self, id_address: Address) -> DataAccessResult<&Member<C>>;
136
137 fn get_committers(&self) -> DataAccessResult<Vec<Address>>;
138
139 fn get_dkg_start_block_height(&self) -> DataAccessResult<usize>;
140
141 fn get_dkg_status(&self) -> DataAccessResult<DKGStatus>;
142
143 fn is_committer(&self, id_address: Address) -> DataAccessResult<bool>;
144}
145
146#[async_trait]
147pub trait BLSTasksFetcher<T: Task> {
148 async fn contains(&self, task_request_id: &[u8]) -> DataAccessResult<bool>;
149
150 async fn get(&self, task_request_id: &[u8]) -> DataAccessResult<T>;
151
152 async fn is_handled(&self, task_request_id: &[u8]) -> DataAccessResult<bool>;
153}
154
155#[async_trait]
156pub trait BLSTasksUpdater<T: Task> {
157 async fn add(&mut self, task: T) -> DataAccessResult<()>;
158
159 async fn check_and_get_available_tasks(
160 &mut self,
161 current_block_height: usize,
162 current_group_index: usize,
163 randomness_task_exclusive_window: usize,
164 ) -> DataAccessResult<Vec<T>>;
165}
166
167#[async_trait]
168pub trait SignatureResultCacheFetcher<T: ResultCache> {
169 async fn contains(&self, task_request_id: &[u8]) -> DataAccessResult<bool>;
170
171 async fn get(&self, task_request_id: &[u8]) -> DataAccessResult<BLSResultCache<T>>;
172}
173
174#[derive(Debug, PartialEq, Clone, Copy)]
175pub enum BLSResultCacheState {
176 NotCommitted,
177 Committing,
178 Committed,
179 CommittedByOthers,
180 Expired,
181 FAULTY,
182}
183
184impl BLSResultCacheState {
185 pub fn to_i32(&self) -> i32 {
186 match self {
187 BLSResultCacheState::NotCommitted => 0,
188 BLSResultCacheState::Committing => 1,
189 BLSResultCacheState::Committed => 2,
190 BLSResultCacheState::CommittedByOthers => 3,
191 BLSResultCacheState::Expired => 4,
192 BLSResultCacheState::FAULTY => 5,
193 }
194 }
195}
196
197impl From<i32> for BLSResultCacheState {
198 fn from(b: i32) -> Self {
199 match b {
200 0 => BLSResultCacheState::NotCommitted,
201 1 => BLSResultCacheState::Committing,
202 2 => BLSResultCacheState::Committed,
203 3 => BLSResultCacheState::CommittedByOthers,
204 4 => BLSResultCacheState::Expired,
205 5 => BLSResultCacheState::FAULTY,
206 _ => panic!("Invalid BLSResultCacheState"),
207 }
208 }
209}
210
211#[async_trait]
212pub trait SignatureResultCacheUpdater<T: ResultCache> {
213 async fn get_ready_to_commit_signatures(
214 &mut self,
215 current_block_height: usize,
216 ) -> DataAccessResult<Vec<T>>;
217
218 async fn add(
219 &mut self,
220 group_index: usize,
221 task: T::Task,
222 message: T::M,
223 threshold: usize,
224 ) -> DataAccessResult<bool>;
225
226 async fn add_partial_signature(
227 &mut self,
228 task_request_id: Vec<u8>,
229 member_address: Address,
230 partial_signature: Vec<u8>,
231 ) -> DataAccessResult<bool>;
232
233 async fn update_commit_result(
234 &mut self,
235 task_request_id: &[u8],
236 status: BLSResultCacheState,
237 ) -> DataAccessResult<()>;
238
239 async fn incr_committed_times(&mut self, task_request_id: &[u8]) -> DataAccessResult<()>;
240}
241
242pub trait ResultCache: Task + Clone {
243 type Task: Debug;
244 type M;
245}