]> Untitled Git - bdk/commitdiff
test(chain): reuse indexer helper in apply_changeset clamp test
authorbusayo-OD <toyinoluwabusayo@gmail.com>
Sun, 18 Oct 2026 08:05:56 +0000 (09:05 +0100)
committerbusayo-OD <toyinoluwabusayo@gmail.com>
Sun, 18 Oct 2026 13:48:30 +0000 (14:48 +0100)
Reuse `indexer_with_last_revealed_at` to simplify the test setup for
`apply_changeset_clamps_out_of_range_index_and_derives_correct_spk`.

Move the helper above its first use.

crates/chain/src/indexer/keychain_txout.rs

index 25e36dbf549676970a4dc76613fb315edbbe4779..4717cffdc807e659f949f762e43eb72609bcc154 100644 (file)
@@ -1227,8 +1227,8 @@ mod test {
         assert!(index.spk_cache.is_empty());
     }
 
-    #[test]
-    fn apply_changeset_clamps_out_of_range_index_and_derives_correct_spk() {
+    /// Build an index with `last_revealed` (and the matching spk) at `last_revealed_index`.
+    fn indexer_with_last_revealed_at(last_revealed_index: u32) -> KeychainTxOutIndex<i32> {
         let s = DESCRIPTORS[0];
         let desc = Descriptor::parse_descriptor(&Secp256k1::new(), s)
             .unwrap()
@@ -1236,14 +1236,22 @@ mod test {
         let mut index = KeychainTxOutIndex::new(0, false);
         let did = desc.descriptor_id();
         let _ = index.insert_descriptor(0i32, desc.clone());
-        let seed_index = BIP32_MAX_INDEX - 1;
+
         let spk = desc
-            .at_derivation_index(seed_index)
+            .at_derivation_index(last_revealed_index)
             .unwrap()
             .script_pubkey();
-        index.inner.insert_spk((0i32, seed_index), spk);
+        index.inner.insert_spk((0i32, last_revealed_index), spk);
+        index.last_revealed.insert(did, last_revealed_index);
+        index
+    }
+
+    #[test]
+    fn apply_changeset_clamps_out_of_range_index_and_derives_correct_spk() {
+        let mut index = indexer_with_last_revealed_at(BIP32_MAX_INDEX - 1);
+        let desc = index.get_descriptor(0i32).unwrap().clone();
         let changeset = ChangeSet {
-            last_revealed: [(did, BIP32_MAX_INDEX + 1)].into(),
+            last_revealed: [(desc.descriptor_id(), BIP32_MAX_INDEX + 1)].into(),
             ..Default::default()
         };
         index.apply_changeset(changeset);
@@ -1258,25 +1266,6 @@ mod test {
         );
     }
 
-    /// Build an index with `last_revealed` (and the matching spk) at `last_revealed_index`.
-    fn indexer_with_last_revealed_at(last_revealed_index: u32) -> KeychainTxOutIndex<i32> {
-        let s = DESCRIPTORS[0];
-        let desc = Descriptor::parse_descriptor(&Secp256k1::new(), s)
-            .unwrap()
-            .0;
-        let mut index = KeychainTxOutIndex::new(0, false);
-        let did = desc.descriptor_id();
-        let _ = index.insert_descriptor(0i32, desc.clone());
-
-        let spk = desc
-            .at_derivation_index(last_revealed_index)
-            .unwrap()
-            .script_pubkey();
-        index.inner.insert_spk((0i32, last_revealed_index), spk);
-        index.last_revealed.insert(did, last_revealed_index);
-        index
-    }
-
     #[test]
     fn reveal_next_spk_and_next_unused_spk_return_last_script_when_saturated() {
         let mut index = indexer_with_last_revealed_at(BIP32_MAX_INDEX);