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