From 9d5313df7eedad8562c822f5477747e924929fd3 Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Sun, 31 Jan 2021 00:55:54 +1000 Subject: move-only: Add txorphanage module This module captures orphan tracking code for tx relay. Can be reviewed with --color-moved=dimmed-zebra --- src/txorphanage.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/txorphanage.h (limited to 'src/txorphanage.h') diff --git a/src/txorphanage.h b/src/txorphanage.h new file mode 100644 index 0000000000..d97b4b1f43 --- /dev/null +++ b/src/txorphanage.h @@ -0,0 +1,54 @@ +// Copyright (c) 2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_TXORPHANAGE_H +#define BITCOIN_TXORPHANAGE_H + +#include +#include +#include + +/** Expiration time for orphan transactions in seconds */ +static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60; + +/** Guards orphan transactions and extra txs for compact blocks */ +extern RecursiveMutex g_cs_orphans; + +struct COrphanTx { + // When modifying, adapt the copy of this definition in tests/DoS_tests. + CTransactionRef tx; + NodeId fromPeer; + int64_t nTimeExpire; + size_t list_pos; +}; + +int EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans); +void EraseOrphansFor(NodeId peer); +unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans); + +/** Map from txid to orphan transaction record. Limited by + * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */ +extern std::map mapOrphanTransactions GUARDED_BY(g_cs_orphans); + +/** Index from wtxid into the mapOrphanTransactions to lookup orphan + * transactions using their witness ids. */ +extern std::map::iterator> g_orphans_by_wtxid GUARDED_BY(g_cs_orphans); + + struct IteratorComparator + { + template + bool operator()(const I& a, const I& b) const + { + return &(*a) < &(*b); + } + }; + + /** Index from the parents' COutPoint into the mapOrphanTransactions. Used + * to remove orphan transactions from the mapOrphanTransactions */ + extern std::map::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(g_cs_orphans); + + /** Orphan transactions in vector for quick random eviction */ + extern std::vector::iterator> g_orphan_list GUARDED_BY(g_cs_orphans); + +#endif // BITCOIN_TXORPHANAGE_H -- cgit v1.2.3