Skip to content

Commit

Permalink
Dzejkop/identity-history (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop authored Nov 16, 2023
1 parent fc4fd96 commit b918ad8
Show file tree
Hide file tree
Showing 7 changed files with 907 additions and 177 deletions.
136 changes: 58 additions & 78 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use std::time::Instant;
use anyhow::Result as AnyhowResult;
use chrono::{Duration, Utc};
use clap::Parser;
use hyper::StatusCode;
use ruint::Uint;
use semaphore::poseidon_tree::LazyPoseidonTree;
use semaphore::protocol::verify_proof;
use serde::Serialize;
use tracing::{info, instrument, warn};

use crate::contracts::{IdentityManager, SharedIdentityManager};
Expand All @@ -21,87 +19,16 @@ use crate::identity_tree::{
};
use crate::prover::map::initialize_prover_maps;
use crate::prover::{self, ProverConfiguration, ProverType, Provers};
use crate::server::data::{
IdentityHistoryEntry, IdentityHistoryEntryKind, IdentityHistoryEntryStatus,
InclusionProofResponse, ListBatchSizesResponse, VerifySemaphoreProofQuery,
VerifySemaphoreProofRequest, VerifySemaphoreProofResponse,
};
use crate::server::error::Error as ServerError;
use crate::server::{ToResponseCode, VerifySemaphoreProofQuery, VerifySemaphoreProofRequest};
use crate::task_monitor::TaskMonitor;
use crate::utils::tree_updates::dedup_tree_updates;
use crate::{contracts, task_monitor};

#[derive(Serialize)]
#[serde(transparent)]
pub struct InclusionProofResponse(InclusionProof);

impl InclusionProofResponse {
#[must_use]
pub fn hide_processed_status(mut self) -> Self {
self.0.status = if self.0.status == Status::Processed(ProcessedStatus::Processed) {
Status::Processed(ProcessedStatus::Pending)
} else {
self.0.status
};

self
}
}

impl From<InclusionProof> for InclusionProofResponse {
fn from(value: InclusionProof) -> Self {
Self(value)
}
}

impl ToResponseCode for InclusionProofResponse {
fn to_response_code(&self) -> StatusCode {
match self.0.status {
Status::Unprocessed(UnprocessedStatus::Failed) => StatusCode::BAD_REQUEST,
Status::Unprocessed(UnprocessedStatus::New)
| Status::Processed(ProcessedStatus::Pending) => StatusCode::ACCEPTED,
Status::Processed(ProcessedStatus::Mined | ProcessedStatus::Processed) => {
StatusCode::OK
}
}
}
}

#[derive(Serialize)]
#[serde(transparent)]
pub struct ListBatchSizesResponse(Vec<ProverConfiguration>);

impl From<Vec<ProverConfiguration>> for ListBatchSizesResponse {
fn from(value: Vec<ProverConfiguration>) -> Self {
Self(value)
}
}

impl ToResponseCode for ListBatchSizesResponse {
fn to_response_code(&self) -> StatusCode {
StatusCode::OK
}
}

#[derive(Serialize)]
#[serde(transparent)]
pub struct VerifySemaphoreProofResponse(RootItem);

impl VerifySemaphoreProofResponse {
#[must_use]
pub fn hide_processed_status(mut self) -> Self {
self.0.status = if self.0.status == ProcessedStatus::Processed {
ProcessedStatus::Pending
} else {
self.0.status
};

self
}
}

impl ToResponseCode for VerifySemaphoreProofResponse {
fn to_response_code(&self) -> StatusCode {
StatusCode::OK
}
}

#[derive(Clone, Debug, PartialEq, Parser)]
#[group(skip)]
pub struct Options {
Expand Down Expand Up @@ -637,6 +564,59 @@ impl App {
Ok(())
}

pub async fn identity_history(
&self,
commitment: &Hash,
) -> Result<Vec<IdentityHistoryEntry>, ServerError> {

Check warning on line 570 in src/app.rs

View workflow job for this annotation

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/app.rs:567:5 | 567 | / pub async fn identity_history( 568 | | &self, 569 | | commitment: &Hash, 570 | | ) -> Result<Vec<IdentityHistoryEntry>, ServerError> { | |_______________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc = note: `#[warn(clippy::missing_errors_doc)]` implied by `#[warn(clippy::pedantic)]`

Check warning on line 570 in src/app.rs

View workflow job for this annotation

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/app.rs:567:5 | 567 | / pub async fn identity_history( 568 | | &self, 569 | | commitment: &Hash, 570 | | ) -> Result<Vec<IdentityHistoryEntry>, ServerError> { | |_______________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc = note: `#[warn(clippy::missing_errors_doc)]` implied by `#[warn(clippy::pedantic)]`
let entries = self
.database
.get_identity_history_entries(commitment)
.await?;

let mut history = vec![];

for entry in entries {
let mut status = match entry.status {
Status::Processed(ProcessedStatus::Pending) => IdentityHistoryEntryStatus::Pending,
Status::Processed(ProcessedStatus::Processed) => IdentityHistoryEntryStatus::Mined,
Status::Processed(ProcessedStatus::Mined) => IdentityHistoryEntryStatus::Bridged,
Status::Unprocessed(UnprocessedStatus::New) => IdentityHistoryEntryStatus::Buffered,
// This status virtually never happens so we'll mark it as buffered
Status::Unprocessed(UnprocessedStatus::Failed) => {
IdentityHistoryEntryStatus::Buffered
}
};

match status {
// A pending identity can be present in the batching tree and therefore status
// should be set to Batched
IdentityHistoryEntryStatus::Pending => {
if let Some(leaf_index) = entry.leaf_index {
if self.tree_state.get_batching_tree().get_leaf(leaf_index)
== entry.commitment
{
status = IdentityHistoryEntryStatus::Batched;
}
}
}
IdentityHistoryEntryStatus::Buffered if entry.held_back => {
status = IdentityHistoryEntryStatus::Queued;
}
_ => (),
}

let kind = if entry.commitment == Uint::ZERO {
IdentityHistoryEntryKind::Deletion
} else {
IdentityHistoryEntryKind::Insertion
};

history.push(IdentityHistoryEntry { kind, status });
}

Ok(history)
}

fn merge_env_provers(options: prover::Options, existing_provers: &mut Provers) -> Provers {
let options_set: HashSet<ProverConfiguration> = options
.prover_urls
Expand Down
2 changes: 0 additions & 2 deletions src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,6 @@ impl IdentityManager {
let packed_deletion_indices: &[u8] = delete_identities.packed_deletion_indices.as_ref();
let indices = unpack_indices(packed_deletion_indices);

tracing::error!("unpacked = {indices:?}");

let padding_index = 2u32.pow(self.tree_depth as u32);

Check warning on line 382 in src/contracts/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers

warning: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers --> src/contracts/mod.rs:382:38 | 382 | let padding_index = 2u32.pow(self.tree_depth as u32); | ^^^^^^^^^^^^^^^^^^^^^^ | = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ... = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation = note: `#[warn(clippy::cast_possible_truncation)]` implied by `#[warn(clippy::pedantic)]` help: ... or use `try_from` and handle the error accordingly | 382 | let padding_index = 2u32.pow(u32::try_from(self.tree_depth)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check warning on line 382 in src/contracts/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers

warning: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers --> src/contracts/mod.rs:382:38 | 382 | let padding_index = 2u32.pow(self.tree_depth as u32); | ^^^^^^^^^^^^^^^^^^^^^^ | = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ... = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation = note: `#[warn(clippy::cast_possible_truncation)]` implied by `#[warn(clippy::pedantic)]` help: ... or use `try_from` and handle the error accordingly | 382 | let padding_index = 2u32.pow(u32::try_from(self.tree_depth)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check warning on line 382 in src/contracts/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers

warning: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers --> src/contracts/mod.rs:382:38 | 382 | let padding_index = 2u32.pow(self.tree_depth as u32); | ^^^^^^^^^^^^^^^^^^^^^^ | = help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ... = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation = note: `#[warn(clippy::cast_possible_truncation)]` implied by `#[warn(clippy::pedantic)]` help: ... or use `try_from` and handle the error accordingly | 382 | let padding_index = 2u32.pow(u32::try_from(self.tree_depth)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ok(indices
Expand Down
Loading

0 comments on commit b918ad8

Please sign in to comment.