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.
252// Note that generation 0 is never inserted, so it is ok if253// <*,0,*> is missing.254ASSERT_TRUE((gen(pos) == 0) ||255(gen(pos) > static_cast<Key>(initial_state.Get(key(pos)))))256<< "key: " << key(pos) << "; gen: " << gen(pos)257<< "; initgen: " << initial_state.Get(key(pos));259// Advance to next key in the valid key space260if (key(pos) < key(current)) {261pos = MakeKey(key(pos) + 1, 0);262} else {263pos = MakeKey(key(pos), gen(pos) + 1);264}265}267if (!iter.Valid()) {268break;269}271if (rnd->Next() % 2) {272iter.Next();273pos = MakeKey(key(pos), gen(pos) + 1);274} else {275Key new_target = RandomTarget(rnd);276if (new_target > pos) {277pos = new_target;278iter.Seek(new_target);279}280}281}282}283};284const uint32_t ConcurrentTest::K;286// Simple test that does single-threaded testing of the ConcurrentTest287// scaffolding.288TEST(SkipTest, ConcurrentWithoutThreads) {289ConcurrentTest test;290Random rnd(test::RandomSeed());291for (int i = 0; i < 10000; i++) {292test.ReadStep(&rnd);293test.WriteStep(&rnd);294}295}297class TestState {298public:299ConcurrentTest t_;300int seed_;301std::atomic<bool> quit_flag_;303enum ReaderState { STARTING, RUNNING, DONE };305explicit TestState(int s)306: seed_(s), quit_flag_(false), state_(STARTING), state_cv_(&mu_) {}308void Wait(ReaderState s) LOCKS_EXCLUDED(mu_) {309mu_.Lock();310while (state_ != s) {311state_cv_.Wait();312}313mu_.Unlock();314}316void Change(ReaderState s) LOCKS_EXCLUDED(mu_) {317mu_.Lock();318state_ = s;319state_cv_.Signal();320mu_.Unlock();321}323private:324port::Mutex mu_;325ReaderState state_ GUARDED_BY(mu_);326port::CondVar state_cv_ GUARDED_BY(mu_);327};329static void ConcurrentReader(void* arg) {330TestState* state = reinterpret_cast<TestState*>(arg);331Random rnd(state->seed_);332int64_t reads = 0;333state->Change(TestState::RUNNING);334while (!state->quit_flag_.load(std::memory_order_acquire)) {335state->t_.ReadStep(&rnd);336++reads;337}338state->Change(TestState::DONE);339}341static void RunConcurrent(int run) {342const int seed = test::RandomSeed() + (run * 100);343Random rnd(seed);344const int N = 1000;345const int kSize = 1000;346for (int i = 0; i < N; i++) {347if ((i % 100) == 0) {348fprintf(stderr, "Run %d of %d\n", i, N);349}350TestState state(seed + 1);351Env::Default()->Schedule(ConcurrentReader, &state);352state.Wait(TestState::RUNNING);353for (int i = 0; i < kSize; i++) {354state.t_.WriteStep(&rnd);355}356state.quit_flag_.store(true, std::memory_order_release);357state.Wait(TestState::DONE);358}359}361TEST(SkipTest, Concurrent1) { RunConcurrent(1); }362TEST(SkipTest, Concurrent2) { RunConcurrent(2); }363TEST(SkipTest, Concurrent3) { RunConcurrent(3); }364TEST(SkipTest, Concurrent4) { RunConcurrent(4); }365TEST(SkipTest, Concurrent5) { RunConcurrent(5); }367} // namespace leveldb369int 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.