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) 2011 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 "db/dbformat.h"6#include "util/logging.h"7#include "util/testharness.h"9namespace leveldb {11static std::string IKey(const std::string& user_key, uint64_t seq,12ValueType vt) {13std::string encoded;14AppendInternalKey(&encoded, ParsedInternalKey(user_key, seq, vt));15return encoded;16}18static std::string Shorten(const std::string& s, const std::string& l) {19std::string result = s;20InternalKeyComparator(BytewiseComparator()).FindShortestSeparator(&result, l);21return result;22}24static std::string ShortSuccessor(const std::string& s) {25std::string result = s;26InternalKeyComparator(BytewiseComparator()).FindShortSuccessor(&result);27return result;28}30static void TestKey(const std::string& key, uint64_t seq, ValueType vt) {31std::string encoded = IKey(key, seq, vt);33Slice in(encoded);34ParsedInternalKey decoded("", 0, kTypeValue);36ASSERT_TRUE(ParseInternalKey(in, &decoded));37ASSERT_EQ(key, decoded.user_key.ToString());38ASSERT_EQ(seq, decoded.sequence);39ASSERT_EQ(vt, decoded.type);41ASSERT_TRUE(!ParseInternalKey(Slice("bar"), &decoded));42}44class FormatTest {};46TEST(FormatTest, InternalKey_EncodeDecode) {47const char* keys[] = {"", "k", "hello", "longggggggggggggggggggggg"};48const uint64_t seq[] = {1,492,503,51(1ull << 8) - 1,521ull << 8,53(1ull << 8) + 1,54(1ull << 16) - 1,551ull << 16,56(1ull << 16) + 1,57(1ull << 32) - 1,581ull << 32,59(1ull << 32) + 1};60for (int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {61for (int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {62TestKey(keys[k], seq[s], kTypeValue);63TestKey("hello", 1, kTypeDeletion);64}65}66}68TEST(FormatTest, InternalKey_DecodeFromEmpty) {69InternalKey internal_key;71ASSERT_TRUE(!internal_key.DecodeFrom(""));72}74TEST(FormatTest, InternalKeyShortSeparator) {75// When user keys are same76ASSERT_EQ(IKey("foo", 100, kTypeValue),77Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 99, kTypeValue)));78ASSERT_EQ(79IKey("foo", 100, kTypeValue),80Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 101, kTypeValue)));81ASSERT_EQ(82IKey("foo", 100, kTypeValue),83Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeValue)));84ASSERT_EQ(85IKey("foo", 100, kTypeValue),86Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeDeletion)));88// When user keys are misordered89ASSERT_EQ(IKey("foo", 100, kTypeValue),90Shorten(IKey("foo", 100, kTypeValue), IKey("bar", 99, kTypeValue)));92// When user keys are different, but correctly ordered93ASSERT_EQ(94IKey("g", kMaxSequenceNumber, kValueTypeForSeek),95Shorten(IKey("foo", 100, kTypeValue), IKey("hello", 200, kTypeValue)));97// When start user key is prefix of limit user key98ASSERT_EQ(99IKey("foo", 100, kTypeValue),100Shorten(IKey("foo", 100, kTypeValue), IKey("foobar", 200, kTypeValue)));102// When limit user key is prefix of start user key103ASSERT_EQ(104IKey("foobar", 100, kTypeValue),105Shorten(IKey("foobar", 100, kTypeValue), IKey("foo", 200, kTypeValue)));106}108TEST(FormatTest, InternalKeyShortestSuccessor) {109ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),110ShortSuccessor(IKey("foo", 100, kTypeValue)));111ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),112ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));113}115TEST(FormatTest, ParsedInternalKeyDebugString) {116ParsedInternalKey key("The \"key\" in 'single quotes'", 42, kTypeValue);118ASSERT_EQ("'The \"key\" in 'single quotes'' @ 42 : 1", key.DebugString());119}121TEST(FormatTest, InternalKeyDebugString) {122InternalKey key("The \"key\" in 'single quotes'", 42, kTypeValue);123ASSERT_EQ("'The \"key\" in 'single quotes'' @ 42 : 1", key.DebugString());125InternalKey invalid_key;126ASSERT_EQ("(bad)", invalid_key.DebugString());127}129} // namespace leveldb131int 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.