<h1>BIP157/158 Overview</h1>
<p><em>This answer is a slight modification on the description used in <a href="https://bitcoinops.org/en/newsletters/2019/04/23/#footnotes" rel="nofollow noreferrer">Bitcoin Optech Newsletter #43</a>. Full credit and thanks to Dave Harding!</em></p>
<p><a href="https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki" rel="nofollow noreferrer">BIP158</a> introduces <em>Compact block filters</em>, which are are based on an efficient method for encoding
a list of equally-sized items. In the case of the &quot;basic&quot; block
filters described in the BIP, this is a list of all the
spendable output scriptPubKeys in the current block plus all
the scriptPubKeys for the outputs spent by this block's inputs
(what developers call previous outputs (prevouts)).   Each of the
scriptPubKeys is hashed to give each item the same size and then
these items are sorted into a list that has duplicated elements
removed.  This list is then encoded using the <a href="https://en.wikipedia.org/wiki/Golomb_coding#Rice_coding" rel="nofollow noreferrer">Golomb-Rice Coded
Sets</a> (GCS) algorithm also described in BIP158, losslessly
reducing the size of the list.  This specific basic filter provides
enough information for anyone who knows a Bitcoin address to find
any block containing a transaction either paying that address
(output scriptPubKey) or spending funds previously received to that
address (prevout scriptPubKey).  The search may produce false-postive
matches (so blocks which don't contain transactions for that address
will be included in the results), but will never result in false-negatives
(so blocks that <em>do</em> contain transactions for that address will never
be omitted from the results).</p>
<p>A separate BIP, <a href="https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki" rel="nofollow noreferrer">BIP157</a>, describes how these compact block
filters can be served over the network using the Bitcoin P2P
protocol.  BIP157 is designed to work with BIP158 &quot;basic&quot; filters
but it can also be extended to support additional filters that
encode lists of other items.  One particularly noteworthy part of
BIP157 is that it introduces the concept of <em>filter headers</em> where
the header for each filter commits to a hash of the previous block's
filter header plus a hash of the current filter.  This creates a
chain of filters similar to Bitcoin's chain of blocks and is
designed to make it easy to compare filters from multiple peers:
each peer can send just the filter header (32 bytes) and, if there
are any headers that don't match, the client can request earlier and
earlier headers in the chain until the divergence point is found.
Generating a filter header on demand for a particular block
would require hashing all previous filters, so servers that provide
BIP157 filter headers will generally need to store them in advance.</p>
<h1>Bitcoin Core Implementation</h1>
<p>Bitcoin Core can generate and store
block filters and filter headers, and users can access them using the <code>getblockfilter</code>
RPC (implemented with <a href="https://github.com/bitcoin/bitcoin/pull/12254" rel="nofollow noreferrer">PR#12254</a> &amp; <a href="https://github.com/bitcoin/bitcoin/pull/14121" rel="nofollow noreferrer">PR#14121</a>).</p>
<p>Bitcoin Core can serve block filters over the P2P network, but <a href="https://github.com/bitcoin/bitcoin/blob/master/src/net_processing.h#L22" rel="nofollow noreferrer">not by default</a>; it can be enabled with <code>-peerblockfilters=1</code>. BIP157 was implemented with a series of PRs, as shown in <a href="https://github.com/bitcoin/bitcoin/pull/18876" rel="nofollow noreferrer">PR#18876</a>.</p>
<h2>Future Related Developments</h2>
<p><a href="https://github.com/bitcoin/bitcoin/pull/23549" rel="nofollow noreferrer">PR#23549</a>(v25.0) and <a href="https://github.com/bitcoin/bitcoin/pull/25957" rel="nofollow noreferrer">PR#25957</a>(v25.0)
are different efforts to achieve faster wallet rescans by using the node's already constructed compact block filter index (<code>-blockfilterindex=1</code>).</p>
<h2></h2>

CC BY-SA 4.0
2019-04-23T14:48:53.577Z
2023-07-30T19:59:42.977Z
2023-07-30T19:59:42.977Z
jnewbery
<p>BIP 157 and BIP 158 are both concerned with Compact Block filters. What is the distinction between them, and which of them does Bitcoin Core currently support?</p>

<bitcoin-core><spv><compact-client-side-block-filters>
What's the distinction between BIP 157 and BIP 158? Are they supported by Bitcoin Core?
jnewbery
# What's the distinction between BIP 157 and BIP 158? Are they supported by Bitcoin Core?

(Asked by: username_0 on 2019-04-23)

BIP 157 and BIP 158 are both concerned with Compact Block filters. What is the distinction between them, and which of them does Bitcoin Core currently support?

**Tags:** bitcoin-core, spv, compact-client-side-block-filters


---

## 1 answers

### Answer by username_0 on 2019-04-23. Score: 13

# BIP157/158 Overview

*This answer is a slight modification on the description used in [Bitcoin Optech Newsletter #43](https://bitcoinops.org/en/newsletters/2019/04/23/#footnotes). Full credit and thanks to Dave Harding!*

[BIP158](https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki) introduces *Compact block filters*, which are are based on an efficient method for encoding
a list of equally-sized items. In the case of the "basic" block
filters described in the BIP, this is a list of all the
spendable output scriptPubKeys in the current block plus all
the scriptPubKeys for the outputs spent by this block's inputs
(what developers call previous outputs (prevouts)). Each of the
scriptPubKeys is hashed to give each item the same size and then
these items are sorted into a list that has duplicated elements
removed. This list is then encoded using the [Golomb-Rice Coded
Sets](https://en.wikipedia.org/wiki/Golomb_coding#Rice_coding) (GCS) algorithm also described in BIP158, losslessly
reducing the size of the list. This specific basic filter provides
enough information for anyone who knows a Bitcoin address to find
any block containing a transaction either paying that address
(output scriptPubKey) or spending funds previously received to that
address (prevout scriptPubKey). The search may produce false-postive
matches (so blocks which don't contain transactions for that address
will be included in the results), but will never result in false-negatives
(so blocks that *do* contain transactions for that address will never
be omitted from the results).

A separate BIP, [BIP157](https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki), describes how these compact block
filters can be served over the network using the Bitcoin P2P
protocol. BIP157 is designed to work with BIP158 "basic" filters
but it can also be extended to support additional filters that
encode lists of other items. One particularly noteworthy part of
BIP157 is that it introduces the concept of *filter headers* where
the header for each filter commits to a hash of the previous block's
filter header plus a hash of the current filter. This creates a
chain of filters similar to Bitcoin's chain of blocks and is
designed to make it easy to compare filters from multiple peers:
each peer can send just the filter header (32 bytes) and, if there
are any headers that don't match, the client can request earlier and
earlier headers in the chain until the divergence point is found.
Generating a filter header on demand for a particular block
would require hashing all previous filters, so servers that provide
BIP157 filter headers will generally need to store them in advance.

# Bitcoin Core Implementation

Bitcoin Core can generate and store
block filters and filter headers, and users can access them using the `getblockfilter`
RPC (implemented with [PR#12254](https://github.com/bitcoin/bitcoin/pull/12254) & [PR#14121](https://github.com/bitcoin/bitcoin/pull/14121)).

Bitcoin Core can serve block filters over the P2P network, but [not by default](https://github.com/bitcoin/bitcoin/blob/master/src/net_processing.h#L22); it can be enabled with `-peerblockfilters=1`. BIP157 was implemented with a series of PRs, as shown in [PR#18876](https://github.com/bitcoin/bitcoin/pull/18876).

## Future Related Developments

[PR#23549](https://github.com/bitcoin/bitcoin/pull/23549)(v25.0) and [PR#25957](https://github.com/bitcoin/bitcoin/pull/25957)(v25.0)
are different efforts to achieve faster wallet rescans by using the node's already constructed compact block filter index (`-blockfilterindex=1`).

##


---

