Client-side block filtering

From Bitcoin Wiki
Jump to navigation Jump to search

Client-side block filtering (also called client side bloom filters or committed bloom filters) is a way for lightweight wallets to synchronize their history and balance. It aims to preserve the privacy of users and to resist lies by omission from third-party servers or nodes. The peer-to-peer form of the scheme is a standard, specified in Bitcoin Improvement Proposal 157 (BIP 157), that lets light clients obtain filters of block content from full nodes and download full blocks when a filter matches data they are watching for.[1] The filters themselves are defined in BIP 158.[2]

A full node builds one deterministic filter per block. For the basic filter type the filter holds the scripts spent by the block's inputs and the scripts of its outputs. A filter can test whether an element is in the set: false positives are possible, but false negatives are not.[2] A lightweight wallet downloads the filters and checks them for its own scripts. Blocks that match are downloaded in full from the peer-to-peer network and used to find the wallet's history and current balance.[1]

Comparison with Bloom filters

BIP 157 describes itself as the opposite of BIP 37. Under BIP 37 the client sends a Bloom filter of its own addresses to a full node; under BIP 157 full nodes generate filters and serve them to the client.[1] the BIP 37 protocol has known flaws: it weakens client privacy, a malicious full node can leave out relevant transactions with little risk of being detected, and crafted Bloom filters can make honest nodes spend heavy I/O and CPU, a denial-of-service vector. Because filters are deterministic, a node builds each one only once, when a block is connected, and stores it on disk.[1]

Privacy

The privacy properties come from a few features:

  • The wallet addresses are never sent to any other entity, even indirectly.
  • Wallets download full blocks like any other node on the bitcoin peer-to-peer network, so peers may not realize that the downloaded blocks are important. Blocks are the natural unit of pagination for the block chain. blocks can be downloaded from any source, so that no one peer gets complete information on the data a client needs.[1]
  • Wallets can download full blocks in parallel from several different nodes, although an internet service provider could still see all the downloaded blocks. BIP 157 recommends downloading matching blocks from outbound peers at random to reduce the privacy loss from transaction intersection analysis.[1]
  • Filters have a false positive rate, so not every downloaded block indicates a filter match.[2]

Privacy-relevant information is still leaked if an adversary can find out which blocks the wallet has downloaded. Data fusion between the transaction graph in the public block chain and the list of downloaded blocks can reveal the wallet's addresses in some situations. A model and calculations published on the Bitcoin developer mailing list show that client-side block filtering works best for privacy when the wallet makes only a few of its own transactions.[3] This can be made harder by stopping the adversary from finding which blocks have been downloaded, perhaps by downloading each set of blocks over a new Tor circuit.

The best privacy for wallet synchronization is still obtained by using a full node which downloads all blocks.

  • The wallet addresses are never sent to any other entity, even indirectly.
  • Each block typically has a few thousand addresses in it, so there is a large anonymity set for which individual address caused a filter match.
  • Implementing wallets would download full blocks just like any other node on the bitcoin p2p network, so peers may not realize that the downloaded blocks are important.

Peer protocol

BIP 157 defines six new peer-to-peer messages:[1]

BIP 157 messages[1]
Request Response Purpose
getcfilters cfilter Filters of one type for a range of blocks, one cfilter message per block; the range must be fewer than 1,000 blocks
getcfheaders cfheaders Filter headers for a range of fewer than 2,000 blocks, sent as one previous filter header and a list of filter hashes
getcfcheckpt cfcheckpt Filter headers at every height that is a positive multiple of 1,000

Filter headers

The hash of a filter is the double-SHA256 of the serialized filter. Each filter header is the double-SHA256 of that hash joined with the previous filter header, and the previous filter header for the genesis block is 32 zero bytes. Filter headers therefore form a chain that commits to all earlier filters, in the same way that block headers commit to earlier blocks.[1]

A client first syncs block headers, then downloads and checks filter headers, and only then downloads filters. It should connect to several peers that serve filters. If two peers send conflicting filter headers, the client finds the first block on which they disagree, downloads that block, computes the correct filter itself and bans the peer that was wrong. a light client with at least one honest peer can identify the correct block filters.[1]

Full nodes that support the BIP should build filters for each new block and keep them as an index, and should not build filters on request, because small filters derived from large blocks would again allow denial-of-service attacks. Such nodes may prune block data once the filters have been stored.[1] Support for the basic filter type is signalled with the service bit NODE_COMPACT_FILTERS.[2]

Consensus-critical filters

An alternative is a softfork that would require blocks to commit to their filters, so that lightweight wallets could check filters against block headers instead of comparing peers. this would need a network-wide change to the consensus rules and proposes a solution purely at the peer-to-peer layer instead.[1] BIP 158 leaves outputs that start with OP_RETURN out of the basic filter so that such a commitment could later be placed in an OP_RETURN output.[2] Any filter design that is soft-forked in would have to be kept in bitcoin software permanently, unless the rule has a sunset provision. Then the economic majority would enforce the correctness of those filters, and so lightweight wallets would have a strong security model. No such softfork has been activated. Implementations of client-side block filtering obtain their filters from semi-trusted sources. Whichever implementation ends up being soft-forked would have to be included in bitcoin code forever (unless the soft fork rule has a sunset).

Software support

The BIPs name the lightninglabs Neutrino library as the reference light client.[1][2] Bitcoin Core can index BIP 158 filters from version 0.19.0.1 and serves them to peers with BIP 157 from version 0.21.0, when started with -blockfilterindex=1 -peerblockfilters=1. Version 0.19.0 was tagged but never released.[4][5][6][7]

Neutrino

One implementation of client-side block filtering is Neutrino also known as BIP157 and BIP158 which is used by some Lightning Network lightweight wallets.

See also

External links

References