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.
251return height;252}254template <typename Key, class Comparator>255bool SkipList<Key, Comparator>::KeyIsAfterNode(const Key& key, Node* n) const {256// null n is considered infinite257return (n != nullptr) && (compare_(n->key, key) < 0);258}260template <typename Key, class Comparator>261typename SkipList<Key, Comparator>::Node*262SkipList<Key, Comparator>::FindGreaterOrEqual(const Key& key,263Node** prev) const {264Node* x = head_;265int level = GetMaxHeight() - 1;266while (true) {267Node* next = x->Next(level);268if (KeyIsAfterNode(key, next)) {269// Keep searching in this list270x = next;271} else {272if (prev != nullptr) prev[level] = x;273if (level == 0) {274return next;275} else {276// Switch to next list277level--;278}279}280}281}283template <typename Key, class Comparator>284typename SkipList<Key, Comparator>::Node*285SkipList<Key, Comparator>::FindLessThan(const Key& key) const {286Node* x = head_;287int level = GetMaxHeight() - 1;288while (true) {289assert(x == head_ || compare_(x->key, key) < 0);290Node* next = x->Next(level);291if (next == nullptr || compare_(next->key, key) >= 0) {292if (level == 0) {293return x;294} else {295// Switch to next list296level--;297}298} else {299x = next;300}301}302}304template <typename Key, class Comparator>305typename SkipList<Key, Comparator>::Node* SkipList<Key, Comparator>::FindLast()306const {307Node* x = head_;308int level = GetMaxHeight() - 1;309while (true) {310Node* next = x->Next(level);311if (next == nullptr) {312if (level == 0) {313return x;314} else {315// Switch to next list316level--;317}318} else {319x = next;320}321}322}324template <typename Key, class Comparator>325SkipList<Key, Comparator>::SkipList(Comparator cmp, Arena* arena)326: compare_(cmp),327arena_(arena),328head_(NewNode(0 /* any key will do */, kMaxHeight)),329max_height_(1),330rnd_(0xdeadbeef) {331for (int i = 0; i < kMaxHeight; i++) {332head_->SetNext(i, nullptr);333}334}336template <typename Key, class Comparator>337void SkipList<Key, Comparator>::Insert(const Key& key) {338// TODO(opt): We can use a barrier-free variant of FindGreaterOrEqual()339// here since Insert() is externally synchronized.340Node* prev[kMaxHeight];341Node* x = FindGreaterOrEqual(key, prev);343// Our data structure does not allow duplicate insertion344assert(x == nullptr || !Equal(key, x->key));346int height = RandomHeight();347if (height > GetMaxHeight()) {348for (int i = GetMaxHeight(); i < height; i++) {349prev[i] = head_;350}351// It is ok to mutate max_height_ without any synchronization352// with concurrent readers. A concurrent reader that observes353// the new value of max_height_ will see either the old value of354// new level pointers from head_ (nullptr), or a new value set in355// the loop below. In the former case the reader will356// immediately drop to the next level since nullptr sorts after all357// keys. In the latter case the reader will use the new node.358max_height_.store(height, std::memory_order_relaxed);359}361x = NewNode(key, height);362for (int i = 0; i < height; i++) {363// NoBarrier_SetNext() suffices since we will add a barrier when364// we publish a pointer to "x" in prev[i].365x->NoBarrier_SetNext(i, prev[i]->NoBarrier_Next(i));366prev[i]->SetNext(i, x);367}368}370template <typename Key, class Comparator>371bool SkipList<Key, Comparator>::Contains(const Key& key) const {372Node* x = FindGreaterOrEqual(key, nullptr);373if (x != nullptr && Equal(key, x->key)) {374return true;375} else {376return false;377}378}380} // namespace leveldb382#endif // STORAGE_LEVELDB_DB_SKIPLIST_H_
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.