mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-13 19:37:04 +01:00
p2p: Cache inbound reconciling peers count
It helps to avoid recomputing every time we consider a transaction for fanout/reconciliation. Co-authored-by: Sergi Delgado Segura <sergi.delgado.s@gmail.com>
This commit is contained in:
parent
8384cadb96
commit
ea0f6d9d0d
1 changed files with 18 additions and 0 deletions
|
@ -79,6 +79,12 @@ private:
|
||||||
*/
|
*/
|
||||||
std::unordered_map<NodeId, std::variant<uint64_t, TxReconciliationState>> m_states GUARDED_BY(m_txreconciliation_mutex);
|
std::unordered_map<NodeId, std::variant<uint64_t, TxReconciliationState>> m_states GUARDED_BY(m_txreconciliation_mutex);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Keeps track of how many of the registered peers are inbound. Updated on registering or
|
||||||
|
* forgetting peers.
|
||||||
|
*/
|
||||||
|
size_t m_inbounds_count GUARDED_BY(m_txreconciliation_mutex){0};
|
||||||
|
|
||||||
TxReconciliationState* GetRegisteredPeerState(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(m_txreconciliation_mutex)
|
TxReconciliationState* GetRegisteredPeerState(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(m_txreconciliation_mutex)
|
||||||
{
|
{
|
||||||
AssertLockHeld(m_txreconciliation_mutex);
|
AssertLockHeld(m_txreconciliation_mutex);
|
||||||
|
@ -139,6 +145,9 @@ public:
|
||||||
bool emplaced = m_states.emplace(peer_id, std::move(new_state)).second;
|
bool emplaced = m_states.emplace(peer_id, std::move(new_state)).second;
|
||||||
Assume(emplaced);
|
Assume(emplaced);
|
||||||
|
|
||||||
|
if (is_peer_inbound && m_inbounds_count < std::numeric_limits<size_t>::max()) {
|
||||||
|
++m_inbounds_count;
|
||||||
|
}
|
||||||
return ReconciliationRegisterResult::SUCCESS;
|
return ReconciliationRegisterResult::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,6 +203,15 @@ public:
|
||||||
{
|
{
|
||||||
AssertLockNotHeld(m_txreconciliation_mutex);
|
AssertLockNotHeld(m_txreconciliation_mutex);
|
||||||
LOCK(m_txreconciliation_mutex);
|
LOCK(m_txreconciliation_mutex);
|
||||||
|
const auto peer = m_states.find(peer_id);
|
||||||
|
if (peer == m_states.end()) return;
|
||||||
|
|
||||||
|
const auto registered = std::get_if<TxReconciliationState>(&peer->second);
|
||||||
|
if (registered && !registered->m_we_initiate) {
|
||||||
|
Assert(m_inbounds_count > 0);
|
||||||
|
--m_inbounds_count;
|
||||||
|
}
|
||||||
|
|
||||||
if (m_states.erase(peer_id)) {
|
if (m_states.erase(peer_id)) {
|
||||||
LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Forget txreconciliation state of peer=%d\n", peer_id);
|
LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Forget txreconciliation state of peer=%d\n", peer_id);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue