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) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <clientversion.h>6#include <common/args.h>7#include <flatfile.h>8#include <streams.h>9#include <test/util/setup_common.h>11#include <boost/test/unit_test.hpp>13BOOST_FIXTURE_TEST_SUITE(flatfile_tests, BasicTestingSetup)15BOOST_AUTO_TEST_CASE(flatfile_filename)16{17const auto data_dir = m_args.GetDataDirBase();19FlatFilePos pos(456, 789);21FlatFileSeq seq1(data_dir, "a", 16 * 1024);22BOOST_CHECK_EQUAL(seq1.FileName(pos), data_dir / "a00456.dat");24FlatFileSeq seq2(data_dir / "a", "b", 16 * 1024);25BOOST_CHECK_EQUAL(seq2.FileName(pos), data_dir / "a" / "b00456.dat");27// Check default constructor IsNull28assert(FlatFilePos{}.IsNull());29}31BOOST_AUTO_TEST_CASE(flatfile_open)32{33const auto data_dir = m_args.GetDataDirBase();34FlatFileSeq seq(data_dir, "a", 16 * 1024);36std::string line1("A purely peer-to-peer version of electronic cash would allow online "37"payments to be sent directly from one party to another without going "38"through a financial institution.");39std::string line2("Digital signatures provide part of the solution, but the main benefits are "40"lost if a trusted third party is still required to prevent double-spending.");42uint64_t pos1{0};43uint64_t pos2{pos1 + GetSerializeSize(line1)};45// Write first line to file.46{47AutoFile file{seq.Open(FlatFilePos(0, pos1))};48file << LIMITED_STRING(line1, 256);49BOOST_REQUIRE_EQUAL(file.fclose(), 0);50}52// Attempt to append to file opened in read-only mode.53{54AutoFile file{seq.Open(FlatFilePos(0, pos2), true)};55BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), std::ios_base::failure);56}58// Append second line to file.59{60AutoFile file{seq.Open(FlatFilePos(0, pos2))};61file << LIMITED_STRING(line2, 256);62BOOST_REQUIRE_EQUAL(file.fclose(), 0);63}65// Read text from file in read-only mode.66{67std::string text;68AutoFile file{seq.Open(FlatFilePos(0, pos1), true)};70file >> LIMITED_STRING(text, 256);71BOOST_CHECK_EQUAL(text, line1);73file >> LIMITED_STRING(text, 256);74BOOST_CHECK_EQUAL(text, line2);75}77// Read text from file with position offset.78{79std::string text;80AutoFile file{seq.Open(FlatFilePos(0, pos2))};82file >> LIMITED_STRING(text, 256);83BOOST_CHECK_EQUAL(text, line2);84BOOST_REQUIRE_EQUAL(file.fclose(), 0);85}87// Ensure another file in the sequence has no data.88{89std::string text;90AutoFile file{seq.Open(FlatFilePos(1, pos2))};91BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), std::ios_base::failure);92BOOST_REQUIRE_EQUAL(file.fclose(), 0);93}94}96BOOST_AUTO_TEST_CASE(flatfile_allocate)97{98const auto data_dir = m_args.GetDataDirBase();99FlatFileSeq seq(data_dir, "a", 100);101bool out_of_space;103BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 0), 1, out_of_space), 100U);104BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 0))), 100U);105BOOST_CHECK(!out_of_space);107BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 1, out_of_space), 0U);108BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 100U);109BOOST_CHECK(!out_of_space);111BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 2, out_of_space), 101U);112BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 200U);113BOOST_CHECK(!out_of_space);114}116BOOST_AUTO_TEST_CASE(flatfile_flush)117{118const auto data_dir = m_args.GetDataDirBase();119FlatFileSeq seq(data_dir, "a", 100);121bool out_of_space;122seq.Allocate(FlatFilePos(0, 0), 1, out_of_space);124// Flush without finalize should not truncate file.125seq.Flush(FlatFilePos(0, 1));126BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 100U);128// Flush with finalize should truncate file.129seq.Flush(FlatFilePos(0, 1), true);130BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 1U);131}133BOOST_AUTO_TEST_SUITE_END()
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.