]> Git Repo - VerusCoin.git/blob - src/txmempool.h
Auto merge of #1747 - str4d:fix-spendingkey-locking, r=str4d
[VerusCoin.git] / src / txmempool.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef BITCOIN_TXMEMPOOL_H
7 #define BITCOIN_TXMEMPOOL_H
8
9 #include <list>
10
11 #include "amount.h"
12 #include "coins.h"
13 #include "primitives/transaction.h"
14 #include "sync.h"
15
16 class CAutoFile;
17
18 inline double AllowFreeThreshold()
19 {
20     return COIN * 144 / 250;
21 }
22
23 inline bool AllowFree(double dPriority)
24 {
25     // Large (in bytes) low-priority (new, small-coin) transactions
26     // need a fee.
27     return dPriority > AllowFreeThreshold();
28 }
29
30 /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
31 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
32
33 /**
34  * CTxMemPool stores these:
35  */
36 class CTxMemPoolEntry
37 {
38 private:
39     CTransaction tx;
40     CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
41     size_t nTxSize; //! ... and avoid recomputing tx size
42     size_t nModSize; //! ... and modified size for priority
43     size_t nUsageSize; //! ... and total memory usage
44     int64_t nTime; //! Local time when entering the mempool
45     double dPriority; //! Priority when entering the mempool
46     unsigned int nHeight; //! Chain height when entering the mempool
47     bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
48
49 public:
50     CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
51                     int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
52     CTxMemPoolEntry();
53     CTxMemPoolEntry(const CTxMemPoolEntry& other);
54
55     const CTransaction& GetTx() const { return this->tx; }
56     double GetPriority(unsigned int currentHeight) const;
57     CAmount GetFee() const { return nFee; }
58     size_t GetTxSize() const { return nTxSize; }
59     int64_t GetTime() const { return nTime; }
60     unsigned int GetHeight() const { return nHeight; }
61     bool WasClearAtEntry() const { return hadNoDependencies; }
62     size_t DynamicMemoryUsage() const { return nUsageSize; }
63 };
64
65 class CBlockPolicyEstimator;
66
67 /** An inpoint - a combination of a transaction and an index n into its vin */
68 class CInPoint
69 {
70 public:
71     const CTransaction* ptx;
72     uint32_t n;
73
74     CInPoint() { SetNull(); }
75     CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
76     void SetNull() { ptx = NULL; n = (uint32_t) -1; }
77     bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
78     size_t DynamicMemoryUsage() const { return 0; }
79 };
80
81 /**
82  * CTxMemPool stores valid-according-to-the-current-best-chain
83  * transactions that may be included in the next block.
84  *
85  * Transactions are added when they are seen on the network
86  * (or created by the local node), but not all transactions seen
87  * are added to the pool: if a new transaction double-spends
88  * an input of a transaction in the pool, it is dropped,
89  * as are non-standard transactions.
90  */
91 class CTxMemPool
92 {
93 private:
94     bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
95     unsigned int nTransactionsUpdated;
96     CBlockPolicyEstimator* minerPolicyEstimator;
97
98     uint64_t totalTxSize = 0; //! sum of all mempool tx' byte sizes
99     uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
100
101 public:
102     mutable CCriticalSection cs;
103     std::map<uint256, CTxMemPoolEntry> mapTx;
104     std::map<COutPoint, CInPoint> mapNextTx;
105     std::map<uint256, const CTransaction*> mapNullifiers;
106     std::map<uint256, std::pair<double, CAmount> > mapDeltas;
107
108     CTxMemPool(const CFeeRate& _minRelayFee);
109     ~CTxMemPool();
110
111     /**
112      * If sanity-checking is turned on, check makes sure the pool is
113      * consistent (does not contain two transactions that spend the same inputs,
114      * all inputs are in the mapNextTx array). If sanity-checking is turned off,
115      * check does nothing.
116      */
117     void check(const CCoinsViewCache *pcoins) const;
118     void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
119
120     bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
121     void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
122     void removeWithAnchor(const uint256 &invalidRoot);
123     void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
124     void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
125     void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
126                         std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
127     void clear();
128     void queryHashes(std::vector<uint256>& vtxid);
129     void pruneSpent(const uint256& hash, CCoins &coins);
130     unsigned int GetTransactionsUpdated() const;
131     void AddTransactionsUpdated(unsigned int n);
132     /**
133      * Check that none of this transactions inputs are in the mempool, and thus
134      * the tx is not dependent on other mempool transactions to be included in a block.
135      */
136     bool HasNoInputsOf(const CTransaction& tx) const;
137
138     /** Affect CreateNewBlock prioritisation of transactions */
139     void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
140     void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
141     void ClearPrioritisation(const uint256 hash);
142
143     unsigned long size()
144     {
145         LOCK(cs);
146         return mapTx.size();
147     }
148
149     uint64_t GetTotalTxSize()
150     {
151         LOCK(cs);
152         return totalTxSize;
153     }
154
155     bool exists(uint256 hash) const
156     {
157         LOCK(cs);
158         return (mapTx.count(hash) != 0);
159     }
160
161     bool lookup(uint256 hash, CTransaction& result) const;
162
163     /** Estimate fee rate needed to get into the next nBlocks */
164     CFeeRate estimateFee(int nBlocks) const;
165
166     /** Estimate priority needed to get into the next nBlocks */
167     double estimatePriority(int nBlocks) const;
168     
169     /** Write/Read estimates to disk */
170     bool WriteFeeEstimates(CAutoFile& fileout) const;
171     bool ReadFeeEstimates(CAutoFile& filein);
172
173     size_t DynamicMemoryUsage() const;
174 };
175
176 /** 
177  * CCoinsView that brings transactions from a memorypool into view.
178  * It does not check for spendings by memory pool transactions.
179  */
180 class CCoinsViewMemPool : public CCoinsViewBacked
181 {
182 protected:
183     CTxMemPool &mempool;
184
185 public:
186     CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
187     bool GetNullifier(const uint256 &txid) const;
188     bool GetCoins(const uint256 &txid, CCoins &coins) const;
189     bool HaveCoins(const uint256 &txid) const;
190 };
191
192 #endif // BITCOIN_TXMEMPOOL_H
This page took 0.033328 seconds and 4 git commands to generate.