Skip to content

Commit

Permalink
chore: add database migration for removing active gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
m1sterc001guy committed Mar 9, 2024
1 parent ea1f525 commit 99d296a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fedimint-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ async fn get_note_summary(client: &ClientHandle) -> anyhow::Result<serde_json::V
}

async fn get_gateway(
client: &ClientArc,
client: &ClientHandle,
gateway_id: Option<secp256k1::PublicKey>,
) -> Option<LightningGateway> {
let lightning_module = client.get_first_module::<LightningClientModule>();
Expand Down
23 changes: 9 additions & 14 deletions fedimint-client/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::BTreeMap;
use std::pin::Pin;

use fedimint_core::api::{ApiVersionSet, InviteCode};
use fedimint_core::config::{ClientConfig, FederationId};
Expand All @@ -10,6 +9,7 @@ use fedimint_core::db::{
};
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::module::registry::ModuleDecoderRegistry;
use fedimint_core::util::BoxFuture;
use fedimint_core::{impl_db_lookup, impl_db_record};
use fedimint_logging::LOG_DB;
use futures::StreamExt;
Expand Down Expand Up @@ -315,19 +315,14 @@ impl_db_record!(

/// `ClientMigrationFn` is a function that modules can implement to "migrate"
/// the database to the next database version.
pub type ClientMigrationFn = for<'r, 'tx> fn(
&'r mut DatabaseTransaction<'tx>,
ModuleInstanceId,
Vec<(Vec<u8>, OperationId)>, // active states
Vec<(Vec<u8>, OperationId)>, // inactive states
ModuleDecoderRegistry,
) -> Pin<
Box<
dyn futures::Future<Output = anyhow::Result<Option<(Vec<DynState>, Vec<DynState>)>>>
+ Send
+ 'r,
>,
>;
pub type ClientMigrationFn =
for<'r, 'tx> fn(
&'r mut DatabaseTransaction<'tx>,
ModuleInstanceId,
Vec<(Vec<u8>, OperationId)>, // active states
Vec<(Vec<u8>, OperationId)>, // inactive states
ModuleDecoderRegistry,
) -> BoxFuture<'r, anyhow::Result<Option<(Vec<DynState>, Vec<DynState>)>>>;

/// `apply_migrations_client` iterates from the on disk database version for the
/// client module up to `target_db_version` and executes all of the migrations
Expand Down
2 changes: 1 addition & 1 deletion fedimint-load-test-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ fn get_db_path(archive_dir: Option<PathBuf>) -> Option<PathBuf> {
}

async fn get_lightning_gateway(
client: &ClientArc,
client: &ClientHandle,
gateway_id: Option<String>,
) -> Option<LightningGateway> {
let gateway_id = parse_gateway_id(gateway_id.or(None)?.as_str()).expect("Invalid gateway id");
Expand Down
4 changes: 3 additions & 1 deletion fedimint-wasm-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ mod tests {
Ok(())
}

async fn get_gateway(client: &fedimint_client::ClientHandle) -> anyhow::Result<LightningGateway> {
async fn get_gateway(
client: &fedimint_client::ClientHandle,
) -> anyhow::Result<LightningGateway> {
let lightning_module = client.get_first_module::<LightningClientModule>();
let gws = lightning_module.list_gateways().await;
let gw_api = faucet::gateway_api().await?;
Expand Down
23 changes: 21 additions & 2 deletions modules/fedimint-ln-client/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::time::SystemTime;
use bitcoin_hashes::sha256;
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::{impl_db_lookup, impl_db_record};
use fedimint_ln_common::LightningGatewayRegistration;
use serde::Serialize;
use strum_macros::EnumIter;

Expand All @@ -11,10 +12,12 @@ use crate::OutgoingLightningPayment;
#[repr(u8)]
#[derive(Clone, EnumIter, Debug)]
pub enum DbKeyPrefix {
PaymentResult = 0x29,
MetaOverrides = 0x30,
// Deprecated
// Lightning Gateway cache records are stored the same way as the server, using 0x45
// [`fedimint_ln_common::db::DbKeyPrefix::LightningGateway`].
ActiveGateway = 0x28,
PaymentResult = 0x29,
MetaOverrides = 0x30,
}

impl std::fmt::Display for DbKeyPrefix {
Expand All @@ -23,6 +26,22 @@ impl std::fmt::Display for DbKeyPrefix {
}
}

#[derive(Debug, Encodable, Decodable, Serialize)]
pub struct LightningGatewayKey;

#[derive(Debug, Encodable, Decodable)]
pub struct LightningGatewayKeyPrefix;

impl_db_record!(
key = LightningGatewayKey,
value = LightningGatewayRegistration,
db_prefix = DbKeyPrefix::ActiveGateway,
);
impl_db_lookup!(
key = LightningGatewayKey,
query_prefix = LightningGatewayKeyPrefix
);

#[derive(Debug, Encodable, Decodable, Serialize)]
pub struct PaymentResultKey {
pub payment_hash: sha256::Hash,
Expand Down
38 changes: 36 additions & 2 deletions modules/fedimint-ln-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use async_stream::stream;
use bitcoin::{KeyPair, Network};
use bitcoin_hashes::{sha256, Hash};
use db::{DbKeyPrefix, PaymentResult, PaymentResultKey};
use fedimint_client::db::ClientMigrationFn;
use fedimint_client::derivable_secret::ChildId;
use fedimint_client::module::init::{ClientModuleInit, ClientModuleInitArgs};
use fedimint_client::module::recovery::NoModuleBackup;
Expand Down Expand Up @@ -49,7 +50,8 @@ use fedimint_ln_common::contracts::{
use fedimint_ln_common::db::{LightningGatewayKey, LightningGatewayKeyPrefix};
use fedimint_ln_common::{
ContractOutput, LightningClientContext, LightningCommonInit, LightningGateway,
LightningGatewayAnnouncement, LightningModuleTypes, LightningOutput, LightningOutputV0,
LightningGatewayAnnouncement, LightningGatewayRegistration, LightningModuleTypes,
LightningOutput, LightningOutputV0,
};
use futures::StreamExt;
use incoming::IncomingSmError;
Expand Down Expand Up @@ -231,7 +233,7 @@ pub struct LightningClientInit;
#[apply(async_trait_maybe_send!)]
impl ModuleInit for LightningClientInit {
type Common = LightningCommonInit;
const DATABASE_VERSION: DatabaseVersion = DatabaseVersion(0);
const DATABASE_VERSION: DatabaseVersion = DatabaseVersion(1);

async fn dump_database(
&self,
Expand All @@ -244,8 +246,15 @@ impl ModuleInit for LightningClientInit {
prefix_names.is_empty() || prefix_names.contains(&f.to_string().to_lowercase())
});

let filtered_prefixes_common = fedimint_ln_common::db::DbKeyPrefix::iter().filter(|f| {
prefix_names.is_empty() || prefix_names.contains(&f.to_string().to_lowercase())
});

for table in filtered_prefixes {
match table {
DbKeyPrefix::ActiveGateway => {
// Active gateway is deprecated
}
DbKeyPrefix::PaymentResult => {
push_db_pair_items!(
dbtx,
Expand All @@ -269,6 +278,19 @@ impl ModuleInit for LightningClientInit {
}
}

for table in filtered_prefixes_common {
if let fedimint_ln_common::db::DbKeyPrefix::LightningGateway = table {
push_db_pair_items!(
dbtx,
LightningGatewayKeyPrefix,
LightningGatewayKey,
LightningGatewayRegistration,
ln_client_items,
"Lightning Gateways"
);
}
}

Box::new(ln_client_items.into_iter())
}
}
Expand All @@ -292,6 +314,18 @@ impl ClientModuleInit for LightningClientInit {
async fn init(&self, args: &ClientModuleInitArgs<Self>) -> anyhow::Result<Self::Module> {
Ok(LightningClientModule::new(args).await?)
}

fn get_database_migrations(&self) -> BTreeMap<DatabaseVersion, ClientMigrationFn> {
let mut migrations: BTreeMap<DatabaseVersion, ClientMigrationFn> = BTreeMap::new();
migrations.insert(DatabaseVersion(0), move |dbtx, _, _, _, _| {
Box::pin(async {
dbtx.remove_entry(&crate::db::LightningGatewayKey).await;
Ok(None)
})
});

migrations
}
}

/// Client side lightning module
Expand Down
17 changes: 15 additions & 2 deletions modules/fedimint-ln-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,11 @@ mod fedimint_migration_tests {
valid_until: fedimint_core::time::now(),
};

dbtx.insert_new_entry(&LightningGatewayKey(pk), &lightning_gateway_registration)
.await;
dbtx.insert_new_entry(
&fedimint_ln_client::db::LightningGatewayKey,
&lightning_gateway_registration,
)
.await;

dbtx.insert_new_entry(
&PaymentResultKey {
Expand Down Expand Up @@ -869,6 +872,16 @@ mod fedimint_migration_tests {

for prefix in fedimint_ln_client::db::DbKeyPrefix::iter() {
match prefix {
fedimint_ln_client::db::DbKeyPrefix::ActiveGateway => {
// Active gateway is deprecated, there should be no records
let active_gateway = dbtx
.get_value(&fedimint_ln_client::db::LightningGatewayKey)
.await;
ensure!(
active_gateway.is_none(),
"validate migrations found an active gateway"
);
}
fedimint_ln_client::db::DbKeyPrefix::PaymentResult => {
let payment_results = dbtx
.find_by_prefix(&PaymentResultPrefix)
Expand Down

0 comments on commit 99d296a

Please sign in to comment.