miden_node_store/allowlist/
mod.rs1use std::path::Path;
7
8use miden_node_db::sqlite::{DbReader, DbWriter, WriteTx};
9use miden_node_tracing::instrument;
10use miden_protocol::account::AccountId;
11use thiserror::Error;
12
13use crate::DatabaseError;
14
15mod invitation;
16mod migrations;
17mod queries;
18
19pub use invitation::{InvalidInvitationCode, InvitationCode};
20
21#[cfg(test)]
22mod tests;
23
24#[derive(Clone, Debug)]
26pub struct InvitationEntry {
27 pub invitation_code: InvitationCode,
28 pub account_id: Option<AccountId>,
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub struct InvitationImportOutcome {
34 pub invitation_added: bool,
35 pub registered_account: Option<AccountId>,
37}
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum InvitationStatus {
42 Unknown,
43 Unused,
44 Registered(AccountId),
45}
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub struct InvitationInfo {
50 pub account_id: Option<AccountId>,
51 pub allowlisted_at: i64,
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub enum RegistrationOutcome {
57 Registered,
58 AlreadyRegistered,
60}
61
62#[derive(Debug, Error)]
64pub enum AllowlistError {
65 #[error("invitation code does not exist")]
66 InvitationNotFound,
67 #[error("invitation code is already registered to another account")]
68 InvitationAlreadyUsed,
69 #[error("account {0} is already registered")]
70 AccountAlreadyRegistered(AccountId),
71 #[error("account registry database operation failed")]
72 Database(#[source] DatabaseError),
73}
74
75#[derive(Clone)]
77pub struct AccountAllowlistReader {
78 db: DbReader,
79}
80
81impl AccountAllowlistReader {
82 #[instrument(
84 name = "allowlisted_at",
85 fields(account.id = account_id),
86 err(level = "debug"),
87 )]
88 pub async fn allowlisted_at(
89 &self,
90 account_id: AccountId,
91 ) -> Result<Option<i64>, DatabaseError> {
92 self.db
93 .read("allowlist.allowlisted_at", move |tx| queries::allowlisted_at(tx, account_id))
94 .await
95 .map_err(DatabaseError::DatabaseError)
96 }
97
98 #[instrument(name = "invitation_info", err(level = "debug"))]
100 pub async fn invitation_info(
101 &self,
102 invitation_code: InvitationCode,
103 ) -> Result<Option<InvitationInfo>, DatabaseError> {
104 self.db
105 .read("allowlist.invitation_info", move |tx| {
106 queries::invitation_info(tx, &invitation_code)
107 })
108 .await
109 .map_err(DatabaseError::DatabaseError)
110 }
111
112 #[instrument(
114 name = "contains_account",
115 fields(account.id = account_id),
116 err(level = "debug"),
117 )]
118 pub async fn contains_account(&self, account_id: AccountId) -> Result<bool, DatabaseError> {
119 self.db
120 .read("allowlist.contains_account", move |tx| {
121 queries::contains_account(tx, account_id)
122 })
123 .await
124 .map_err(DatabaseError::DatabaseError)
125 }
126
127 #[instrument(name = "invitation_status", err(level = "debug"))]
129 pub async fn invitation_status(
130 &self,
131 invitation_code: InvitationCode,
132 ) -> Result<InvitationStatus, DatabaseError> {
133 self.db
134 .read("allowlist.invitation_status", move |tx| {
135 queries::invitation_status(tx, &invitation_code)
136 })
137 .await
138 .map_err(DatabaseError::DatabaseError)
139 }
140}
141
142pub struct AccountAllowlist {
149 writer: DbWriter,
150 reader: AccountAllowlistReader,
151}
152
153impl std::ops::Deref for AccountAllowlist {
154 type Target = AccountAllowlistReader;
155
156 fn deref(&self) -> &Self::Target {
157 &self.reader
158 }
159}
160
161impl AccountAllowlist {
162 pub fn bootstrap(database_filepath: impl AsRef<Path>) -> Result<(), DatabaseError> {
166 let migrator = migrations::migrator()
167 .map_err(miden_node_db::DatabaseError::migration)
168 .map_err(DatabaseError::DatabaseError)?;
169 migrator
170 .create_database(database_filepath)
171 .and_then(|mut database| database.migrate_to(migrator.latest_version()))
172 .map_err(miden_node_db::DatabaseError::migration)
173 .map_err(DatabaseError::DatabaseError)
174 }
175
176 pub fn load(database_filepath: impl AsRef<Path>) -> Result<Self, DatabaseError> {
180 let database_filepath = database_filepath.as_ref();
181 let migrator = migrations::migrator()
182 .map_err(miden_node_db::DatabaseError::migration)
183 .map_err(DatabaseError::DatabaseError)?;
184 migrator
185 .verify_latest_schema(database_filepath)
186 .map_err(miden_node_db::DatabaseError::migration)
187 .map_err(DatabaseError::DatabaseError)?;
188 let (writer, reader) =
189 miden_node_db::sqlite::open(database_filepath).map_err(DatabaseError::DatabaseError)?;
190 Ok(Self {
191 writer,
192 reader: AccountAllowlistReader { db: reader },
193 })
194 }
195
196 pub fn migrate(database_filepath: impl AsRef<Path>) -> Result<(), DatabaseError> {
198 let migrator = migrations::migrator()
199 .map_err(miden_node_db::DatabaseError::migration)
200 .map_err(DatabaseError::DatabaseError)?;
201 migrator
202 .open_database(database_filepath)
203 .and_then(|mut database| database.migrate_to(migrator.latest_version()))
204 .map_err(miden_node_db::DatabaseError::migration)
205 .map_err(DatabaseError::DatabaseError)
206 }
207
208 pub fn reader(&self) -> AccountAllowlistReader {
210 self.reader.clone()
211 }
212
213 #[instrument(
220 name = "import_invitation",
221 fields(account.id = entry.account_id),
222 err,
223 )]
224 pub async fn import_invitation(
225 &self,
226 entry: InvitationEntry,
227 ) -> Result<InvitationImportOutcome, AllowlistError> {
228 self.transact("allowlist.import_invitation", move |tx| {
229 queries::import_invitation(tx, &entry)
230 })
231 .await
232 }
233
234 #[instrument(
238 name = "add_account",
239 fields(account.id = account_id),
240 err,
241 )]
242 pub async fn add_account(&self, account_id: AccountId) -> Result<bool, DatabaseError> {
243 self.writer
244 .write("allowlist.add_account", move |tx| queries::add_account(tx, account_id))
245 .await
246 .map_err(DatabaseError::DatabaseError)
247 }
248
249 #[instrument(
254 name = "register_account",
255 fields(account.id = account_id),
256 err,
257 )]
258 pub async fn register_account(
259 &self,
260 invitation_code: InvitationCode,
261 account_id: AccountId,
262 ) -> Result<RegistrationOutcome, AllowlistError> {
263 self.transact("allowlist.register_account", move |tx| {
264 queries::register_account(tx, &invitation_code, account_id)
265 })
266 .await
267 }
268
269 async fn transact<T: Send + 'static>(
270 &self,
271 name: &'static str,
272 query: impl FnOnce(&WriteTx<'_>) -> Result<T, AllowlistError> + Send + 'static,
273 ) -> Result<T, AllowlistError> {
274 let tx = self
275 .writer
276 .begin_write()
277 .await
278 .map_err(DatabaseError::DatabaseError)
279 .map_err(AllowlistError::Database)?;
280 let result = tx
281 .run(name, move |tx| Ok::<_, miden_node_db::DatabaseError>(query(tx)))
282 .await
283 .map_err(DatabaseError::DatabaseError)
284 .map_err(AllowlistError::Database)
285 .and_then(std::convert::identity);
286
287 match result {
288 Ok(value) => {
289 tx.commit()
290 .await
291 .map_err(DatabaseError::DatabaseError)
292 .map_err(AllowlistError::Database)?;
293 Ok(value)
294 },
295 Err(error) => {
296 tx.rollback()
297 .await
298 .map_err(DatabaseError::DatabaseError)
299 .map_err(AllowlistError::Database)?;
300 Err(error)
301 },
302 }
303 }
304}