How to create a SPV client?



What are the steps a SPV client takes in order to sync?

  • Connect to a peer
  • Set bloom filter for wallet addresses
  • Download all block-headers since latest checkpoint
  • Request every block again with filters set
  • Update wallet balance based on returned blocks

Obviously the above steps are wrong since it makes no sense to first download all blockheaders to determine the correct chain, and then re-request all blocks to calculate the wallet balance.

But what is the correct procedure?


Posted 2014-03-30T18:06:58.880

Reputation: 1 927



This is what I do:

  1. Connect to peer
  2. Set bloom filter
  3. Send 'getblocks' message
  4. Send 'getdata' message with MSG_FILTERED_BLOCK set for any new blocks

Note that 'getblocks' returns a list of block chain hashes from the specified starting point, not the blocks themselves. Then 'getdata' returns 'merkleblock' messages instead of full blocks. The peer follows the 'merkleblock' message with any transactions that match the bloom filter as separate 'tx' messages.

More details are in BIP0037

If you want an example, my Java wallet can be found on GitHub


Posted 2014-03-30T18:06:58.880

Reputation: 2 063


A couple examples may help you:

My project is not complete (in particular, as of this writing, there is no bloom filter implementation), but it may eventually do what you need.

Richard Kiss

Posted 2014-03-30T18:06:58.880

Reputation: 74