Reading Bitcoin source code 3

In the previous article, I read line 123 of the AppInit function: InitParameterInteraction () function. This function is implemented in src / init.cpp, and the network parameters are set by parameter initialization.

1. First parse the -bind, -whitebind parameters. If these two parameters are set, -listen will be set to true, allowing listening to accept external connections, even if the -connect or -proxy parameters are set later, they will listen to the bound address

    // when specifying an explicit binding address, you want to listen on it
    // even when -connect or -proxy is specified
    if (gArgs.IsArgSet("-bind")) {
        if (gArgs.SoftSetBoolArg("-listen", true))
            LogPrintf("%s: parameter interaction: -bind set -> setting -listen=1\n", __func__);
    }
    if (gArgs.IsArgSet("-whitebind")) {
        if (gArgs.SoftSetBoolArg("-listen", true))
            LogPrintf("%s: parameter interaction: -whitebind set -> setting -listen=1\n", __func__);
    }

2. The -connect parameter is used to connect only trusted nodes. If the -connect parameter is set, -dnsseed and -listen will be set to false, which does not allow listening to requests and does not randomly discover nodes in the network through dns seeds.

    if (gArgs.IsArgSet("-connect")) {
        // when only connecting to trusted nodes, do not seed via DNS, or listen by default
        if (gArgs.SoftSetBoolArg("-dnsseed", false))
            LogPrintf("%s: parameter interaction: -connect set -> setting -dnsseed=0\n", __func__);
        if (gArgs.SoftSetBoolArg("-listen", false))
            LogPrintf("%s: parameter interaction: -connect set -> setting -listen=0\n", __func__);
    }

3, -proxy is to set the proxy server,
if this parameter is set to disable monitoring, set -listen to false.
Set -upnp to false, use global plug and play (UPNP) to map the listening port. When UPnP is closed, your node will still be connected to the other 8 peer nodes on the Bitcoin network to accept new blocks and transactions. However, it will not accept incoming connections from other peer nodes unless you manually enable port forwarding on your router.
At the same time, set -discover to false. -Discover indicates whether you want other nodes in the network to find their own address. If a proxy is set, naturally it should be set to false here.

    if (gArgs.IsArgSet("-proxy")) {
        // to protect privacy, do not listen by default if a default proxy server is specified
        if (gArgs.SoftSetBoolArg("-listen", false))
            LogPrintf("%s: parameter interaction: -proxy set -> setting -listen=0\n", __func__);
        // to protect privacy, do not use UPNP when a proxy is set. The user may still specify -listen=1
        // to listen locally, so don't rely on this happening through -listen below.
        if (gArgs.SoftSetBoolArg("-upnp", false))
            LogPrintf("%s: parameter interaction: -proxy set -> setting -upnp=0\n", __func__);
        // to protect privacy, do not discover addresses by default
        if (gArgs.SoftSetBoolArg("-discover", false))
            LogPrintf("%s: parameter interaction: -proxy set -> setting -discover=0\n", __func__);
    }

4. If monitoring is not set, set the -upnp, -discover, and -listenonion parameters to false, and -listennonion means anonymous address monitoring.

    if (!gArgs.GetBoolArg("-listen", DEFAULT_LISTEN)) {
        // do not map ports or try to retrieve public IP when not listening (pointless)
        if (gArgs.SoftSetBoolArg("-upnp", false))
            LogPrintf("%s: parameter interaction: -listen=0 -> setting -upnp=0\n", __func__);
        if (gArgs.SoftSetBoolArg("-discover", false))
            LogPrintf("%s: parameter interaction: -listen=0 -> setting -discover=0\n", __func__);
        if (gArgs.SoftSetBoolArg("-listenonion", false))
            LogPrintf("%s: parameter interaction: -listen=0 -> setting -listenonion=0\n", __func__);
    }

5. This -externalip indicates the specified public address, that is, synchronize the block information and broadcast transaction information from the specified public address, all messages are only sent to the specified public address, and do not look for other addresses

    if (gArgs.IsArgSet("-externalip")) {
        // if an explicit public IP is specified, do not try to find others
        if (gArgs.SoftSetBoolArg("-discover", false))
            LogPrintf("%s: parameter interaction: -externalip set -> setting -discover=0\n", __func__);
    }

6. If -blocksonly is set to true, -blocksonly means only packaged blocks are accepted, DEFAULT_BLOCKSONLY default is false, and -whitelistrelay is disabled, which means that transactions forwarded from nodes in the whitelist are not accepted.

    // disable whitelistrelay in blocksonly mode
    if (gArgs.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)) {
        if (gArgs.SoftSetBoolArg("-whitelistrelay", false))
            LogPrintf("%s: parameter interaction: -blocksonly=1 -> setting -whitelistrelay=0\n", __func__);
    }

7.If -whitelistforcerelay is set to true, it means that the transaction information relayed from the whitelist is forced to be forwarded, then set -whitelistrelay to true

    // Forcing relay from whitelisted hosts implies we will accept relays from them in the first place.
    if (gArgs.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
        if (gArgs.SoftSetBoolArg("-whitelistrelay", true))
            LogPrintf("%s: parameter interaction: -whitelistforcerelay=1 -> setting -whitelistrelay=1\n", __func__);
    }

8. The parameter -blockmaxsize has been replaced by -blockmaxweight, and the concept of block size in SegWit is replaced by block weight. Refer to https://en.bitcoin.it/wiki/Weight_units, WITNESS_SCALE_FACTOR constant in src / consensus / consensus .h definition, static const int WITNESS_SCALE_FACTOR = 4;

    if (gArgs.IsArgSet("-blockmaxsize")) {
        unsigned int max_size = gArgs.GetArg("-blockmaxsize", 0);
        if (gArgs.SoftSetArg("blockmaxweight", strprintf("%d", max_size * WITNESS_SCALE_FACTOR))) {
            LogPrintf("%s: parameter interaction: -blockmaxsize=%d -> setting -blockmaxweight=%d (-blockmaxsize is deprecated!)\n", __func__, max_size, max_size * WITNESS_SCALE_FACTOR);
        } else {
            LogPrintf("%s: Ignoring blockmaxsize setting which is overridden by blockmaxweight", __func__);
        }
    }
}
Published 30 original articles · praised 74 · 230,000 views +

Guess you like

Origin blog.csdn.net/ruiyiin/article/details/104633275