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