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