Skip to content

Commit

Permalink
transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
0xForerunner committed Feb 8, 2024
1 parent 95ec082 commit 1811b01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
33 changes: 19 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,21 @@ impl App {
Ok(())
}

pub async fn delete_identity(&self, commitment: &Hash) -> Result<(), ServerError> {
let mut tx = self.database.begin().await?;
self.delete_identity_tx(&mut tx, commitment).await?;
tx.commit().await?;
Ok(())
}

/// Queues a deletion from the merkle tree.
///
/// # Errors
///
/// Will return `Err` if identity is already queued, not in the tree, or the
/// queue malfunctions.
#[instrument(level = "debug", skip(self))]
pub async fn delete_identity(
#[instrument(level = "debug", skip(self, tx))]
pub async fn delete_identity_tx(
&self,
tx: &mut Transaction<'_, Postgres>,
commitment: &Hash,
Expand Down Expand Up @@ -412,11 +419,7 @@ impl App {
}

// Check if the id is already queued for deletion
if self
.database
.identity_is_queued_for_deletion(commitment)
.await?
{
if tx.identity_is_queued_for_deletion(commitment).await? {
return Err(ServerError::IdentityQueuedForDeletion);
}

Expand All @@ -428,9 +431,7 @@ impl App {
}

// If the id has not been deleted, insert into the deletions table
self.database
.insert_new_deletion(leaf_index, commitment)
.await?;
tx.insert_new_deletion(leaf_index, commitment).await?;

Ok(())
}
Expand Down Expand Up @@ -473,17 +474,21 @@ impl App {
return Err(ServerError::UnreducedCommitment);
}

if self.database.identity_exists(*new_commitment).await? {
let mut tx = self.database.begin().await?;

if tx.identity_exists(*new_commitment).await? {
return Err(ServerError::DuplicateCommitment);
}

// Delete the existing id and insert the commitments into the recovery table
self.delete_identity(existing_commitment).await?;
self.delete_identity_tx(&mut tx, existing_commitment)
.await?;

self.database
.insert_new_recovery(existing_commitment, new_commitment)
tx.insert_new_recovery(existing_commitment, new_commitment)
.await?;

tx.commit().await?;

Ok(())
}

Expand Down
9 changes: 7 additions & 2 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ impl Database {
// Create a connection pool
let pool = PoolOptions::<Postgres>::new()
.max_connections(config.max_connections)
.after_connect(|conn, _| {
Box::pin(async move {
conn.execute("SET DEFAULT_TRANSACTION_ISOLATION TO 'SERIALIZABLE'")
.await?;
Ok(())
})
})
.connect(config.database.expose())
.await
.context("error connecting to database")?;
Expand Down Expand Up @@ -188,8 +195,6 @@ impl Database {
let mined_status = ProcessedStatus::Mined;

let mut tx = self.pool.begin().await?;
tx.execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;")
.await?;

let root_id = tx.get_id_by_root(root).await?;

Expand Down
2 changes: 2 additions & 0 deletions src/server/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub enum Error {
NoProversOnIdInsert,
#[error("Identity Manager had no provers on point of identity deletion.")]
NoProversOnIdDeletion,
#[error(transparent)]
Sqlx(#[from] sqlx::Error),
#[error("The tree is uninitialized. Try again in a few moments.")]
TreeStateUninitialized,
#[error(transparent)]
Expand Down

0 comments on commit 1811b01

Please sign in to comment.