Skip to content

Commit dbcb574

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#20541: Move special CAddress-without-nTime logic to net_processing
75290ae Drop us=... message in net debug for sending version message (Pieter Wuille) 5d47860 refactor: move CAddress-without-nTime logic to net_processing (Pieter Wuille) Pull request description: Historically, the VERSION message contains an "addrMe" and an "addrYou". As these are sent before version negotiation is complete, the protocol version is INIT_PROTO_VERSION (209), and in that protocol, CAddress is serialized without nTime. This is in fact the only situation left where a CAddress is (de)serialized without nTime. As it's such a simple structure (CService for ip/port + uint64_t for nServices), just inline that structure in the few places where it occurs, and remove the logic for dealing with missing nTime from CAddress. ACKs for top commit: Zero-1729: crACK 75290ae jonatack: ACK 75290ae vasild: ACK 75290ae Tree-SHA512: ccd9f478e1766fb2ad845d512b090e6297b82100640545ca490d930785801da3b429d40400bc2eb2cbe9057d5d12362ab33f8d650a1ca9e9e85857aef36ec414
2 parents f6f7a12 + 75290ae commit dbcb574

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/net_processing.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,25 +1087,25 @@ void PeerManagerImpl::PushNodeVersion(CNode& pnode, int64_t nTime)
10871087
// Note that pnode->GetLocalServices() is a reflection of the local
10881088
// services we were offering when the CNode object was created for this
10891089
// peer.
1090-
ServiceFlags nLocalNodeServices = pnode.GetLocalServices();
1090+
uint64_t my_services{pnode.GetLocalServices()};
10911091
uint64_t nonce = pnode.GetLocalNonce();
10921092
const int nNodeStartingHeight{m_best_height};
10931093
NodeId nodeid = pnode.GetId();
10941094
CAddress addr = pnode.addr;
10951095

1096-
CAddress addrYou = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ?
1097-
addr :
1098-
CAddress(CService(), addr.nServices);
1099-
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
1096+
CService addr_you = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ? addr : CService();
1097+
uint64_t your_services{addr.nServices};
11001098

11011099
const bool tx_relay = !m_ignore_incoming_txs && pnode.m_tx_relay != nullptr;
1102-
m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
1100+
m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, my_services, nTime,
1101+
your_services, addr_you, // Together the pre-version-31402 serialization of CAddress "addrYou" (without nTime)
1102+
my_services, CService(), // Together the pre-version-31402 serialization of CAddress "addrMe" (without nTime)
11031103
nonce, strSubVersion, nNodeStartingHeight, tx_relay));
11041104

11051105
if (fLogIPs) {
1106-
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, txrelay=%d, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), tx_relay, nodeid);
1106+
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, them=%s, txrelay=%d, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addr_you.ToString(), tx_relay, nodeid);
11071107
} else {
1108-
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, txrelay=%d, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), tx_relay, nodeid);
1108+
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, txrelay=%d, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, tx_relay, nodeid);
11091109
}
11101110
}
11111111

@@ -2487,21 +2487,20 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
24872487
}
24882488

24892489
int64_t nTime;
2490-
CAddress addrMe;
2491-
CAddress addrFrom;
2490+
CService addrMe;
24922491
uint64_t nNonce = 1;
2493-
uint64_t nServiceInt;
24942492
ServiceFlags nServices;
24952493
int nVersion;
24962494
std::string cleanSubVer;
24972495
int starting_height = -1;
24982496
bool fRelay = true;
24992497

2500-
vRecv >> nVersion >> nServiceInt >> nTime >> addrMe;
2498+
vRecv >> nVersion >> Using<CustomUintFormatter<8>>(nServices) >> nTime;
25012499
if (nTime < 0) {
25022500
nTime = 0;
25032501
}
2504-
nServices = ServiceFlags(nServiceInt);
2502+
vRecv.ignore(8); // Ignore the addrMe service bits sent by the peer
2503+
vRecv >> addrMe;
25052504
if (!pfrom.IsInboundConn())
25062505
{
25072506
m_addrman.SetServices(pfrom.addr, nServices);
@@ -2520,8 +2519,14 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
25202519
return;
25212520
}
25222521

2523-
if (!vRecv.empty())
2524-
vRecv >> addrFrom >> nNonce;
2522+
if (!vRecv.empty()) {
2523+
// The version message includes information about the sending node which we don't use:
2524+
// - 8 bytes (service bits)
2525+
// - 16 bytes (ipv6 address)
2526+
// - 2 bytes (port)
2527+
vRecv.ignore(26);
2528+
vRecv >> nNonce;
2529+
}
25252530
if (!vRecv.empty()) {
25262531
std::string strSubVer;
25272532
vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);

src/protocol.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ class CAddress : public CService
396396
// ambiguous what that would mean. Make sure no code relying on that is introduced:
397397
assert(!(s.GetType() & SER_GETHASH));
398398
bool use_v2;
399-
bool store_time;
400399
if (s.GetType() & SER_DISK) {
401400
// In the disk serialization format, the encoding (v1 or v2) is determined by a flag version
402401
// that's part of the serialization itself. ADDRV2_FORMAT in the stream version only determines
@@ -413,24 +412,16 @@ class CAddress : public CService
413412
} else {
414413
throw std::ios_base::failure("Unsupported CAddress disk format version");
415414
}
416-
store_time = true;
417415
} else {
418416
// In the network serialization format, the encoding (v1 or v2) is determined directly by
419417
// the value of ADDRV2_FORMAT in the stream version, as no explicitly encoded version
420418
// exists in the stream.
421419
assert(s.GetType() & SER_NETWORK);
422420
use_v2 = s.GetVersion() & ADDRV2_FORMAT;
423-
// The only time we serialize a CAddress object without nTime is in
424-
// the initial VERSION messages which contain two CAddress records.
425-
// At that point, the serialization version is INIT_PROTO_VERSION.
426-
// After the version handshake, serialization version is >=
427-
// MIN_PEER_PROTO_VERSION and all ADDR messages are serialized with
428-
// nTime.
429-
store_time = s.GetVersion() != INIT_PROTO_VERSION;
430421
}
431422

432423
SER_READ(obj, obj.nTime = TIME_INIT);
433-
if (store_time) READWRITE(obj.nTime);
424+
READWRITE(obj.nTime);
434425
// nServices is serialized as CompactSize in V2; as uint64_t in V1.
435426
if (use_v2) {
436427
uint64_t services_tmp;

0 commit comments

Comments
 (0)