Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
Remove and delete previous federation data
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyRonning committed Jan 26, 2024
1 parent 2872196 commit 974cdda
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
21 changes: 18 additions & 3 deletions mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub(crate) struct FederationClient<S: MutinyStorage> {
pub(crate) uuid: String,
pub(crate) fedimint_client: ClientArc,
storage: S,
fedimint_storage: FedimintStorage<S>,
pub(crate) logger: Arc<MutinyLogger>,
}

Expand Down Expand Up @@ -184,13 +185,13 @@ impl<S: MutinyStorage> FederationClient<S> {
client_builder.with_module(LightningClientInit);

log_trace!(logger, "Building fedimint client db");
let db = FedimintStorage::new(
let fedimint_storage = FedimintStorage::new(
storage.clone(),
federation_info.federation_id().to_string(),
logger.clone(),
)
.await?
.into();
.await?;
let db = fedimint_storage.clone().into();

if get_config_from_db(&db).await.is_none() {
client_builder.with_federation_info(federation_info.clone());
Expand Down Expand Up @@ -226,6 +227,7 @@ impl<S: MutinyStorage> FederationClient<S> {
Ok(FederationClient {
uuid,
fedimint_client,
fedimint_storage,
storage: storage.clone(),
logger,
})
Expand Down Expand Up @@ -527,6 +529,10 @@ impl<S: MutinyStorage> FederationClient<S> {
welcome_message: self.fedimint_client.get_meta("welcome_message"),
}
}

pub async fn delete_fedimint_storage(&self) -> Result<(), MutinyError> {
self.fedimint_storage.delete_store().await
}
}

// A federation private key will be derived from
Expand Down Expand Up @@ -770,6 +776,15 @@ impl<S: MutinyStorage> FedimintStorage<S> {
fedimint_memory: Arc::new(fedimint_memory),
})
}

pub async fn delete_store(&self) -> Result<(), MutinyError> {
let mut mem_db_tx = self.begin_transaction().await;
mem_db_tx.raw_remove_by_prefix(&[]).await?;
mem_db_tx
.commit_tx()
.await
.map_err(|_| MutinyError::write_err(MutinyStorageError::IndexedDBError))
}
}

fn key_id(federation_id: &str) -> String {
Expand Down
1 change: 1 addition & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
self.storage
.insert_federations(federation_storage_guard.clone())
.await?;
fedimint_client.delete_fedimint_storage().await?;
federations_guard.remove(&federation_id);
} else {
return Err(MutinyError::NotFound);
Expand Down
6 changes: 5 additions & 1 deletion mutiny-core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ pub trait MutinyStorage: Clone + Sized + Send + Sync + 'static {
}

/// Inserts the federation indexes into storage
async fn insert_federations(&self, federations: FederationStorage) -> Result<(), MutinyError> {
async fn insert_federations(
&self,
mut federations: FederationStorage,
) -> Result<(), MutinyError> {
federations.version += 1;
let version = Some(federations.version);
self.set_data_async(FEDERATIONS_KEY.to_string(), federations, version)
.await
Expand Down

0 comments on commit 974cdda

Please sign in to comment.