chore(chain): s/replenish_lookahead/replenish_inner_index/

This commit is contained in:
LLFourn 2024-06-11 14:55:06 +10:00 committed by 志宇
parent 8779afdb0b
commit 4d3846abf4
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8

View File

@ -154,7 +154,7 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
if self.last_revealed.get(did) < Some(&index) { if self.last_revealed.get(did) < Some(&index) {
self.last_revealed.insert(*did, index); self.last_revealed.insert(*did, index);
changeset.last_revealed.insert(*did, index); changeset.last_revealed.insert(*did, index);
self.replenish_lookahead(*did, &keychain, self.lookahead); self.replenish_inner_index(*did, &keychain, self.lookahead);
} }
} }
changeset changeset
@ -370,17 +370,17 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
descriptor: Descriptor<DescriptorPublicKey>, descriptor: Descriptor<DescriptorPublicKey>,
) -> Result<ChangeSet<K>, InsertDescriptorError<K>> { ) -> Result<ChangeSet<K>, InsertDescriptorError<K>> {
let mut changeset = ChangeSet::<K>::default(); let mut changeset = ChangeSet::<K>::default();
let desc_id = descriptor.descriptor_id(); let did = descriptor.descriptor_id();
if !self.keychains_to_descriptor_ids.contains_key(&keychain) if !self.keychains_to_descriptor_ids.contains_key(&keychain)
&& !self.descriptor_ids_to_keychains.contains_key(&desc_id) && !self.descriptor_ids_to_keychains.contains_key(&did)
{ {
self.descriptor_ids_to_descriptors self.descriptor_ids_to_descriptors
.insert(desc_id, descriptor.clone()); .insert(did, descriptor.clone());
self.keychains_to_descriptor_ids self.keychains_to_descriptor_ids
.insert(keychain.clone(), desc_id); .insert(keychain.clone(), did);
self.descriptor_ids_to_keychains self.descriptor_ids_to_keychains
.insert(desc_id, keychain.clone()); .insert(did, keychain.clone());
self.replenish_lookahead_keychain(&keychain, self.lookahead); self.replenish_inner_index(did, &keychain, self.lookahead);
changeset changeset
.keychains_added .keychains_added
.insert(keychain.clone(), descriptor); .insert(keychain.clone(), descriptor);
@ -390,7 +390,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
.descriptor_ids_to_descriptors .descriptor_ids_to_descriptors
.get(existing_desc_id) .get(existing_desc_id)
.expect("invariant"); .expect("invariant");
if *existing_desc_id != desc_id { if *existing_desc_id != did {
return Err(InsertDescriptorError::KeychainAlreadyAssigned { return Err(InsertDescriptorError::KeychainAlreadyAssigned {
existing_assignment: descriptor.clone(), existing_assignment: descriptor.clone(),
keychain, keychain,
@ -398,10 +398,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
} }
} }
if let Some(existing_keychain) = self.descriptor_ids_to_keychains.get(&desc_id) { if let Some(existing_keychain) = self.descriptor_ids_to_keychains.get(&did) {
let descriptor = self let descriptor = self
.descriptor_ids_to_descriptors .descriptor_ids_to_descriptors
.get(&desc_id) .get(&did)
.expect("invariant") .expect("invariant")
.clone(); .clone();
@ -443,24 +443,25 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
.filter(|&index| index > 0); .filter(|&index| index > 0);
if let Some(temp_lookahead) = temp_lookahead { if let Some(temp_lookahead) = temp_lookahead {
self.replenish_lookahead_keychain(keychain, temp_lookahead); self.replenish_inner_index_keychain(keychain, temp_lookahead);
} }
} }
} }
fn replenish_lookahead_did(&mut self, did: DescriptorId, lookahead: u32) { fn replenish_inner_index_did(&mut self, did: DescriptorId, lookahead: u32) {
if let Some(keychain) = self.descriptor_ids_to_keychains.get(&did).cloned() { if let Some(keychain) = self.descriptor_ids_to_keychains.get(&did).cloned() {
self.replenish_lookahead(did, &keychain, lookahead); self.replenish_inner_index(did, &keychain, lookahead);
} }
} }
fn replenish_lookahead_keychain(&mut self, keychain: &K, lookahead: u32) { fn replenish_inner_index_keychain(&mut self, keychain: &K, lookahead: u32) {
if let Some(did) = self.keychains_to_descriptor_ids.get(keychain) { if let Some(did) = self.keychains_to_descriptor_ids.get(keychain) {
self.replenish_lookahead(*did, keychain, lookahead); self.replenish_inner_index(*did, keychain, lookahead);
} }
} }
fn replenish_lookahead(&mut self, did: DescriptorId, keychain: &K, lookahead: u32) { /// Syncs the state of the inner spk index after changes to a keychain
fn replenish_inner_index(&mut self, did: DescriptorId, keychain: &K, lookahead: u32) {
let descriptor = self let descriptor = self
.descriptor_ids_to_descriptors .descriptor_ids_to_descriptors
.get(&did) .get(&did)
@ -677,6 +678,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
/// pubkeys are revealed, then both of these will be empty. /// pubkeys are revealed, then both of these will be empty.
/// ///
/// Returns None if the provided `keychain` doesn't exist. /// Returns None if the provided `keychain` doesn't exist.
#[must_use]
pub fn reveal_to_target( pub fn reveal_to_target(
&mut self, &mut self,
keychain: &K, keychain: &K,
@ -718,15 +720,9 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
if new { if new {
let did = self.keychains_to_descriptor_ids.get(keychain)?; let did = self.keychains_to_descriptor_ids.get(keychain)?;
let descriptor = self.descriptor_ids_to_descriptors.get(did)?;
let spk = descriptor
.at_derivation_index(next_index)
.expect("already checked index is not too high")
.script_pubkey();
let _ = self.inner.insert_spk((keychain.clone(), next_index), spk);
self.last_revealed.insert(*did, next_index); self.last_revealed.insert(*did, next_index);
changeset.last_revealed.insert(*did, next_index); changeset.last_revealed.insert(*did, next_index);
self.replenish_lookahead_keychain(keychain, self.lookahead); self.replenish_inner_index(*did, keychain, self.lookahead);
} }
let script = self let script = self
.inner .inner
@ -830,7 +826,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
} }
for did in last_revealed.keys() { for did in last_revealed.keys() {
self.replenish_lookahead_did(*did, self.lookahead); self.replenish_inner_index_did(*did, self.lookahead);
} }
} }
} }
@ -966,3 +962,13 @@ impl<K> Default for ChangeSet<K> {
} }
} }
} }
#[derive(Clone, Debug, Eq, PartialEq)]
/// The keychain doesn't exist. Most likley hasn't been inserted with [`KeychainTxOutIndex::insert_descriptor`].
pub struct NoSuchKeychain<K>(K);
impl<K: Debug> core::fmt::Display for NoSuchKeychain<K> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "no such keychain {:?} exists", &self.0)
}
}