<p>This is what I do:</p>

<ol>
<li>Connect to peer</li>
<li>Set bloom filter</li>
<li>Send 'getblocks' message</li>
<li>Send 'getdata' message with MSG_FILTERED_BLOCK set for any new blocks</li>
</ol>

<p>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.</p>

<p>More details are in <a href="https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki" rel="nofollow"><strong>BIP0037</strong></a></p>

<p>If you want an example, my Java wallet can be found on <a href="https://github.com/ScripterRon" rel="nofollow"><strong>GitHub</strong></a></p>

CC BY-SA 3.0
2014-04-01T02:01:36.760Z
2014-04-01T02:01:36.760Z
ScripterRon
<p>A couple examples may help you:</p>

<p><a href="https://code.google.com/p/bitcoinj/" rel="nofollow">https://code.google.com/p/bitcoinj/</a></p>

<p>My project <a href="https://github.com/richardkiss/pycoinnet" rel="nofollow">https://github.com/richardkiss/pycoinnet</a> is not complete (in particular, as of this writing, there is no bloom filter implementation), but it may eventually do what you need.</p>

CC BY-SA 3.0
2014-04-01T01:11:34.900Z
2014-04-01T01:11:34.900Z
Richard Kiss
<p>What are the steps a SPV client takes in order to sync?</p>

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

<p>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.</p>

<p>But what is the correct procedure?</p>

<blockchain><client><development><block-header><spv>
How to create a SPV client?
Maestro
# How to create a SPV client?

(Asked by: username_0 on 2014-03-30)

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?

**Tags:** blockchain, client, development, block-header, spv


---

## 2 answers

### Answer by username_1 on 2014-04-01. Score: 4

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**](https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki)

If you want an example, my Java wallet can be found on [**GitHub**](https://github.com/username_1)


---

### Answer by username_2 on 2014-04-01. Score: 1

A couple examples may help you:

<https://code.google.com/p/bitcoinj/>

My project <https://github.com/username_2/pycoinnet> is not complete (in particular, as of this writing, there is no bloom filter implementation), but it may eventually do what you need.


---

