Skip to content

Commit

Permalink
test(chain): applying changesets one-by-one vs aggregate should be same
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlinjin committed May 5, 2024
1 parent 2a125f8 commit 16366b4
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions crates/chain/tests/test_keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,51 @@ fn insert_descriptor_no_change() {
"inserting the same descriptor for keychain should return an empty changeset",
);
}

#[test]
fn applying_changesets_one_by_one_vs_aggregate_must_have_same_result() {
let desc = parse_descriptor(DESCRIPTORS[0]);
let changesets: &[ChangeSet<TestKeychain>] = &[
ChangeSet {
keychains_added: [(TestKeychain::Internal, desc.clone())].into(),
last_revealed: [].into(),
},
ChangeSet {
keychains_added: [(TestKeychain::External, desc.clone())].into(),
last_revealed: [(desc.descriptor_id(), 12)].into(),
},
];

let mut indexer_a = KeychainTxOutIndex::<TestKeychain>::new(0);
for changeset in changesets {
indexer_a.apply_changeset(changeset.clone());
}

let mut indexer_b = KeychainTxOutIndex::<TestKeychain>::new(0);
let aggregate_changesets = changesets
.iter()
.cloned()
.reduce(|mut agg, cs| {
agg.append(cs);
agg
})
.expect("must aggregate changesets");
indexer_b.apply_changeset(aggregate_changesets);

assert_eq!(
indexer_a.keychains().collect::<Vec<_>>(),
indexer_b.keychains().collect::<Vec<_>>()
);
assert_eq!(
indexer_a.spk_at_index(TestKeychain::External, 0),
indexer_b.spk_at_index(TestKeychain::External, 0)
);
assert_eq!(
indexer_a.spk_at_index(TestKeychain::Internal, 0),
indexer_b.spk_at_index(TestKeychain::Internal, 0)
);
assert_eq!(
indexer_a.last_revealed_indices(),
indexer_b.last_revealed_indices()
);
}

0 comments on commit 16366b4

Please sign in to comment.