]> Git Repo - VerusCoin.git/blame - src/txmempool.h
add spentindex RPC for bitcore block explorer
[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
bc909a7a 4// file COPYING or https://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"
68e174e2 15#include "addressindex.h"
86b23f37 16#include "spentindex.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;
98f30100
M
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
49 size_t nUsageSize; //!< ... and total memory usage
50 CFeeRate feeRate; //!< ... and fee per kB
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
54 bool hadNoDependencies; //!< Not dependent on any other txs when it entered the mempool
55 bool spendsCoinbase; //!< keep track of transactions that spend a coinbase
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 }
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:
98f30100 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
98f30100
M
133 uint64_t totalTxSize = 0; //!< sum of all mempool tx' byte sizes
134 uint64_t cachedInnerUsage; //!< sum of dynamic memory usage of all the map elements (NOT the maps themselves)
13fc83c7 135
9669920f 136 std::map<uint256, const CTransaction*> mapSproutNullifiers;
685e936c
EOW
137 std::map<uint256, const CTransaction*> mapSaplingNullifiers;
138
28d20bdb 139 void checkNullifiers(ShieldedType type) const;
685e936c 140
319b1160 141public:
e328fa32
AH
142 typedef boost::multi_index_container<
143 CTxMemPoolEntry,
144 boost::multi_index::indexed_by<
145 // sorted by txid
146 boost::multi_index::ordered_unique<mempoolentry_txid>,
147 // sorted by fee rate
148 boost::multi_index::ordered_non_unique<
149 boost::multi_index::identity<CTxMemPoolEntry>,
150 CompareTxMemPoolEntryByFee
151 >
152 >
153 > indexed_transaction_set;
154
319b1160 155 mutable CCriticalSection cs;
e328fa32 156 indexed_transaction_set mapTx;
68e174e2
LR
157
158private:
86b23f37 159 // insightexplorer
68e174e2
LR
160 std::map<CMempoolAddressDeltaKey, CMempoolAddressDelta, CMempoolAddressDeltaKeyCompare> mapAddress;
161 std::map<uint256, std::vector<CMempoolAddressDeltaKey> > mapAddressInserted;
86b23f37
LR
162 std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mapSpent;
163 std::map<uint256, std::vector<CSpentIndexKey>> mapSpentInserted;
68e174e2
LR
164
165public:
319b1160 166 std::map<COutPoint, CInPoint> mapNextTx;
a372168e 167 std::map<uint256, std::pair<double, CAmount> > mapDeltas;
9669920f 168
13fc83c7 169 CTxMemPool(const CFeeRate& _minRelayFee);
171ca774 170 ~CTxMemPool();
319b1160 171
7329fdd1 172 /**
319b1160
GA
173 * If sanity-checking is turned on, check makes sure the pool is
174 * consistent (does not contain two transactions that spend the same inputs,
175 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
176 * check does nothing.
177 */
d0867acb 178 void check(const CCoinsViewCache *pcoins) const;
a0c977ca 179 void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }
319b1160 180
b649e039 181 bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
68e174e2 182
86b23f37 183 // START insightexplorer
68e174e2
LR
184 void addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view);
185 void getAddressIndex(const std::vector<std::pair<uint160, int>>& addresses,
186 std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta>>& results);
187 void removeAddressIndex(const uint256& txhash);
188
86b23f37
LR
189 void addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view);
190 bool getSpentIndex(const CSpentIndexKey &key, CSpentIndexValue &value);
191 void removeSpentIndex(const uint256 txhash);
192 // END insightexplorer
193
93a18a36 194 void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
98d2f090 195 void removeWithAnchor(const uint256 &invalidRoot, ShieldedType type);
233c9eb6 196 void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags);
93a18a36 197 void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
9bb37bf0 198 void removeExpired(unsigned int nBlockHeight);
171ca774 199 void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
b649e039 200 std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
34a64fe0 201 void removeWithoutBranchId(uint32_t nMemPoolBranchId);
319b1160
GA
202 void clear();
203 void queryHashes(std::vector<uint256>& vtxid);
204 void pruneSpent(const uint256& hash, CCoins &coins);
205 unsigned int GetTransactionsUpdated() const;
206 void AddTransactionsUpdated(unsigned int n);
b649e039
AM
207 /**
208 * Check that none of this transactions inputs are in the mempool, and thus
209 * the tx is not dependent on other mempool transactions to be included in a block.
210 */
211 bool HasNoInputsOf(const CTransaction& tx) const;
319b1160 212
2a72d459 213 /** Affect CreateNewBlock prioritisation of transactions */
a372168e
MF
214 void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
215 void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
2a72d459
LD
216 void ClearPrioritisation(const uint256 hash);
217
28d20bdb 218 bool nullifierExists(const uint256& nullifier, ShieldedType type) const;
685e936c 219
319b1160
GA
220 unsigned long size()
221 {
222 LOCK(cs);
223 return mapTx.size();
224 }
bde5c8b0 225
6f2c26a4
JG
226 uint64_t GetTotalTxSize()
227 {
228 LOCK(cs);
229 return totalTxSize;
230 }
319b1160 231
b649e039 232 bool exists(uint256 hash) const
319b1160
GA
233 {
234 LOCK(cs);
235 return (mapTx.count(hash) != 0);
236 }
237
238 bool lookup(uint256 hash, CTransaction& result) const;
171ca774 239
7329fdd1 240 /** Estimate fee rate needed to get into the next nBlocks */
171ca774 241 CFeeRate estimateFee(int nBlocks) const;
7329fdd1
MF
242
243 /** Estimate priority needed to get into the next nBlocks */
171ca774 244 double estimatePriority(int nBlocks) const;
7329fdd1
MF
245
246 /** Write/Read estimates to disk */
171ca774
GA
247 bool WriteFeeEstimates(CAutoFile& fileout) const;
248 bool ReadFeeEstimates(CAutoFile& filein);
bde5c8b0
PW
249
250 size_t DynamicMemoryUsage() const;
a0c977ca
S
251
252 /** Return nCheckFrequency */
253 uint32_t GetCheckFrequency() const {
254 return nCheckFrequency;
255 }
319b1160
GA
256};
257
7329fdd1
MF
258/**
259 * CCoinsView that brings transactions from a memorypool into view.
260 * It does not check for spendings by memory pool transactions.
261 */
a0fa20a1
PW
262class CCoinsViewMemPool : public CCoinsViewBacked
263{
264protected:
265 CTxMemPool &mempool;
266
267public:
7c70438d 268 CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
28d20bdb 269 bool GetNullifier(const uint256 &txid, ShieldedType type) const;
a3dc587a
DK
270 bool GetCoins(const uint256 &txid, CCoins &coins) const;
271 bool HaveCoins(const uint256 &txid) const;
a0fa20a1
PW
272};
273
093303a8 274#endif // BITCOIN_TXMEMPOOL_H
This page took 0.228141 seconds and 4 git commands to generate.