mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-10 06:39:15 +02:00
Merge bitcoin/bitcoin#35148: refactor: Remove confusing DataStream::in_avail() alias
fa204100e1streams: Remove confusing DataStream::in_avail() (MarcoFalke)fa5ab0220emove-only: Extract ProcessPong() helper (MarcoFalke) Pull request description: The `in_avail` alias is confusing (see commit message), so remove it. Also extract the `ProcessPong()` helper, as the only place that called `in_avail`. ACKs for top commit: l0rinc: code review ACKfa204100e1sedited: ACKfa204100e1Tree-SHA512: b5a069abb471c42d39e91a3c55fc7f18fb9ccf8591408b3dd43210d0287785369b6a5d6b59b847d85247a494f208dbbe4a7e88b24caf9b3e6c8fea2157cd0ee0
This commit is contained in:
@@ -1075,6 +1075,8 @@ private:
|
||||
*/
|
||||
void ProcessGetCFCheckPt(CNode& node, Peer& peer, DataStream& vRecv);
|
||||
|
||||
void ProcessPong(CNode& pfrom, Peer& peer, NodeClock::time_point ping_end, DataStream& vRecv);
|
||||
|
||||
/** Checks if address relay is permitted with peer. If needed, initializes
|
||||
* the m_addr_known bloom filter and sets m_addr_relay_enabled to true.
|
||||
*
|
||||
@@ -4902,63 +4904,7 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
|
||||
}
|
||||
|
||||
if (msg_type == NetMsgType::PONG) {
|
||||
const auto ping_end{time_received};
|
||||
uint64_t nonce = 0;
|
||||
size_t nAvail = vRecv.in_avail();
|
||||
bool bPingFinished = false;
|
||||
std::string sProblem;
|
||||
|
||||
if (nAvail >= sizeof(nonce)) {
|
||||
vRecv >> nonce;
|
||||
|
||||
// Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
|
||||
if (peer.m_ping_nonce_sent != 0) {
|
||||
if (nonce == peer.m_ping_nonce_sent) {
|
||||
// Matching pong received, this ping is no longer outstanding
|
||||
bPingFinished = true;
|
||||
const auto ping_time = ping_end - peer.m_ping_start.load();
|
||||
if (ping_time.count() >= 0) {
|
||||
// Let connman know about this successful ping-pong
|
||||
pfrom.PongReceived(ping_time);
|
||||
if (pfrom.IsPrivateBroadcastConn()) {
|
||||
m_tx_for_private_broadcast.NodeConfirmedReception(pfrom.GetId());
|
||||
LogDebug(BCLog::PRIVBROADCAST, "Got a PONG (the transaction will probably reach the network), marking for disconnect, %s",
|
||||
pfrom.LogPeer());
|
||||
pfrom.fDisconnect = true;
|
||||
}
|
||||
} else {
|
||||
// This should never happen
|
||||
sProblem = "Timing mishap";
|
||||
}
|
||||
} else {
|
||||
// Nonce mismatches are normal when pings are overlapping
|
||||
sProblem = "Nonce mismatch";
|
||||
if (nonce == 0) {
|
||||
// This is most likely a bug in another implementation somewhere; cancel this ping
|
||||
bPingFinished = true;
|
||||
sProblem = "Nonce zero";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sProblem = "Unsolicited pong without ping";
|
||||
}
|
||||
} else {
|
||||
// This is most likely a bug in another implementation somewhere; cancel this ping
|
||||
bPingFinished = true;
|
||||
sProblem = "Short payload";
|
||||
}
|
||||
|
||||
if (!(sProblem.empty())) {
|
||||
LogDebug(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
|
||||
pfrom.GetId(),
|
||||
sProblem,
|
||||
peer.m_ping_nonce_sent,
|
||||
nonce,
|
||||
nAvail);
|
||||
}
|
||||
if (bPingFinished) {
|
||||
peer.m_ping_nonce_sent = 0;
|
||||
}
|
||||
ProcessPong(pfrom, peer, /*ping_end=*/time_received, vRecv);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5608,6 +5554,66 @@ bool PeerManagerImpl::RejectIncomingTxs(const CNode& peer) const
|
||||
return false;
|
||||
}
|
||||
|
||||
void PeerManagerImpl::ProcessPong(CNode& pfrom, Peer& peer, const NodeClock::time_point ping_end, DataStream& vRecv)
|
||||
{
|
||||
uint64_t nonce = 0;
|
||||
const size_t nAvail{vRecv.size()};
|
||||
bool bPingFinished = false;
|
||||
std::string sProblem;
|
||||
|
||||
if (nAvail >= sizeof(nonce)) {
|
||||
vRecv >> nonce;
|
||||
|
||||
// Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
|
||||
if (peer.m_ping_nonce_sent != 0) {
|
||||
if (nonce == peer.m_ping_nonce_sent) {
|
||||
// Matching pong received, this ping is no longer outstanding
|
||||
bPingFinished = true;
|
||||
const auto ping_time = ping_end - peer.m_ping_start.load();
|
||||
if (ping_time.count() >= 0) {
|
||||
// Let connman know about this successful ping-pong
|
||||
pfrom.PongReceived(ping_time);
|
||||
if (pfrom.IsPrivateBroadcastConn()) {
|
||||
m_tx_for_private_broadcast.NodeConfirmedReception(pfrom.GetId());
|
||||
LogDebug(BCLog::PRIVBROADCAST, "Got a PONG (the transaction will probably reach the network), marking for disconnect, %s",
|
||||
pfrom.LogPeer());
|
||||
pfrom.fDisconnect = true;
|
||||
}
|
||||
} else {
|
||||
// This should never happen
|
||||
sProblem = "Timing mishap";
|
||||
}
|
||||
} else {
|
||||
// Nonce mismatches are normal when pings are overlapping
|
||||
sProblem = "Nonce mismatch";
|
||||
if (nonce == 0) {
|
||||
// This is most likely a bug in another implementation somewhere; cancel this ping
|
||||
bPingFinished = true;
|
||||
sProblem = "Nonce zero";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sProblem = "Unsolicited pong without ping";
|
||||
}
|
||||
} else {
|
||||
// This is most likely a bug in another implementation somewhere; cancel this ping
|
||||
bPingFinished = true;
|
||||
sProblem = "Short payload";
|
||||
}
|
||||
|
||||
if (!(sProblem.empty())) {
|
||||
LogDebug(BCLog::NET, "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
|
||||
pfrom.GetId(),
|
||||
sProblem,
|
||||
peer.m_ping_nonce_sent,
|
||||
nonce,
|
||||
nAvail);
|
||||
}
|
||||
if (bPingFinished) {
|
||||
peer.m_ping_nonce_sent = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool PeerManagerImpl::SetupAddressRelay(const CNode& node, Peer& peer)
|
||||
{
|
||||
// We don't participate in addr relay with outbound block-relay-only
|
||||
|
||||
@@ -188,7 +188,6 @@ public:
|
||||
return std::string{UCharCast(data()), UCharCast(data() + size())};
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Vector subset
|
||||
//
|
||||
@@ -209,8 +208,6 @@ public:
|
||||
//
|
||||
// Stream subset
|
||||
//
|
||||
int in_avail() const { return size(); }
|
||||
|
||||
void read(std::span<value_type> dst)
|
||||
{
|
||||
if (dst.size() == 0) return;
|
||||
|
||||
Reference in New Issue
Block a user