bdk icon indicating copy to clipboard operation
bdk copied to clipboard

Sync to specific block

Open Ash-L2L opened this issue 11 months ago • 3 comments

Describe the enhancement
Currently, there is no way to sync a Wallet to a specific block (and no further). One can specify an initial chain tip in a sync request, but there is no way to specify a target chain tip. A sync always ends with the current best tip. There is no way to disconnect a wallet tip either, so syncing to current best tip and then disconnecting blocks is not possible (#1271, bitcoindevkit/bdk#1655).

It should be possible to either:

Use case
In an application that needs to keep a wallet in sync with some other component that updates on each new tip (eg. a mempool), an initial sync process is quite awkward:

  1. Load wallet
  2. Subscribe to sequence/rawblock/hashblock
  3. Sync wallet
  4. Sync other component to wallet tip
  5. Persist wallet
  6. Ignore tip updates older than wallet tip
  7. For each new tip, apply block to wallet and other component, and persist wallet

In particular, the requirement to sync a wallet to the current tip before anything else is a constraint on application architecture - it is more complex to perform initial sync concurrently, since a wallet may sync to a different tip than another component. If that other component also does not support disconnecting blocks, one needs to repeatedly attempt to sync each to the same block.

If specifying a target chain tip for sync was possible, an initial sync process could look like

  1. Load wallet
  2. Subscribe to sequence/rawblock/hashblock
  3. Get current best tip
  4. Sync & persist wallet, sync other component to best tip
  5. Ignore tip updates older than wallet tip
  6. For each new tip, apply block to wallet and other component, and persist wallet

If disconnecting blocks was possible, an initial sync process could look like

  1. Load wallet
  2. Subscribe to sequence/rawblock/hashblock
  3. Get current best tip
  4. Sync wallet, sync other component to best tip
  5. If wallet tip is behind best tip, connect blocks; If wallet tip is ahead, disconnect blocks
  6. Persist wallet
  7. Ignore tip updates older than shared tip
  8. For each new tip, apply block to wallet and other component, and persist wallet

Ash-L2L avatar Feb 02 '25 09:02 Ash-L2L

Thanks for writing this up. I have some further questions.

Do you need the wallet to be able to sync up to a specific blockhash? Or is syncing up to a block height good enough?

Let's say the network tip is at height 1000 and we instruct the wallet to sync up to height 950. For the transactions in blocks 951 to 1000 and in the mempool, would your application be okay for them to just appear unconfirmed in the wallet?

evanlinjin avatar Jun 18 '25 20:06 evanlinjin

Hello! What kind of responsibilities would these "other components" typically have in this context? (e.g., relying on confirmation status for rebroadcasting transactions, indexing txs, etc.) Just trying to better understand the use case.

LagginTimes avatar Jun 18 '25 21:06 LagginTimes

Do you need the wallet to be able to sync up to a specific blockhash? Or is syncing up to a block height good enough?

Syncing to a specific blockhash seems preferable, although it is likely that a block height is good enough - one almost always intends to sync to the active chain. Is syncing to a particular block height easier to implement for some reason?

For the transactions in blocks 951 to 1000 and in the mempool, would your application be okay for them to just appear unconfirmed in the wallet?

Yes, this is fine

Hello! What kind of responsibilities would these "other components" typically have in this context? (e.g., relying on confirmation status for rebroadcasting transactions, indexing txs, etc.) Just trying to better understand the use case

An example would be a block template builder - one might wish to handle payouts, receiving address for coinbase txs, etc when constructing a block template. Manually keeping a wallet in sync with a mempool would be nice here

Ash-L2L avatar Sep 10 '25 16:09 Ash-L2L