refactor(chain): improve replenish lookeahd internals
see: 4eb1e288a9 (r1630943639)
This commit is contained in:
parent
5a584d0fd8
commit
69f2a695f7
@ -142,15 +142,15 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
|
|||||||
|
|
||||||
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet {
|
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet {
|
||||||
let mut changeset = ChangeSet::default();
|
let mut changeset = ChangeSet::default();
|
||||||
if let Some((keychain, index)) = self.inner.scan_txout(outpoint, txout) {
|
if let Some((keychain, index)) = self.inner.scan_txout(outpoint, txout).cloned() {
|
||||||
let did = self
|
let did = self
|
||||||
.keychains_to_descriptor_ids
|
.keychains_to_descriptor_ids
|
||||||
.get(keychain)
|
.get(&keychain)
|
||||||
.expect("invariant");
|
.expect("invariant");
|
||||||
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(*did);
|
self.replenish_lookahead(*did, &keychain, self.lookahead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
changeset
|
changeset
|
||||||
@ -376,7 +376,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
.insert(keychain.clone(), desc_id);
|
.insert(keychain.clone(), desc_id);
|
||||||
self.descriptor_ids_to_keychains
|
self.descriptor_ids_to_keychains
|
||||||
.insert(desc_id, keychain.clone());
|
.insert(desc_id, keychain.clone());
|
||||||
self.replenish_lookahead(&keychain, self.lookahead);
|
self.replenish_lookahead_keychain(&keychain, self.lookahead);
|
||||||
changeset
|
changeset
|
||||||
.keychains_added
|
.keychains_added
|
||||||
.insert(keychain.clone(), descriptor);
|
.insert(keychain.clone(), descriptor);
|
||||||
@ -439,22 +439,27 @@ 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, temp_lookahead);
|
self.replenish_lookahead_keychain(keychain, temp_lookahead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replenish_lookahead_did(&mut self, did: DescriptorId) {
|
fn replenish_lookahead_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(&keychain, self.lookahead);
|
self.replenish_lookahead(did, &keychain, lookahead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replenish_lookahead(&mut self, keychain: &K, lookahead: u32) {
|
fn replenish_lookahead_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replenish_lookahead(&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)
|
||||||
.expect("invariant");
|
.expect("invariant");
|
||||||
let next_store_index = self
|
let next_store_index = self
|
||||||
.inner
|
.inner
|
||||||
@ -462,18 +467,16 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
.range(&(keychain.clone(), u32::MIN)..=&(keychain.clone(), u32::MAX))
|
.range(&(keychain.clone(), u32::MIN)..=&(keychain.clone(), u32::MAX))
|
||||||
.last()
|
.last()
|
||||||
.map_or(0, |((_, index), _)| *index + 1);
|
.map_or(0, |((_, index), _)| *index + 1);
|
||||||
let next_reveal_index = self.last_revealed.get(did).map_or(0, |v| *v + 1);
|
let next_reveal_index = self.last_revealed.get(&did).map_or(0, |v| *v + 1);
|
||||||
for (new_index, new_spk) in SpkIterator::new_with_range(
|
for (new_index, new_spk) in
|
||||||
descriptor,
|
SpkIterator::new_with_range(descriptor, next_store_index..next_reveal_index + lookahead)
|
||||||
next_store_index..next_reveal_index + lookahead,
|
{
|
||||||
) {
|
|
||||||
let _inserted = self
|
let _inserted = self
|
||||||
.inner
|
.inner
|
||||||
.insert_spk((keychain.clone(), new_index), new_spk);
|
.insert_spk((keychain.clone(), new_index), new_spk);
|
||||||
debug_assert!(_inserted, "replenish lookahead: must not have existing spk: keychain={:?}, lookahead={}, next_store_index={}, next_reveal_index={}", keychain, lookahead, next_store_index, next_reveal_index);
|
debug_assert!(_inserted, "replenish lookahead: must not have existing spk: keychain={:?}, lookahead={}, next_store_index={}, next_reveal_index={}", keychain, lookahead, next_store_index, next_reveal_index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Get an unbounded spk iterator over a given `keychain`. Returns `None` if the provided
|
/// Get an unbounded spk iterator over a given `keychain`. Returns `None` if the provided
|
||||||
/// keychain doesn't exist
|
/// keychain doesn't exist
|
||||||
@ -719,7 +722,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
|||||||
let _ = self.inner.insert_spk((keychain.clone(), next_index), spk);
|
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, self.lookahead);
|
self.replenish_lookahead_keychain(keychain, self.lookahead);
|
||||||
}
|
}
|
||||||
let script = self
|
let script = self
|
||||||
.inner
|
.inner
|
||||||
@ -823,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.replenish_lookahead_did(*did, self.lookahead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user