The Collection of Worlds: Iterating the Network Topology
The Collection of Worlds: Iterating the Network Topology
[!NOTE] Technical Context:
netbase.cpp| Lines 127-133In lines 127 through 133 of
netbase.cpp, we see the implementation ofGetNetworkNames. This function is the "Discovery Tool" for the node's supported networks. It provides a way for the node to list its entire "Vocabulary of Networks" to a user or another internal module.1.
GetNetworkNames: The Batch InquiryLine 129 defines the function:
std::vector<std::string> GetNetworkNames(bool append_unroutable).While the previous function (
GetNetworkName) translated a single enum, this function returns a List of All Names. This is used to populate UI dropdowns, help menus, and RPC results (like thenetworkslist ingetnetworkinfo).The
append_unroutableFlagThe function takes a Boolean parameter that controls whether the "Unroutable" category should be included. This is an architectural "Option."
True: Include everything, even the void. (Useful for developers and diagnostics).
False: Only show the "Real" networks. (Useful for end-users).
2. The Vector of Discovery
Line 131 initializes the result vector:
std::vector<std::string> names;.By using a
std::vector, the architect ensures that the order of the networks is preserved. This is a subtle but important part of User Experience. The order of networks in the code (IPv4, IPv6, Onion, etc.) will be the same order the user sees in the logs.3. The Enum Loop:
0toNET_MAXLine 132 contains the core iteration:
for (int n = 0; n < NET_MAX; ++n) {.The Beauty of the Enum Pattern
This loop demonstrates why the
NET_MAXsentinel we discussed in Part 048 is so important. By setting the loop limit toNET_MAX, the node automatically "Knows" how many networks exist.If a developer adds a new network type tomorrow, they don't have to change this loop. The loop will automatically pick up the new network because
NET_MAX(by definition) will have been incremented by the compiler. This is "Extensible by Design" architecture.4. The Complexity of Type Casting
Note that the loop uses an
int n, but it will eventually need to callGetNetworkName(enum Network).Internally, C++ enums are just integers. This loop leverages that fact to "Scan" the entire range of possible networks. It is a "Brute-Force Discovery" of the node's own capabilities.
5. Architectural Synthesis: The Comprehensive Registry
GetNetworkNamesis the "Registry" of the network stack. It provides a single point of truth for what "Worlds" the node is capable of speaking to.This batch operation is essential for Configuration Validation. When a user starts the node with a command-line flag like
-onlynet=ipv4, the node uses this function to verify that "ipv4" is actually a valid network name. It provides the "Vocabulary" against which all user input is measured.Conclusion: The Map is Fully Drawn
Lines 127-133 provide the final "Overview" of the network world. We have moved from individual names to a complete, iterable list of all supported protocols. The node now has a full "Index" of the global internet topology.
In the final Article 0050 of this batch, we will see how the
unroutableflag is handled and examine theSetProxyfunction, the master configuration method for the entirenetbasemodule.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: