Skip to content

Commit

Permalink
rpc: addnode arg to use BIP324 v2 p2p
Browse files Browse the repository at this point in the history
Co-authored-by: Pieter Wuille <bitcoin-dev@wuille.net>
  • Loading branch information
2 people authored and sipa committed Oct 2, 2023
1 parent 62d21ee commit c73cd42
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
32 changes: 16 additions & 16 deletions src/net.cpp
Expand Up @@ -2452,7 +2452,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
// Perform cheap checks before locking a mutex.
else if (!dnsseed && !use_seednodes) {
LOCK(m_added_nodes_mutex);
if (m_added_nodes.empty()) {
if (m_added_node_params.empty()) {
add_fixed_seeds_now = true;
LogPrintf("Adding fixed seeds as -dnsseed=0 (or IPv4/IPv6 connections are disabled via -onlynet) and neither -addnode nor -seednode are provided\n");
}
Expand Down Expand Up @@ -2725,11 +2725,11 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
{
std::vector<AddedNodeInfo> ret;

std::list<std::string> lAddresses(0);
std::list<AddedNodeParams> lAddresses(0);
{
LOCK(m_added_nodes_mutex);
ret.reserve(m_added_nodes.size());
std::copy(m_added_nodes.cbegin(), m_added_nodes.cend(), std::back_inserter(lAddresses));
ret.reserve(m_added_node_params.size());
std::copy(m_added_node_params.cbegin(), m_added_node_params.cend(), std::back_inserter(lAddresses));
}


Expand All @@ -2749,9 +2749,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
}
}

for (const std::string& strAddNode : lAddresses) {
CService service(LookupNumeric(strAddNode, GetDefaultPort(strAddNode)));
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
for (const auto& addr : lAddresses) {
CService service(LookupNumeric(addr.m_added_node, GetDefaultPort(addr.m_added_node)));
AddedNodeInfo addedNode{addr, CService(), false, false};
if (service.IsValid()) {
// strAddNode is an IP:port
auto it = mapConnected.find(service);
Expand All @@ -2762,7 +2762,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
}
} else {
// strAddNode is a name
auto it = mapConnectedByName.find(strAddNode);
auto it = mapConnectedByName.find(addr.m_added_node);
if (it != mapConnectedByName.end()) {
addedNode.resolvedAddress = it->second.second;
addedNode.fConnected = true;
Expand Down Expand Up @@ -2792,7 +2792,7 @@ void CConnman::ThreadOpenAddedConnections()
}
tried = true;
CAddress addr(CService(), NODE_NONE);
OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/false);
OpenNetworkConnection(addr, false, &grant, info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
return;
}
Expand Down Expand Up @@ -3384,23 +3384,23 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
return cache_entry.m_addrs_response_cache;
}

bool CConnman::AddNode(const std::string& strNode)
bool CConnman::AddNode(const AddedNodeParams& add)
{
LOCK(m_added_nodes_mutex);
for (const std::string& it : m_added_nodes) {
if (strNode == it) return false;
for (const auto& it : m_added_node_params) {
if (add.m_added_node == it.m_added_node) return false;
}

m_added_nodes.push_back(strNode);
m_added_node_params.push_back(add);
return true;
}

bool CConnman::RemoveAddedNode(const std::string& strNode)
{
LOCK(m_added_nodes_mutex);
for(std::vector<std::string>::iterator it = m_added_nodes.begin(); it != m_added_nodes.end(); ++it) {
if (strNode == *it) {
m_added_nodes.erase(it);
for (auto it = m_added_node_params.begin(); it != m_added_node_params.end(); ++it) {
if (strNode == it->m_added_node) {
m_added_node_params.erase(it);
return true;
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/net.h
Expand Up @@ -98,9 +98,13 @@ static constexpr bool DEFAULT_V2_TRANSPORT{false};

typedef int64_t NodeId;

struct AddedNodeInfo
{
std::string strAddedNode;
struct AddedNodeParams {
std::string m_added_node;
bool m_use_v2transport;
};

struct AddedNodeInfo {
AddedNodeParams m_params;
CService resolvedAddress;
bool fConnected;
bool fInbound;
Expand Down Expand Up @@ -1075,7 +1079,11 @@ class CConnman
vWhitelistedRange = connOptions.vWhitelistedRange;
{
LOCK(m_added_nodes_mutex);
m_added_nodes = connOptions.m_added_nodes;

for (const std::string& added_node : connOptions.m_added_nodes) {
// -addnode cli arg does not currently have a way to signal BIP324 support
m_added_node_params.push_back({added_node, false});
}
}
m_onion_binds = connOptions.onion_binds;
}
Expand Down Expand Up @@ -1162,7 +1170,7 @@ class CConnman
// Count the number of block-relay-only peers we have over our limit.
int GetExtraBlockRelayCount() const;

bool AddNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
std::vector<AddedNodeInfo> GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);

Expand Down Expand Up @@ -1387,7 +1395,10 @@ class CConnman
const NetGroupManager& m_netgroupman;
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
Mutex m_addr_fetches_mutex;
std::vector<std::string> m_added_nodes GUARDED_BY(m_added_nodes_mutex);

// connection string and whether to use v2 p2p
std::vector<AddedNodeParams> m_added_node_params GUARDED_BY(m_added_nodes_mutex);

mutable Mutex m_added_nodes_mutex;
std::vector<CNode*> m_nodes GUARDED_BY(m_nodes_mutex);
std::list<CNode*> m_nodes_disconnected;
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Expand Up @@ -301,6 +301,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "addpeeraddress", 2, "tried"},
{ "sendmsgtopeer", 0, "peer_id" },
{ "stop", 0, "wait" },
{ "addnode", 2, "v2transport" },
};
// clang-format on

Expand Down
18 changes: 12 additions & 6 deletions src/rpc/net.cpp
Expand Up @@ -289,11 +289,12 @@ static RPCHelpMan addnode()
{
{"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the peer to connect to"},
{"command", RPCArg::Type::STR, RPCArg::Optional::NO, "'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once"},
{"v2transport", RPCArg::Type::BOOL, RPCArg::Default{false}, "Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)"},
},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{
HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\"")
+ HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\" true")
+ HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\" true")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
Expand All @@ -307,17 +308,22 @@ static RPCHelpMan addnode()
CConnman& connman = EnsureConnman(node);

const std::string node_arg{request.params[0].get_str()};
bool use_v2transport = self.Arg<bool>(2);

if (use_v2transport && !(node.connman->GetLocalServices() & NODE_P2P_V2)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
}

if (command == "onetry")
{
CAddress addr;
connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grantOutbound=*/nullptr, node_arg.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/false);
connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grantOutbound=*/nullptr, node_arg.c_str(), ConnectionType::MANUAL, use_v2transport);
return UniValue::VNULL;
}

if (command == "add")
{
if (!connman.AddNode(node_arg)) {
if (!connman.AddNode({node_arg, use_v2transport})) {
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
}
}
Expand Down Expand Up @@ -475,7 +481,7 @@ static RPCHelpMan getaddednodeinfo()
if (!request.params[0].isNull()) {
bool found = false;
for (const AddedNodeInfo& info : vInfo) {
if (info.strAddedNode == request.params[0].get_str()) {
if (info.m_params.m_added_node == request.params[0].get_str()) {
vInfo.assign(1, info);
found = true;
break;
Expand All @@ -490,7 +496,7 @@ static RPCHelpMan getaddednodeinfo()

for (const AddedNodeInfo& info : vInfo) {
UniValue obj(UniValue::VOBJ);
obj.pushKV("addednode", info.strAddedNode);
obj.pushKV("addednode", info.m_params.m_added_node);
obj.pushKV("connected", info.fConnected);
UniValue addresses(UniValue::VARR);
if (info.fConnected) {
Expand Down
1 change: 1 addition & 0 deletions src/rpc/util.cpp
Expand Up @@ -682,6 +682,7 @@ TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get
TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;);

// Required arg or optional arg with default value.
TMPL_INST(CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg)->get_bool(););
TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>(););
TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>(););
TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str(););
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/connman.cpp
Expand Up @@ -61,7 +61,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
},
[&] {
connman.AddNode(random_string);
connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
},
[&] {
connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
Expand Down

0 comments on commit c73cd42

Please sign in to comment.