generated from recmo/rust-service-template
-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relax transaction isolation levels + fix insert identities constraints #795
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f320d3e
API level txs don't need to be RepeatableRead or Serializable
Dzejkop a664468
Tree init read commiteed
Dzejkop b4b465d
WIP
Dzejkop b4115e5
Relax most transactions in processor.rs
Dzejkop 88f8a48
wip
Dzejkop 28b34c3
wip
Dzejkop 5f65706
fix test
Dzejkop 3f9b79b
fmt
Dzejkop c38d99b
informative comment
Dzejkop 9dd897e
Preserve deletion logic
Dzejkop 7ec2697
Remove invalid tests
Dzejkop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,24 +4,22 @@ use std::sync::{Arc, OnceLock}; | |
use chrono::{Duration, Utc}; | ||
use ruint::Uint; | ||
use semaphore::protocol::verify_proof; | ||
use sqlx::{Postgres, Transaction}; | ||
use tracing::{info, instrument, warn}; | ||
|
||
use crate::config::Config; | ||
use crate::contracts::IdentityManager; | ||
use crate::database::methods::DbMethods as _; | ||
use crate::database::Database; | ||
use crate::database::{Database, IsolationLevel}; | ||
use crate::ethereum::Ethereum; | ||
use crate::identity::processor::{ | ||
IdentityProcessor, OffChainIdentityProcessor, OnChainIdentityProcessor, | ||
}; | ||
use crate::identity::validator::IdentityValidator; | ||
use crate::identity_tree::initializer::TreeInitializer; | ||
use crate::identity_tree::{Hash, InclusionProof, RootItem, TreeState, TreeVersionReadOps}; | ||
use crate::identity_tree::{Hash, InclusionProof, RootItem, TreeState, TreeVersionOps}; | ||
use crate::prover::map::initialize_prover_maps; | ||
use crate::prover::repository::ProverRepository; | ||
use crate::prover::{ProverConfig, ProverType}; | ||
use crate::retry_tx; | ||
use crate::server::data::{ | ||
InclusionProofResponse, ListBatchSizesResponse, VerifySemaphoreProofQuery, | ||
VerifySemaphoreProofRequest, VerifySemaphoreProofResponse, | ||
|
@@ -157,22 +155,19 @@ impl App { | |
|
||
// TODO: ensure that the id is not in the tree or in unprocessed identities | ||
|
||
if self.database.identity_exists(commitment).await? { | ||
let mut tx = self | ||
.database | ||
.begin_tx(IsolationLevel::ReadCommitted) | ||
.await?; | ||
|
||
if tx.identity_exists(commitment).await? { | ||
return Err(ServerError::DuplicateCommitment); | ||
} | ||
|
||
self.database | ||
.insert_new_identity(commitment, Utc::now()) | ||
.await?; | ||
tx.insert_new_identity(commitment, Utc::now()).await?; | ||
|
||
Ok(()) | ||
} | ||
tx.commit().await?; | ||
|
||
pub async fn delete_identity_tx(&self, commitment: &Hash) -> Result<(), ServerError> { | ||
retry_tx!(self.database.pool, tx, { | ||
self.delete_identity(&mut tx, commitment).await | ||
}) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
|
@@ -182,12 +177,13 @@ impl App { | |
/// | ||
/// Will return `Err` if identity is already queued, not in the tree, or the | ||
/// queue malfunctions. | ||
#[instrument(level = "debug", skip(self, tx))] | ||
pub async fn delete_identity( | ||
&self, | ||
tx: &mut Transaction<'_, Postgres>, | ||
commitment: &Hash, | ||
) -> Result<(), ServerError> { | ||
#[instrument(level = "debug", skip(self))] | ||
pub async fn delete_identity(&self, commitment: &Hash) -> Result<(), ServerError> { | ||
let mut tx = self | ||
.database | ||
.begin_tx(IsolationLevel::RepeatableRead) | ||
.await?; | ||
|
||
// Ensure that deletion provers exist | ||
if !self.prover_repository.has_deletion_provers().await { | ||
warn!( | ||
|
@@ -213,76 +209,18 @@ impl App { | |
return Err(ServerError::IdentityAlreadyDeleted); | ||
} | ||
|
||
// Check if the id is already queued for deletion | ||
if tx.identity_is_queued_for_deletion(commitment).await? { | ||
return Err(ServerError::IdentityQueuedForDeletion); | ||
} | ||
|
||
// Check if there are any deletions, if not, set the latest deletion timestamp | ||
// to now to ensure that the new deletion is processed by the next deletion | ||
// interval | ||
if tx.get_deletions().await?.is_empty() { | ||
tx.update_latest_deletion(Utc::now()).await?; | ||
} | ||
|
||
// If the id has not been deleted, insert into the deletions table | ||
tx.insert_new_deletion(leaf_index, commitment).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Queues a recovery of an identity. | ||
/// | ||
/// i.e. deletion and reinsertion after a set period of time. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Will return `Err` if identity is already queued for deletion, not in the | ||
/// tree, or the queue malfunctions. | ||
#[instrument(level = "debug", skip(self))] | ||
pub async fn recover_identity( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First step in disabling recovery - deleting the API endpoint |
||
&self, | ||
existing_commitment: &Hash, | ||
new_commitment: &Hash, | ||
) -> Result<(), ServerError> { | ||
retry_tx!(self.database.pool, tx, { | ||
if self.identity_validator.is_initial_leaf(new_commitment) { | ||
warn!( | ||
?new_commitment, | ||
"Attempt to insert initial leaf in recovery." | ||
); | ||
return Err(ServerError::InvalidCommitment); | ||
} | ||
tx.commit().await?; | ||
|
||
if !self.prover_repository.has_insertion_provers().await { | ||
warn!( | ||
?new_commitment, | ||
"Identity Manager has no provers. Add provers with /addBatchSize request." | ||
); | ||
return Err(ServerError::NoProversOnIdInsert); | ||
} | ||
|
||
if !self.identity_validator.is_reduced(*new_commitment) { | ||
warn!( | ||
?new_commitment, | ||
"The new identity commitment is not reduced." | ||
); | ||
return Err(ServerError::UnreducedCommitment); | ||
} | ||
|
||
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(&mut tx, existing_commitment).await?; | ||
|
||
tx.insert_new_recovery(existing_commitment, new_commitment) | ||
.await?; | ||
|
||
Ok(()) | ||
}) | ||
.await | ||
Ok(()) | ||
} | ||
|
||
fn merge_env_provers( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
insert_new_deletion
method has been changed to be idempotent. Therefore there's no longer a need to check if we have already accepted a deletion