Anoncoin  0.9.4
P2P Digital Currency
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 // Copyright (c) 2013-2014 The Anoncoin Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef ANONCOIN_TXMEMPOOL_H
7 #define ANONCOIN_TXMEMPOOL_H
8 
9 #include <list>
10 
11 #include "coins.h"
12 #include "core.h"
13 #include "sync.h"
14 
16 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
17 
18 /*
19  * CTxMemPool stores these:
20  */
22 {
23 private:
25  int64_t nFee; // Cached to avoid expensive parent-transaction lookups
26  size_t nTxSize; // ... and avoid recomputing tx size
27  int64_t nTime; // Local time when entering the mempool
28  double dPriority; // Priority when entering the mempool
29  unsigned int nHeight; // Chain height when entering the mempool
30 
31 public:
32  CTxMemPoolEntry(const CTransaction& _tx, int64_t _nFee,
33  int64_t _nTime, double _dPriority, unsigned int _nHeight);
35  CTxMemPoolEntry(const CTxMemPoolEntry& other);
36 
37  const CTransaction& GetTx() const { return this->tx; }
38  double GetPriority(unsigned int currentHeight) const;
39  int64_t GetFee() const { return nFee; }
40  size_t GetTxSize() const { return nTxSize; }
41  int64_t GetTime() const { return nTime; }
42  unsigned int GetHeight() const { return nHeight; }
43 };
44 
45 /*
46  * CTxMemPool stores valid-according-to-the-current-best-chain
47  * transactions that may be included in the next block.
48  *
49  * Transactions are added when they are seen on the network
50  * (or created by the local node), but not all transactions seen
51  * are added to the pool: if a new transaction double-spends
52  * an input of a transaction in the pool, it is dropped,
53  * as are non-standard transactions.
54  */
56 {
57 private:
58  bool fSanityCheck; // Normally false, true if -checkmempool or -regtest
59  unsigned int nTransactionsUpdated;
60 
61 public:
63  std::map<uint256, CTxMemPoolEntry> mapTx;
64  std::map<COutPoint, CInPoint> mapNextTx;
65 
66  CTxMemPool();
67 
68  /*
69  * If sanity-checking is turned on, check makes sure the pool is
70  * consistent (does not contain two transactions that spend the same inputs,
71  * all inputs are in the mapNextTx array). If sanity-checking is turned off,
72  * check does nothing.
73  */
74  void check(CCoinsViewCache *pcoins) const;
75  void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
76 
77  bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
78  void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
79  void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
80  void clear();
81  void queryHashes(std::vector<uint256>& vtxid);
82  void pruneSpent(const uint256& hash, CCoins &coins);
83  unsigned int GetTransactionsUpdated() const;
84  void AddTransactionsUpdated(unsigned int n);
85 
86  unsigned long size()
87  {
88  LOCK(cs);
89  return mapTx.size();
90  }
91 
92  bool exists(uint256 hash)
93  {
94  LOCK(cs);
95  return (mapTx.count(hash) != 0);
96  }
97 
98  bool lookup(uint256 hash, CTransaction& result) const;
99 };
100 
104 {
105 protected:
107 
108 public:
109  CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn);
110  bool GetCoins(const uint256 &txid, CCoins &coins);
111  bool HaveCoins(const uint256 &txid);
112 };
113 
114 #endif /* ANONCOIN_TXMEMPOOL_H */
void check(CCoinsViewCache *pcoins) const
Definition: txmempool.cpp:140
bool HaveCoins(const uint256 &txid)
Definition: txmempool.cpp:212
unsigned int GetHeight() const
Definition: txmempool.h:42
bool lookup(uint256 hash, CTransaction &result) const
Definition: txmempool.cpp:190
double dPriority
Definition: txmempool.h:28
std::map< COutPoint, CInPoint > mapNextTx
Definition: txmempool.h:64
CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn)
Definition: txmempool.cpp:199
void setSanityCheck(bool _fSanityCheck)
Definition: txmempool.h:75
void clear()
Definition: txmempool.cpp:132
unsigned int nHeight
Definition: txmempool.h:29
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:180
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:69
CTransaction tx
Definition: txmempool.h:24
Definition: txmempool.h:21
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:60
unsigned long size()
Definition: txmempool.h:86
Abstract view on the open txout dataset.
Definition: coins.h:259
int64_t nFee
Definition: txmempool.h:25
#define LOCK(cs)
Definition: sync.h:157
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: txmempool.cpp:201
bool fSanityCheck
Definition: txmempool.h:58
double GetPriority(unsigned int currentHeight) const
Definition: txmempool.cpp:31
int64_t GetTime() const
Definition: txmempool.h:41
std::map< uint256, CTxMemPoolEntry > mapTx
Definition: txmempool.h:63
size_t nTxSize
Definition: txmempool.h:26
unsigned int nTransactionsUpdated
Definition: txmempool.h:59
int64_t nTime
Definition: txmempool.h:27
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:66
CCriticalSection cs
Definition: txmempool.h:62
CTxMemPoolEntry()
Definition: txmempool.cpp:12
void removeConflicts(const CTransaction &tx, std::list< CTransaction > &removed)
Definition: txmempool.cpp:115
256-bit unsigned integer
Definition: uint256.h:532
void pruneSpent(const uint256 &hash, CCoins &coins)
Definition: txmempool.cpp:47
const CTransaction & GetTx() const
Definition: txmempool.h:37
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry)
Definition: txmempool.cpp:73
bool exists(uint256 hash)
Definition: txmempool.h:92
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:179
CCoinsView backed by another CCoinsView.
Definition: coins.h:290
int64_t GetFee() const
Definition: txmempool.h:39
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:309
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:103
CTxMemPool & mempool
Definition: txmempool.h:106
size_t GetTxSize() const
Definition: txmempool.h:40