|
| 1 | +// Copyright (c) 2022 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <node/txreconciliation.h> |
| 6 | + |
| 7 | +#include <util/check.h> |
| 8 | +#include <util/system.h> |
| 9 | + |
| 10 | +#include <unordered_map> |
| 11 | +#include <variant> |
| 12 | + |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +/** Static salt component used to compute short txids for sketch construction, see BIP-330. */ |
| 17 | +const std::string RECON_STATIC_SALT = "Tx Relay Salting"; |
| 18 | +const HashWriter RECON_SALT_HASHER = TaggedHash(RECON_STATIC_SALT); |
| 19 | + |
| 20 | +/** |
| 21 | + * Salt (specified by BIP-330) constructed from contributions from both peers. It is used |
| 22 | + * to compute transaction short IDs, which are then used to construct a sketch representing a set |
| 23 | + * of transactions we want to announce to the peer. |
| 24 | + */ |
| 25 | +uint256 ComputeSalt(uint64_t salt1, uint64_t salt2) |
| 26 | +{ |
| 27 | + // According to BIP-330, salts should be combined in ascending order. |
| 28 | + return (HashWriter(RECON_SALT_HASHER) << std::min(salt1, salt2) << std::max(salt1, salt2)).GetSHA256(); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Keeps track of txreconciliation-related per-peer state. |
| 33 | + */ |
| 34 | +class TxReconciliationState |
| 35 | +{ |
| 36 | +public: |
| 37 | + /** |
| 38 | + * TODO: This field is public to ignore -Wunused-private-field. Make private once used in |
| 39 | + * the following commits. |
| 40 | + * |
| 41 | + * Reconciliation protocol assumes using one role consistently: either a reconciliation |
| 42 | + * initiator (requesting sketches), or responder (sending sketches). This defines our role. |
| 43 | + * |
| 44 | + */ |
| 45 | + bool m_we_initiate; |
| 46 | + |
| 47 | + /** |
| 48 | + * TODO: These fields are public to ignore -Wunused-private-field. Make private once used in |
| 49 | + * the following commits. |
| 50 | + * |
| 51 | + * These values are used to salt short IDs, which is necessary for transaction reconciliations. |
| 52 | + */ |
| 53 | + uint64_t m_k0, m_k1; |
| 54 | + |
| 55 | + TxReconciliationState(bool we_initiate, uint64_t k0, uint64_t k1) : m_we_initiate(we_initiate), m_k0(k0), m_k1(k1) {} |
| 56 | +}; |
| 57 | + |
| 58 | +} // namespace |
| 59 | + |
| 60 | +/** Actual implementation for TxReconciliationTracker's data structure. */ |
| 61 | +class TxReconciliationTracker::Impl |
| 62 | +{ |
| 63 | +private: |
| 64 | + mutable Mutex m_txreconciliation_mutex; |
| 65 | + |
| 66 | + // Local protocol version |
| 67 | + uint32_t m_recon_version; |
| 68 | + |
| 69 | + /** |
| 70 | + * Keeps track of txreconciliation states of eligible peers. |
| 71 | + * For pre-registered peers, the locally generated salt is stored. |
| 72 | + * For registered peers, the locally generated salt is forgotten, and the state (including |
| 73 | + * "full" salt) is stored instead. |
| 74 | + */ |
| 75 | + std::unordered_map<NodeId, std::variant<uint64_t, TxReconciliationState>> m_states GUARDED_BY(m_txreconciliation_mutex); |
| 76 | + |
| 77 | +public: |
| 78 | + explicit Impl(uint32_t recon_version) : m_recon_version(recon_version) {} |
| 79 | + |
| 80 | + uint64_t PreRegisterPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex) |
| 81 | + { |
| 82 | + AssertLockNotHeld(m_txreconciliation_mutex); |
| 83 | + LOCK(m_txreconciliation_mutex); |
| 84 | + // We do not support txreconciliation salt/version updates. |
| 85 | + assert(m_states.find(peer_id) == m_states.end()); |
| 86 | + |
| 87 | + LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Pre-register peer=%d\n", peer_id); |
| 88 | + const uint64_t local_salt{GetRand(UINT64_MAX)}; |
| 89 | + |
| 90 | + // We do this exactly once per peer (which are unique by NodeId, see GetNewNodeId) so it's |
| 91 | + // safe to assume we don't have this record yet. |
| 92 | + Assert(m_states.emplace(peer_id, local_salt).second); |
| 93 | + return local_salt; |
| 94 | + } |
| 95 | + |
| 96 | + ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, bool is_peer_recon_initiator, |
| 97 | + bool is_peer_recon_responder, uint32_t peer_recon_version, |
| 98 | + uint64_t remote_salt) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex) |
| 99 | + { |
| 100 | + AssertLockNotHeld(m_txreconciliation_mutex); |
| 101 | + LOCK(m_txreconciliation_mutex); |
| 102 | + auto recon_state = m_states.find(peer_id); |
| 103 | + |
| 104 | + // A peer should be in the pre-registered state to proceed here. |
| 105 | + if (recon_state == m_states.end()) return NOT_FOUND; |
| 106 | + uint64_t* local_salt = std::get_if<uint64_t>(&recon_state->second); |
| 107 | + // A peer is already registered. This should be checked by the caller. |
| 108 | + Assume(local_salt); |
| 109 | + |
| 110 | + // If the peer supports the version which is lower than ours, we downgrade to the version |
| 111 | + // it supports. For now, this only guarantees that nodes with future reconciliation |
| 112 | + // versions have the choice of reconciling with this current version. However, they also |
| 113 | + // have the choice to refuse supporting reconciliations if the common version is not |
| 114 | + // satisfactory (e.g. too low). |
| 115 | + const uint32_t recon_version{std::min(peer_recon_version, m_recon_version)}; |
| 116 | + // v1 is the lowest version, so suggesting something below must be a protocol violation. |
| 117 | + if (recon_version < 1) return PROTOCOL_VIOLATION; |
| 118 | + |
| 119 | + // Must match SENDTXRCNCL logic. |
| 120 | + const bool they_initiate = is_peer_recon_initiator && is_peer_inbound; |
| 121 | + const bool we_initiate = !is_peer_inbound && is_peer_recon_responder; |
| 122 | + |
| 123 | + // If we ever announce support for both requesting and responding, this will need |
| 124 | + // tie-breaking. For now, this is mutually exclusive because both are based on the |
| 125 | + // inbound flag. |
| 126 | + assert(!(they_initiate && we_initiate)); |
| 127 | + |
| 128 | + // The peer set both flags to false, we treat it as a protocol violation. |
| 129 | + if (!(they_initiate || we_initiate)) return PROTOCOL_VIOLATION; |
| 130 | + |
| 131 | + LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Register peer=%d with the following params: " /* Continued */ |
| 132 | + "we_initiate=%i, they_initiate=%i.\n", |
| 133 | + peer_id, we_initiate, they_initiate); |
| 134 | + |
| 135 | + const uint256 full_salt{ComputeSalt(*local_salt, remote_salt)}; |
| 136 | + recon_state->second = TxReconciliationState(we_initiate, full_salt.GetUint64(0), full_salt.GetUint64(1)); |
| 137 | + return SUCCESS; |
| 138 | + } |
| 139 | + |
| 140 | + void ForgetPeer(NodeId peer_id) EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex) |
| 141 | + { |
| 142 | + AssertLockNotHeld(m_txreconciliation_mutex); |
| 143 | + LOCK(m_txreconciliation_mutex); |
| 144 | + if (m_states.erase(peer_id)) { |
| 145 | + LogPrintLevel(BCLog::TXRECONCILIATION, BCLog::Level::Debug, "Forget txreconciliation state of peer=%d\n", peer_id); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + bool IsPeerRegistered(NodeId peer_id) const EXCLUSIVE_LOCKS_REQUIRED(!m_txreconciliation_mutex) |
| 150 | + { |
| 151 | + AssertLockNotHeld(m_txreconciliation_mutex); |
| 152 | + LOCK(m_txreconciliation_mutex); |
| 153 | + auto recon_state = m_states.find(peer_id); |
| 154 | + return (recon_state != m_states.end() && |
| 155 | + std::holds_alternative<TxReconciliationState>(recon_state->second)); |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +TxReconciliationTracker::TxReconciliationTracker(uint32_t recon_version) : m_impl{std::make_unique<TxReconciliationTracker::Impl>(recon_version)} {} |
| 160 | + |
| 161 | +TxReconciliationTracker::~TxReconciliationTracker() = default; |
| 162 | + |
| 163 | +uint64_t TxReconciliationTracker::PreRegisterPeer(NodeId peer_id) |
| 164 | +{ |
| 165 | + return m_impl->PreRegisterPeer(peer_id); |
| 166 | +} |
| 167 | + |
| 168 | +ReconciliationRegisterResult TxReconciliationTracker::RegisterPeer(NodeId peer_id, bool is_peer_inbound, |
| 169 | + bool is_peer_recon_initiator, bool is_peer_recon_responder, |
| 170 | + uint32_t peer_recon_version, uint64_t remote_salt) |
| 171 | +{ |
| 172 | + return m_impl->RegisterPeer(peer_id, is_peer_inbound, is_peer_recon_initiator, is_peer_recon_responder, |
| 173 | + peer_recon_version, remote_salt); |
| 174 | +} |
| 175 | + |
| 176 | +void TxReconciliationTracker::ForgetPeer(NodeId peer_id) |
| 177 | +{ |
| 178 | + m_impl->ForgetPeer(peer_id); |
| 179 | +} |
| 180 | + |
| 181 | +bool TxReconciliationTracker::IsPeerRegistered(NodeId peer_id) const |
| 182 | +{ |
| 183 | + return m_impl->IsPeerRegistered(peer_id); |
| 184 | +} |
0 commit comments