Skip to main content

Overview

BloomFilter implements the 2048-bit bloom filter used in Ethereum block headers for efficient log filtering. Each block header contains a logs bloom that allows quick elimination of blocks that definitely don’t contain logs matching a filter query.

Ethereum Bloom Filters

Ethereum uses bloom filters in block headers to enable efficient log queries:
  • Size: 256 bytes (2048 bits)
  • Hash functions: 3
  • Purpose: Quick elimination of non-matching blocks
When querying logs by address or topics, nodes check the bloom filter first. A negative result means the block definitely doesn’t contain matching logs. A positive result means it might contain matches (requires full scan).

API

Constants

create

Create a new bloom filter with specified parameters.
Parameters:
  • m - Number of bits in the filter (must be positive)
  • k - Number of hash functions (must be positive)
Throws: InvalidBloomFilterParameterError if parameters are invalid

add

Add an item to the bloom filter. Mutates the filter in place.
Adding is idempotent - adding the same item twice has no additional effect.

contains

Check if an item might be in the filter.
Important: Bloom filters have no false negatives but can have false positives. A true result requires verification against actual data.

merge

Combine two bloom filters using bitwise OR. Both filters must have the same parameters.
Throws: InvalidBloomFilterParameterError if filters have different m or k values

combine

Combine multiple bloom filters into one.
Throws: InvalidBloomFilterParameterError if filters have different parameters or array is empty

toHex

Convert bloom filter to hex string.

fromHex

Create bloom filter from hex string.
Throws: InvalidBloomFilterLengthError if hex length doesn’t match expected size

isEmpty

Check if all bits are zero (no items added).

density

Calculate the percentage of bits set (0 to 1).
Higher density means higher false positive rate.

expectedFalsePositiveRate

Calculate theoretical false positive probability.
Formula: (1 - e^(-k*n/m))^k where k = hash functions, n = items, m = bits

Type

BloomFilter is a branded Uint8Array with attached k and m parameters.

Use Cases

Block Range Queries

Combine blooms to filter entire block ranges:

Log Subscription Filtering

Filter incoming logs efficiently:

Error Handling

Performance Notes

  • O(k) for add and contains operations where k = hash function count
  • O(n) for merge/combine where n = filter byte size
  • No allocations for add/contains (mutates in place or returns boolean)
  • Merge/combine allocate new filter

See Also

  • EventLog - Log type used in transactions
  • Keccak256 - Hash function for topics
  • Address - Address type for log filtering