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
API
Constants
create
Create a new bloom filter with specified parameters.m- Number of bits in the filter (must be positive)k- Number of hash functions (must be positive)
InvalidBloomFilterParameterError if parameters are invalid
add
Add an item to the bloom filter. Mutates the filter in place.contains
Check if an item might be in the filter.true result requires verification against actual data.
merge
Combine two bloom filters using bitwise OR. Both filters must have the same parameters.InvalidBloomFilterParameterError if filters have different m or k values
combine
Combine multiple bloom filters into one.InvalidBloomFilterParameterError if filters have different parameters or array is empty
toHex
Convert bloom filter to hex string.fromHex
Create bloom filter from hex string.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).expectedFalsePositiveRate
Calculate theoretical false positive probability.(1 - e^(-k*n/m))^k where k = hash functions, n = items, m = bits
Type
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

