]>
Commit | Line | Data |
---|---|---|
319b1160 GA |
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | // Copyright (c) 2009-2013 The Bitcoin developers | |
3 | // Distributed under the MIT/X11 software license, see the accompanying | |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | |
5 | #ifndef BITCOIN_TXMEMPOOL_H | |
6 | #define BITCOIN_TXMEMPOOL_H | |
7 | ||
93a18a36 GA |
8 | #include <list> |
9 | ||
a0fa20a1 | 10 | #include "coins.h" |
319b1160 | 11 | #include "core.h" |
51ed9ec9 | 12 | #include "sync.h" |
319b1160 | 13 | |
171ca774 GA |
14 | inline bool AllowFree(double dPriority) |
15 | { | |
16 | // Large (in bytes) low-priority (new, small-coin) transactions | |
17 | // need a fee. | |
18 | return dPriority > COIN * 144 / 250; | |
19 | } | |
20 | ||
a0fa20a1 PW |
21 | /** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */ |
22 | static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF; | |
23 | ||
4d707d51 GA |
24 | /* |
25 | * CTxMemPool stores these: | |
26 | */ | |
27 | class CTxMemPoolEntry | |
28 | { | |
29 | private: | |
30 | CTransaction tx; | |
31 | int64_t nFee; // Cached to avoid expensive parent-transaction lookups | |
32 | size_t nTxSize; // ... and avoid recomputing tx size | |
33 | int64_t nTime; // Local time when entering the mempool | |
34 | double dPriority; // Priority when entering the mempool | |
35 | unsigned int nHeight; // Chain height when entering the mempool | |
36 | ||
37 | public: | |
38 | CTxMemPoolEntry(const CTransaction& _tx, int64_t _nFee, | |
39 | int64_t _nTime, double _dPriority, unsigned int _nHeight); | |
40 | CTxMemPoolEntry(); | |
41 | CTxMemPoolEntry(const CTxMemPoolEntry& other); | |
42 | ||
43 | const CTransaction& GetTx() const { return this->tx; } | |
44 | double GetPriority(unsigned int currentHeight) const; | |
45 | int64_t GetFee() const { return nFee; } | |
46 | size_t GetTxSize() const { return nTxSize; } | |
47 | int64_t GetTime() const { return nTime; } | |
48 | unsigned int GetHeight() const { return nHeight; } | |
49 | }; | |
50 | ||
171ca774 GA |
51 | class CMinerPolicyEstimator; |
52 | ||
319b1160 GA |
53 | /* |
54 | * CTxMemPool stores valid-according-to-the-current-best-chain | |
55 | * transactions that may be included in the next block. | |
56 | * | |
57 | * Transactions are added when they are seen on the network | |
58 | * (or created by the local node), but not all transactions seen | |
59 | * are added to the pool: if a new transaction double-spends | |
60 | * an input of a transaction in the pool, it is dropped, | |
61 | * as are non-standard transactions. | |
62 | */ | |
63 | class CTxMemPool | |
64 | { | |
65 | private: | |
66 | bool fSanityCheck; // Normally false, true if -checkmempool or -regtest | |
67 | unsigned int nTransactionsUpdated; | |
171ca774 | 68 | CMinerPolicyEstimator* minerPolicyEstimator; |
319b1160 GA |
69 | |
70 | public: | |
71 | mutable CCriticalSection cs; | |
4d707d51 | 72 | std::map<uint256, CTxMemPoolEntry> mapTx; |
319b1160 GA |
73 | std::map<COutPoint, CInPoint> mapNextTx; |
74 | ||
75 | CTxMemPool(); | |
171ca774 | 76 | ~CTxMemPool(); |
319b1160 GA |
77 | |
78 | /* | |
79 | * If sanity-checking is turned on, check makes sure the pool is | |
80 | * consistent (does not contain two transactions that spend the same inputs, | |
81 | * all inputs are in the mapNextTx array). If sanity-checking is turned off, | |
82 | * check does nothing. | |
83 | */ | |
a0fa20a1 | 84 | void check(CCoinsViewCache *pcoins) const; |
319b1160 GA |
85 | void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; } |
86 | ||
4d707d51 | 87 | bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry); |
93a18a36 GA |
88 | void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false); |
89 | void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed); | |
171ca774 GA |
90 | void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight, |
91 | std::list<CTransaction>& conflicts); | |
319b1160 GA |
92 | void clear(); |
93 | void queryHashes(std::vector<uint256>& vtxid); | |
94 | void pruneSpent(const uint256& hash, CCoins &coins); | |
95 | unsigned int GetTransactionsUpdated() const; | |
96 | void AddTransactionsUpdated(unsigned int n); | |
97 | ||
98 | unsigned long size() | |
99 | { | |
100 | LOCK(cs); | |
101 | return mapTx.size(); | |
102 | } | |
103 | ||
104 | bool exists(uint256 hash) | |
105 | { | |
106 | LOCK(cs); | |
107 | return (mapTx.count(hash) != 0); | |
108 | } | |
109 | ||
110 | bool lookup(uint256 hash, CTransaction& result) const; | |
171ca774 GA |
111 | |
112 | // Estimate fee rate needed to get into the next | |
113 | // nBlocks | |
114 | CFeeRate estimateFee(int nBlocks) const; | |
115 | // Estimate priority needed to get into the next | |
116 | // nBlocks | |
117 | double estimatePriority(int nBlocks) const; | |
118 | // Write/Read estimates to disk | |
119 | bool WriteFeeEstimates(CAutoFile& fileout) const; | |
120 | bool ReadFeeEstimates(CAutoFile& filein); | |
319b1160 GA |
121 | }; |
122 | ||
a0fa20a1 PW |
123 | /** CCoinsView that brings transactions from a memorypool into view. |
124 | It does not check for spendings by memory pool transactions. */ | |
125 | class CCoinsViewMemPool : public CCoinsViewBacked | |
126 | { | |
127 | protected: | |
128 | CTxMemPool &mempool; | |
129 | ||
130 | public: | |
131 | CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn); | |
132 | bool GetCoins(const uint256 &txid, CCoins &coins); | |
133 | bool HaveCoins(const uint256 &txid); | |
134 | }; | |
135 | ||
319b1160 | 136 | #endif /* BITCOIN_TXMEMPOOL_H */ |