Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2012 The LevelDB Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include "leveldb/filter_policy.h"7#include "leveldb/slice.h"8#include "util/hash.h"10namespace leveldb {12namespace {13static uint32_t BloomHash(const Slice& key) {14return Hash(key.data(), key.size(), 0xbc9f1d34);15}17class BloomFilterPolicy : public FilterPolicy {18public:19explicit BloomFilterPolicy(int bits_per_key) : bits_per_key_(bits_per_key) {20// We intentionally round down to reduce probing cost a little bit21k_ = static_cast<size_t>(bits_per_key * 0.69); // 0.69 =~ ln(2)22if (k_ < 1) k_ = 1;23if (k_ > 30) k_ = 30;24}26const char* Name() const override { return "leveldb.BuiltinBloomFilter2"; }28void CreateFilter(const Slice* keys, int n, std::string* dst) const override {29// Compute bloom filter size (in both bits and bytes)30size_t bits = n * bits_per_key_;32// For small n, we can see a very high false positive rate. Fix it33// by enforcing a minimum bloom filter length.34if (bits < 64) bits = 64;36size_t bytes = (bits + 7) / 8;37bits = bytes * 8;39const size_t init_size = dst->size();40dst->resize(init_size + bytes, 0);41dst->push_back(static_cast<char>(k_)); // Remember # of probes in filter42char* array = &(*dst)[init_size];43for (int i = 0; i < n; i++) {44// Use double-hashing to generate a sequence of hash values.45// See analysis in [Kirsch,Mitzenmacher 2006].46uint32_t h = BloomHash(keys[i]);47const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits48for (size_t j = 0; j < k_; j++) {49const uint32_t bitpos = h % bits;50array[bitpos / 8] |= (1 << (bitpos % 8));51h += delta;52}53}54}56bool KeyMayMatch(const Slice& key, const Slice& bloom_filter) const override {57const size_t len = bloom_filter.size();58if (len < 2) return false;60const char* array = bloom_filter.data();61const size_t bits = (len - 1) * 8;63// Use the encoded k so that we can read filters generated by64// bloom filters created using different parameters.65const size_t k = array[len - 1];66if (k > 30) {67// Reserved for potentially new encodings for short bloom filters.68// Consider it a match.69return true;70}72uint32_t h = BloomHash(key);73const uint32_t delta = (h >> 17) | (h << 15); // Rotate right 17 bits74for (size_t j = 0; j < k; j++) {75const uint32_t bitpos = h % bits;76if ((array[bitpos / 8] & (1 << (bitpos % 8))) == 0) return false;77h += delta;78}79return true;80}82private:83size_t bits_per_key_;84size_t k_;85};86} // namespace88const FilterPolicy* NewBloomFilterPolicy(int bits_per_key) {89return new BloomFilterPolicy(bits_per_key);90}92} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.