Compact blocks

From Bitcoin Wiki
Jump to navigation Jump to search

Compact blocks are a standard, specified in Bitcoin Improvement Proposal 152 (BIP 152), that lets peers announce and transfer blocks using short transaction identifiers instead of full transaction data.[1] A receiving node rebuilds the block from transactions already in its mempool and requests only the ones it is missing. Version 1 of the protocol uses txids. Version 2 uses wtxids and carries witness data for Segregated Witness.[1]

Motivation

Before compact blocks, every transaction in a block was sent when the block was relayed, although most of them were usually already known to the receiving node. this caused moderate inbound bandwidth spikes and could cause very large outbound spikes for nodes that received a block before their peers. Buffer bloat during such spikes could make consumer internet connections temporarily unusable.[1] The goal of the work is explicitly not to reduce block transfer latency, but the BIP says it does reduce latency as a side effect and forms a foundation for later work aimed at low-latency block transfer.[1]

Protocol

BIP 152 adds a new inventory type, MSG_CMPCT_BLOCK (4), and four messages:[1]

BIP 152 messages[1]
Message Contents and use
sendcmpct A 1-byte boolean and an 8-byte version number. The boolean selects high-bandwidth (1) or low-bandwidth (0) mode.
cmpctblock The 80-byte block header, an 8-byte nonce, a 6-byte short ID for each transaction not sent in full, and a list of prefilled transactions that always includes the coinbase.
getblocktxn The block hash and the differentially encoded indexes of the transactions the receiver is missing.
blocktxn The requested transactions.

In high-bandwidth mode the sender pushes a cmpctblock as soon as it has a valid header for the new block, before fully validating the block. In low-bandwidth mode the sender announces new blocks with inv or headers messages after validation, as in BIP 130, and the receiver may then request a compact block with getdata.[1] Nodes with enough inbound bandwidth are recommended to request high-bandwidth mode from up to three peers, ideally those that recently delivered blocks fastest, and must not request it from more than three. All nodes should send sendcmpct to all suitable peers. Senders should limit prefilled transactions to 10 KB and, when in doubt, include only the coinbase.[1]

When a cmpctblock arrives, the receiver computes the short ID of each transaction in its mempool and compares them with those in the message, then asks for the rest with getblocktxn. Short IDs may occasionally collide, and nodes must not be penalized for such collisions.[1] Because high-bandwidth announcements can come before full validation, nodes should not ban a peer for announcing an invalid block whose header is valid. Nodes signal this by using protocol version 70015 or higher. SPV clients generally do not use high-bandwidth mode, because they would be accepting unvalidated transaction lists from a peer.[1]


Short transaction IDs

A short ID is computed by hashing the block header followed by the nonce with single SHA-256, using the first two little-endian 64-bit integers of the result as the keys for SipHash-2-4 over the transaction ID, and dropping the two most significant bytes of the SipHash output to leave 6 bytes.[1] Using the block hash in the key prevents an attacker from grinding collisions that affect every link, so collisions between honest nodes are expected to be random. The BIP calculates that 48-bit short IDs are enough for blocks of up to 10,000 transactions and mempools of up to 100,000 transactions, with a failure to reconstruct in at most one of 281,474 blocks; a failure only means falling back to normal block relay.[1]

Version 2 computes short IDs from the wtxid and sends transactions with witness data. It is a separate protocol version because high-bandwidth blocks can arrive unrequested, so whether they include witnesses has to be agreed in advance, and because different witnesses for the same transaction must be told apart during reconstruction.[1]


Mempool synchronization quality dominates reconstruction success. Peers that recently shared transactions through ordinary inv relay reconstruct most blocks with few or no getblocktxn round trips. After a long partition or during a large unexpected block, missing-transaction requests rise and latency approaches that of full block relay.

Software support

Other full node implementations (btcd, libbitcoin, knots) implement BIP152 with varying completeness. Bitcoin Core implemented version 1 in pull request #8068 and version 2 in pull request #8393.[1] Version 0.13.0 introduced compact block relay, which it enables automatically between compatible peers.[2] Support for compact blocks together with segwit was included in 0.13.1.[3] The 0.13.0 release notes add that blocks containing many transactions a node has not seen, for example ones its policy discourages, are relayed more slowly than blocks made mostly of transactions already in its mempool.[2]

Compact blocks are a different mechanism from the compact block filters used by light clients.

Relay problem

Before compact blocks, a newly mined block was relayed as a full block message containing every transaction. Most of those transactions were already in receiving nodes' mempools, so the extra bandwidth was wasted. Outbound spikes caused bufferbloat on residential links and delayed further relay. Compact block relay aims to cut that bandwidth. Lower latency is a side effect, not the primary goal. Later work such as FIBRE and erasure-coded relay targeted latency more directly.

Deployment

Version 2 short IDs over wtxid avoid ambiguity when the same txid could pair with different witnesses. SegWit-aware nodes prefer version 2 once negotiated. Compact block relay is orthogonal to Erlay, which targets unconfirmed transaction announcements rather than block transfer. Operators still use compact blocks as the default low-bandwidth path for new blocks among fully validating peers. High-bandwidth mode sends the compact block to selected peers without waiting for an inv round trip, which cuts latency when the peer is expected to reconstruct successfully.

See also

External links

References