Line data Source code
1 : // Copyright (c) 2021 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 <txorphanage.h>
6 :
7 : #include <consensus/validation.h>
8 : #include <logging.h>
9 : #include <policy/policy.h>
10 : #include <stats/client.h>
11 :
12 : #include <cassert>
13 :
14 : /** Expiration time for orphan transactions in seconds */
15 : static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
16 : /** Minimum time between orphan transactions expire time checks in seconds */
17 : static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
18 :
19 :
20 111 : bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
21 : {
22 111 : LOCK(m_mutex);
23 :
24 111 : const uint256& hash = tx->GetHash();
25 111 : if (m_orphans.count(hash))
26 20 : return false;
27 :
28 : // Ignore big transactions, to avoid a
29 : // send-big-orphans memory exhaustion attack. If a peer has a legitimate
30 : // large transaction with a missing parent then we assume
31 : // it will rebroadcast it later, after the parent transaction(s)
32 : // have been mined or received.
33 : // 100 orphans, each of which is at most 99,999 bytes big is
34 : // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
35 91 : unsigned int sz = GetSerializeSize(*tx, CTransaction::CURRENT_VERSION);
36 91 : if (sz > MAX_STANDARD_TX_SIZE)
37 : {
38 10 : LogPrint(BCLog::MEMPOOL, "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
39 10 : return false;
40 : }
41 :
42 81 : auto ret = m_orphans.emplace(hash, OrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME, m_orphan_list.size(), sz});
43 81 : assert(ret.second);
44 81 : m_orphan_list.push_back(ret.first);
45 162 : for (const CTxIn& txin : tx->vin) {
46 81 : m_outpoint_to_orphan_it[txin.prevout].insert(ret.first);
47 : }
48 :
49 81 : m_orphan_tx_size += sz;
50 :
51 81 : LogPrint(BCLog::MEMPOOL, "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
52 : m_orphans.size(), m_outpoint_to_orphan_it.size());
53 81 : ::g_stats_client->inc("transactions.orphans.add", 1.0f);
54 81 : ::g_stats_client->gauge("transactions.orphans", m_orphans.size());
55 :
56 81 : return true;
57 111 : }
58 :
59 0 : int TxOrphanage::EraseTx(const uint256& txid)
60 : {
61 0 : LOCK(m_mutex);
62 0 : return _EraseTx(txid);
63 0 : }
64 :
65 80 : int TxOrphanage::_EraseTx(const uint256& txid)
66 : {
67 80 : AssertLockHeld(m_mutex);
68 80 : std::map<uint256, OrphanTx>::iterator it = m_orphans.find(txid);
69 80 : if (it == m_orphans.end())
70 0 : return 0;
71 160 : for (const CTxIn& txin : it->second.tx->vin)
72 : {
73 80 : auto itPrev = m_outpoint_to_orphan_it.find(txin.prevout);
74 80 : if (itPrev == m_outpoint_to_orphan_it.end())
75 0 : continue;
76 80 : itPrev->second.erase(it);
77 80 : if (itPrev->second.empty())
78 80 : m_outpoint_to_orphan_it.erase(itPrev);
79 : }
80 :
81 80 : size_t old_pos = it->second.list_pos;
82 80 : assert(m_orphan_list[old_pos] == it);
83 80 : if (old_pos + 1 != m_orphan_list.size()) {
84 : // Unless we're deleting the last entry in m_orphan_list, move the last
85 : // entry to the position we're deleting.
86 76 : auto it_last = m_orphan_list.back();
87 76 : m_orphan_list[old_pos] = it_last;
88 76 : it_last->second.list_pos = old_pos;
89 76 : }
90 80 : m_orphan_list.pop_back();
91 :
92 80 : assert(m_orphan_tx_size >= it->second.nTxSize);
93 80 : m_orphan_tx_size -= it->second.nTxSize;
94 80 : m_orphans.erase(it);
95 80 : ::g_stats_client->inc("transactions.orphans.remove", 1.0f);
96 80 : ::g_stats_client->gauge("transactions.orphans", m_orphans.size());
97 80 : return 1;
98 80 : }
99 :
100 28 : void TxOrphanage::EraseForPeer(NodeId peer)
101 : {
102 28 : LOCK(m_mutex);
103 :
104 28 : m_peer_work_set.erase(peer);
105 :
106 28 : int nErased = 0;
107 28 : std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
108 262 : while (iter != m_orphans.end())
109 : {
110 234 : std::map<uint256, OrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
111 234 : if (maybeErase->second.fromPeer == peer)
112 : {
113 6 : nErased += _EraseTx(maybeErase->second.tx->GetHash());
114 6 : }
115 : }
116 28 : if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
117 28 : }
118 :
119 3 : void TxOrphanage::LimitOrphans(unsigned int max_orphans_size)
120 : {
121 3 : LOCK(m_mutex);
122 :
123 3 : unsigned int nEvicted = 0;
124 : static int64_t nNextSweep;
125 3 : int64_t nNow = GetTime();
126 3 : if (nNextSweep <= nNow) {
127 : // Sweep out expired orphan pool entries:
128 1 : int nErased = 0;
129 1 : int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
130 1 : std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
131 75 : while (iter != m_orphans.end())
132 : {
133 74 : std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
134 74 : if (maybeErase->second.nTimeExpire <= nNow) {
135 0 : nErased += _EraseTx(maybeErase->second.tx->GetHash());
136 0 : } else {
137 74 : nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
138 : }
139 : }
140 : // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
141 1 : nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
142 1 : if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
143 1 : }
144 3 : FastRandomContext rng;
145 77 : while (!m_orphans.empty() && m_orphan_tx_size > max_orphans_size)
146 : {
147 : // Evict a random orphan:
148 74 : size_t randompos = rng.randrange(m_orphan_list.size());
149 74 : _EraseTx(m_orphan_list[randompos]->first);
150 74 : ++nEvicted;
151 : }
152 3 : if (nEvicted > 0) LogPrint(BCLog::MEMPOOL, "orphanage overflow, removed %u tx\n", nEvicted);
153 3 : }
154 :
155 1 : void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx, NodeId peer)
156 : {
157 1 : LOCK(m_mutex);
158 :
159 : // Get this peer's work set, emplacing an empty set it didn't exist
160 1 : std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(peer).first->second;
161 :
162 4 : for (unsigned int i = 0; i < tx.vout.size(); i++) {
163 3 : const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(tx.GetHash(), i));
164 3 : if (it_by_prev != m_outpoint_to_orphan_it.end()) {
165 2 : for (const auto& elem : it_by_prev->second) {
166 1 : orphan_work_set.insert(elem->first);
167 : }
168 1 : }
169 3 : }
170 1 : }
171 :
172 1 : bool TxOrphanage::HaveTx(const uint256& txid) const
173 : {
174 1 : LOCK(m_mutex);
175 1 : return m_orphans.count(txid);
176 1 : }
177 :
178 3 : CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer, NodeId& originator, bool& more)
179 : {
180 3 : LOCK(m_mutex);
181 :
182 3 : auto work_set_it = m_peer_work_set.find(peer);
183 3 : if (work_set_it != m_peer_work_set.end()) {
184 1 : auto& work_set = work_set_it->second;
185 1 : while (!work_set.empty()) {
186 1 : uint256 txid = *work_set.begin();
187 1 : work_set.erase(work_set.begin());
188 :
189 1 : const auto orphan_it = m_orphans.find(txid);
190 1 : if (orphan_it != m_orphans.end()) {
191 1 : more = !work_set.empty();
192 1 : originator = orphan_it->second.fromPeer;
193 1 : return orphan_it->second.tx;
194 : }
195 : }
196 0 : }
197 2 : more = false;
198 2 : return nullptr;
199 3 : }
200 :
201 1 : void TxOrphanage::SetCandidatesByBlock(const CBlock& block)
202 : {
203 1 : AssertLockNotHeld(m_mutex);
204 : // As these candidates are generated from a block, they have no peer to attribute it to. We use
205 : // NodeId -1 for this reason and need to flush the last set before processing this one.
206 2 : WITH_LOCK(m_mutex, m_peer_work_set.try_emplace(NodeId{-1}).first->second.clear());
207 2 : for (const auto& ptx : block.vtx) {
208 1 : AddChildrenToWorkSet(*ptx, /*peer=*/-1);
209 : }
210 1 : }
211 :
212 0 : void TxOrphanage::EraseForBlock(const CBlock& block)
213 : {
214 0 : LOCK(m_mutex);
215 :
216 0 : std::vector<uint256> vOrphanErase;
217 :
218 0 : for (const CTransactionRef& ptx : block.vtx) {
219 0 : const CTransaction& tx = *ptx;
220 :
221 : // Which orphan pool entries must we evict?
222 0 : for (const auto& txin : tx.vin) {
223 0 : auto itByPrev = m_outpoint_to_orphan_it.find(txin.prevout);
224 0 : if (itByPrev == m_outpoint_to_orphan_it.end()) continue;
225 0 : for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
226 0 : const CTransaction& orphanTx = *(*mi)->second.tx;
227 0 : const uint256& orphanHash = orphanTx.GetHash();
228 0 : vOrphanErase.push_back(orphanHash);
229 0 : }
230 : }
231 : }
232 :
233 : // Erase orphan transactions included or precluded by this block
234 0 : if (vOrphanErase.size()) {
235 0 : int nErased = 0;
236 0 : for (const uint256& orphanHash : vOrphanErase) {
237 0 : nErased += _EraseTx(orphanHash);
238 : }
239 0 : LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
240 0 : }
241 0 : }
242 :
243 2 : bool TxOrphanage::HaveMoreWork(NodeId peer)
244 : {
245 2 : LOCK(m_mutex);
246 2 : auto it = m_peer_work_set.find(peer);
247 2 : return it != m_peer_work_set.end() && !it->second.empty();
248 2 : }
|