]> Git Repo - VerusCoin.git/blob - src/txmempool.h
Merge pull request #6098
[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     int64_t nTime; //! Local time when entering the mempool
44     double dPriority; //! Priority when entering the mempool
45     unsigned int nHeight; //! Chain height when entering the mempool
46     bool hadNoDependencies; //! Not dependent on any other txs when it entered the mempool
47
48 public:
49     CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
50                     int64_t _nTime, double _dPriority, unsigned int _nHeight, bool poolHasNoInputsOf = false);
51     CTxMemPoolEntry();
52     CTxMemPoolEntry(const CTxMemPoolEntry& other);
53
54     const CTransaction& GetTx() const { return this->tx; }
55     double GetPriority(unsigned int currentHeight) const;
56     CAmount GetFee() const { return nFee; }
57     size_t GetTxSize() const { return nTxSize; }
58     int64_t GetTime() const { return nTime; }
59     unsigned int GetHeight() const { return nHeight; }
60     bool WasClearAtEntry() const { return hadNoDependencies; }
61 };
62
63 class CBlockPolicyEstimator;
64
65 /** An inpoint - a combination of a transaction and an index n into its vin */
66 class CInPoint
67 {
68 public:
69     const CTransaction* ptx;
70     uint32_t n;
71
72     CInPoint() { SetNull(); }
73     CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
74     void SetNull() { ptx = NULL; n = (uint32_t) -1; }
75     bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
76 };
77
78 /**
79  * CTxMemPool stores valid-according-to-the-current-best-chain
80  * transactions that may be included in the next block.
81  *
82  * Transactions are added when they are seen on the network
83  * (or created by the local node), but not all transactions seen
84  * are added to the pool: if a new transaction double-spends
85  * an input of a transaction in the pool, it is dropped,
86  * as are non-standard transactions.
87  */
88 class CTxMemPool
89 {
90 private:
91     bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
92     unsigned int nTransactionsUpdated;
93     CBlockPolicyEstimator* minerPolicyEstimator;
94
95     uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
96
97 public:
98     mutable CCriticalSection cs;
99     std::map<uint256, CTxMemPoolEntry> mapTx;
100     std::map<COutPoint, CInPoint> mapNextTx;
101     std::map<uint256, std::pair<double, CAmount> > mapDeltas;
102
103     CTxMemPool(const CFeeRate& _minRelayFee);
104     ~CTxMemPool();
105
106     /**
107      * If sanity-checking is turned on, check makes sure the pool is
108      * consistent (does not contain two transactions that spend the same inputs,
109      * all inputs are in the mapNextTx array). If sanity-checking is turned off,
110      * check does nothing.
111      */
112     void check(const CCoinsViewCache *pcoins) const;
113     void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
114
115     bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool fCurrentEstimate = true);
116     void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
117     void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
118     void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
119     void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
120                         std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);
121     void clear();
122     void queryHashes(std::vector<uint256>& vtxid);
123     void pruneSpent(const uint256& hash, CCoins &coins);
124     unsigned int GetTransactionsUpdated() const;
125     void AddTransactionsUpdated(unsigned int n);
126     /**
127      * Check that none of this transactions inputs are in the mempool, and thus
128      * the tx is not dependent on other mempool transactions to be included in a block.
129      */
130     bool HasNoInputsOf(const CTransaction& tx) const;
131
132     /** Affect CreateNewBlock prioritisation of transactions */
133     void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
134     void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
135     void ClearPrioritisation(const uint256 hash);
136
137     unsigned long size()
138     {
139         LOCK(cs);
140         return mapTx.size();
141     }
142     uint64_t GetTotalTxSize()
143     {
144         LOCK(cs);
145         return totalTxSize;
146     }
147
148     bool exists(uint256 hash) const
149     {
150         LOCK(cs);
151         return (mapTx.count(hash) != 0);
152     }
153
154     bool lookup(uint256 hash, CTransaction& result) const;
155
156     /** Estimate fee rate needed to get into the next nBlocks */
157     CFeeRate estimateFee(int nBlocks) const;
158
159     /** Estimate priority needed to get into the next nBlocks */
160     double estimatePriority(int nBlocks) const;
161     
162     /** Write/Read estimates to disk */
163     bool WriteFeeEstimates(CAutoFile& fileout) const;
164     bool ReadFeeEstimates(CAutoFile& filein);
165 };
166
167 /** 
168  * CCoinsView that brings transactions from a memorypool into view.
169  * It does not check for spendings by memory pool transactions.
170  */
171 class CCoinsViewMemPool : public CCoinsViewBacked
172 {
173 protected:
174     CTxMemPool &mempool;
175
176 public:
177     CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
178     bool GetCoins(const uint256 &txid, CCoins &coins) const;
179     bool HaveCoins(const uint256 &txid) const;
180 };
181
182 #endif // BITCOIN_TXMEMPOOL_H
This page took 0.037209 seconds and 4 git commands to generate.