From 16366b47ef1e38fafe220433e8ad78c7ab5f4f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Sun, 5 May 2024 14:00:49 +0800 Subject: [PATCH] test(chain): applying changesets one-by-one vs aggregate should be same --- .../chain/tests/test_keychain_txout_index.rs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/crates/chain/tests/test_keychain_txout_index.rs b/crates/chain/tests/test_keychain_txout_index.rs index 443383fd5..849cfee14 100644 --- a/crates/chain/tests/test_keychain_txout_index.rs +++ b/crates/chain/tests/test_keychain_txout_index.rs @@ -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] = &[ + 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::::new(0); + for changeset in changesets { + indexer_a.apply_changeset(changeset.clone()); + } + + let mut indexer_b = KeychainTxOutIndex::::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::>(), + indexer_b.keychains().collect::>() + ); + 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() + ); +}