Master Core  v0.0.9 - 49a5c0d97abf09ef2911ddfe8d9551df59f9efd3-dirty
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
txmempool.h
Go to the documentation of this file.
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 
8 #include <list>
9 
10 #include "coins.h"
11 #include "core.h"
12 #include "sync.h"
13 
15 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
16 
17 /*
18  * CTxMemPool stores these:
19  */
21 {
22 private:
24  int64_t nFee; // Cached to avoid expensive parent-transaction lookups
25  size_t nTxSize; // ... and avoid recomputing tx size
26  int64_t nTime; // Local time when entering the mempool
27  double dPriority; // Priority when entering the mempool
28  unsigned int nHeight; // Chain height when entering the mempool
29 
30 public:
31  CTxMemPoolEntry(const CTransaction& _tx, int64_t _nFee,
32  int64_t _nTime, double _dPriority, unsigned int _nHeight);
34  CTxMemPoolEntry(const CTxMemPoolEntry& other);
35 
36  const CTransaction& GetTx() const { return this->tx; }
37  double GetPriority(unsigned int currentHeight) const;
38  int64_t GetFee() const { return nFee; }
39  size_t GetTxSize() const { return nTxSize; }
40  int64_t GetTime() const { return nTime; }
41  unsigned int GetHeight() const { return nHeight; }
42 };
43 
44 /*
45  * CTxMemPool stores valid-according-to-the-current-best-chain
46  * transactions that may be included in the next block.
47  *
48  * Transactions are added when they are seen on the network
49  * (or created by the local node), but not all transactions seen
50  * are added to the pool: if a new transaction double-spends
51  * an input of a transaction in the pool, it is dropped,
52  * as are non-standard transactions.
53  */
55 {
56 private:
57  bool fSanityCheck; // Normally false, true if -checkmempool or -regtest
58  unsigned int nTransactionsUpdated;
59 
60 public:
62  std::map<uint256, CTxMemPoolEntry> mapTx;
63  std::map<COutPoint, CInPoint> mapNextTx;
64 
65  CTxMemPool();
66 
67  /*
68  * If sanity-checking is turned on, check makes sure the pool is
69  * consistent (does not contain two transactions that spend the same inputs,
70  * all inputs are in the mapNextTx array). If sanity-checking is turned off,
71  * check does nothing.
72  */
73  void check(CCoinsViewCache *pcoins) const;
74  void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
75 
76  bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
77  void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
78  void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
79  void clear();
80  void queryHashes(std::vector<uint256>& vtxid);
81  void pruneSpent(const uint256& hash, CCoins &coins);
82  unsigned int GetTransactionsUpdated() const;
83  void AddTransactionsUpdated(unsigned int n);
84 
85  unsigned long size()
86  {
87  LOCK(cs);
88  return mapTx.size();
89  }
90 
91  bool exists(uint256 hash)
92  {
93  LOCK(cs);
94  return (mapTx.count(hash) != 0);
95  }
96 
97  bool lookup(uint256 hash, CTransaction& result) const;
98 };
99 
103 {
104 protected:
106 
107 public:
108  CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn);
109  bool GetCoins(const uint256 &txid, CCoins &coins);
110  bool HaveCoins(const uint256 &txid);
111 };
112 
113 #endif /* BITCOIN_TXMEMPOOL_H */
void check(CCoinsViewCache *pcoins) const
Definition: txmempool.cpp:139
bool HaveCoins(const uint256 &txid)
Definition: txmempool.cpp:211
unsigned int GetHeight() const
Definition: txmempool.h:41
bool lookup(uint256 hash, CTransaction &result) const
Definition: txmempool.cpp:189
double dPriority
Definition: txmempool.h:27
std::map< COutPoint, CInPoint > mapNextTx
Definition: txmempool.h:63
CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn)
Definition: txmempool.cpp:198
void setSanityCheck(bool _fSanityCheck)
Definition: txmempool.h:74
void clear()
Definition: txmempool.cpp:131
unsigned int nHeight
Definition: txmempool.h:28
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:179
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:68
CTransaction tx
Definition: txmempool.h:23
Definition: txmempool.h:20
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:59
unsigned long size()
Definition: txmempool.h:85
Abstract view on the open txout dataset.
Definition: coins.h:258
int64_t nFee
Definition: txmempool.h:24
#define LOCK(cs)
Definition: sync.h:156
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: txmempool.cpp:200
bool fSanityCheck
Definition: txmempool.h:57
double GetPriority(unsigned int currentHeight) const
Definition: txmempool.cpp:30
int64_t GetTime() const
Definition: txmempool.h:40
std::map< uint256, CTxMemPoolEntry > mapTx
Definition: txmempool.h:62
size_t nTxSize
Definition: txmempool.h:25
unsigned int nTransactionsUpdated
Definition: txmempool.h:58
int64_t nTime
Definition: txmempool.h:26
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:65
CCriticalSection cs
Definition: txmempool.h:61
CTxMemPoolEntry()
Definition: txmempool.cpp:11
void removeConflicts(const CTransaction &tx, std::list< CTransaction > &removed)
Definition: txmempool.cpp:114
256-bit unsigned integer
Definition: uint256.h:531
void pruneSpent(const uint256 &hash, CCoins &coins)
Definition: txmempool.cpp:46
const CTransaction & GetTx() const
Definition: txmempool.h:36
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry)
Definition: txmempool.cpp:72
bool exists(uint256 hash)
Definition: txmempool.h:91
static const unsigned int MEMPOOL_HEIGHT
Fake height value used in CCoins to signify they are only in the memory pool (since 0...
Definition: txmempool.h:15
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
CCoinsView backed by another CCoinsView.
Definition: coins.h:289
int64_t GetFee() const
Definition: txmempool.h:38
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:308
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:102
CTxMemPool & mempool
Definition: txmempool.h:105
size_t GetTxSize() const
Definition: txmempool.h:39