mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 11:57:28 -03:00
net: don't run callbacks on nodes that haven't completed the version handshake
Since ForEach* are can be used to send messages to all nodes, the caller may end up sending a message before the version handshake is complete. To limit this, filter out these nodes. While we're at it, may as well filter out disconnected nodes as well. Delete unused methods rather than updating them.
This commit is contained in:
parent
2046617b5e
commit
12752af0cc
2 changed files with 25 additions and 57 deletions
|
@ -2630,6 +2630,11 @@ void CNode::AskFor(const CInv& inv)
|
|||
mapAskFor.insert(std::make_pair(nRequestTime, inv));
|
||||
}
|
||||
|
||||
bool CConnman::NodeFullyConnected(const CNode* pnode)
|
||||
{
|
||||
return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;
|
||||
}
|
||||
|
||||
void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
|
||||
{
|
||||
size_t nMessageSize = msg.data.size();
|
||||
|
@ -2680,7 +2685,7 @@ bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
|
|||
break;
|
||||
}
|
||||
}
|
||||
return found != nullptr && func(found);
|
||||
return found != nullptr && NodeFullyConnected(found) && func(found);
|
||||
}
|
||||
|
||||
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
|
||||
|
|
75
src/net.h
75
src/net.h
|
@ -161,76 +161,34 @@ public:
|
|||
|
||||
void PushMessage(CNode* pnode, CSerializedNetMsg&& msg);
|
||||
|
||||
template<typename Callable>
|
||||
bool ForEachNodeContinueIf(Callable&& func)
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (auto&& node : vNodes)
|
||||
if(!func(node))
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
template<typename Callable>
|
||||
bool ForEachNodeContinueIf(Callable&& func) const
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (const auto& node : vNodes)
|
||||
if(!func(node))
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
template<typename Callable, typename CallableAfter>
|
||||
bool ForEachNodeContinueIfThen(Callable&& pre, CallableAfter&& post)
|
||||
{
|
||||
bool ret = true;
|
||||
LOCK(cs_vNodes);
|
||||
for (auto&& node : vNodes)
|
||||
if(!pre(node)) {
|
||||
ret = false;
|
||||
break;
|
||||
}
|
||||
post();
|
||||
return ret;
|
||||
};
|
||||
|
||||
template<typename Callable, typename CallableAfter>
|
||||
bool ForEachNodeContinueIfThen(Callable&& pre, CallableAfter&& post) const
|
||||
{
|
||||
bool ret = true;
|
||||
LOCK(cs_vNodes);
|
||||
for (const auto& node : vNodes)
|
||||
if(!pre(node)) {
|
||||
ret = false;
|
||||
break;
|
||||
}
|
||||
post();
|
||||
return ret;
|
||||
};
|
||||
|
||||
template<typename Callable>
|
||||
void ForEachNode(Callable&& func)
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (auto&& node : vNodes)
|
||||
func(node);
|
||||
for (auto&& node : vNodes) {
|
||||
if (NodeFullyConnected(node))
|
||||
func(node);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Callable>
|
||||
void ForEachNode(Callable&& func) const
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (const auto& node : vNodes)
|
||||
func(node);
|
||||
for (auto&& node : vNodes) {
|
||||
if (NodeFullyConnected(node))
|
||||
func(node);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Callable, typename CallableAfter>
|
||||
void ForEachNodeThen(Callable&& pre, CallableAfter&& post)
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (auto&& node : vNodes)
|
||||
pre(node);
|
||||
for (auto&& node : vNodes) {
|
||||
if (NodeFullyConnected(node))
|
||||
pre(node);
|
||||
}
|
||||
post();
|
||||
};
|
||||
|
||||
|
@ -238,8 +196,10 @@ public:
|
|||
void ForEachNodeThen(Callable&& pre, CallableAfter&& post) const
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (const auto& node : vNodes)
|
||||
pre(node);
|
||||
for (auto&& node : vNodes) {
|
||||
if (NodeFullyConnected(node))
|
||||
pre(node);
|
||||
}
|
||||
post();
|
||||
};
|
||||
|
||||
|
@ -372,6 +332,9 @@ private:
|
|||
void RecordBytesRecv(uint64_t bytes);
|
||||
void RecordBytesSent(uint64_t bytes);
|
||||
|
||||
// Whether the node should be passed out in ForEach* callbacks
|
||||
static bool NodeFullyConnected(const CNode* pnode);
|
||||
|
||||
// Network usage totals
|
||||
CCriticalSection cs_totalBytesRecv;
|
||||
CCriticalSection cs_totalBytesSent;
|
||||
|
|
Loading…
Reference in a new issue