Skip to content

Commit

Permalink
Fix scan from creating multiple accounts. Fix send to another account
Browse files Browse the repository at this point in the history
  • Loading branch information
bayk committed Sep 4, 2024
1 parent 4368783 commit f449504
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 46 deletions.
4 changes: 2 additions & 2 deletions controller/tests/self_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn self_spend_impl(test_dir: &'static str) -> Result<(), wallet::Error> {
debug!("===========the outputs list are {:?}", output_list);
assert!(output_list.len() == 4);

libwallet::owner::self_spend_particular_putput(
libwallet::owner::self_spend_particular_output(
wallet1.clone(),
mask1,
output_list[0].clone(),
Expand All @@ -115,7 +115,7 @@ fn self_spend_impl(test_dir: &'static str) -> Result<(), wallet::Error> {
1,
true,
)?;
libwallet::owner::self_spend_particular_putput(
libwallet::owner::self_spend_particular_output(
wallet1.clone(),
mask1,
output_list[1].clone(),
Expand Down
8 changes: 4 additions & 4 deletions impls/src/backends/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,19 +492,19 @@ where
fn next_child<'a>(
&mut self,
keychain_mask: Option<&SecretKey>,
parent_key_id: Option<Identifier>,
parent_key_id: Option<&Identifier>,
height: Option<u64>,
) -> Result<Identifier, Error> {
let parent_key_id = parent_key_id.unwrap_or(self.parent_key_id.clone());
let parent_key_id = parent_key_id.unwrap_or(&self.parent_key_id).clone();
let mut deriv_idx = {
let batch = self.db.batch()?;
let deriv_key = to_key(DERIV_PREFIX, &mut self.parent_key_id.to_bytes().to_vec());
let deriv_key = to_key(DERIV_PREFIX, &mut parent_key_id.to_bytes().to_vec());
match batch.get_ser(&deriv_key, None)? {
Some(idx) => idx,
None => 0,
}
};
let mut return_path = self.parent_key_id.to_path();
let mut return_path = parent_key_id.to_path();
return_path.depth += 1;
return_path.path[return_path.depth as usize - 1] = ChildNumber::from(deriv_idx);
if let Some(hei) = height {
Expand Down
4 changes: 2 additions & 2 deletions libwallet/src/api_impl/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@ where
}

///
pub fn self_spend_particular_putput<'a, L, C, K>(
pub fn self_spend_particular_output<'a, L, C, K>(
wallet_inst: Arc<Mutex<Box<dyn WalletInst<'a, L, C, K>>>>,
keychain_mask: Option<&SecretKey>,
output: OutputData,
Expand Down Expand Up @@ -1583,7 +1583,7 @@ where
{
let k = w.keychain(keychain_mask)?;

let key_id = keys::next_available_key(&mut *w, keychain_mask)?;
let key_id = keys::next_available_key(&mut *w, keychain_mask, None)?;

let blind = k.derive_key(amount, &key_id, SwitchCommitmentType::Regular)?;
let commit = k.secp().commit(amount, blind.clone())?;
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/api_impl/owner_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,7 @@ where
};

for _ in 0..secondary_key_size {
keys.push(wallet.next_child(keychain_mask, Some(parent_key_id.clone()), None)?);
keys.push(wallet.next_child(keychain_mask, Some(&parent_key_id), None)?);
}

let context = (**swap_api).create_context(
Expand Down
68 changes: 37 additions & 31 deletions libwallet/src/api_impl/owner_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,50 @@ pub fn start_updater_log_thread(
) -> Result<JoinHandle<()>, Error> {
let handle = thread::Builder::new()
.name("wallet-updater-status".to_string())
.spawn(move || loop {
let running = running_state.load(Ordering::Relaxed);
while let Ok(m) = rx.try_recv() {
// save to our message queue to be read by other consumers
{
let mut q = queue.lock();
q.insert(0, m.clone());
while q.len() > MESSAGE_QUEUE_MAX_LEN {
q.pop();
}
}
match m {
StatusMessage::UpdatingOutputs(_show_progress, s) => info!("{}", s),
StatusMessage::FullScanWarn(s) => warn!("{}", s),
StatusMessage::Scanning(show_progress, s, m) => {
info!("{}", s);
if show_progress {
warn!("Scanning - {}% complete", m);
} else {
info!("Scanning - {}% complete", m);
.spawn(move || {
let mut updated_percent: u8 = 250;
loop {
let running = running_state.load(Ordering::Relaxed);
while let Ok(m) = rx.try_recv() {
// save to our message queue to be read by other consumers
{
let mut q = queue.lock();
q.insert(0, m.clone());
while q.len() > MESSAGE_QUEUE_MAX_LEN {
q.pop();
}
}
StatusMessage::ScanningComplete(show_progress, s) => {
if show_progress {
warn!("{}", s);
} else {
match m {
StatusMessage::UpdatingOutputs(_show_progress, s) => info!("{}", s),
StatusMessage::FullScanWarn(s) => warn!("{}", s),
StatusMessage::Scanning(show_progress, s, m) => {
info!("{}", s);
if updated_percent != m {
if show_progress {
warn!("Scanning - {}% complete", m);
} else {
info!("Scanning - {}% complete", m);
}
updated_percent = m;
}
}
StatusMessage::ScanningComplete(show_progress, s) => {
if show_progress {
warn!("{}", s);
} else {
info!("{}", s);
}
}
StatusMessage::Warning(s) => warn!("{}", s),
StatusMessage::Info(s) => info!("{}", s),
}
StatusMessage::Warning(s) => warn!("{}", s),
StatusMessage::Info(s) => info!("{}", s),
}
if !running {
// Need to check first, then read, and exit
break;
}
thread::sleep(Duration::from_millis(100));
}
if !running {
// Need to check first, then read, and exit
break;
}
thread::sleep(Duration::from_millis(100));
})?;

Ok(handle)
Expand Down
3 changes: 2 additions & 1 deletion libwallet/src/internal/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ use std::collections::HashSet;
pub fn next_available_key<'a, T: ?Sized, C, K>(
wallet: &mut T,
keychain_mask: Option<&SecretKey>,
parent_key_id: Option<&Identifier>,
) -> Result<Identifier, Error>
where
T: WalletBackend<'a, C, K>,
C: NodeClient + 'a,
K: Keychain + 'a,
{
let child = wallet.next_child(keychain_mask, None, None)?;
let child = wallet.next_child(keychain_mask, parent_key_id, None)?;
Ok(child)
}

Expand Down
4 changes: 4 additions & 0 deletions libwallet/src/internal/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ where
let mut batch = w.batch(keychain_mask)?;

let parent_key_id = output.key_id.parent_path();
let mut path = parent_key_id.to_path();
// Resetting reply attack prevention block number to 0, so we could calculate parent correctly
path.path[3] = ChildNumber::from(0);
let parent_key_id = path.to_identifier();

if !found_parents.contains_key(&parent_key_id) {
found_parents.insert(parent_key_id.clone(), 0);
Expand Down
7 changes: 5 additions & 2 deletions libwallet/src/internal/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ where
let mut sum = 0;
for oaui in output_amounts_unwrapped {
sum = sum + oaui;
key_vec_amounts.push((keys::next_available_key(wallet, keychain_mask)?, oaui));
key_vec_amounts.push((
keys::next_available_key(wallet, keychain_mask, Some(&parent_key_id))?,
oaui,
));
i = i + 1;
}
if sum != slate.amount {
Expand All @@ -355,7 +358,7 @@ where
let key_str = key_id_opt.unwrap();
Identifier::from_hex(key_str)?
} else {
keys::next_available_key(wallet, keychain_mask)?
keys::next_available_key(wallet, keychain_mask, Some(&parent_key_id))?
};

let output_amount: u64 = if i == num_outputs - 1 {
Expand Down
4 changes: 2 additions & 2 deletions libwallet/src/internal/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,9 @@ where
let key_id = match key_id {
Some(key_id) => match keys::retrieve_existing_key(wallet, key_id, None) {
Ok(k) => k.0,
Err(_) => keys::next_available_key(wallet, keychain_mask)?,
Err(_) => keys::next_available_key(wallet, keychain_mask, None)?,
},
None => keys::next_available_key(wallet, keychain_mask)?,
None => keys::next_available_key(wallet, keychain_mask, None)?,
};

{
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ where
fn next_child(
&mut self,
keychain_mask: Option<&SecretKey>,
parent_key_id: Option<Identifier>,
parent_key_id: Option<&Identifier>,
height: Option<u64>,
) -> Result<Identifier, Error>;

Expand Down

0 comments on commit f449504

Please sign in to comment.