net: Add AdvertisedVersion() for protocol version advertised to a peer

This commit is contained in:
Anthony Towns
2026-05-06 23:44:28 +10:00
parent 94ed45427c
commit 3210fc477a
4 changed files with 13 additions and 6 deletions

View File

@@ -831,6 +831,13 @@ public:
return m_conn_type == ConnectionType::PRIVATE_BROADCAST;
}
/** Protocol version advertised in our VERSION message.
* Private broadcast connections use a fixed version to maximise anonymity. */
int AdvertisedVersion() const
{
return IsPrivateBroadcastConn() ? WTXID_RELAY_VERSION : PROTOCOL_VERSION;
}
bool IsInboundConn() const {
return m_conn_type == ConnectionType::INBOUND;
}

View File

@@ -1575,7 +1575,7 @@ void PeerManagerImpl::PushNodeVersion(CNode& pnode, const Peer& peer)
MakeAndPushMessage(
pnode,
NetMsgType::VERSION,
PROTOCOL_VERSION,
pnode.AdvertisedVersion(),
my_services,
my_time,
// your_services + CNetAddr::V1(your_addr) is the pre-version-31402 serialization of your_addr (without nTime)
@@ -1589,7 +1589,7 @@ void PeerManagerImpl::PushNodeVersion(CNode& pnode, const Peer& peer)
LogDebug(
BCLog::NET, "send version message: version=%d, blocks=%d%s, txrelay=%d, peer=%d\n",
PROTOCOL_VERSION, my_height,
pnode.AdvertisedVersion(), my_height,
fLogIPs ? strprintf(", them=%s", your_addr.ToStringAddrPort()) : "",
my_tx_relay, pnode.GetId());
}
@@ -3663,7 +3663,7 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
}
// Change version
const int greatest_common_version = std::min(nVersion, PROTOCOL_VERSION);
const int greatest_common_version = std::min(nVersion, pfrom.AdvertisedVersion());
pfrom.SetCommonVersion(greatest_common_version);
pfrom.nVersion = nVersion;

View File

@@ -56,7 +56,7 @@ void ConnmanTestMsg::Handshake(CNode& node,
FlushSendBuffer(node); // Drop the verack message added by SendMessages.
if (node.fDisconnect) return;
assert(node.nVersion == version);
assert(node.GetCommonVersion() == std::min(version, PROTOCOL_VERSION));
assert(node.GetCommonVersion() == std::min(version, node.AdvertisedVersion()));
CNodeStateStats statestats;
assert(peerman.GetNodeStateStats(node.GetId(), statestats));
assert(statestats.m_relay_txs == (relay_txs && !node.IsBlockOnlyConn()));

View File

@@ -14,7 +14,6 @@ from test_framework.p2p import (
P2PDataStore,
P2PInterface,
P2P_SERVICES,
P2P_VERSION,
start_p2p_listener,
)
from test_framework.messages import (
@@ -46,6 +45,7 @@ from test_framework.wallet import (
MiniWallet,
)
P2P_PRIVATE_VERSION = 70016
NUM_PRIVATE_BROADCAST_PER_TX = 3
@@ -195,7 +195,7 @@ class P2PPrivateBroadcast(BitcoinTestFramework):
})
dummy_address = CAddress()
dummy_address.nServices = 0
assert_equal(peer.last_message["version"].nVersion, P2P_VERSION)
assert_equal(peer.last_message["version"].nVersion, P2P_PRIVATE_VERSION)
assert_equal(peer.last_message["version"].nServices, 0)
assert_equal(peer.last_message["version"].nTime, 0)
assert_equal(peer.last_message["version"].addrTo, dummy_address)