Skip to content

Commit

Permalink
disabled bloomfilter exporting in nym-api
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Oct 10, 2024
1 parent 9341db5 commit 54a4fc8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 76 deletions.
7 changes: 2 additions & 5 deletions nym-api/src/ecash/api_routes/spending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ pub async fn batch_redeem_tickets(
#[openapi(tag = "Ecash")]
#[get("/double-spending-filter-v1")]
pub async fn double_spending_filter_v1(
state: &RocketState<EcashState>,
_state: &RocketState<EcashState>,
) -> crate::ecash::error::Result<Json<SpentCredentialsResponse>> {
let spent_credentials_export = state.get_bloomfilter_bytes().await;
Ok(Json(SpentCredentialsResponse::new(
spent_credentials_export,
)))
Err(EcashError::Restricted)
}
10 changes: 4 additions & 6 deletions nym-api/src/ecash/api_routes/spending_axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

use crate::ecash::error::EcashError;
use crate::ecash::state::EcashState;
use crate::node_status_api::models::AxumResult;
use crate::node_status_api::models::{AxumErrorResponse, AxumResult};
use crate::v2::AxumAppState;
use axum::{Json, Router};
use nym_api_requests::constants::MIN_BATCH_REDEMPTION_DELAY;
use nym_api_requests::ecash::models::{
BatchRedeemTicketsBody, EcashBatchTicketRedemptionResponse, EcashTicketVerificationRejection,
EcashTicketVerificationResponse, SpentCredentialsResponse, VerifyEcashTicketBody,
};
use nym_api_requests::models::RequestError;
use nym_compact_ecash::identify::IdentifyResult;
use nym_ecash_time::EcashTime;
use std::collections::HashSet;
Expand Down Expand Up @@ -236,10 +237,7 @@ async fn batch_redeem_tickets(
)
)]
async fn double_spending_filter_v1(
state: Arc<EcashState>,
_state: Arc<EcashState>,
) -> AxumResult<Json<SpentCredentialsResponse>> {
let spent_credentials_export = state.get_bloomfilter_bytes().await;
Ok(Json(SpentCredentialsResponse::new(
spent_credentials_export,
)))
AxumResult::Err(AxumErrorResponse::internal_msg("permanently restricted"))
}
3 changes: 3 additions & 0 deletions nym-api/src/ecash/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub type Result<T, E = EcashError> = std::result::Result<T, E>;

#[derive(Debug, Error)]
pub enum EcashError {
#[error("permanently restricted")]
Restricted,

#[error(transparent)]
IOError(#[from] std::io::Error),

Expand Down
52 changes: 1 addition & 51 deletions nym-api/src/ecash/state/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use crate::ecash::keys::KeyPair;
use nym_config::defaults::BloomfilterParameters;
use nym_crypto::asymmetric::identity;
use nym_ecash_double_spending::DoubleSpendingFilter;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use time::{Date, OffsetDateTime};
use time::Date;
use tokio::sync::RwLock;

pub(crate) struct TicketDoubleSpendingFilter {
Expand Down Expand Up @@ -70,28 +69,13 @@ impl TicketDoubleSpendingFilter {
self.today_filter.dump_bitmap()
}

pub(crate) fn export_global_bitmap(&self) -> Vec<u8> {
self.global_filter.dump_bitmap()
}

pub(crate) fn advance_day(&mut self, date: Date, new_global: DoubleSpendingFilter) {
self.built_on = date;
self.global_filter = new_global;
self.today_filter.reset();
}
}

pub(crate) struct ExportedDoubleSpendingFilterData {
pub(crate) last_exported_at: OffsetDateTime,
pub(crate) bytes: Vec<u8>,
}

#[derive(Clone)]
pub(crate) struct ExportedDoubleSpendingFilter {
pub(crate) being_exported: Arc<AtomicBool>,
pub(crate) data: Arc<RwLock<ExportedDoubleSpendingFilterData>>,
}

pub(crate) struct LocalEcashState {
pub(crate) ecash_keypair: KeyPair,
pub(crate) identity_keypair: identity::KeyPair,
Expand All @@ -102,9 +86,6 @@ pub(crate) struct LocalEcashState {

// the actual, up to date, bloomfilter
pub(crate) double_spending_filter: Arc<RwLock<TicketDoubleSpendingFilter>>,

// the cached byte representation of the bloomfilter to be used by the clients
pub(crate) exported_double_spending_filter: ExportedDoubleSpendingFilter,
}

impl LocalEcashState {
Expand All @@ -118,38 +99,7 @@ impl LocalEcashState {
identity_keypair,
partial_coin_index_signatures: Default::default(),
partial_expiration_date_signatures: Default::default(),
exported_double_spending_filter: ExportedDoubleSpendingFilter {
being_exported: Arc::new(Default::default()),
data: Arc::new(RwLock::new(ExportedDoubleSpendingFilterData {
last_exported_at: OffsetDateTime::now_utc(),
bytes: double_spending_filter.export_global_bitmap(),
})),
},
double_spending_filter: Arc::new(RwLock::new(double_spending_filter)),
}
}

pub(crate) fn maybe_background_update_exported_bloomfilter(&self) {
// make sure another query hasn't already spawned an exporting task
let Ok(should_export) = self
.exported_double_spending_filter
.being_exported
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
else {
return;
};

let filter = self.double_spending_filter.clone();
let exported = self.exported_double_spending_filter.clone();

if should_export {
tokio::spawn(async move {
log::debug!("exporting bloomfilter bitmap");
let new = filter.read().await.export_global_bitmap();
let mut exported_guard = exported.data.write().await;
exported_guard.last_exported_at = OffsetDateTime::now_utc();
exported_guard.bytes = new;
});
}
}
}
15 changes: 1 addition & 14 deletions nym-api/src/ecash/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use nym_ecash_time::cred_exp_date;
use nym_validator_client::nyxd::AccountId;
use nym_validator_client::EcashApiClient;
use time::ext::NumericalDuration;
use time::{Date, Duration, OffsetDateTime};
use time::{Date, OffsetDateTime};
use tokio::sync::RwLockReadGuard;

pub(crate) mod auxiliary;
Expand Down Expand Up @@ -839,17 +839,4 @@ impl EcashState {

res
}

pub async fn get_bloomfilter_bytes(&self) -> Vec<u8> {
let guard = self.local.exported_double_spending_filter.data.read().await;

let bytes = guard.bytes.clone();

// see if it's been > 5min since last export (that value is arbitrary)
if guard.last_exported_at + Duration::minutes(5) < OffsetDateTime::now_utc() {
self.local.maybe_background_update_exported_bloomfilter();
}

bytes
}
}

0 comments on commit 54a4fc8

Please sign in to comment.