use vec<identifiers> in persistentaccountdataprivate to avoid the hacky workaround with identifier=0 for unused accounts

This commit is contained in:
Sergio Chouhy 2026-04-27 21:09:33 -03:00
parent c3f47f6685
commit f0b89f8acb
3 changed files with 18 additions and 50 deletions

View File

@ -70,7 +70,13 @@ impl WalletChainStore {
public_tree.insert(data.account_id, data.chain_index, data.data);
}
PersistentAccountData::Private(data) => {
private_tree.insert(data.account_id, data.chain_index, data.data);
let npk = data.data.value.0.nullifier_public_key;
let chain_index = data.chain_index;
for identifier in &data.identifiers {
let account_id = nssa::AccountId::from((&npk, *identifier));
private_tree.account_id_map.insert(account_id, chain_index.clone());
}
private_tree.key_map.insert(chain_index, data.data);
}
PersistentAccountData::Preconfigured(acc_data) => match acc_data {
InitialAccountData::Public(data) => {
@ -282,10 +288,7 @@ mod tests {
data: public_data,
}),
PersistentAccountData::Private(Box::new(PersistentAccountDataPrivate {
account_id: nssa::AccountId::from((
&private_data.value.0.nullifier_public_key,
0_u128,
)),
identifiers: vec![],
chain_index: ChainIndex::root(),
data: private_data,
})),

View File

@ -28,7 +28,7 @@ pub struct PersistentAccountDataPublic {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistentAccountDataPrivate {
pub account_id: nssa::AccountId,
pub identifiers: Vec<nssa_core::Identifier>,
pub chain_index: ChainIndex,
pub data: ChildKeysPrivate,
}
@ -123,16 +123,6 @@ impl PersistentStorage {
}
}
impl PersistentAccountData {
#[must_use]
pub fn account_id(&self) -> nssa::AccountId {
match &self {
Self::Public(acc) => acc.account_id,
Self::Private(acc) => acc.account_id,
Self::Preconfigured(acc) => acc.account_id(),
}
}
}
impl From<PublicAccountPrivateInitialData> for InitialAccountData {
fn from(value: PublicAccountPrivateInitialData) -> Self {

View File

@ -165,41 +165,16 @@ pub fn produce_data_for_storage(
}
}
let covered_private_chain_indices: std::collections::BTreeSet<_> = user_data
.private_key_tree
.account_id_map
.values()
.cloned()
.collect();
for (account_id, key) in &user_data.private_key_tree.account_id_map {
if let Some(data) = user_data.private_key_tree.key_map.get(key) {
vec_for_storage.push(
PersistentAccountDataPrivate {
account_id: *account_id,
chain_index: key.clone(),
data: data.clone(),
}
.into(),
);
}
}
// Also persist fresh nodes that have no identifiers yet and are therefore absent from
// account_id_map. Without this, a restart before the first sync would lose the node
// entirely, making it impossible to detect incoming transfers.
for (chain_index, node) in &user_data.private_key_tree.key_map {
if !covered_private_chain_indices.contains(chain_index) {
let account_id = nssa::AccountId::from((&node.value.0.nullifier_public_key, 0_u128));
vec_for_storage.push(
PersistentAccountDataPrivate {
account_id,
chain_index: chain_index.clone(),
data: node.clone(),
}
.into(),
);
}
let identifiers = node.value.1.iter().map(|(id, _)| *id).collect();
vec_for_storage.push(
PersistentAccountDataPrivate {
identifiers,
chain_index: chain_index.clone(),
data: node.clone(),
}
.into(),
);
}
for (account_id, key) in &user_data.default_pub_account_signing_keys {