Skip to content

Commit 4246ccb

Browse files
author
bay
committed
Cleanup broken root_key_id for outputs, so all of them was in correct account.
1 parent 20b7f3f commit 4246ccb

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

impls/src/lifecycle/default.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ use crate::config::{
2020
MWC_WALLET_DIR,
2121
};
2222
use crate::core::global;
23-
use crate::keychain::Keychain;
23+
use crate::keychain::{ChildNumber, Keychain};
2424
use crate::libwallet::swap::ethereum::generate_ethereum_wallet;
2525
use crate::libwallet::{Error, NodeClient, WalletBackend, WalletLCProvider};
2626
use crate::lifecycle::seed::WalletSeed;
2727
use crate::util::secp::key::SecretKey;
2828
use crate::util::ZeroingString;
2929
use crate::LMDBBackend;
30-
use mwc_wallet_libwallet::types::{FLAG_CONTEXT_CLEARED, FLAG_NEW_WALLET};
31-
use mwc_wallet_libwallet::{Context, TxLogEntryType};
30+
use mwc_wallet_libwallet::types::{
31+
FLAG_CONTEXT_CLEARED, FLAG_NEW_WALLET, FLAG_OUTPUTS_ROOT_KEY_ID_CORRECTION,
32+
};
33+
use mwc_wallet_libwallet::{Context, OutputData, TxLogEntryType};
3234
use mwc_wallet_util::mwc_util::logger::LoggingConfig;
3335
use std::collections::HashMap;
3436
use std::fs;
@@ -302,6 +304,7 @@ where
302304
let mask = wallet.set_keychain(Box::new(keychain), create_mask, use_test_rng)?;
303305

304306
{
307+
// Cleaning dangling contexts.
305308
let mut batch = wallet.batch(mask.as_ref())?;
306309
if !batch.load_flag(FLAG_CONTEXT_CLEARED, false)? {
307310
let mut contexts: HashMap<Uuid, Context> = batch
@@ -333,6 +336,31 @@ where
333336
batch.commit()?;
334337
}
335338

339+
{
340+
// Cleaning broken root_key_id from outputs. It is old bug that was fixed on Sept 2024, but the data was never fixed.
341+
let mut batch = wallet.batch(mask.as_ref())?;
342+
if !batch.load_flag(FLAG_OUTPUTS_ROOT_KEY_ID_CORRECTION, false)? {
343+
let broken_outputs: Vec<OutputData> = batch
344+
.iter()
345+
.filter(|o| {
346+
// We need to fix last element of path if it is not 0
347+
let path = o.root_key_id.to_path();
348+
let last_id = u32::from(path.path[3]);
349+
last_id != 0
350+
})
351+
.collect();
352+
353+
for mut out in broken_outputs {
354+
let mut path = out.root_key_id.to_path();
355+
path.path[3] = ChildNumber::from(0);
356+
out.root_key_id = path.to_identifier();
357+
batch.save(out)?;
358+
}
359+
batch.save_flag(FLAG_OUTPUTS_ROOT_KEY_ID_CORRECTION)?;
360+
}
361+
batch.commit()?;
362+
}
363+
336364
self.backend = Some(Box::new(wallet));
337365
Ok(mask)
338366
}

libwallet/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ where
277277
pub const FLAG_NEW_WALLET: u64 = 1;
278278
/// Need to clean private context on the first wallet run
279279
pub const FLAG_CONTEXT_CLEARED: u64 = 2;
280+
/// Need to correct outputs root_key_id values to recover from prev bug
281+
pub const FLAG_OUTPUTS_ROOT_KEY_ID_CORRECTION: u64 = 3;
280282

281283
/// Batch trait to update the output data backend atomically. Trying to use a
282284
/// batch after commit MAY result in a panic. Due to this being a trait, the

0 commit comments

Comments
 (0)