mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
p2p: Unify Send and Receive protocol versions
There is no change in behavior on the P2P network.
This commit is contained in:
parent
147d50d63e
commit
e9a6d8b13b
7 changed files with 39 additions and 73 deletions
26
src/net.cpp
26
src/net.cpp
|
@ -621,32 +621,6 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
|
|||
return true;
|
||||
}
|
||||
|
||||
void CNode::SetSendVersion(int nVersionIn)
|
||||
{
|
||||
// Send version may only be changed in the version message, and
|
||||
// only one version message is allowed per session. We can therefore
|
||||
// treat this value as const and even atomic as long as it's only used
|
||||
// once a version message has been successfully processed. Any attempt to
|
||||
// set this twice is an error.
|
||||
if (nSendVersion != 0) {
|
||||
error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
|
||||
} else {
|
||||
nSendVersion = nVersionIn;
|
||||
}
|
||||
}
|
||||
|
||||
int CNode::GetSendVersion() const
|
||||
{
|
||||
// The send version should always be explicitly set to
|
||||
// INIT_PROTO_VERSION rather than using this value until SetSendVersion
|
||||
// has been called.
|
||||
if (nSendVersion == 0) {
|
||||
error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
|
||||
return INIT_PROTO_VERSION;
|
||||
}
|
||||
return nSendVersion;
|
||||
}
|
||||
|
||||
int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
|
||||
{
|
||||
// copy data to temporary parsing buffer
|
||||
|
|
13
src/net.h
13
src/net.h
|
@ -827,7 +827,6 @@ public:
|
|||
|
||||
std::deque<CInv> vRecvGetData;
|
||||
uint64_t nRecvBytes GUARDED_BY(cs_vRecv){0};
|
||||
std::atomic<int> nRecvVersion{INIT_PROTO_VERSION};
|
||||
|
||||
std::atomic<int64_t> nLastSend{0};
|
||||
std::atomic<int64_t> nLastRecv{0};
|
||||
|
@ -1014,6 +1013,7 @@ private:
|
|||
const NodeId id;
|
||||
const uint64_t nLocalHostNonce;
|
||||
const ConnectionType m_conn_type;
|
||||
std::atomic<int> m_greatest_common_version{INIT_PROTO_VERSION};
|
||||
|
||||
//! Services offered to this peer.
|
||||
//!
|
||||
|
@ -1033,7 +1033,6 @@ private:
|
|||
const ServiceFlags nLocalServices;
|
||||
|
||||
const int nMyStartingHeight;
|
||||
int nSendVersion{0};
|
||||
NetPermissionFlags m_permissionFlags{ PF_NONE };
|
||||
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
|
||||
|
||||
|
@ -1065,16 +1064,14 @@ public:
|
|||
|
||||
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete);
|
||||
|
||||
void SetRecvVersion(int nVersionIn)
|
||||
void SetCommonVersion(int greatest_common_version)
|
||||
{
|
||||
nRecvVersion = nVersionIn;
|
||||
m_greatest_common_version = greatest_common_version;
|
||||
}
|
||||
int GetRecvVersion() const
|
||||
int GetCommonVersion() const
|
||||
{
|
||||
return nRecvVersion;
|
||||
return m_greatest_common_version;
|
||||
}
|
||||
void SetSendVersion(int nVersionIn);
|
||||
int GetSendVersion() const;
|
||||
|
||||
CService GetAddrLocal() const;
|
||||
//! May not be called more than once
|
||||
|
|
|
@ -669,12 +669,12 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman& connma
|
|||
// As per BIP152, we only get 3 of our peers to announce
|
||||
// blocks using compact encodings.
|
||||
connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [&connman, nCMPCTBLOCKVersion](CNode* pnodeStop){
|
||||
connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion));
|
||||
connman.PushMessage(pnodeStop, CNetMsgMaker(pnodeStop->GetCommonVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/false, nCMPCTBLOCKVersion));
|
||||
return true;
|
||||
});
|
||||
lNodesAnnouncingHeaderAndIDs.pop_front();
|
||||
}
|
||||
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetSendVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion));
|
||||
connman.PushMessage(pfrom, CNetMsgMaker(pfrom->GetCommonVersion()).Make(NetMsgType::SENDCMPCT, /*fAnnounceUsingCMPCTBLOCK=*/true, nCMPCTBLOCKVersion));
|
||||
lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
|
||||
return true;
|
||||
});
|
||||
|
@ -1585,7 +1585,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
|
|||
LogPrint(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom.GetId());
|
||||
}
|
||||
}
|
||||
const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||
// disconnect node in case we have reached the outbound limit for serving historical blocks
|
||||
if (send &&
|
||||
connman.OutboundTargetReached(true) &&
|
||||
|
@ -1728,7 +1728,7 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
|
|||
|
||||
std::deque<CInv>::iterator it = pfrom.vRecvGetData.begin();
|
||||
std::vector<CInv> vNotFound;
|
||||
const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||
|
||||
const std::chrono::seconds now = GetTime<std::chrono::seconds>();
|
||||
// Get last mempool request time
|
||||
|
@ -1834,14 +1834,14 @@ void PeerManager::SendBlockTransactions(CNode& pfrom, const CBlock& block, const
|
|||
resp.txn[i] = block.vtx[req.indexes[i]];
|
||||
}
|
||||
LOCK(cs_main);
|
||||
const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||
int nSendFlags = State(pfrom.GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::BLOCKTXN, resp));
|
||||
}
|
||||
|
||||
void PeerManager::ProcessHeadersMessage(CNode& pfrom, const std::vector<CBlockHeader>& headers, bool via_compact_block)
|
||||
{
|
||||
const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||
size_t nCount = headers.size();
|
||||
|
||||
if (nCount == 0) {
|
||||
|
@ -2211,7 +2211,7 @@ static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainPara
|
|||
}
|
||||
|
||||
for (const auto& filter : filters) {
|
||||
CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
|
||||
CSerializedNetMsg msg = CNetMsgMaker(peer.GetCommonVersion())
|
||||
.Make(NetMsgType::CFILTER, filter);
|
||||
connman.PushMessage(&peer, std::move(msg));
|
||||
}
|
||||
|
@ -2263,7 +2263,7 @@ static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainPar
|
|||
return;
|
||||
}
|
||||
|
||||
CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
|
||||
CSerializedNetMsg msg = CNetMsgMaker(peer.GetCommonVersion())
|
||||
.Make(NetMsgType::CFHEADERS,
|
||||
filter_type_ser,
|
||||
stop_index->GetBlockHash(),
|
||||
|
@ -2315,7 +2315,7 @@ static void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainPar
|
|||
}
|
||||
}
|
||||
|
||||
CSerializedNetMsg msg = CNetMsgMaker(peer.GetSendVersion())
|
||||
CSerializedNetMsg msg = CNetMsgMaker(peer.GetCommonVersion())
|
||||
.Make(NetMsgType::CFCHECKPT,
|
||||
filter_type_ser,
|
||||
stop_index->GetBlockHash(),
|
||||
|
@ -2406,10 +2406,10 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
|
|||
PushNodeVersion(pfrom, m_connman, GetAdjustedTime());
|
||||
|
||||
if (nVersion >= WTXID_RELAY_VERSION) {
|
||||
m_connman.PushMessage(&pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::WTXIDRELAY));
|
||||
m_connman.PushMessage(&pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::WTXIDRELAY));
|
||||
}
|
||||
|
||||
m_connman.PushMessage(&pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
|
||||
m_connman.PushMessage(&pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::VERACK));
|
||||
|
||||
pfrom.nServices = nServices;
|
||||
pfrom.SetAddrLocal(addrMe);
|
||||
|
@ -2431,7 +2431,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
|
|||
}
|
||||
|
||||
// Change version
|
||||
pfrom.SetSendVersion(nSendVersion);
|
||||
pfrom.SetCommonVersion(nSendVersion);
|
||||
pfrom.nVersion = nVersion;
|
||||
|
||||
if((nServices & NODE_WITNESS))
|
||||
|
@ -2520,11 +2520,11 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
|
|||
}
|
||||
|
||||
// At this point, the outgoing message serialization version can't change.
|
||||
const CNetMsgMaker msgMaker(pfrom.GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||
|
||||
if (msg_type == NetMsgType::VERACK)
|
||||
{
|
||||
pfrom.SetRecvVersion(std::min(pfrom.nVersion.load(), PROTOCOL_VERSION));
|
||||
pfrom.SetCommonVersion(std::min(pfrom.nVersion.load(), PROTOCOL_VERSION));
|
||||
|
||||
if (!pfrom.IsInboundConn()) {
|
||||
// Mark this node as currently connected, so we update its timestamp later.
|
||||
|
@ -3872,7 +3872,7 @@ bool PeerManager::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgP
|
|||
}
|
||||
CNetMessage& msg(msgs.front());
|
||||
|
||||
msg.SetVersion(pfrom->GetRecvVersion());
|
||||
msg.SetVersion(pfrom->GetCommonVersion());
|
||||
// Check network magic
|
||||
if (!msg.m_valid_netmagic) {
|
||||
LogPrint(BCLog::NET, "PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.m_command), pfrom->GetId());
|
||||
|
@ -3920,7 +3920,7 @@ void PeerManager::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
|
|||
AssertLockHeld(cs_main);
|
||||
|
||||
CNodeState &state = *State(pto.GetId());
|
||||
const CNetMsgMaker msgMaker(pto.GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pto.GetCommonVersion());
|
||||
|
||||
if (!state.m_chain_sync.m_protect && pto.IsOutboundOrBlockRelayConn() && state.fSyncStarted) {
|
||||
// This is an outbound peer subject to disconnection if they don't
|
||||
|
@ -4082,7 +4082,7 @@ bool PeerManager::SendMessages(CNode* pto)
|
|||
return true;
|
||||
|
||||
// If we get here, the outgoing message serialization version is set and can't change.
|
||||
const CNetMsgMaker msgMaker(pto->GetSendVersion());
|
||||
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
|
||||
|
||||
//
|
||||
// Message: ping
|
||||
|
|
|
@ -85,7 +85,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
|
|||
// Mock an outbound peer
|
||||
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
|
||||
CNode dummyNode1(id++, ServiceFlags(NODE_NETWORK | NODE_WITNESS), 0, INVALID_SOCKET, addr1, 0, 0, CAddress(), "", ConnectionType::OUTBOUND_FULL_RELAY);
|
||||
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
|
||||
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
|
||||
|
||||
peerLogic->InitializeNode(&dummyNode1);
|
||||
dummyNode1.nVersion = 1;
|
||||
|
@ -138,7 +138,7 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerManager &pee
|
|||
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
|
||||
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK | NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", ConnectionType::OUTBOUND_FULL_RELAY));
|
||||
CNode &node = *vNodes.back();
|
||||
node.SetSendVersion(PROTOCOL_VERSION);
|
||||
node.SetCommonVersion(PROTOCOL_VERSION);
|
||||
|
||||
peerLogic.InitializeNode(&node);
|
||||
node.nVersion = 1;
|
||||
|
@ -230,7 +230,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
|
|||
banman->ClearBanned();
|
||||
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
|
||||
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 0, 0, CAddress(), "", ConnectionType::INBOUND);
|
||||
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
|
||||
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
|
||||
peerLogic->InitializeNode(&dummyNode1);
|
||||
dummyNode1.nVersion = 1;
|
||||
dummyNode1.fSuccessfullyConnected = true;
|
||||
|
@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
|
|||
|
||||
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
|
||||
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, 1, 1, CAddress(), "", ConnectionType::INBOUND);
|
||||
dummyNode2.SetSendVersion(PROTOCOL_VERSION);
|
||||
dummyNode2.SetCommonVersion(PROTOCOL_VERSION);
|
||||
peerLogic->InitializeNode(&dummyNode2);
|
||||
dummyNode2.nVersion = 1;
|
||||
dummyNode2.fSuccessfullyConnected = true;
|
||||
|
@ -281,7 +281,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
|
|||
|
||||
CAddress addr(ip(0xa0b0c001), NODE_NONE);
|
||||
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, 4, 4, CAddress(), "", ConnectionType::INBOUND);
|
||||
dummyNode.SetSendVersion(PROTOCOL_VERSION);
|
||||
dummyNode.SetCommonVersion(PROTOCOL_VERSION);
|
||||
peerLogic->InitializeNode(&dummyNode);
|
||||
dummyNode.nVersion = 1;
|
||||
dummyNode.fSuccessfullyConnected = true;
|
||||
|
|
|
@ -48,7 +48,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
fuzzed_data_provider.ConsumeRandomLengthString(32),
|
||||
fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH})};
|
||||
while (fuzzed_data_provider.ConsumeBool()) {
|
||||
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 12)) {
|
||||
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 11)) {
|
||||
case 0: {
|
||||
node.CloseSocketDisconnect();
|
||||
break;
|
||||
|
@ -58,7 +58,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
break;
|
||||
}
|
||||
case 2: {
|
||||
node.SetSendVersion(fuzzed_data_provider.ConsumeIntegral<int>());
|
||||
node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
|
@ -71,21 +71,17 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
break;
|
||||
}
|
||||
case 4: {
|
||||
node.SetRecvVersion(fuzzed_data_provider.ConsumeIntegral<int>());
|
||||
break;
|
||||
}
|
||||
case 5: {
|
||||
const CNode* add_ref_node = node.AddRef();
|
||||
assert(add_ref_node == &node);
|
||||
break;
|
||||
}
|
||||
case 6: {
|
||||
case 5: {
|
||||
if (node.GetRefCount() > 0) {
|
||||
node.Release();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 7: {
|
||||
case 6: {
|
||||
if (node.m_addr_known == nullptr) {
|
||||
break;
|
||||
}
|
||||
|
@ -96,7 +92,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
node.AddAddressKnown(*addr_opt);
|
||||
break;
|
||||
}
|
||||
case 8: {
|
||||
case 7: {
|
||||
if (node.m_addr_known == nullptr) {
|
||||
break;
|
||||
}
|
||||
|
@ -108,7 +104,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
node.PushAddress(*addr_opt, fast_random_context);
|
||||
break;
|
||||
}
|
||||
case 9: {
|
||||
case 8: {
|
||||
const std::optional<CInv> inv_opt = ConsumeDeserializable<CInv>(fuzzed_data_provider);
|
||||
if (!inv_opt) {
|
||||
break;
|
||||
|
@ -116,11 +112,11 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
node.AddKnownTx(inv_opt->hash);
|
||||
break;
|
||||
}
|
||||
case 10: {
|
||||
case 9: {
|
||||
node.PushTxInventory(ConsumeUInt256(fuzzed_data_provider));
|
||||
break;
|
||||
}
|
||||
case 11: {
|
||||
case 10: {
|
||||
const std::optional<CService> service_opt = ConsumeDeserializable<CService>(fuzzed_data_provider);
|
||||
if (!service_opt) {
|
||||
break;
|
||||
|
@ -128,7 +124,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
node.SetAddrLocal(*service_opt);
|
||||
break;
|
||||
}
|
||||
case 12: {
|
||||
case 11: {
|
||||
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
|
||||
bool complete;
|
||||
node.ReceiveMsgBytes((const char*)b.data(), b.size(), complete);
|
||||
|
@ -143,10 +139,9 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
(void)node.GetLocalNonce();
|
||||
(void)node.GetLocalServices();
|
||||
(void)node.GetMyStartingHeight();
|
||||
(void)node.GetRecvVersion();
|
||||
const int ref_count = node.GetRefCount();
|
||||
assert(ref_count >= 0);
|
||||
(void)node.GetSendVersion();
|
||||
(void)node.GetCommonVersion();
|
||||
(void)node.RelayAddrsWithConn();
|
||||
|
||||
const NetPermissionFlags net_permission_flags = fuzzed_data_provider.ConsumeBool() ?
|
||||
|
|
|
@ -71,7 +71,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
CNode& p2p_node = *MakeUnique<CNode>(0, ServiceFlags(NODE_NETWORK | NODE_WITNESS | NODE_BLOOM), 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, ConnectionType::OUTBOUND_FULL_RELAY).release();
|
||||
p2p_node.fSuccessfullyConnected = true;
|
||||
p2p_node.nVersion = PROTOCOL_VERSION;
|
||||
p2p_node.SetSendVersion(PROTOCOL_VERSION);
|
||||
p2p_node.SetCommonVersion(PROTOCOL_VERSION);
|
||||
connman.AddTestNode(p2p_node);
|
||||
g_setup->m_node.peerman->InitializeNode(&p2p_node);
|
||||
try {
|
||||
|
|
|
@ -51,7 +51,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
|
|||
p2p_node.fSuccessfullyConnected = true;
|
||||
p2p_node.fPauseSend = false;
|
||||
p2p_node.nVersion = PROTOCOL_VERSION;
|
||||
p2p_node.SetSendVersion(PROTOCOL_VERSION);
|
||||
p2p_node.SetCommonVersion(PROTOCOL_VERSION);
|
||||
g_setup->m_node.peerman->InitializeNode(&p2p_node);
|
||||
|
||||
connman.AddTestNode(p2p_node);
|
||||
|
|
Loading…
Add table
Reference in a new issue