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// protect 2 localhost and 2 onions, plus 4 other peers, sorted by longest uptime.252BOOST_CHECK(IsProtected(25316, [](NodeEvictionCandidate& c) {254c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};255c.m_is_local = (c.id == 6 || c.id == 9 || c.id == 11 || c.id == 12);256c.m_network = (c.id == 8 || c.id == 10) ? NET_ONION : NET_IPV6;257},258/*protected_peer_ids=*/{0, 1, 2, 3, 6, 8, 9, 10},259/*unprotected_peer_ids=*/{4, 5, 7, 11, 12, 13, 14, 15},260random_context));262// Combined test: expect having 5 localhost and 1 onion peer out of 16 to263// protect 3 localhost (recovering the unused onion slot), 1 onion, and 4264// others, sorted by longest uptime.265BOOST_CHECK(IsProtected(26616, [](NodeEvictionCandidate& c) {267c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};268c.m_is_local = (c.id > 10);269c.m_network = (c.id == 10) ? NET_ONION : NET_IPV4;270},271/*protected_peer_ids=*/{0, 1, 2, 3, 10, 11, 12, 13},272/*unprotected_peer_ids=*/{4, 5, 6, 7, 8, 9, 14, 15},273random_context));275// Combined test: expect having 1 localhost and 4 onion peers out of 16 to276// protect 1 localhost and 3 onions (recovering the unused localhost slot),277// plus 4 others, sorted by longest uptime.278BOOST_CHECK(IsProtected(27916, [](NodeEvictionCandidate& c) {280c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};281c.m_is_local = (c.id == 15);282c.m_network = (c.id > 6 && c.id < 11) ? NET_ONION : NET_IPV6;283},284/*protected_peer_ids=*/{0, 1, 2, 3, 7, 8, 9, 15},285/*unprotected_peer_ids=*/{5, 6, 10, 11, 12, 13, 14},286random_context));288// Combined test: expect having 2 onion and 4 I2P out of 12 peers to protect289// 2 onion (prioritized for having fewer candidates) and 1 I2P, plus 3290// others, sorted by longest uptime.291BOOST_CHECK(IsProtected(292num_peers, [](NodeEvictionCandidate& c) {293c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};294c.m_is_local = false;295if (c.id == 8 || c.id == 10) {296c.m_network = NET_ONION;297} else if (c.id == 6 || c.id == 9 || c.id == 11 || c.id == 12) {298c.m_network = NET_I2P;299} else {300c.m_network = NET_IPV4;301}302},303/*protected_peer_ids=*/{0, 1, 2, 6, 8, 10},304/*unprotected_peer_ids=*/{3, 4, 5, 7, 9, 11},305random_context));307// Tests with 3 networks...309// Combined test: expect having 1 localhost, 1 I2P and 1 onion peer out of 4310// to protect 1 I2P, 0 localhost, 0 onion and 1 other peer (2 total), sorted311// by longest uptime; stable sort breaks tie with array order of I2P first.312BOOST_CHECK(IsProtected(3134, [](NodeEvictionCandidate& c) {314c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};315c.m_is_local = (c.id == 2);316if (c.id == 3) {317c.m_network = NET_I2P;318} else if (c.id == 1) {319c.m_network = NET_ONION;320} else {321c.m_network = NET_IPV6;322}323},324/*protected_peer_ids=*/{0, 3},325/*unprotected_peer_ids=*/{1, 2},326random_context));328// Combined test: expect having 1 localhost, 1 I2P and 1 onion peer out of 7329// to protect 1 I2P, 0 localhost, 0 onion and 2 other peers (3 total) sorted330// by longest uptime; stable sort breaks tie with array order of I2P first.331BOOST_CHECK(IsProtected(3327, [](NodeEvictionCandidate& c) {333c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};334c.m_is_local = (c.id == 4);335if (c.id == 6) {336c.m_network = NET_I2P;337} else if (c.id == 5) {338c.m_network = NET_ONION;339} else {340c.m_network = NET_IPV6;341}342},343/*protected_peer_ids=*/{0, 1, 6},344/*unprotected_peer_ids=*/{2, 3, 4, 5},345random_context));347// Combined test: expect having 1 localhost, 1 I2P and 1 onion peer out of 8348// to protect 1 I2P, 1 localhost, 0 onion and 2 other peers (4 total) sorted349// by uptime; stable sort breaks tie with array order of I2P then localhost.350BOOST_CHECK(IsProtected(3518, [](NodeEvictionCandidate& c) {352c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};353c.m_is_local = (c.id == 6);354if (c.id == 5) {355c.m_network = NET_I2P;356} else if (c.id == 4) {357c.m_network = NET_ONION;358} else {359c.m_network = NET_IPV6;360}361},362/*protected_peer_ids=*/{0, 1, 5, 6},363/*unprotected_peer_ids=*/{2, 3, 4, 7},364random_context));366// Combined test: expect having 4 localhost, 2 I2P, and 2 onion peers out of367// 16 to protect 1 localhost, 2 I2P, and 1 onion (4/16 total), plus 4 others368// for 8 total, sorted by longest uptime.369BOOST_CHECK(IsProtected(37016, [](NodeEvictionCandidate& c) {371c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};372c.m_is_local = (c.id == 6 || c.id > 11);373if (c.id == 7 || c.id == 11) {374c.m_network = NET_I2P;375} else if (c.id == 9 || c.id == 10) {376c.m_network = NET_ONION;377} else {378c.m_network = NET_IPV4;379}380},381/*protected_peer_ids=*/{0, 1, 2, 3, 6, 7, 9, 11},382/*unprotected_peer_ids=*/{4, 5, 8, 10, 12, 13, 14, 15},383random_context));385// Combined test: expect having 1 localhost, 8 I2P and 1 onion peer out of386// 24 to protect 1, 4, and 1 (6 total), plus 6 others for 12/24 total,387// sorted by longest uptime.388BOOST_CHECK(IsProtected(38924, [](NodeEvictionCandidate& c) {390c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};391c.m_is_local = (c.id == 12);392if (c.id > 14 && c.id < 23) { // 4 protected instead of usual 2393c.m_network = NET_I2P;394} else if (c.id == 23) {395c.m_network = NET_ONION;396} else {397c.m_network = NET_IPV6;398}399},400/*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 12, 15, 16, 17, 18, 23},401/*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11, 13, 14, 19, 20, 21, 22},402random_context));404// Combined test: expect having 1 localhost, 3 I2P and 6 onion peers out of405// 24 to protect 1, 3, and 2 (6 total, I2P has fewer candidates and so gets the406// unused localhost slot), plus 6 others for 12/24 total, sorted by longest uptime.407BOOST_CHECK(IsProtected(40824, [](NodeEvictionCandidate& c) {409c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};410c.m_is_local = (c.id == 15);411if (c.id == 12 || c.id == 14 || c.id == 17) {412c.m_network = NET_I2P;413} else if (c.id > 17) { // 4 protected instead of usual 2414c.m_network = NET_ONION;415} else {416c.m_network = NET_IPV4;417}418},419/*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 12, 14, 15, 17, 18, 19},420/*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11, 13, 16, 20, 21, 22, 23},421random_context));423// Combined test: expect having 1 localhost, 7 I2P and 4 onion peers out of424// 24 to protect 1 localhost, 2 I2P, and 3 onions (6 total), plus 6 others425// for 12/24 total, sorted by longest uptime.426BOOST_CHECK(IsProtected(42724, [](NodeEvictionCandidate& c) {428c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};429c.m_is_local = (c.id == 13);430if (c.id > 16) {431c.m_network = NET_I2P;432} else if (c.id == 12 || c.id == 14 || c.id == 15 || c.id == 16) {433c.m_network = NET_ONION;434} else {435c.m_network = NET_IPV6;436}437},438/*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 17, 18},439/*unprotected_peer_ids=*/{6, 7, 8, 9, 10, 11, 16, 19, 20, 21, 22, 23},440random_context));442// Combined test: expect having 8 localhost, 4 CJDNS, and 3 onion peers out443// of 24 to protect 2 of each (6 total), plus 6 others for 12/24 total,444// sorted by longest uptime.445BOOST_CHECK(IsProtected(44624, [](NodeEvictionCandidate& c) {447c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};448c.m_is_local = (c.id > 15);449if (c.id > 10 && c.id < 15) {450c.m_network = NET_CJDNS;451} else if (c.id > 6 && c.id < 10) {452c.m_network = NET_ONION;453} else {454c.m_network = NET_IPV4;455}456},457/*protected_peer_ids=*/{0, 1, 2, 3, 4, 5, 7, 8, 11, 12, 16, 17},458/*unprotected_peer_ids=*/{6, 9, 10, 13, 14, 15, 18, 19, 20, 21, 22, 23},459random_context));461// Tests with 4 networks...463// Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer464// out of 5 to protect 1 CJDNS, 0 I2P, 0 localhost, 0 onion and 1 other peer465// (2 total), sorted by longest uptime; stable sort breaks tie with array466// order of CJDNS first.467BOOST_CHECK(IsProtected(4685, [](NodeEvictionCandidate& c) {469c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};470c.m_is_local = (c.id == 3);471if (c.id == 4) {472c.m_network = NET_CJDNS;473} else if (c.id == 1) {474c.m_network = NET_I2P;475} else if (c.id == 2) {476c.m_network = NET_ONION;477} else {478c.m_network = NET_IPV6;479}480},481/*protected_peer_ids=*/{0, 4},482/*unprotected_peer_ids=*/{1, 2, 3},483random_context));485// Combined test: expect having 1 CJDNS, 1 I2P, 1 localhost and 1 onion peer486// out of 7 to protect 1 CJDNS, 0, I2P, 0 localhost, 0 onion and 2 other487// peers (3 total) sorted by longest uptime; stable sort breaks tie with488// array order of CJDNS first.489BOOST_CHECK(IsProtected(4907, [](NodeEvictionCandidate& c) {491c.m_connected = NodeSeconds{std::chrono::seconds{c.id}};492c.m_is_local = (c.id == 4);493if (c.id == 6) {494c.m_network = NET_CJDNS;495} else if (c.id == 5) {496c.m_network = NET_I2P;497} else if (c.id == 3) {498c.m_network = NET_ONION;499} else {500c.m_network = NET_IPV4;
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.