Skip to content

Commit 2512a6e

Browse files
committed
blanket-v2-only option
1 parent 8754d05 commit 2512a6e

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

src/init.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,13 @@ bool AppInitParameterInteraction(const ArgsManager& args)
938938
// Signal NODE_P2P_V2 if BIP324 v2 transport is enabled.
939939
if (args.GetBoolArg("-v2transport", DEFAULT_V2_TRANSPORT)) {
940940
nLocalServices = ServiceFlags(nLocalServices | NODE_P2P_V2);
941+
} else {
942+
std::optional<std::string> cmd = args.GetArg("-v2transport");
943+
if (cmd.has_value() && cmd.value() == "only") {
944+
nLocalServices = ServiceFlags(nLocalServices | NODE_P2P_V2);
945+
g_v2_only = true;
946+
LogPrintf("### BLANKET V2 ONLY OPTION SET\n");
947+
}
941948
}
942949

943950
// Signal NODE_COMPACT_FILTERS if peerblockfilters and basic filters index are both enabled.

src/net.cpp

+31-1
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,13 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
17931793
// detected, so use it whenever we signal NODE_P2P_V2.
17941794
const bool use_v2transport(nLocalServices & NODE_P2P_V2);
17951795

1796+
if (g_v2_only && !use_v2transport) {
1797+
//TODO: do we really need to log? do we log connection was accepted?
1798+
LogPrint(BCLog::NET, "### 1. NOT V2 PEER - INBOUND CONNECTION\n");
1799+
LogPrint(BCLog::NET, "connection from %s dropped (v1 disabled)\n", addr.ToStringAddrPort());
1800+
return;
1801+
}
1802+
17961803
CNode* pnode = new CNode(id,
17971804
std::move(sock),
17981805
addr,
@@ -1893,7 +1900,7 @@ void CConnman::DisconnectNodes()
18931900
// Add to reconnection list if appropriate. We don't reconnect right here, because
18941901
// the creation of a connection is a blocking operation (up to several seconds),
18951902
// and we don't want to hold up the socket handler thread for that long.
1896-
if (pnode->m_transport->ShouldReconnectV1()) {
1903+
if (!g_v2_only && pnode->m_transport->ShouldReconnectV1()) {
18971904
reconnections_to_add.push_back({
18981905
.addr_connect = pnode->addr,
18991906
.grant = std::move(pnode->grantOutbound),
@@ -2346,6 +2353,13 @@ void CConnman::ProcessAddrFetch()
23462353
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
23472354
// peer doesn't support it or immediately disconnects us for another reason.
23482355
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
2356+
if (g_v2_only && !use_v2transport) {
2357+
//TODO: better if this can be handled in OpenNetworkConnection() itself
2358+
//because this is at a loss of addrfetch connection
2359+
// TODO: check how connections are made only if a specific service flag is advertised
2360+
LogPrint(BCLog::NET, "### 2. NOT V2 PEER FOR ADDR_FETCH\n");
2361+
return;
2362+
}
23492363
CAddress addr;
23502364
CSemaphoreGrant grant(*semOutbound, /*fTry=*/true);
23512365
if (grant) {
@@ -2463,6 +2477,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24632477
// Attempt v2 connection if we support v2 - we'll reconnect with v1 if our
24642478
// peer doesn't support it or immediately disconnects us for another reason.
24652479
const bool use_v2transport(GetLocalServices() & NODE_P2P_V2);
2480+
if (g_v2_only && !use_v2transport) {
2481+
//TODO: IMP: does this break out of the next for loop too?
2482+
LogPrint(BCLog::NET, "### 3. NOT V2 PEER - BEFORE MANUAL (INSIDE OPENCON)\n");
2483+
return;
2484+
}
24662485
for (int64_t nLoop = 0;; nLoop++)
24672486
{
24682487
for (const std::string& strAddr : connect)
@@ -2766,6 +2785,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
27662785
continue;
27672786
}
27682787

2788+
bool use_v2transport(addr.nServices & GetLocalServices() & NODE_P2P_V2);
2789+
if (g_v2_only && !use_v2transport) {
2790+
LogPrint(BCLog::NET, "### 4.0 NOT V2 PEER (IN OPENCON) - WE TRY AGAIN FOR V2\n");
2791+
continue;
2792+
}
27692793
addrConnect = addr;
27702794
break;
27712795
}
@@ -2883,6 +2907,7 @@ void CConnman::ThreadOpenAddedConnections()
28832907
}
28842908
tried = true;
28852909
CAddress addr(CService(), NODE_NONE);
2910+
//TODO
28862911
OpenNetworkConnection(addr, false, std::move(grant), info.m_params.m_added_node.c_str(), ConnectionType::MANUAL, info.m_params.m_use_v2transport);
28872912
if (!interruptNet.sleep_for(std::chrono::milliseconds(500))) return;
28882913
grant = CSemaphoreGrant(*semAddnode, /*fTry=*/true);
@@ -2916,7 +2941,12 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
29162941
return;
29172942
}
29182943
} else if (FindNode(std::string(pszDest)))
2944+
return; //todo: how is it possible to hit this path
2945+
2946+
if (g_v2_only && !use_v2transport) {
2947+
LogPrint(BCLog::NET, "### 5. NOT V2 PEER (IN OPENNETWORKCONN())\n");
29192948
return;
2949+
}
29202950

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

src/netbase.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ std::chrono::milliseconds g_socks5_recv_timeout = 20s;
4141
CThreadInterrupt g_socks5_interrupt;
4242

4343
ReachableNets g_reachable_nets;
44+
bool g_v2_only = false;
4445

4546
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
4647
{

src/netbase.h

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class ReachableNets {
149149
};
150150

151151
extern ReachableNets g_reachable_nets;
152+
extern bool g_v2_only;
152153

153154
/**
154155
* Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.

src/rpc/net.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ static RPCHelpMan addnode()
335335
const auto node_arg{self.Arg<std::string>("node")};
336336
bool node_v2transport = connman.GetLocalServices() & NODE_P2P_V2;
337337
bool use_v2transport = self.MaybeArg<bool>("v2transport").value_or(node_v2transport);
338+
//TODO: disallow v1 if v2_only = true; (throw an error)
338339

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

0 commit comments

Comments
 (0)