]> Git Repo - VerusCoin.git/blame - src/txmempool.h
Fix exception
[VerusCoin.git] / src / txmempool.h
CommitLineData
319b1160 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
7329fdd1 3// Distributed under the MIT software license, see the accompanying
319b1160 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
093303a8 5
319b1160
GA
6#ifndef BITCOIN_TXMEMPOOL_H
7#define BITCOIN_TXMEMPOOL_H
8
93a18a36
GA
9#include <list>
10
8b78a819
T
11#include "addressindex.h"
12#include "spentindex.h"
eda37330 13#include "amount.h"
a0fa20a1 14#include "coins.h"
d2270111 15#include "primitives/transaction.h"
51ed9ec9 16#include "sync.h"
319b1160 17
e328fa32
AH
18#undef foreach
19#include "boost/multi_index_container.hpp"
20#include "boost/multi_index/ordered_index.hpp"
21
fa736190
CF
22class CAutoFile;
23
c1c9d5b4
CL
24inline double AllowFreeThreshold()
25{
26 return COIN * 144 / 250;
27}
28
171ca774
GA
29inline bool AllowFree(double dPriority)
30{
31 // Large (in bytes) low-priority (new, small-coin) transactions
32 // need a fee.
c1c9d5b4 33 return dPriority > AllowFreeThreshold();
171ca774
GA
34}
35
a0fa20a1
PW
36/** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
37static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
38
7329fdd1 39/**
4d707d51
GA
40 * CTxMemPool stores these:
41 */
42class CTxMemPoolEntry
43{
44private:
45 CTransaction tx;
7329fdd1
MF
46 CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
47 size_t nTxSize; //! ... and avoid recomputing tx size
48 size_t nModSize; //! ... and modified size for priority
bde5c8b0 49 size_t nUsageSize; //! ... and total memory usage
e328fa32 50 CFeeRate feeRate; //! ... and fee per kB
7329fdd1
MF
51 int64_t nTime; //! Local time when entering the mempool
52 double dPriority; //! Priority when entering the mempool
53 unsigned int nHeight; //! Chain height when entering the mempool
b649e039 54 bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
a4b25180 55 bool spendsCoinbase; //! keep track of transactions that spend a coinbase
34a64fe0 56 uint32_t nBranchId; //! Branch ID this transaction is known to commit to, cached for efficiency
4d707d51
GA
57
58public:
a372168e 59 CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
a4b25180 60 int64_t _nTime, double _dPriority, unsigned int _nHeight,
34a64fe0 61 bool poolHasNoInputsOf, bool spendsCoinbase, uint32_t nBranchId);
4d707d51
GA
62 CTxMemPoolEntry();
63 CTxMemPoolEntry(const CTxMemPoolEntry& other);
64
65 const CTransaction& GetTx() const { return this->tx; }
66 double GetPriority(unsigned int currentHeight) const;
a372168e 67 CAmount GetFee() const { return nFee; }
e328fa32 68 CFeeRate GetFeeRate() const { return feeRate; }
4d707d51
GA
69 size_t GetTxSize() const { return nTxSize; }
70 int64_t GetTime() const { return nTime; }
71 unsigned int GetHeight() const { return nHeight; }
b649e039 72 bool WasClearAtEntry() const { return hadNoDependencies; }
bde5c8b0 73 size_t DynamicMemoryUsage() const { return nUsageSize; }
a4b25180
SD
74
75 bool GetSpendsCoinbase() const { return spendsCoinbase; }
34a64fe0 76 uint32_t GetValidatedBranchId() const { return nBranchId; }
4d707d51
GA
77};
78
e328fa32
AH
79// extracts a TxMemPoolEntry's transaction hash
80struct mempoolentry_txid
81{
82 typedef uint256 result_type;
83 result_type operator() (const CTxMemPoolEntry &entry) const
84 {
85 return entry.GetTx().GetHash();
86 }
87};
88
89class CompareTxMemPoolEntryByFee
90{
91public:
92 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b)
93 {
94 if (a.GetFeeRate() == b.GetFeeRate())
95 return a.GetTime() < b.GetTime();
96 return a.GetFeeRate() > b.GetFeeRate();
97 }
4d707d51
GA
98};
99
b649e039 100class CBlockPolicyEstimator;
171ca774 101
e8ea0fd1 102/** An inpoint - a combination of a transaction and an index n into its vin */
103class CInPoint
104{
105public:
106 const CTransaction* ptx;
107 uint32_t n;
108
109 CInPoint() { SetNull(); }
110 CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
111 void SetNull() { ptx = NULL; n = (uint32_t) -1; }
112 bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
bde5c8b0 113 size_t DynamicMemoryUsage() const { return 0; }
e8ea0fd1 114};
115
7329fdd1 116/**
319b1160
GA
117 * CTxMemPool stores valid-according-to-the-current-best-chain
118 * transactions that may be included in the next block.
119 *
120 * Transactions are added when they are seen on the network
121 * (or created by the local node), but not all transactions seen
122 * are added to the pool: if a new transaction double-spends
123 * an input of a transaction in the pool, it is dropped,
124 * as are non-standard transactions.
125 */
126class CTxMemPool
127{
128private:
934fd197 129 uint32_t nCheckFrequency; //! Value n means that n times in 2^32 we check.
319b1160 130 unsigned int nTransactionsUpdated;
b649e039 131 CBlockPolicyEstimator* minerPolicyEstimator;
319b1160 132
43873535 133 uint64_t totalTxSize = 0; //! sum of all mempool tx' byte sizes
bde5c8b0 134 uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
13fc83c7 135
319b1160 136public:
e328fa32
AH
137 typedef boost::multi_index_container<
138 CTxMemPoolEntry,
139 boost::multi_index::indexed_by<
140 // sorted by txid
141 boost::multi_index::ordered_unique<mempoolentry_txid>,
142 // sorted by fee rate
143 boost::multi_index::ordered_non_unique<
144 boost::multi_index::identity<CTxMemPoolEntry>,
145 CompareTxMemPoolEntryByFee
146 >
147 >
148 > indexed_transaction_set;
149
319b1160 150 mutable CCriticalSection cs;
e328fa32 151 indexed_transaction_set mapTx;
8b78a819
T
152
153private:
154 typedef std::map<CMempoolAddressDeltaKey, CMempoolAddressDelta, CMempoolAddressDeltaKeyCompare> addressDeltaMap;
155 addressDeltaMap mapAddress;
156
157 typedef std::map<uint256, std::vector<CMempoolAddressDeltaKey> > addressDeltaMapInserted;
158 addressDeltaMapInserted mapAddressInserted;
159
160 typedef std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mapSpentIndex;
161 mapSpentIndex mapSpent;
162
163 typedef std::map<uint256, std::vector<CSpentIndexKey> > mapSpentIndexInserted;
164 mapSpentIndexInserted mapSpentInserted;
165
166public:
319b1160 167 std::map<COutPoint, CInPoint> mapNextTx;
bb64be52 168 std::map<uint256, const CTransaction*> mapNullifiers;
a372168e 169 std::map<uint256, std::pair<double, CAmount> > mapDeltas;
319b1160 170
13fc83c7 171 CTxMemPool(const CFeeRate& _minRelayFee);
171ca774 172 ~CTxMemPool();
319b1160 173
7329fdd1 174 /**
319b1160
GA
175 * If sanity-checking is turned on, check makes sure the pool is
176 * consistent (does not contain two transactions that spend the same inputs,
177 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
178 * check does nothing.
179 */
d0867acb 180 void check(const CCoinsViewCache *pcoins) const;
934fd197 181 void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967296.0; }
319b1160 182
b649e039 183 bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
8b78a819
T
184 void addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view);
185 bool getAddressIndex(std::vector<std::pair<uint160, int> > &addresses,
186 std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results);
187 bool removeAddressIndex(const uint256 txhash);
188
189 void addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view);
190 bool getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
191 bool removeSpentIndex(const uint256 txhash);
93a18a36 192 void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
a8ac403d 193 void removeWithAnchor(const uint256 &invalidRoot);
233c9eb6 194 void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags);
93a18a36 195 void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
9bb37bf0 196 void removeExpired(unsigned int nBlockHeight);
171ca774 197 void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
b649e039 198 std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
34a64fe0 199 void removeWithoutBranchId(uint32_t nMemPoolBranchId);
319b1160
GA
200 void clear();
201 void queryHashes(std::vector<uint256>& vtxid);
202 void pruneSpent(const uint256& hash, CCoins &coins);
203 unsigned int GetTransactionsUpdated() const;
204 void AddTransactionsUpdated(unsigned int n);
b649e039
AM
205 /**
206 * Check that none of this transactions inputs are in the mempool, and thus
207 * the tx is not dependent on other mempool transactions to be included in a block.
208 */
209 bool HasNoInputsOf(const CTransaction& tx) const;
319b1160 210
2a72d459 211 /** Affect CreateNewBlock prioritisation of transactions */
a372168e
MF
212 void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
213 void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
2a72d459
LD
214 void ClearPrioritisation(const uint256 hash);
215
319b1160
GA
216 unsigned long size()
217 {
218 LOCK(cs);
219 return mapTx.size();
220 }
bde5c8b0 221
6f2c26a4
JG
222 uint64_t GetTotalTxSize()
223 {
224 LOCK(cs);
225 return totalTxSize;
226 }
319b1160 227
b649e039 228 bool exists(uint256 hash) const
319b1160
GA
229 {
230 LOCK(cs);
231 return (mapTx.count(hash) != 0);
232 }
233
234 bool lookup(uint256 hash, CTransaction& result) const;
171ca774 235
7329fdd1 236 /** Estimate fee rate needed to get into the next nBlocks */
171ca774 237 CFeeRate estimateFee(int nBlocks) const;
7329fdd1
MF
238
239 /** Estimate priority needed to get into the next nBlocks */
171ca774 240 double estimatePriority(int nBlocks) const;
7329fdd1
MF
241
242 /** Write/Read estimates to disk */
171ca774
GA
243 bool WriteFeeEstimates(CAutoFile& fileout) const;
244 bool ReadFeeEstimates(CAutoFile& filein);
bde5c8b0
PW
245
246 size_t DynamicMemoryUsage() const;
319b1160
GA
247};
248
7329fdd1
MF
249/**
250 * CCoinsView that brings transactions from a memorypool into view.
251 * It does not check for spendings by memory pool transactions.
252 */
a0fa20a1
PW
253class CCoinsViewMemPool : public CCoinsViewBacked
254{
255protected:
256 CTxMemPool &mempool;
257
258public:
7c70438d 259 CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
616f8d05 260 bool GetNullifier(const uint256 &txid) const;
a3dc587a
DK
261 bool GetCoins(const uint256 &txid, CCoins &coins) const;
262 bool HaveCoins(const uint256 &txid) const;
a0fa20a1
PW
263};
264
093303a8 265#endif // BITCOIN_TXMEMPOOL_H
This page took 0.235372 seconds and 4 git commands to generate.