11#include <unordered_map>
18const std::string RECON_STATIC_SALT =
"Tx Relay Salting";
26uint256 ComputeSalt(uint64_t salt1, uint64_t salt2)
29 return (
HashWriter(RECON_SALT_HASHER) << std::min(salt1, salt2) << std::max(salt1, salt2)).GetSHA256();
35class TxReconciliationState
57 TxReconciliationState(
bool we_initiate, uint64_t k0, uint64_t k1) : m_we_initiate(we_initiate), m_k0(k0), m_k1(k1) {}
92 Assume(m_states.emplace(peer_id, local_salt).second);
101 auto recon_state = m_states.find(peer_id);
105 if (std::holds_alternative<TxReconciliationState>(recon_state->second)) {
109 uint64_t local_salt = *std::get_if<uint64_t>(&recon_state->second);
116 const uint32_t recon_version{std::min(peer_recon_version,
m_recon_version)};
121 peer_id, is_peer_inbound);
123 const uint256 full_salt{ComputeSalt(local_salt, remote_salt)};
124 recon_state->second = TxReconciliationState(!is_peer_inbound, full_salt.GetUint64(0), full_salt.GetUint64(1));
132 if (m_states.erase(peer_id)) {
141 auto recon_state = m_states.find(peer_id);
142 return (recon_state != m_states.end() &&
143 std::holds_alternative<TxReconciliationState>(recon_state->second));
153 return m_impl->PreRegisterPeer(peer_id);
157 uint32_t peer_recon_version, uint64_t remote_salt)
159 return m_impl->RegisterPeer(peer_id, is_peer_inbound, peer_recon_version, remote_salt);
164 m_impl->ForgetPeer(peer_id);
169 return m_impl->IsPeerRegistered(peer_id);
#define Assume(val)
Assume is the identity function.
uint64_t rand64() noexcept
Generate a random 64-bit integer.
A writer stream (for serialization) that computes a 256-bit hash.
Actual implementation for TxReconciliationTracker's data structure.
uint64_t PreRegisterPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
std::unordered_map< NodeId, std::variant< uint64_t, TxReconciliationState > > m_states GUARDED_BY(m_txreconciliation_mutex)
Keeps track of txreconciliation states of eligible peers.
Mutex m_txreconciliation_mutex
void ForgetPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
bool IsPeerRegistered(NodeId peer_id) const EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
Impl(uint32_t recon_version)
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, uint32_t peer_recon_version, uint64_t remote_salt) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex)
Transaction reconciliation is a way for nodes to efficiently announce transactions.
~TxReconciliationTracker()
bool IsPeerRegistered(NodeId peer_id) const
Check if a peer is registered to reconcile transactions with us.
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, uint32_t peer_recon_version, uint64_t remote_salt)
Step 0.
const std::unique_ptr< Impl > m_impl
TxReconciliationTracker(uint32_t recon_version)
uint64_t PreRegisterPeer(NodeId peer_id)
Step 0.
void ForgetPeer(NodeId peer_id)
Attempts to forget txreconciliation-related state of the peer (if we previously stored any).
HashWriter TaggedHash(const std::string &tag)
Return a HashWriter primed for tagged hashes (as specified in BIP 340).
#define LogPrintLevel(category, level,...)
#define AssertLockNotHeld(cs)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
ReconciliationRegisterResult