Repository: bitcoin/bitcoin
File: src/rpc/net.cpp (C++)

Code before:
            auto msg{TryParseHex<unsigned char>(request.params[2].get_str())};
            if (!msg.has_value()) {
                throw JSONRPCError(RPC_INVALID_PARAMETER, "Error parsing input for msg");
            }

            NodeContext& node = EnsureAnyNodeContext(request.context);
            CConnman& connman = EnsureConnman(node);

            CSerializedNetMsg msg_ser;
            msg_ser.data = msg.value();
            msg_ser.m_type = msg_type;

            bool success = connman.ForNode(peer_id, [&](CNode* node) {
                connman.PushMessage(node, std::move(msg_ser));
                return true;
            });

            if (!success) {
                throw JSONRPCError(RPC_MISC_ERROR, "Error: Could not send message to peer");
            }

            UniValue ret{UniValue::VOBJ};
            return ret;
        },
    };
}

static RPCHelpMan getaddrmaninfo()
{
    return RPCHelpMan{"getaddrmaninfo",
                      "\nProvides information about the node's address manager by returning the number of "
                      "addresses in the `new` and `tried` tables and their sum for all networks.\n"
                      "This RPC is for testing only.\n",
                      {},
                      RPCResult{
                              RPCResult::Type::OBJ_DYN, "", "json object with network type as keys",
                              {
                                      {RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ")",
                                       {
                                               {RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."},
                                               {RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
                                               {RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
                                       }},
                              }
                      },
                      RPCExamples{
                              HelpExampleCli("getaddrmaninfo", "")
                              + HelpExampleRpc("getaddrmaninfo", "")
                      },
                      [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
                      {

Review comment (refactor):
Could add `EnsureAnyAddrman` and remove this line as well?

Code after:
            }

            NodeContext& node = EnsureAnyNodeContext(request.context);
            CConnman& connman = EnsureConnman(node);

            CSerializedNetMsg msg_ser;
            msg_ser.data = msg.value();
            msg_ser.m_type = msg_type;

            bool success = connman.ForNode(peer_id, [&](CNode* node) {
                connman.PushMessage(node, std::move(msg_ser));
                return true;
            });

            if (!success) {
                throw JSONRPCError(RPC_MISC_ERROR, "Error: Could not send message to peer");
            }

            UniValue ret{UniValue::VOBJ};
            return ret;
        },
    };
}

static RPCHelpMan getaddrmaninfo()
{
    return RPCHelpMan{
        "getaddrmaninfo",
        "\nProvides information about the node's address manager by returning the number of "
        "addresses in the `new` and `tried` tables and their sum for all networks.\n",
        {},
        RPCResult{
            RPCResult::Type::OBJ_DYN, "", "json object with network type as keys", {
                {RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ", all_networks)", {
                {RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."},
                {RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
                {RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
            }},
        }},
        RPCExamples{HelpExampleCli("getaddrmaninfo", "") + HelpExampleRpc("getaddrmaninfo", "")},
        [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
            AddrMan& addrman = EnsureAnyAddrman(request.context);

            UniValue ret(UniValue::VOBJ);
            for (int n = 0; n < NET_MAX; ++n) {
                enum Network network = static_cast<enum Network>(n);
                if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
                UniValue obj(UniValue::VOBJ);
                obj.pushKV("new", addrman.Size(network, true));
                obj.pushKV("tried", addrman.Size(network, false));
                obj.pushKV("total", addrman.Size(network));