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.
251}253TEST(CorruptionTest, TableFileIndexData) {254Build(10000); // Enough to build multiple Tables255DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);256dbi->TEST_CompactMemTable();258Corrupt(kTableFile, -2000, 500);259Reopen();260Check(5000, 9999);261}263TEST(CorruptionTest, MissingDescriptor) {264Build(1000);265RepairDB();266Reopen();267Check(1000, 1000);268}270TEST(CorruptionTest, SequenceNumberRecovery) {271ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));272ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));273ASSERT_OK(db_->Put(WriteOptions(), "foo", "v3"));274ASSERT_OK(db_->Put(WriteOptions(), "foo", "v4"));275ASSERT_OK(db_->Put(WriteOptions(), "foo", "v5"));276RepairDB();277Reopen();278std::string v;279ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));280ASSERT_EQ("v5", v);281// Write something. If sequence number was not recovered properly,282// it will be hidden by an earlier write.283ASSERT_OK(db_->Put(WriteOptions(), "foo", "v6"));284ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));285ASSERT_EQ("v6", v);286Reopen();287ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));288ASSERT_EQ("v6", v);289}291TEST(CorruptionTest, CorruptedDescriptor) {292ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));293DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);294dbi->TEST_CompactMemTable();295dbi->TEST_CompactRange(0, nullptr, nullptr);297Corrupt(kDescriptorFile, 0, 1000);298Status s = TryReopen();299ASSERT_TRUE(!s.ok());301RepairDB();302Reopen();303std::string v;304ASSERT_OK(db_->Get(ReadOptions(), "foo", &v));305ASSERT_EQ("hello", v);306}308TEST(CorruptionTest, CompactionInputError) {309Build(10);310DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);311dbi->TEST_CompactMemTable();312const int last = config::kMaxMemCompactLevel;313ASSERT_EQ(1, Property("leveldb.num-files-at-level" + NumberToString(last)));315Corrupt(kTableFile, 100, 1);316Check(5, 9);318// Force compactions by writing lots of values319Build(10000);320Check(10000, 10000);321}323TEST(CorruptionTest, CompactionInputErrorParanoid) {324options_.paranoid_checks = true;325options_.write_buffer_size = 512 << 10;326Reopen();327DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);329// Make multiple inputs so we need to compact.330for (int i = 0; i < 2; i++) {331Build(10);332dbi->TEST_CompactMemTable();333Corrupt(kTableFile, 100, 1);334env_.SleepForMicroseconds(100000);335}336dbi->CompactRange(nullptr, nullptr);338// Write must fail because of corrupted table339std::string tmp1, tmp2;340Status s = db_->Put(WriteOptions(), Key(5, &tmp1), Value(5, &tmp2));341ASSERT_TRUE(!s.ok()) << "write did not fail in corrupted paranoid db";342}344TEST(CorruptionTest, UnrelatedKeys) {345Build(10);346DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);347dbi->TEST_CompactMemTable();348Corrupt(kTableFile, 100, 1);350std::string tmp1, tmp2;351ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));352std::string v;353ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));354ASSERT_EQ(Value(1000, &tmp2).ToString(), v);355dbi->TEST_CompactMemTable();356ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));357ASSERT_EQ(Value(1000, &tmp2).ToString(), v);358}360} // namespace leveldb362int 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.