Skip to content

Commit

Permalink
blanket-v2-only option
Browse files Browse the repository at this point in the history
  • Loading branch information
stratospher committed Aug 10, 2024
1 parent 8754d05 commit 2512a6e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,13 @@ bool AppInitParameterInteraction(const ArgsManager& args)
// Signal NODE_P2P_V2 if BIP324 v2 transport is enabled.
if (args.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT)) {
nLocalServices = ServiceFlags(nLocalServices | NODE_P2P_V2);
} else {
std::optional<std::string> cmd = args.GetArg("-v2transport");
if (cmd.has_value() && cmd.value() == "only") {
nLocalServices = ServiceFlags(nLocalServices | NODE_P2P_V2);
g_v2_only = true;
LogPrintf("### BLANKET V2 ONLY OPTION SET\n");
}
}

// Signal NODE_COMPACT_FILTERS if peerblockfilters and basic filters index are both enabled.
Expand Down
32 changes: 31 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,13 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
// detected, so use it whenever we signal NODE_P2P_V2.
const bool use_v2transport(nLocalServices & NODE_P2P_V2);

if (g_v2_only && !use_v2transport) {
//TODO: do we really need to log? do we log connection was accepted?
LogPrint(BCLog::NET, "### 1. NOT V2 PEER - INBOUND CONNECTION\n");
LogPrint(BCLog::NET, "connection from %s dropped (v1 disabled)\n", addr.ToStringAddrPort());
return;
}

CNode* pnode = new CNode(id,
std::move(sock),
addr,
Expand Down Expand Up @@ -1893,7 +1900,7 @@ void CConnman::DisconnectNodes()
// Add to reconnection list if appropriate. We don't reconnect right here, because
// the creation of a connection is a blocking operation (up to several seconds),
// and we don't want to hold up the socket handler thread for that long.
if (pnode->m_transport->ShouldReconnectV1()) {
if (!g_v2_only && pnode->m_transport->ShouldReconnectV1()) {
reconnections_to_add.push_back({
.addr_connect = pnode->addr,
.grant = std::move(pnode->grantOutbound),
Expand Down Expand Up @@ -2346,6 +2353,13 @@ void CConnman::ProcessAddrFetch()
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
// peer doesn't support it or immediately disconnects us for another reason.
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
if (g_v2_only && !use_v2transport) {
//TODO: better if this can be handled in OpenNetworkConnection() itself
//because this is at a loss of addrfetch connection
// TODO: check how connections are made only if a specific service flag is advertised
LogPrint(BCLog::NET, "### 2. NOT V2 PEER FOR ADDR_FETCH\n");
return;
}
CAddress addr;
CSemaphoreGrant grant(*semOutbound, /*fTry=*/true);
if (grant) {
Expand Down Expand Up @@ -2463,6 +2477,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
// peer doesn't support it or immediately disconnects us for another reason.
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
if (g_v2_only && !use_v2transport) {
//TODO: IMP: does this break out of the next for loop too?
LogPrint(BCLog::NET, "### 3. NOT V2 PEER - BEFORE MANUAL (INSIDE OPENCON)\n");
return;
}
for (int64_t nLoop = 0;; nLoop++)
{
for (const std::string& strAddr : connect)
Expand Down Expand Up @@ -2766,6 +2785,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
continue;
}

bool use_v2transport(addr.nServices & GetLocalServices() & NODE_P2P_V2);
if (g_v2_only && !use_v2transport) {
LogPrint(BCLog::NET, "### 4.0 NOT V2 PEER (IN OPENCON) - WE TRY AGAIN FOR V2\n");
continue;
}
addrConnect = addr;
break;
}
Expand Down Expand Up @@ -2883,6 +2907,7 @@ void CConnman::ThreadOpenAddedConnections()
}
tried = true;
CAddress addr(CService(), NODE_NONE);
//TODO
OpenNetworkConnection(addr, false, std::move(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;
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
Expand Down Expand Up @@ -2916,7 +2941,12 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
return;
}
} else if (FindNode(std::string(pszDest)))
return; //todo: how is it possible to hit this path

if (g_v2_only && !use_v2transport) {
LogPrint(BCLog::NET, "### 5. NOT V2 PEER (IN OPENNETWORKCONN())\n");
return;
}

CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, conn_type, use_v2transport);

Expand Down
1 change: 1 addition & 0 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ std::chrono::milliseconds g_socks5_recv_timeout = 20s;
CThreadInterrupt g_socks5_interrupt;

ReachableNets g_reachable_nets;
bool g_v2_only = false;

std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
{
Expand Down
1 change: 1 addition & 0 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class ReachableNets {
};

extern ReachableNets g_reachable_nets;
extern bool g_v2_only;

/**
* Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
Expand Down
1 change: 1 addition & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ static RPCHelpMan addnode()
const auto node_arg{self.Arg<std::string>("node")};
bool node_v2transport = connman.GetLocalServices() & NODE_P2P_V2;
bool use_v2transport = self.MaybeArg<bool>("v2transport").value_or(node_v2transport);
//TODO: disallow v1 if v2_only = true; (throw an error)

if (use_v2transport && !node_v2transport) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error: v2transport requested but not enabled (see -v2transport)");
Expand Down

0 comments on commit 2512a6e

Please sign in to comment.