Sparse eth_getLogs: the header bloom says yes too often
TL;DR
A filter for a rare address or topic must find the few blocks with a match. The header bloom of a mainnet block is now so full that it lets 31 to 46% of the blocks through. Only 0.1 to 0.5% of the blocks have a match. Cinder 0.9.5 writes a log filter row for each block: the sorted 24-bit fingerprints of its log keys, about 3.5 KB. We compared it with reth 2.7 on the measurement server, on the same head. Sparse queries over 10 000 blocks took 12 to 13 ms against 1.6 to 1.8 s. Cinder served 113 to 143 times the requests per second. Dense queries now take 0.51 to 0.71 of the reth time. In 0.9.2 they took 2.41 times the reth time. All 1 195 compared answers were equal.
Ask a mainnet node for the logs of one DEX pool factory over the last 10 000 blocks. There are about 63 such logs. On the same head and the same blocks, reth 2.7 answered in 1 593 ms by median, and Cinder 0.9.5 in 12.7 ms. With 4 connections, Cinder served 113 times the requests per second of reth. For a Transfer to one wallet, the factor was 143.
The reason is not a faster way to read receipts. The node reads far fewer of them. This post explains why the bloom filter in each block header no longer does its job on mainnet, what we put next to it, the variants we measured and rejected, and what the row costs in disk and write time.
What eth_getLogs does, and what a sparse filter is
eth_getLogs returns the events (logs) that contracts emitted in a range
of blocks. A filter names an address, topics (the event type and its
indexed arguments), or both. Indexers, wallets and dashboards send these
requests all day, often over thousands of blocks.
A filter is sparse when few blocks have a match: one factory
contract, one rare event, the transfers to one wallet. A filter is
dense when most blocks have a match: a stablecoin address, the
ERC-20 Transfer event. A dense query costs the time to read and send
many logs. A sparse query should cost almost nothing, because the
answer is small. On mainnet today it does not, because of the bloom.
The bloom says yes on almost half of the blocks
Each block header has a 2 048-bit log bloom. Each address and topic of each log in the block sets 3 bits in it. To check a block, a node looks at the 3 bits of the filter’s address. If one bit is 0, the block surely has no match, and the node skips it. If all 3 bits are 1, the block can have a match, and the node must read the receipts of the block to find out.
This worked when blocks had few logs. Today a mainnet block has about 1 150 distinct log keys (addresses and topics), so most of the 2 048 bits are 1. We measured it on 50 000 blocks:
For each 100 blocks that the header bloom lets through, the new row lets through 0.41 to 1.65.
| Row | Header bloom lets it through | Log filter row lets it through |
|---|---|---|
| A DEX pool factory (one address) (196 blocks have a match) | 19 627 blocks | 196 blocks |
| A pool-creation event (one topic) (259 blocks have a match) | 15 688 blocks | 259 blocks |
| Transfer to one wallet (two topics) (70 blocks have a match) | 22 993 blocks | 95 blocks |
On these blocks, the header bloom let a sparse filter through on 31 to 46% of the blocks, but only 0.1 to 0.5% of the blocks had a match. Every block that the bloom passes costs a read of all its receipts, about 260 KB. On a 50 000-block query, 85% of the time was this walk.
The wallet row shows one more thing. 25 of its 95 blocks are not false passes. These blocks have a Transfer log and a log with the wallet as topic 2, but not in the same log. A check per block cannot remove them. Only a read of the logs can.
The fix: a row of fingerprints for each block
For each block, Cinder 0.9.5 writes a log filter row in a new database table:
- A version byte and the first 8 bytes of the block hash.
- One fingerprint for each distinct log key of the block. A key is the address of a log, or a topic with its position (0 to 3). The fingerprint is the top 24 bits of a 64-bit hash of the key, stored in 3 bytes, in ascending order.
When the header bloom lets a filter through, the node reads the row. The row must have the fingerprint of an address of the filter, and of a topic at each topic position of the filter. If not, the block has no match, and the node does not read its receipts.
Two properties keep the answer the same:
- A key of the block always has its fingerprint in the row. The row never removes a block that has a match.
- A fingerprint can also match a key that the block does not have (a false pass). Then the node reads the receipts, as before.
A block without a row, with a row of another version, or with the hash
of another block (after a reorg) is read as before. A database of an
older Cinder works without rows, and cinder --build-log-filters writes
the rows of the blocks that have none, also while a node runs on the
database.
The variants we measured, and why 24 bits
Before the change, a read-only probe wrote the real rows of 50 000 blocks and ran the same filters through each variant. The gate was: a sparse filter must pass less than 10% of the blocks that the header bloom passes. The 24-bit fingerprints passed 1.0%, 1.65% and 0.41%.
| Variant | Bytes per block | Blocks passed: factory, event, wallet | Decision |
|---|---|---|---|
| A bigger bloom, 4 KB, 3 bits per key | 4 104 | 274, 334, 192 | rejected: more than one database page per block |
| A bloom of one page (4 064 bytes), 6 bits per key | 4 073 | 268, 324, 170 | rejected: passes more blocks for the same size |
| 16-bit fingerprints | smaller | 492, 563, 493 | rejected: too many false passes |
| 32-bit fingerprints | 4 597 | 196, 259, 95 | rejected: the same blocks as 24 bits, 25% larger |
| 24-bit fingerprints | 3 450 | 196, 259, 95 | kept |
A bloom of one page lets 40 to 80% more blocks through than the 24-bit row, for about the same size. A bloom tests bits. A sorted list of fingerprints tests exact 24-bit values. With about 1 150 keys in a block, a false match of 24 bits is rare.
What the row costs
Disk. On a database with the history of 51 239 blocks, the new table took 252 MB, 4.9 KB per block with the database overhead. That is 1.9% of the receipts. For the 92 000 blocks of a full node’s history window, it is about 450 MB.
Write time. The writer thread (the thread that writes executed blocks
to the database, after the node answers the consensus client) builds the
row from the receipts of the block. In two write runs, the row added
118 and 85 microseconds
per block. engine_newPayload does not wait for it.
Dense queries. A dense query reads one row per block, in 1 to 2 microseconds. On a development computer, the dense queries stayed within 2.3% of the time before, inside the noise of that computer.
Against reth 2.7, on the same head
On sparse filters, Cinder answered in 12 to 54 ms, and reth took 1.6 to 6.0 s.
| Row | Cinder 0.9.5 | reth 2.7 |
|---|---|---|
| DEX pool factory, 10 000 blocks (about 63 logs) | 12.7 ms | 1 593 ms |
| Transfer to one wallet, 10 000 blocks (about 23 logs) | 12 ms | 1 814 ms |
| DEX pool factory, 40 000 blocks (301 logs) | 51.2 ms | 5 967 ms |
| Transfer to one wallet, 40 000 blocks | 53.8 ms | 5 999 ms |
| Sparse case | reth 2.7, median | Cinder 0.9.5, median | reth req/s | Cinder req/s | Cinder / reth, req/s |
|---|---|---|---|---|---|
| DEX pool factory, 10 000 blocks, 4 connections | 1 673 ms | 14.5 ms | 2.40 | 272 | 113 |
| Transfer to one wallet, 10 000 blocks, 4 connections | 1 893 ms | 12.9 ms | 2.13 | 305 | 143 |
| DEX pool factory, 40 000 blocks, 1 connection | 5 967 ms | 51.2 ms | 0.15 | 19.5 | 130 |
| Transfer to one wallet, 40 000 blocks, 1 connection | 5 999 ms | 53.8 ms | 0.15 | 18.5 | 123 |
The record gives the cause: the log filter rows. Cinder 0.9.4 read the receipts of each block that the header bloom let through. Cinder 0.9.5 reads them only for the blocks that the row lets through. The gain comes from the reads that the row removes, not from a faster read.
Dense queries: from 2.41 times reth’s time to about two thirds
The row does nothing for a dense filter: almost every block has a match. Dense queries were slow on Cinder for other reasons. In the 0.9.2 record, a 100-block query on a stablecoin address took 2.41 times reth’s time. A profile of that query found two causes:
- 47% of the time was keccak: the node hashed every transaction before the one with a match, and hashed each filter value again for each receipt bloom.
- 25% was JSON: a tree of objects for each log, then the text of the tree. The block hash, number and time were written again for each log.
The fixes:
- Hash only the transaction with a match.
- Find the 3 bloom bits of each filter value once for each query.
- Write the answer directly as JSON text. Write the fields of each block and of each transaction once.
- Size one buffer for the whole answer, and send it without more copies. On a development computer, the 100-block query went from 67.1 to 23.5 ms against 0.9.4.
At 1 connection, Cinder 0.9.5 took 0.51 to 0.71 of reth’s time on dense queries.
| Row | Cinder 0.9.5 | reth 2.7 |
|---|---|---|
| Stablecoin, 100 recent blocks (about 8 000 logs, 5.2 MB) | 48.7 ms | 68.2 ms |
| Stablecoin, 100 older blocks (16 944 logs, 10.8 MB) | 131 ms | 257 ms |
| Transfer event, 30 blocks (about 17 500 logs, 11.3 MB) | 107 ms | 158 ms |
| Dense case, 1 connection | reth 2.7 | Cinder 0.9.5 | Cinder / reth |
|---|---|---|---|
| Stablecoin, 100 recent blocks | 68.2 ms | 48.7 ms | 0.71 |
| Stablecoin, 100 older blocks | 257 ms | 131 ms | 0.51 |
| Transfer event, 30 blocks | 158 ms | 107 ms | 0.68 |
At 4 and 16 connections the two nodes gave the same throughput on these cases. That is not a tie between the nodes: the network forward between the client and the server carried about 120 to 140 MB/s of answers, and both nodes filled it. These rows measure the forward.
The answers are the same
A faster wrong answer is worth nothing to an indexer. Each change of this work was checked against the code before it, byte for byte:
- The filter rows: 1 300 random filters on the scratch database, 0 differences. The filters had ranges of 1 to 50 000 blocks, 0 to 4 addresses, and topics that are present, absent or at another position.
- The dense changes: 528 120 logs in the random queries of that change, 0 differences.
- The single answer buffer: 1 789 requests, about 5.2 million logs, 0 differences, on HTTP and WebSocket.
Against reth 2.7 on the measurement server: all 1 195 compared answer pairs were equal, among them the sparse and dense log cases. The three large log answers were equal in bytes too.
What we did not do, and what this does not show
- No log index yet. A full index of log keys (the FilterMaps design of EIP-7745 is the one to study) can also remove the pass over the rows. After this change, a 50 000-block sparse query takes 40 to 55 ms on a development computer, mostly the read of 50 000 rows. That is good enough for now.
- No background backfill. A node with an old database does not build
the rows by itself. The command
cinder --build-log-filtersdoes it, and a block without a row works as before. - Old ranges against reth are short. reth 2.7 as a full node keeps logs from block 26 200 000, so the oldest range we could ask both nodes starts about 44 000 blocks below the head. Cinder keeps a longer history. We have no comparison for it.
- Compression. reth compresses an answer with gzip when the client asks for it. Cinder does not. Our client did not ask, so both nodes sent the same bytes. Through a slow link, a client that asks for gzip can get large answers from reth faster.
- One server, one day. Other work ran on the server and on the client computer. Read the ratios, not the times across records. The probe, the disk and the write numbers come from a development computer, not from the measurement server.
If you run eth_getLogs for others
Sparse filters over a long range are the requests that hold a worker for
seconds. With the rows, they take tens of milliseconds, and the answers
do not change. An existing database needs one run of
cinder --build-log-filters for the blocks it has. New blocks get their
row when the node writes them. The method pages, with the error codes and
limits that match reth, are in the
JSON-RPC reference. To run Cinder
against your own traffic, ask for access at
getcinder.dev/access.