[net processing] Move RelayTransaction into PeerManager

We don't mark RelayTransaction as const. Even though it doesn't mutate
PeerManagerImpl state, it _is_ mutating the internal state of a CNode
object, by updating setInventoryTxToSend. In a subsequent commit, that
field will be moved to the Peer object, which is owned by
PeerMangerImpl.

This requires PeerManagerImpl::ReattemptInitialBroadcast() to no longer
be const.
This commit is contained in:
John Newbery committed 2021-03-04 10:22:42 +00:00
1 parent 92b7efcf54
commit a38a4e8f03
3 files changed
+10 -7

No files matched your search

+4 -3
View File
@@ -242,6 +242,7 @@ public:
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override; bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; } bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
void SendPings() override; void SendPings() override;
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) override;
void SetBestHeight(int height) override { m_best_height = height; }; void SetBestHeight(int height) override { m_best_height = height; };
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override; void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv, void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
@@ -255,7 +256,7 @@ private:
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main); void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */ /** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
void ReattemptInitialBroadcast(CScheduler& scheduler) const; void ReattemptInitialBroadcast(CScheduler& scheduler);
/** Get a shared pointer to the Peer object. /** Get a shared pointer to the Peer object.
* May return an empty shared_ptr if the Peer object can't be found. */ * May return an empty shared_ptr if the Peer object can't be found. */
@@ -954,7 +955,7 @@ void PeerManagerImpl::InitializeNode(CNode *pnode)
} }
} }
void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler) const void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
{ {
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs(); std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
@@ -1464,7 +1465,7 @@ void PeerManagerImpl::SendPings()
for(auto& it : m_peer_map) it.second->m_ping_queued = true; for(auto& it : m_peer_map) it.second->m_ping_queued = true;
} }
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman)
{ {
connman.ForEachNode([&txid, &wtxid](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { connman.ForEachNode([&txid, &wtxid](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
AssertLockHeld(::cs_main); AssertLockHeld(::cs_main);
+4 -3
View File
@@ -47,6 +47,10 @@ public:
/** Whether this node ignores txs received over p2p. */ /** Whether this node ignores txs received over p2p. */
virtual bool IgnoresIncomingTxs() = 0; virtual bool IgnoresIncomingTxs() = 0;
/** Relay transaction to all peers. */
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman)
EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
/** Send ping message to all peers */ /** Send ping message to all peers */
virtual void SendPings() = 0; virtual void SendPings() = 0;
@@ -71,7 +75,4 @@ public:
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0; const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0;
}; };
/** Relay transaction to every node */
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
#endif // BITCOIN_NET_PROCESSING_H #endif // BITCOIN_NET_PROCESSING_H
+2 -1
View File
@@ -32,6 +32,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
// node.connman is assigned both before chain clients and before RPC server is accepting calls, // node.connman is assigned both before chain clients and before RPC server is accepting calls,
// and reset after chain clients and RPC sever are stopped. node.connman should never be null here. // and reset after chain clients and RPC sever are stopped. node.connman should never be null here.
assert(node.connman); assert(node.connman);
assert(node.peerman);
assert(node.mempool); assert(node.mempool);
std::promise<void> promise; std::promise<void> promise;
uint256 hashTx = tx->GetHash(); uint256 hashTx = tx->GetHash();
@@ -100,7 +101,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
node.mempool->AddUnbroadcastTx(hashTx); node.mempool->AddUnbroadcastTx(hashTx);
LOCK(cs_main); LOCK(cs_main);
RelayTransaction(hashTx, tx->GetWitnessHash(), *node.connman); node.peerman->RelayTransaction(hashTx, tx->GetWitnessHash(), *node.connman);
} }
return TransactionError::OK; return TransactionError::OK;