eth-bloom

3.1.0 · active · verified Wed Apr 15

eth-bloom is a Python library providing an implementation of the bloom filter used by Ethereum. It allows for efficient probabilistic checks of set membership, commonly used to quickly detect the likely presence of event logs within Ethereum blocks and transaction receipts. The library is actively maintained by the Ethereum Foundation and regularly updated to support modern Python versions.

Warnings

Install

Imports

Quickstart

Demonstrates how to create, populate, query, and combine Ethereum-style Bloom filters, as well as initialize one from an integer representation. Items added to the filter must be byte strings.

from eth_bloom import BloomFilter

# Create an empty BloomFilter
b1 = BloomFilter()
b2 = BloomFilter()

# Add items (must be bytes)
b1.add(b'a')
b1.add(b'common')
b2.add(b'b')
b2.add(b'common')

# Check for membership
print(b'a' in b1)       # True
print(b'b' in b1)       # False
print(b'common' in b1)  # True

# Bloom filters can be combined (union operation)
b3 = b1 + b2
print(b'a' in b3)       # True
print(b'b' in b3)       # True
print(b'common' in b3)  # True

# Initialize from an integer representation (e.g., a block's logsBloom)
bloom_int_representation = int(b1)
b_from_int = BloomFilter(bloom_int_representation)
print(b'a' in b_from_int) # True

view raw JSON →