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 "table/filter_block.h"7#include "leveldb/filter_policy.h"8#include "util/coding.h"9#include "util/hash.h"10#include "util/logging.h"11#include "util/testharness.h"12#include "util/testutil.h"14namespace leveldb {16// For testing: emit an array with one hash value per key17class TestHashFilter : public FilterPolicy {18public:19const char* Name() const override { return "TestHashFilter"; }21void CreateFilter(const Slice* keys, int n, std::string* dst) const override {22for (int i = 0; i < n; i++) {23uint32_t h = Hash(keys[i].data(), keys[i].size(), 1);24PutFixed32(dst, h);25}26}28bool KeyMayMatch(const Slice& key, const Slice& filter) const override {29uint32_t h = Hash(key.data(), key.size(), 1);30for (size_t i = 0; i + 4 <= filter.size(); i += 4) {31if (h == DecodeFixed32(filter.data() + i)) {32return true;33}34}35return false;36}37};39class FilterBlockTest {40public:41TestHashFilter policy_;42};44TEST(FilterBlockTest, EmptyBuilder) {45FilterBlockBuilder builder(&policy_);46Slice block = builder.Finish();47ASSERT_EQ("\\x00\\x00\\x00\\x00\\x0b", EscapeString(block));48FilterBlockReader reader(&policy_, block);49ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));50ASSERT_TRUE(reader.KeyMayMatch(100000, "foo"));51}53TEST(FilterBlockTest, SingleChunk) {54FilterBlockBuilder builder(&policy_);55builder.StartBlock(100);56builder.AddKey("foo");57builder.AddKey("bar");58builder.AddKey("box");59builder.StartBlock(200);60builder.AddKey("box");61builder.StartBlock(300);62builder.AddKey("hello");63Slice block = builder.Finish();64FilterBlockReader reader(&policy_, block);65ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));66ASSERT_TRUE(reader.KeyMayMatch(100, "bar"));67ASSERT_TRUE(reader.KeyMayMatch(100, "box"));68ASSERT_TRUE(reader.KeyMayMatch(100, "hello"));69ASSERT_TRUE(reader.KeyMayMatch(100, "foo"));70ASSERT_TRUE(!reader.KeyMayMatch(100, "missing"));71ASSERT_TRUE(!reader.KeyMayMatch(100, "other"));72}74TEST(FilterBlockTest, MultiChunk) {75FilterBlockBuilder builder(&policy_);77// First filter78builder.StartBlock(0);79builder.AddKey("foo");80builder.StartBlock(2000);81builder.AddKey("bar");83// Second filter84builder.StartBlock(3100);85builder.AddKey("box");87// Third filter is empty89// Last filter90builder.StartBlock(9000);91builder.AddKey("box");92builder.AddKey("hello");94Slice block = builder.Finish();95FilterBlockReader reader(&policy_, block);97// Check first filter98ASSERT_TRUE(reader.KeyMayMatch(0, "foo"));99ASSERT_TRUE(reader.KeyMayMatch(2000, "bar"));100ASSERT_TRUE(!reader.KeyMayMatch(0, "box"));101ASSERT_TRUE(!reader.KeyMayMatch(0, "hello"));103// Check second filter104ASSERT_TRUE(reader.KeyMayMatch(3100, "box"));105ASSERT_TRUE(!reader.KeyMayMatch(3100, "foo"));106ASSERT_TRUE(!reader.KeyMayMatch(3100, "bar"));107ASSERT_TRUE(!reader.KeyMayMatch(3100, "hello"));109// Check third filter (empty)110ASSERT_TRUE(!reader.KeyMayMatch(4100, "foo"));111ASSERT_TRUE(!reader.KeyMayMatch(4100, "bar"));112ASSERT_TRUE(!reader.KeyMayMatch(4100, "box"));113ASSERT_TRUE(!reader.KeyMayMatch(4100, "hello"));115// Check last filter116ASSERT_TRUE(reader.KeyMayMatch(9000, "box"));117ASSERT_TRUE(reader.KeyMayMatch(9000, "hello"));118ASSERT_TRUE(!reader.KeyMayMatch(9000, "foo"));119ASSERT_TRUE(!reader.KeyMayMatch(9000, "bar"));120}122} // namespace leveldb124int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }
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.