Damus
tuma profile picture
tuma

IBC#034: Bitcoin Core Initialization, Step 12: Whitelisting Peers

Article header image

Bind on any and whitelisting peers.

Hello everyone and welcome to this new episode of Inside Bitcoin Code on Nostr!

We concluded the previous episode with discussing about onion port bindings. Today, we continue our deep dive into step 12 of the initialization of Bitcoin Core, discussing about the possibility to bind on any addres and whitelisting peers. [Code Link]

Let’s start!


Bind on Any

First of all, the code checks if bind_on_any is set to true, which happens in case the user did not specify any input argument for -bind and -whitebind.

if (connOptions.bind_on_any)

If so, the code launches the Discover() function, whose goal is to look up IP addresses from all interfaces on the machine and add them to the list of local addresses to self-advertise.

Discover();

Consider supporting this newsletter by using one of my affiliate links. These are not sponsorships, just products I use everyday. Thank you!


-whitelist Input Argument

Then, the code iterates over the input arguments provided by -whitelist through a range-based for loop. This argument can be passed multiple times in the configuration file, and each one of them need to specify an IP address and, optionally, permissions granted to it.

for (const auto& net : args.GetArgs("-whitelist")) {
    .
    .
    .
}

Inside the loop, three variables are declared to store the parsed data from each input argument. Specifically, subnet will hold the whitelisted IP addressconnection_direction will store the direction of the connection with the peer — incomingoutgoing or both —, and error will contain the error string.

NetWhitelistPermissions subnet;
ConnectionDirection connection_direction;
bilingual_str error;

Then, the TryParse() function will attempt to parse the provided input net, storing the result in the declared variables. In case the attempt fails, for example in case of wrong input format, the code returns an InitError.

if (!NetWhitelistPermissions::TryParse(
    net, subnet, connection_direction, error))
        return InitError(error);

Finally, the code checks the connection_direction variable to see whether it is an incoming or an outgoing one.

To do so, the code uses the so-called bitwise operator &, which compares each bit at the same position in the integer and the resultant bit will be set 1 only and only if both corresponding bits are set 1, otherwise it will be unset 0.

The first if checks for the incoming connection. The expression returns true if connection_direction is either In or Both. If so, the subnet variable is added to the vWhitelistedRangeIncoming vector.

if (connection_direction & ConnectionDirection::In) {
    connOptions.vWhitelistedRangeIncoming.push_back(subnet);
}

Then, it checks for the outgoing connection. The expression returns true if connection_direction is either Out or Both. If so, the subnet variable is added to the vWhitelistedRangeOutgoing vector.

if (connection_direction & ConnectionDirection::Out) {
    connOptions.vWhitelistedRangeOutgoing.push_back(subnet);
}

Affiliate Program

Ready to Lead the Change*? If you are looking to professionalize your journey in the Bitcoin ecosystem, the Plan B Network provides the ultimate educational framework. Whether you are aiming to build the next major startup or dive deep into the protocol’s code, they have a Master track for you. Use my code* SS_Varese at checkout to receive 15% OFF on both the Business and Developers tracks!

---

Let’s keep in touch: