Skip to content

fix: preserve spk_cache in custom persistence - #1157

Open
Jeon1691 wants to merge 2 commits into
bitcoindevkit:masterfrom
Jeon1691:fix/preserve-spk-cache
Open

Jeon1691 wants to merge 2 commits into
bitcoindevkit:masterfrom
Jeon1691:fix/preserve-spk-cache

Conversation

@Jeon1691

Copy link
Copy Markdown

Description

Fixes #1099.

IndexerChangeSet only carried last_revealed. Converting from the upstream keychain_txout::ChangeSet dropped spk_cache, and converting back filled it with Default::default(). A wallet using custom persistence with use_spk_cache therefore never stored its script pubkey cache, and derived every script pubkey again on each load.

This adds spk_cache: HashMap<DescriptorId, HashMap<u32, Script>> to IndexerChangeSet and converts it in both directions.

Notes to the reviewers

  • The field is marked #[uniffi(default)], so the generated constructors default it to an empty map (mapOf() in Kotlin, [:] in Swift). Existing IndexerChangeSet(lastRevealed = ...) calls still compile. The record still gains a field, so I left it to you whether this counts as breaking.
  • Custom persisters that only store lastRevealed keep working, but they need to store spkCache as well to benefit from the cache.
  • Upstream only puts the derived script pubkeys into a changeset on the next reveal, not on create. The tests reveal one address before checking what was persisted.

Tests (Rust, src/tests/wallet.rs):

  • test_indexer_changeset_round_trips_spk_cache: upstream changeset → IndexerChangeSet → upstream is lossless.
  • test_custom_persistence_persists_spk_cache: a custom Persistence receives the cached script pubkeys for both keychains, and they match the wallet's addresses.
  • test_custom_persistence_reloads_spk_cache_without_rederiving: after reloading through the custom persister, the next reveal stages only the one newly derived script pubkey. When the cache is not restored on load, every script pubkey of both keychains is staged again.

All three fail on master and pass with this change. cargo fmt, cargo clippy --all-targets -- -D warnings and cargo test --locked pass.

Tests (bindings): CustomPersistenceTests.swift and CustomPersistenceTest.kt implement Persistence in the foreign language. They check that IndexerChangeSet(lastRevealed:) still compiles and defaults spkCache to empty, that the persister receives the cache, and that a reload reuses it.

  • Swift: swift test after build-xcframework.sh on macOS (arm64); the new tests and the existing PersistenceTests/CustomSyncTests pass.
  • Android: connectedAndroidTest on an android-33 arm64 emulator (NDK r27 locally, arm64-v8a only); the new tests and PersistenceTest pass. The two CustomSyncTest cases that need the regtest Electrum server failed with Connection refused because I didn't run that server locally.

Documentation

Changelog

Suggested label: changelog: fixed. I can't add labels from a fork, so could a maintainer apply it?

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing
  • I've added exactly one changelog:* label
  • I've linked the relevant upstream docs or specs above

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

IndexerChangeSet only carried last_revealed, so converting from the
upstream keychain_txout::ChangeSet dropped spk_cache and converting back
replaced it with an empty default. A wallet using custom persistence with
use_spk_cache never stored its script pubkey cache and derived every
script pubkey again on load.

Add spk_cache to IndexerChangeSet and convert it in both directions. The
field defaults to an empty map in the generated constructors, so existing
IndexerChangeSet(lastRevealed) calls keep compiling.

Fixes bitcoindevkit#1099

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Preserve spk_cache in custom persistence

1 participant