fix: suggestions hotfix

This commit is contained in:
Pravdyvy 2025-11-26 07:18:25 +02:00
parent 463942df80
commit 0d11c3688e
6 changed files with 56 additions and 45 deletions

View File

@ -18,14 +18,24 @@ impl FromStr for ChainIndex {
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.starts_with('/') {
return Err(ChainIndexError:NoRootFound);
return Err(ChainIndexError::NoRootFound);
}
s
.split("/")
.map(u32::from_str)
.collect()
.map_err(Into::into)
if s == "/" {
return Ok(ChainIndex(vec![]));
}
let uprooted_substring = s.strip_prefix("/").unwrap();
let splitted_chain: Vec<&str> = uprooted_substring.split("/").collect();
let mut res = vec![];
for split_ch in splitted_chain {
let cci = split_ch.parse()?;
res.push(cci);
}
Ok(Self(res))
}
}
@ -96,7 +106,7 @@ mod tests {
#[test]
fn test_chain_id_child_correct() {
let chain_id = ChainIndex::from_str("/257").unwrap();
let child = chain_id.n_th_child(3);
let child = chain_id.nth_child(3);
assert_eq!(child, ChainIndex::from_str("/257/3").unwrap());
}

View File

@ -137,7 +137,7 @@ mod tests {
#[test]
fn test_keys_deterministic_generation() {
let root_keys = ChildKeysPrivate::root([42; 64]);
let child_keys = root_keys.n_th_child(5);
let child_keys = root_keys.nth_child(5);
assert_eq!(root_keys.cci, None);
assert_eq!(child_keys.cci, Some(5));

View File

@ -72,7 +72,7 @@ mod tests {
#[test]
fn test_keys_deterministic_generation() {
let root_keys = ChildKeysPublic::root([42; 64]);
let child_keys = root_keys.n_th_child(5);
let child_keys = root_keys.nth_child(5);
assert_eq!(root_keys.cci, None);
assert_eq!(child_keys.cci, Some(5));

View File

@ -54,37 +54,36 @@ impl<Node: KeyNode> KeyTree<Node> {
return None;
}
let leftmost_child = parent_id.n_th_child(u32::MIN);
let leftmost_child = parent_id.nth_child(u32::MIN);
if !self.key_map.contains_key(&leftmost_child) {
return Some(0)
return Some(0);
}
} else {
let mut right = u32::MAX - 1;
let mut left_border = u32::MIN;
let mut right_border = u32::MAX;
loop {
let rightmost_child = parent_id.n_th_child(right);
let mut right = u32::MAX - 1;
let mut left_border = u32::MIN;
let mut right_border = u32::MAX;
let rightmost_ref = self.key_map.get(&rightmost_child);
let rightmost_ref_next = self.key_map.get(&rightmost_child.next_in_line());
loop {
let rightmost_child = parent_id.nth_child(right);
match (&rightmost_ref, &rightmost_ref_next) {
(Some(_), Some(_)) => {
left_border = right;
right = (right + right_border) / 2;
}
(Some(_), None) => {
break Some(right + 1);
}
(None, None) => {
right_border = right;
right = (left_border + right) / 2;
}
(None, Some(_)) => {
unreachable!();
}
let rightmost_ref = self.key_map.get(&rightmost_child);
let rightmost_ref_next = self.key_map.get(&rightmost_child.next_in_line());
match (&rightmost_ref, &rightmost_ref_next) {
(Some(_), Some(_)) => {
left_border = right;
right = (right + right_border) / 2;
}
(Some(_), None) => {
break Some(right + 1);
}
(None, None) => {
right_border = right;
right = (left_border + right) / 2;
}
(None, Some(_)) => {
unreachable!();
}
}
}
@ -93,9 +92,9 @@ impl<Node: KeyNode> KeyTree<Node> {
pub fn generate_new_node(&mut self, parent_cci: ChainIndex) -> Option<nssa::Address> {
let father_keys = self.key_map.get(&parent_cci)?;
let next_child_id = self.find_next_last_child_of_id(&parent_cci).unwrap();
let next_cci = parent_cci.n_th_child(next_child_id);
let next_cci = parent_cci.nth_child(next_child_id);
let child_keys = father_keys.n_th_child(next_child_id);
let child_keys = father_keys.nth_child(next_child_id);
let address = child_keys.address();

View File

@ -5,7 +5,7 @@ pub trait KeyNode {
fn chain_code(&self) -> &[u8; 32];
fn child_index(&self) -> &Option<u32>;
fn child_index(&self) -> Option<u32>;
fn address(&self) -> nssa::Address;
}

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, hash_map::Entry};
use anyhow::Result;
use key_protocol::{
@ -128,10 +128,12 @@ impl WalletChainStore {
) {
println!("inserting at address {addr}, this account {account:?}");
let entry = self.user_data
.default_user_private_accounts
.entry(addr)
.and_modify(|data| data.1 = account);
let entry = self
.user_data
.default_user_private_accounts
.entry(addr)
.and_modify(|data| data.1 = account.clone());
if matches!(entry, Entry::Vacant(_)) {
} else {
self.user_data
@ -268,7 +270,7 @@ mod tests {
fn create_sample_persistent_accounts() -> Vec<PersistentAccountData> {
let public_data = ChildKeysPublic::root([42; 64]);
let private_data = ChildKeysPrivate::root([47; 64]);
vec![
PersistentAccountData::Public(PersistentAccountDataPublic {
address: public_data.address(),
@ -279,7 +281,7 @@ mod tests {
address: private_data.address(),
chain_index: ChainIndex::root(),
data: private_data,
})
}),
]
}