Skip to content

Commit

Permalink
chore: migrate old mint oob state
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Nov 5, 2024
1 parent 0fb234b commit d2a88ce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions modules/fedimint-dummy-client/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub(crate) fn get_v1_migrated_state(
let decoders = ModuleDecoderRegistry::default();
let dummy_sm_variant = u16::consensus_decode(cursor, &decoders)?;

// We are only migrating the type of one of the variants, so we do nothing on
// other discriminants.
if dummy_sm_variant != 5 {
return Ok(None);
}
Expand Down
35 changes: 34 additions & 1 deletion modules/fedimint-mint-client/src/client_db.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::io::Cursor;

use fedimint_client::module::init::recovery::RecoveryFromHistoryCommon;
use fedimint_core::core::OperationId;
use fedimint_core::db::{DatabaseTransaction, IDatabaseTransactionOpsCoreTyped as _};
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::module::registry::ModuleDecoderRegistry;
use fedimint_core::{impl_db_lookup, impl_db_record, Amount};
use fedimint_mint_common::Nonce;
use serde::Serialize;
use strum_macros::EnumIter;

use crate::backup::recovery::MintRecoveryState;
use crate::SpendableNoteUndecoded;
use crate::oob::{MintOOBStateMachine, MintOOBStateMachineV1, MintOOBStates, MintOOBStatesV1};
use crate::{MintClientStateMachines, SpendableNoteUndecoded};

#[repr(u8)]
#[derive(Clone, EnumIter, Debug)]
Expand Down Expand Up @@ -110,3 +114,32 @@ pub async fn migrate_to_v1(

Ok(None)
}

/// Maps all `Unreachable` states in the state machine to `OutputDone`
pub(crate) fn migrate_state_to_v2(
operation_id: OperationId,
cursor: &mut Cursor<&[u8]>,
) -> anyhow::Result<Option<(Vec<u8>, OperationId)>> {
let decoders = ModuleDecoderRegistry::default();

let mint_client_state_machine_variant = u16::consensus_decode(cursor, &decoders)?;

let bytes = match mint_client_state_machine_variant {
2 => {
let old_state = MintOOBStateMachineV1::consensus_decode(cursor, &decoders)?;

let new_state = match old_state.state {
MintOOBStatesV1::Created(created) => MintOOBStates::Created(created),
MintOOBStatesV1::UserRefund(refund) => MintOOBStates::UserRefund(refund),
MintOOBStatesV1::TimeoutRefund(refund) => MintOOBStates::TimeoutRefund(refund),
};
MintClientStateMachines::OOB(MintOOBStateMachine {
operation_id: old_state.operation_id,
state: new_state,
})
.consensus_encode_to_vec()
}
_ => return Ok(None),
};
Ok(Some((bytes, operation_id)))
}
9 changes: 7 additions & 2 deletions modules/fedimint-mint-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ use async_stream::{stream, try_stream};
use backup::recovery::MintRecovery;
use base64::Engine as _;
use bitcoin_hashes::{sha256, sha256t, Hash, HashEngine as BitcoinHashEngine};
use client_db::{migrate_to_v1, DbKeyPrefix, NoteKeyPrefix, RecoveryFinalizedKey};
use client_db::{
migrate_state_to_v2, migrate_to_v1, DbKeyPrefix, NoteKeyPrefix, RecoveryFinalizedKey,
};
use event::NoteSpent;
use fedimint_client::db::ClientMigrationFn;
use fedimint_client::db::{migrate_state, ClientMigrationFn};
use fedimint_client::module::init::{
ClientModuleInit, ClientModuleInitArgs, ClientModuleRecoverArgs,
};
Expand Down Expand Up @@ -611,6 +613,9 @@ impl ClientModuleInit for MintClientInit {
migrations.insert(DatabaseVersion(0), |dbtx, _, _| {
Box::pin(migrate_to_v1(dbtx))
});
migrations.insert(DatabaseVersion(1), |_, active_states, inactive_states| {
Box::pin(async { migrate_state(active_states, inactive_states, migrate_state_to_v2) })
});

migrations
}
Expand Down
6 changes: 6 additions & 0 deletions modules/fedimint-mint-client/src/oob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ pub enum MintOOBStates {
UserRefund(MintOOBStatesUserRefund),
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
pub struct MintOOBStateMachineV1 {
pub(crate) operation_id: OperationId,
pub(crate) state: MintOOBStatesV1,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Decodable, Encodable)]
pub struct MintOOBStateMachine {
pub(crate) operation_id: OperationId,
Expand Down

0 comments on commit d2a88ce

Please sign in to comment.