Anoncoin  0.9.4
P2P Digital Currency
txmempool.cpp
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 
7 #include "core.h"
8 #include "txmempool.h"
9 
10 using namespace std;
11 
13 {
14  nHeight = MEMPOOL_HEIGHT;
15 }
16 
18  int64_t _nTime, double _dPriority,
19  unsigned int _nHeight):
20  tx(_tx), nFee(_nFee), nTime(_nTime), dPriority(_dPriority), nHeight(_nHeight)
21 {
22  nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
23 }
24 
26 {
27  *this = other;
28 }
29 
30 double
31 CTxMemPoolEntry::GetPriority(unsigned int currentHeight) const
32 {
33  int64_t nValueIn = tx.GetValueOut()+nFee;
34  double deltaPriority = ((double)(currentHeight-nHeight)*nValueIn)/nTxSize;
35  double dResult = dPriority + deltaPriority;
36  return dResult;
37 }
38 
40 {
41  // Sanity checks off by default for performance, because otherwise
42  // accepting transactions becomes O(N^2) where N is the number
43  // of transactions in the pool
44  fSanityCheck = false;
45 }
46 
47 void CTxMemPool::pruneSpent(const uint256 &hashTx, CCoins &coins)
48 {
49  LOCK(cs);
50 
51  std::map<COutPoint, CInPoint>::iterator it = mapNextTx.lower_bound(COutPoint(hashTx, 0));
52 
53  // iterate over all COutPoints in mapNextTx whose hash equals the provided hashTx
54  while (it != mapNextTx.end() && it->first.hash == hashTx) {
55  coins.Spend(it->first.n); // and remove those outputs from coins
56  it++;
57  }
58 }
59 
61 {
62  LOCK(cs);
63  return nTransactionsUpdated;
64 }
65 
67 {
68  LOCK(cs);
70 }
71 
72 
73 bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry)
74 {
75  // Add to memory pool without checking anything.
76  // Used by main.cpp AcceptToMemoryPool(), which DOES do
77  // all the appropriate checks.
78  LOCK(cs);
79  {
80  mapTx[hash] = entry;
81  const CTransaction& tx = mapTx[hash].GetTx();
82  for (unsigned int i = 0; i < tx.vin.size(); i++)
83  mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
85  }
86  return true;
87 }
88 
89 
90 void CTxMemPool::remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive)
91 {
92  // Remove transaction from memory pool
93  {
94  LOCK(cs);
95  uint256 hash = tx.GetHash();
96  if (fRecursive) {
97  for (unsigned int i = 0; i < tx.vout.size(); i++) {
98  std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(COutPoint(hash, i));
99  if (it == mapNextTx.end())
100  continue;
101  remove(*it->second.ptx, removed, true);
102  }
103  }
104  if (mapTx.count(hash))
105  {
106  removed.push_front(tx);
107  BOOST_FOREACH(const CTxIn& txin, tx.vin)
108  mapNextTx.erase(txin.prevout);
109  mapTx.erase(hash);
111  }
112  }
113 }
114 
115 void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed)
116 {
117  // Remove transactions which depend on inputs of tx, recursively
118  list<CTransaction> result;
119  LOCK(cs);
120  BOOST_FOREACH(const CTxIn &txin, tx.vin) {
121  std::map<COutPoint, CInPoint>::iterator it = mapNextTx.find(txin.prevout);
122  if (it != mapNextTx.end()) {
123  const CTransaction &txConflict = *it->second.ptx;
124  if (txConflict != tx)
125  {
126  remove(txConflict, removed, true);
127  }
128  }
129  }
130 }
131 
133 {
134  LOCK(cs);
135  mapTx.clear();
136  mapNextTx.clear();
138 }
139 
141 {
142  if (!fSanityCheck)
143  return;
144 
145  LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
146 
147  LOCK(cs);
148  for (std::map<uint256, CTxMemPoolEntry>::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
149  unsigned int i = 0;
150  const CTransaction& tx = it->second.GetTx();
151  BOOST_FOREACH(const CTxIn &txin, tx.vin) {
152  // Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
153  std::map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(txin.prevout.hash);
154  if (it2 != mapTx.end()) {
155  const CTransaction& tx2 = it2->second.GetTx();
156  assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
157  } else {
158  CCoins &coins = pcoins->GetCoins(txin.prevout.hash);
159  assert(coins.IsAvailable(txin.prevout.n));
160  }
161  // Check whether its inputs are marked in mapNextTx.
162  std::map<COutPoint, CInPoint>::const_iterator it3 = mapNextTx.find(txin.prevout);
163  assert(it3 != mapNextTx.end());
164  assert(it3->second.ptx == &tx);
165  assert(it3->second.n == i);
166  i++;
167  }
168  }
169  for (std::map<COutPoint, CInPoint>::const_iterator it = mapNextTx.begin(); it != mapNextTx.end(); it++) {
170  uint256 hash = it->second.ptx->GetHash();
171  map<uint256, CTxMemPoolEntry>::const_iterator it2 = mapTx.find(hash);
172  const CTransaction& tx = it2->second.GetTx();
173  assert(it2 != mapTx.end());
174  assert(&tx == it->second.ptx);
175  assert(tx.vin.size() > it->second.n);
176  assert(it->first == it->second.ptx->vin[it->second.n].prevout);
177  }
178 }
179 
180 void CTxMemPool::queryHashes(vector<uint256>& vtxid)
181 {
182  vtxid.clear();
183 
184  LOCK(cs);
185  vtxid.reserve(mapTx.size());
186  for (map<uint256, CTxMemPoolEntry>::iterator mi = mapTx.begin(); mi != mapTx.end(); ++mi)
187  vtxid.push_back((*mi).first);
188 }
189 
190 bool CTxMemPool::lookup(uint256 hash, CTransaction& result) const
191 {
192  LOCK(cs);
193  map<uint256, CTxMemPoolEntry>::const_iterator i = mapTx.find(hash);
194  if (i == mapTx.end()) return false;
195  result = i->second.GetTx();
196  return true;
197 }
198 
200 
201 bool CCoinsViewMemPool::GetCoins(const uint256 &txid, CCoins &coins) {
202  if (base->GetCoins(txid, coins))
203  return true;
204  CTransaction tx;
205  if (mempool.lookup(txid, tx)) {
206  coins = CCoins(tx, MEMPOOL_HEIGHT);
207  return true;
208  }
209  return false;
210 }
211 
213  return mempool.exists(txid) || base->HaveCoins(txid);
214 }
215 
int64_t GetValueOut() const
Definition: core.cpp:110
void check(CCoinsViewCache *pcoins) const
Definition: txmempool.cpp:140
bool HaveCoins(const uint256 &txid)
Definition: txmempool.cpp:212
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 clear()
Definition: txmempool.cpp:132
uint256 GetHash() const
Definition: core.cpp:76
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
unsigned int n
Definition: core.h:28
CTransaction tx
Definition: txmempool.h:24
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:75
bool IsAvailable(unsigned int nPos) const
Definition: coins.h:229
Definition: txmempool.h:21
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:60
unsigned int GetSerializeSize(char a, int, int=0)
Definition: serialize.h:114
Abstract view on the open txout dataset.
Definition: coins.h:259
int64_t nFee
Definition: txmempool.h:25
An input of a transaction.
Definition: core.h:72
virtual bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: coins.cpp:54
#define LOCK(cs)
Definition: sync.h:157
CCoinsView * base
Definition: coins.h:293
std::vector< CTxOut > vout
Definition: core.h:187
bool GetCoins(const uint256 &txid, CCoins &coins)
Definition: txmempool.cpp:201
std::vector< CTxIn > vin
Definition: core.h:186
bool fSanityCheck
Definition: txmempool.h:58
double GetPriority(unsigned int currentHeight) const
Definition: txmempool.cpp:31
virtual bool HaveCoins(const uint256 &txid)
Definition: coins.cpp:56
std::map< uint256, CTxMemPoolEntry > mapTx
Definition: txmempool.h:63
size_t nTxSize
Definition: txmempool.h:26
unsigned int nTransactionsUpdated
Definition: txmempool.h:59
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: core.h:24
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:66
void remove(const CTransaction &tx, std::list< CTransaction > &removed, bool fRecursive=false)
Definition: txmempool.cpp:90
CCriticalSection cs
Definition: txmempool.h:62
CTxMemPool mempool
Definition: main.cpp:40
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
bool Spend(const COutPoint &out, CTxInUndo &undo)
Definition: coins.cpp:31
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry)
Definition: txmempool.cpp:73
bool exists(uint256 hash)
Definition: txmempool.h:92
An inpoint - a combination of a transaction and an index n into its vin.
Definition: core.h:56
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
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:309
COutPoint prevout
Definition: core.h:75
CTxMemPool & mempool
Definition: txmempool.h:106
uint256 hash
Definition: core.h:27