From d5404c2dc0ec86480d817de1da2d210a3d092e2d Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 30 May 2024 12:03:58 -0400 Subject: [PATCH] p2p: Add helper to compute reconciliation tx short ids and a cache of short ids to wtxids --- src/node/txreconciliation.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/node/txreconciliation.cpp b/src/node/txreconciliation.cpp index 1f99adc04b6..921687f253c 100644 --- a/src/node/txreconciliation.cpp +++ b/src/node/txreconciliation.cpp @@ -64,7 +64,28 @@ public: */ std::unordered_set m_local_set; + /** + * Reconciliation sketches are computed over short transaction IDs. + * This is a cache of these IDs enabling faster lookups of full wtxids, + * useful when peer will ask for missing transactions by short IDs + * at the end of a reconciliation round. + * We also use this to keep track of short ID collisions. In case of a + * collision, both transactions should be fanout. + */ + std::map m_short_id_mapping; + TxReconciliationState(bool we_initiate, uint64_t k0, uint64_t k1) : m_we_initiate(we_initiate), m_k0(k0), m_k1(k1) {} + + /** + * Reconciliation sketches are computed over short transaction IDs. + * Short IDs are salted with a link-specific constant value. + */ + uint32_t ComputeShortID(const uint256 wtxid) const + { + const uint64_t s = SipHashUint256(m_k0, m_k1, wtxid); + const uint32_t short_txid = 1 + (s & 0xFFFFFFFF); + return short_txid; + } }; } // namespace