Skip to main content

fedimint_mintv2_client/
migrations.rs

1//! Client database migrations.
2
3use fedimint_core::core::{Account, OperationId};
4use fedimint_core::db::{
5    DatabaseTransaction, DbMigrationError, IDatabaseTransactionOpsCore,
6    IDatabaseTransactionOpsCoreTyped,
7};
8use fedimint_core::encoding::Decodable;
9use fedimint_core::module::registry::ModuleDecoderRegistry;
10use futures::StreamExt;
11
12use crate::SpendableNote;
13use crate::client_db::{DbKeyPrefix, RecoveryState, RecoveryStateKey, SpendableNoteKey};
14
15/// Accounts split the note table, so every note a pre-accounts client wrote is
16/// re-keyed as belonging to [`Account::Primary`] — the only account it could
17/// have meant.
18///
19/// Neither the derivation tree nor the state machines need an equivalent:
20/// primary derives exactly where it always did, so no note has to be
21/// re-issued, and a state machine reads its account off its operation rather
22/// than carrying it, so the ones in flight keep their shape.
23pub(crate) async fn migrate_to_accounts_v1(
24    dbtx: &mut DatabaseTransaction<'_>,
25    _active_states: Vec<(Vec<u8>, OperationId)>,
26    _inactive_states: Vec<(Vec<u8>, OperationId)>,
27) -> Result<Option<(Vec<(Vec<u8>, OperationId)>, Vec<(Vec<u8>, OperationId)>)>, DbMigrationError> {
28    // Read raw: the old key is the note alone, so decoding one through the
29    // current two-field key would read the note's leading denomination byte as
30    // an account and fail. The rows are rewritten rather than edited in place.
31    let prefix = vec![DbKeyPrefix::Note as u8];
32
33    let notes = dbtx
34        .raw_find_by_prefix(&prefix)
35        .await?
36        .map(|(key, _)| key)
37        .collect::<Vec<Vec<u8>>>()
38        .await;
39
40    for key in notes {
41        let note = SpendableNote::consensus_decode_whole(
42            &key[prefix.len()..],
43            &ModuleDecoderRegistry::default(),
44        )?;
45
46        dbtx.raw_remove_entry(&key).await?;
47
48        dbtx.insert_new_entry(&SpendableNoteKey(Account::Primary, note.into()), &())
49            .await;
50    }
51
52    // Recovery keeps its scanned requests tagged with an account now. A
53    // pre-accounts checkpoint can only have scanned primary's subtree, so it
54    // is carried over with every request tagged primary and its progress
55    // intact. Restarting the scan from zero instead would re-derive the notes
56    // a client in usable recovery has minted since the scan's bound and try
57    // to insert them a second time.
58    if let Some(state) = dbtx.remove_entry(&legacy::RecoveryStateKey).await {
59        let state = RecoveryState {
60            next_index: state.next_index,
61            total_items: state.total_items,
62            requests: state
63                .requests
64                .into_iter()
65                .map(|(nonce_hash, request)| (nonce_hash, (Account::Primary, request)))
66                .collect(),
67            nonces: state.nonces,
68        };
69
70        dbtx.insert_new_entry(&RecoveryStateKey, &state).await;
71    }
72
73    Ok(None)
74}
75
76/// The shape the recovery checkpoint had before its requests carried an
77/// account.
78mod legacy;