Skip to content

Commit c73cd42

Browse files
dhruvPieter Wuille
authored andcommitted
rpc: addnode arg to use BIP324 v2 p2p
Co-authored-by: Pieter Wuille <bitcoin-dev@wuille.net>
1 parent 62d21ee commit c73cd42

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

src/net.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -2452,7 +2452,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24522452
// Perform cheap checks before locking a mutex.
24532453
else if (!dnsseed && !use_seednodes) {
24542454
LOCK(m_added_nodes_mutex);
2455-
if (m_added_nodes.empty()) {
2455+
if (m_added_node_params.empty()) {
24562456
add_fixed_seeds_now = true;
24572457
LogPrintf("Adding fixed seeds as -dnsseed=0 (or IPv4/IPv6 connections are disabled via -onlynet) and neither -addnode nor -seednode are provided\n");
24582458
}
@@ -2725,11 +2725,11 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
27252725
{
27262726
std::vector<AddedNodeInfo> ret;
27272727

2728-
std::list<std::string> lAddresses(0);
2728+
std::list<AddedNodeParams> lAddresses(0);
27292729
{
27302730
LOCK(m_added_nodes_mutex);
2731-
ret.reserve(m_added_nodes.size());
2732-
std::copy(m_added_nodes.cbegin(), m_added_nodes.cend(), std::back_inserter(lAddresses));
2731+
ret.reserve(m_added_node_params.size());
2732+
std::copy(m_added_node_params.cbegin(), m_added_node_params.cend(), std::back_inserter(lAddresses));
27332733
}
27342734

27352735

@@ -2749,9 +2749,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
27492749
}
27502750
}
27512751

2752-
for (const std::string& strAddNode : lAddresses) {
2753-
CService service(LookupNumeric(strAddNode, GetDefaultPort(strAddNode)));
2754-
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
2752+
for (const auto& addr : lAddresses) {
2753+
CService service(LookupNumeric(addr.m_added_node, GetDefaultPort(addr.m_added_node)));
2754+
AddedNodeInfo addedNode{addr, CService(), false, false};
27552755
if (service.IsValid()) {
27562756
// strAddNode is an IP:port
27572757
auto it = mapConnected.find(service);
@@ -2762,7 +2762,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
27622762
}
27632763
} else {
27642764
// strAddNode is a name
2765-
auto it = mapConnectedByName.find(strAddNode);
2765+
auto it = mapConnectedByName.find(addr.m_added_node);
27662766
if (it != mapConnectedByName.end()) {
27672767
addedNode.resolvedAddress = it->second.second;
27682768
addedNode.fConnected = true;
@@ -2792,7 +2792,7 @@ void CConnman::ThreadOpenAddedConnections()
27922792
}
27932793
tried = true;
27942794
CAddress addr(CService(), NODE_NONE);
2795-
OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/false);
2795+
OpenNetworkConnection(addr, false, &grant, info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
27962796
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
27972797
return;
27982798
}
@@ -3384,23 +3384,23 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
33843384
return cache_entry.m_addrs_response_cache;
33853385
}
33863386

3387-
bool CConnman::AddNode(const std::string& strNode)
3387+
bool CConnman::AddNode(const AddedNodeParams& add)
33883388
{
33893389
LOCK(m_added_nodes_mutex);
3390-
for (const std::string& it : m_added_nodes) {
3391-
if (strNode == it) return false;
3390+
for (const auto& it : m_added_node_params) {
3391+
if (add.m_added_node == it.m_added_node) return false;
33923392
}
33933393

3394-
m_added_nodes.push_back(strNode);
3394+
m_added_node_params.push_back(add);
33953395
return true;
33963396
}
33973397

33983398
bool CConnman::RemoveAddedNode(const std::string& strNode)
33993399
{
34003400
LOCK(m_added_nodes_mutex);
3401-
for(std::vector<std::string>::iterator it = m_added_nodes.begin(); it != m_added_nodes.end(); ++it) {
3402-
if (strNode == *it) {
3403-
m_added_nodes.erase(it);
3401+
for (auto it = m_added_node_params.begin(); it != m_added_node_params.end(); ++it) {
3402+
if (strNode == it->m_added_node) {
3403+
m_added_node_params.erase(it);
34043404
return true;
34053405
}
34063406
}

src/net.h

+17-6
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ static constexpr bool DEFAULT_V2_TRANSPORT{false};
9898

9999
typedef int64_t NodeId;
100100

101-
struct AddedNodeInfo
102-
{
103-
std::string strAddedNode;
101+
struct AddedNodeParams {
102+
std::string m_added_node;
103+
bool m_use_v2transport;
104+
};
105+
106+
struct AddedNodeInfo {
107+
AddedNodeParams m_params;
104108
CService resolvedAddress;
105109
bool fConnected;
106110
bool fInbound;
@@ -1075,7 +1079,11 @@ class CConnman
10751079
vWhitelistedRange = connOptions.vWhitelistedRange;
10761080
{
10771081
LOCK(m_added_nodes_mutex);
1078-
m_added_nodes = connOptions.m_added_nodes;
1082+
1083+
for (const std::string& added_node : connOptions.m_added_nodes) {
1084+
// -addnode cli arg does not currently have a way to signal BIP324 support
1085+
m_added_node_params.push_back({added_node, false});
1086+
}
10791087
}
10801088
m_onion_binds = connOptions.onion_binds;
10811089
}
@@ -1162,7 +1170,7 @@ class CConnman
11621170
// Count the number of block-relay-only peers we have over our limit.
11631171
int GetExtraBlockRelayCount() const;
11641172

1165-
bool AddNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
1173+
bool AddNode(const AddedNodeParams& add) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
11661174
bool RemoveAddedNode(const std::string& node) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
11671175
std::vector<AddedNodeInfo> GetAddedNodeInfo() const EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex);
11681176

@@ -1387,7 +1395,10 @@ class CConnman
13871395
const NetGroupManager& m_netgroupman;
13881396
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
13891397
Mutex m_addr_fetches_mutex;
1390-
std::vector<std::string> m_added_nodes GUARDED_BY(m_added_nodes_mutex);
1398+
1399+
// connection string and whether to use v2 p2p
1400+
std::vector<AddedNodeParams> m_added_node_params GUARDED_BY(m_added_nodes_mutex);
1401+
13911402
mutable Mutex m_added_nodes_mutex;
13921403
std::vector<CNode*> m_nodes GUARDED_BY(m_nodes_mutex);
13931404
std::list<CNode*> m_nodes_disconnected;

src/rpc/client.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
301301
{ "addpeeraddress", 2, "tried"},
302302
{ "sendmsgtopeer", 0, "peer_id" },
303303
{ "stop", 0, "wait" },
304+
{ "addnode", 2, "v2transport" },
304305
};
305306
// clang-format on
306307

src/rpc/net.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,12 @@ static RPCHelpMan addnode()
289289
{
290290
{"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The address of the peer to connect to"},
291291
{"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"},
292+
{"v2transport", RPCArg::Type::BOOL, RPCArg::Default{false}, "Attempt to connect using BIP324 v2 transport protocol (ignored for 'remove' command)"},
292293
},
293294
RPCResult{RPCResult::Type::NONE, "", ""},
294295
RPCExamples{
295-
HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\"")
296-
+ HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\"")
296+
HelpExampleCli("addnode", "\"192.168.0.6:8333\" \"onetry\" true")
297+
+ HelpExampleRpc("addnode", "\"192.168.0.6:8333\", \"onetry\" true")
297298
},
298299
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
299300
{
@@ -307,17 +308,22 @@ static RPCHelpMan addnode()
307308
CConnman& connman = EnsureConnman(node);
308309

309310
const std::string node_arg{request.params[0].get_str()};
311+
bool use_v2transport = self.Arg<bool>(2);
312+
313+
if (use_v2transport && !(node.connman->GetLocalServices() & NODE_P2P_V2)) {
314+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
315+
}
310316

311317
if (command == "onetry")
312318
{
313319
CAddress addr;
314-
connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grantOutbound=*/nullptr, node_arg.c_str(), ConnectionType::MANUAL, /*use_v2transport=*/false);
320+
connman.OpenNetworkConnection(addr, /*fCountFailure=*/false, /*grantOutbound=*/nullptr, node_arg.c_str(), ConnectionType::MANUAL, use_v2transport);
315321
return UniValue::VNULL;
316322
}
317323

318324
if (command == "add")
319325
{
320-
if (!connman.AddNode(node_arg)) {
326+
if (!connman.AddNode({node_arg, use_v2transport})) {
321327
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
322328
}
323329
}
@@ -475,7 +481,7 @@ static RPCHelpMan getaddednodeinfo()
475481
if (!request.params[0].isNull()) {
476482
bool found = false;
477483
for (const AddedNodeInfo& info : vInfo) {
478-
if (info.strAddedNode == request.params[0].get_str()) {
484+
if (info.m_params.m_added_node == request.params[0].get_str()) {
479485
vInfo.assign(1, info);
480486
found = true;
481487
break;
@@ -490,7 +496,7 @@ static RPCHelpMan getaddednodeinfo()
490496

491497
for (const AddedNodeInfo& info : vInfo) {
492498
UniValue obj(UniValue::VOBJ);
493-
obj.pushKV("addednode", info.strAddedNode);
499+
obj.pushKV("addednode", info.m_params.m_added_node);
494500
obj.pushKV("connected", info.fConnected);
495501
UniValue addresses(UniValue::VARR);
496502
if (info.fConnected) {

src/rpc/util.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ TMPL_INST(nullptr, std::optional<bool>, maybe_arg ? std::optional{maybe_arg->get
682682
TMPL_INST(nullptr, const std::string*, maybe_arg ? &maybe_arg->get_str() : nullptr;);
683683

684684
// Required arg or optional arg with default value.
685+
TMPL_INST(CheckRequiredOrDefault, bool, CHECK_NONFATAL(maybe_arg)->get_bool(););
685686
TMPL_INST(CheckRequiredOrDefault, int, CHECK_NONFATAL(maybe_arg)->getInt<int>(););
686687
TMPL_INST(CheckRequiredOrDefault, uint64_t, CHECK_NONFATAL(maybe_arg)->getInt<uint64_t>(););
687688
TMPL_INST(CheckRequiredOrDefault, const std::string&, CHECK_NONFATAL(maybe_arg)->get_str(););

src/test/fuzz/connman.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
6161
random_string = fuzzed_data_provider.ConsumeRandomLengthString(64);
6262
},
6363
[&] {
64-
connman.AddNode(random_string);
64+
connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
6565
},
6666
[&] {
6767
connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());

0 commit comments

Comments
 (0)