Skip to content

Commit

Permalink
[rpc] addnode rpc arg to use v2 p2p
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv committed Jan 13, 2022
1 parent 587cbca commit 7dc92d4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
32 changes: 22 additions & 10 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
std::vector<CService> resolved;
if (Lookup(pszDest, resolved, default_port, fNameLookup && !HaveNameProxy(), 256) && !resolved.empty()) {
const CService rnd{resolved[GetRand(resolved.size())]};
addrConnect = CAddress{MaybeFlipIPv6toCJDNS(rnd), NODE_NONE};
addrConnect = CAddress{MaybeFlipIPv6toCJDNS(rnd), addrConnect.nServices};
if (!addrConnect.IsValid()) {
LogPrint(BCLog::NET, "Resolver returned invalid address %s for %s\n", addrConnect.ToString(), pszDest);
return nullptr;
Expand Down Expand Up @@ -2146,7 +2146,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
{
std::vector<AddedNodeInfo> ret;

std::list<std::string> lAddresses(0);
std::list<std::pair<std::string, bool>> lAddresses(0);
{
LOCK(m_added_nodes_mutex);
ret.reserve(m_added_nodes.size());
Expand All @@ -2170,7 +2170,11 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
}
}

for (const std::string& strAddNode : lAddresses) {
std::string strAddNode;
bool use_p2p_v2;

for (auto addr : lAddresses) {
std::tie(strAddNode, use_p2p_v2) = addr;
CService service(LookupNumeric(strAddNode, Params().GetDefaultPort(strAddNode)));
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
if (service.IsValid()) {
Expand All @@ -2180,6 +2184,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
addedNode.resolvedAddress = service;
addedNode.fConnected = true;
addedNode.fInbound = it->second;
addedNode.use_p2p_v2 = use_p2p_v2;
}
} else {
// strAddNode is a name
Expand All @@ -2188,6 +2193,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
addedNode.resolvedAddress = it->second.second;
addedNode.fConnected = true;
addedNode.fInbound = it->second.first;
addedNode.use_p2p_v2 = use_p2p_v2;
}
}
ret.emplace_back(std::move(addedNode));
Expand All @@ -2213,6 +2219,11 @@ void CConnman::ThreadOpenAddedConnections()
}
tried = true;
CAddress addr(CService(), NODE_NONE);

if (info.use_p2p_v2) {
addr.nServices = ServiceFlags(addr.nServices | NODE_P2P_V2);
}

OpenNetworkConnection(addr, false, &grant, info.strAddedNode.c_str(), ConnectionType::MANUAL);
if (!interruptNet.sleep_for(std::chrono::milliseconds(500)))
return;
Expand Down Expand Up @@ -2789,22 +2800,22 @@ 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 std::string& strNode, const bool use_p2p_v2)
{
LOCK(m_added_nodes_mutex);
for (const std::string& it : m_added_nodes) {
if (strNode == it) return false;
for (auto it : m_added_nodes) {
if (strNode == it.first) return false;
}

m_added_nodes.push_back(strNode);
m_added_nodes.push_back(std::make_pair(strNode, use_p2p_v2));
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) {
for(auto it = m_added_nodes.begin(); it != m_added_nodes.end(); ++it) {
if (strNode == it->first) {
m_added_nodes.erase(it);
return true;
}
Expand Down Expand Up @@ -2977,7 +2988,8 @@ ServiceFlags CConnman::GetLocalServices() const
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }

CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
: m_connected{GetTime<std::chrono::seconds>()},
: nServices(addrIn.nServices),
m_connected(GetTimeSeconds()),
addr(addrIn),
addrBind(addrBindIn),
m_addr_name{addrNameIn.empty() ? addr.ToStringIPPort() : addrNameIn},
Expand Down
14 changes: 11 additions & 3 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <memory>
#include <optional>
#include <thread>
#include <utility>
#include <vector>

class AddrMan;
Expand Down Expand Up @@ -92,6 +93,7 @@ struct AddedNodeInfo
CService resolvedAddress;
bool fConnected;
bool fInbound;
bool use_p2p_v2;
};

class CNodeStats;
Expand Down Expand Up @@ -792,7 +794,10 @@ class CConnman
vWhitelistedRange = connOptions.vWhitelistedRange;
{
LOCK(m_added_nodes_mutex);
m_added_nodes = connOptions.m_added_nodes;

for (const std::string& strAddedNode: connOptions.m_added_nodes) {
m_added_nodes.push_back(std::make_pair(strAddedNode, false));
}
}
m_onion_binds = connOptions.onion_binds;
}
Expand Down Expand Up @@ -876,7 +881,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);
bool AddNode(const std::string& node, const bool use_p2p_v2);
bool RemoveAddedNode(const std::string& node);
std::vector<AddedNodeInfo> GetAddedNodeInfo() const;

Expand Down Expand Up @@ -1099,7 +1104,10 @@ class CConnman
AddrMan& addrman;
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<std::pair<std::string, bool>> m_added_nodes 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/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ static std::string serviceFlagToStr(size_t bit)
case NODE_WITNESS: return "WITNESS";
case NODE_COMPACT_FILTERS: return "COMPACT_FILTERS";
case NODE_NETWORK_LIMITED: return "NETWORK_LIMITED";
case NODE_P2P_V2: return "P2P_V2";
// Not using default, so we get warned when a case is missing
}

Expand Down
3 changes: 3 additions & 0 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ enum ServiceFlags : uint64_t {
// See BIP159 for details on how this is implemented.
NODE_NETWORK_LIMITED = (1 << 10),

// NODE_P2P_V2 means the node supports BIP324 transport
NODE_P2P_V2 = (1 << 11),

// Bits 24-31 are reserved for temporary experiments. Just pick a bit that
// isn't getting used, or one not being used much, and notify the
// bitcoin-development mailing list. Remember that service bits are just
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "addpeeraddress", 1, "port"},
{ "addpeeraddress", 2, "tried"},
{ "stop", 0, "wait" },
{ "addnode", 2, "p2p_v2" },
};
// clang-format on

Expand Down
11 changes: 8 additions & 3 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ static RPCHelpMan addnode()
{
{"node", RPCArg::Type::STR, RPCArg::Optional::NO, "The node (see getpeerinfo for nodes)"},
{"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"},
{"p2p_v2", RPCArg::Type::BOOL, RPCArg::Default{false}, "Peer supports BIP324 v2 transport protocol"},
},
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 @@ -296,17 +297,21 @@ static RPCHelpMan addnode()
CConnman& connman = EnsureConnman(node);

std::string strNode = request.params[0].get_str();
bool use_p2p_v2 = !request.params[2].isNull() && request.params[2].get_bool();

if (strCommand == "onetry")
{
CAddress addr;
if (use_p2p_v2) {
addr.nServices = ServiceFlags(addr.nServices | NODE_P2P_V2);
}
connman.OpenNetworkConnection(addr, false, nullptr, strNode.c_str(), ConnectionType::MANUAL);
return NullUniValue;
}

if (strCommand == "add")
{
if (!connman.AddNode(strNode)) {
if (!connman.AddNode(strNode, use_p2p_v2)) {
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/connman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ FUZZ_TARGET_INIT(connman, 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 7dc92d4

Please sign in to comment.