PRCYCoin  2.0.0.7rc1
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef BITCOIN_TXMEMPOOL_H
9 #define BITCOIN_TXMEMPOOL_H
10 
11 #include <list>
12 
13 #include "amount.h"
14 #include "coins.h"
15 #include "primitives/transaction.h"
16 #include "sync.h"
17 #include "random.h"
18 
19 class CAutoFile;
20 
21 inline double AllowFreeThreshold()
22 {
23  return COIN * 1440 / 250;
24 }
25 
26 inline bool AllowFree(double dPriority)
27 {
28  // Large (in bytes) low-priority (new, small-coin) transactions
29  // need a fee.
30  return dPriority > AllowFreeThreshold();
31 }
32 
33 
35 static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
36 
41 {
42 private:
45  size_t nTxSize;
46  size_t nModSize;
47  int64_t nTime;
48  double dPriority;
49  unsigned int nHeight;
50 
51 public:
52  CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee, int64_t _nTime, double _dPriority, unsigned int _nHeight);
54  CTxMemPoolEntry(const CTxMemPoolEntry& other);
55 
56  const CTransaction& GetTx() const { return this->tx; }
57  double GetPriority(unsigned int currentHeight) const;
58  CAmount GetFee() const { return nFee; }
59  size_t GetTxSize() const { return nTxSize; }
60  int64_t GetTime() const { return nTime; }
61  unsigned int GetHeight() const { return nHeight; }
62 };
63 
65 
67 class CInPoint
68 {
69 public:
70  const CTransaction* ptx;
71  uint32_t n;
72 
73  CInPoint() { SetNull(); }
74  CInPoint(const CTransaction* ptxIn, uint32_t nIn)
75  {
76  ptx = ptxIn;
77  n = nIn;
78  }
79  void SetNull()
80  {
81  ptx = NULL;
82  n = (uint32_t)-1;
83  }
84  bool IsNull() const { return (ptx == NULL && n == (uint32_t)-1); }
85 };
86 
98 {
99 private:
101  unsigned int nTransactionsUpdated;
103 
105  uint64_t totalTxSize;
106 
107 public:
136  std::map<uint256, CTxMemPoolEntry> mapTx;
137  std::map<COutPoint, CInPoint> mapNextTx;
138  std::map<uint256, std::pair<double, CAmount> > mapDeltas;
139 
140  CTxMemPool(const CFeeRate& _minRelayFee);
141  ~CTxMemPool();
142 
149  void check(const CCoinsViewCache* pcoins) const;
150  void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
151 
152  bool addUnchecked(const uint256& hash, const CTxMemPoolEntry& entry);
153  void remove(const CTransaction& tx, std::list<CTransaction>& removed, bool fRecursive = false);
154  void removeCoinbaseSpends(const CCoinsViewCache* pcoins, unsigned int nMemPoolHeight);
155  void removeConflicts(const CTransaction& tx, std::list<CTransaction>& removed);
156  void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight, std::list<CTransaction>& conflicts);
157  void clear();
158  void queryHashes(std::vector<uint256>& vtxid);
159  void pruneSpent(const uint256& hash, CCoins& coins);
160  unsigned int GetTransactionsUpdated() const;
161  void AddTransactionsUpdated(unsigned int n);
162 
164  void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
165  void ApplyDeltas(const uint256 hash, double& dPriorityDelta, CAmount& nFeeDelta);
166  void ClearPrioritisation(const uint256 hash);
167 
168  unsigned long size()
169  {
170  LOCK(cs);
171  return mapTx.size();
172  }
173  uint64_t GetTotalTxSize()
174  {
175  LOCK(cs);
176  return totalTxSize;
177  }
178 
179  bool exists(uint256 hash)
180  {
181  LOCK(cs);
182  return (mapTx.count(hash) != 0);
183  }
184 
185  bool lookup(uint256 hash, CTransaction& result) const;
186 
188  CFeeRate estimateFee(int nBlocks) const;
189 
191  double estimatePriority(int nBlocks) const;
192 
194  bool WriteFeeEstimates(CAutoFile& fileout) const;
195  bool ReadFeeEstimates(CAutoFile& filein);
196 };
197 
203 {
204 protected:
206 
207 public:
208  CCoinsViewMemPool(CCoinsView* baseIn, CTxMemPool& mempoolIn);
209  bool GetCoins(const uint256& txid, CCoins& coins) const;
210  bool HaveCoins(const uint256& txid) const;
211 };
212 
213 #endif // BITCOIN_TXMEMPOOL_H
CTxMemPool::size
unsigned long size()
Definition: txmempool.h:168
CCoinsViewMemPool::GetCoins
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: txmempool.cpp:706
CTxMemPool::AddTransactionsUpdated
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:395
CTxMemPool::pruneSpent
void pruneSpent(const uint256 &hash, CCoins &coins)
Definition: txmempool.cpp:376
CInPoint::IsNull
bool IsNull() const
Definition: txmempool.h:84
CTxMemPoolEntry::GetHeight
unsigned int GetHeight() const
Definition: txmempool.h:61
transaction.h
CTxMemPool::removeCoinbaseSpends
void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
Definition: txmempool.cpp:468
CTxMemPoolEntry::GetTime
int64_t GetTime() const
Definition: txmempool.h:60
CTxMemPool::minRelayFee
CFeeRate minRelayFee
Definition: txmempool.h:104
AllowFree
bool AllowFree(double dPriority)
Definition: txmempool.h:26
sync.h
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:97
CTxMemPool::exists
bool exists(uint256 hash)
Definition: txmempool.h:179
CTxMemPool::ReadFeeEstimates
bool ReadFeeEstimates(CAutoFile &filein)
Definition: txmempool.cpp:658
AnnotatedMixin< std::recursive_mutex >
CTxMemPoolEntry::GetFee
CAmount GetFee() const
Definition: txmempool.h:58
CTxMemPool::GetTransactionsUpdated
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:389
CTxMemPool::check
void check(const CCoinsViewCache *pcoins) const
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.cpp:540
CInPoint
An inpoint - a combination of a transaction and an index n into its vin.
Definition: txmempool.h:67
CFeeRate
Fee rate in PRCY per kilobyte: CAmount / kB.
Definition: amount.h:39
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:269
CTxMemPool::~CTxMemPool
~CTxMemPool()
Definition: txmempool.cpp:371
CTxMemPoolEntry::nTime
int64_t nTime
... and modified size for priority
Definition: txmempool.h:47
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:346
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:303
random.h
CCoinsViewMemPool::CCoinsViewMemPool
CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn)
Definition: txmempool.cpp:704
CTxMemPoolEntry::dPriority
double dPriority
Local time when entering the mempool.
Definition: txmempool.h:48
CTxMemPool::cs
RecursiveMutex cs
sum of all mempool tx' byte sizes
Definition: txmempool.h:135
CTxMemPool::removeForBlock
void removeForBlock(const std::vector< CTransaction > &vtx, unsigned int nBlockHeight, std::list< CTransaction > &conflicts)
Called when a block is connected.
Definition: txmempool.cpp:512
CTxMemPool::PrioritiseTransaction
void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:675
CTxMemPool::remove
void remove(const CTransaction &tx, std::list< CTransaction > &removed, bool fRecursive=false)
Definition: txmempool.cpp:424
CTxMemPool::setSanityCheck
void setSanityCheck(bool _fSanityCheck)
Definition: txmempool.h:150
CAmount
int64_t CAmount
Amount in PRCY (Can be negative)
Definition: amount.h:17
CCoinsViewMemPool::mempool
CTxMemPool & mempool
Definition: txmempool.h:205
CInPoint::n
uint32_t n
Definition: txmempool.h:71
CTxMemPool::GetTotalTxSize
uint64_t GetTotalTxSize()
Definition: txmempool.h:173
CTxMemPool::lookup
bool lookup(uint256 hash, CTransaction &result) const
Definition: txmempool.cpp:624
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CInPoint::CInPoint
CInPoint()
Definition: txmempool.h:73
CTxMemPool::minerPolicyEstimator
CMinerPolicyEstimator * minerPolicyEstimator
Definition: txmempool.h:102
CTxMemPool::totalTxSize
uint64_t totalTxSize
Passed to constructor to avoid dependency on main.
Definition: txmempool.h:105
AllowFreeThreshold
double AllowFreeThreshold()
Definition: txmempool.h:21
coins.h
CTxMemPoolEntry::tx
CTransaction tx
Definition: txmempool.h:43
CTxMemPoolEntry::nFee
CAmount nFee
Definition: txmempool.h:44
CInPoint::SetNull
void SetNull()
Definition: txmempool.h:79
CTxMemPool::addUnchecked
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry)
Definition: txmempool.cpp:402
CTxMemPool::queryHashes
void queryHashes(std::vector< uint256 > &vtxid)
Definition: txmempool.cpp:614
CTxMemPool::WriteFeeEstimates
bool WriteFeeEstimates(CAutoFile &fileout) const
Write/Read estimates to disk.
Definition: txmempool.cpp:644
CTxMemPool::mapNextTx
std::map< COutPoint, CInPoint > mapNextTx
Definition: txmempool.h:137
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:414
CMinerPolicyEstimator
Definition: txmempool.cpp:148
CCoinsViewBacked
CCoinsView backed by another CCoinsView.
Definition: coins.h:372
CTxMemPoolEntry::nTxSize
size_t nTxSize
Cached to avoid expensive parent-transaction lookups.
Definition: txmempool.h:45
CCoinsViewMemPool::HaveCoins
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
Definition: txmempool.cpp:719
CTxMemPool::CTxMemPool
CTxMemPool(const CFeeRate &_minRelayFee)
Definition: txmempool.cpp:355
CTxMemPoolEntry
CTxMemPool stores these:
Definition: txmempool.h:40
CTxMemPool::mapTx
std::map< uint256, CTxMemPoolEntry > mapTx
Definition: txmempool.h:136
LOCK
#define LOCK(cs)
Definition: sync.h:182
CTxMemPoolEntry::GetPriority
double GetPriority(unsigned int currentHeight) const
Definition: txmempool.cpp:38
CTxMemPool::estimateFee
CFeeRate estimateFee(int nBlocks) const
Estimate fee rate needed to get into the next nBlocks.
Definition: txmempool.cpp:633
CInPoint::CInPoint
CInPoint(const CTransaction *ptxIn, uint32_t nIn)
Definition: txmempool.h:74
CCoinsViewMemPool
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:202
CTxMemPool::ApplyDeltas
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta)
Definition: txmempool.cpp:686
CTxMemPoolEntry::nModSize
size_t nModSize
... and avoid recomputing tx size
Definition: txmempool.h:46
CCoins
Definition: coins.h:77
CTxMemPool::mapDeltas
std::map< uint256, std::pair< double, CAmount > > mapDeltas
Definition: txmempool.h:138
CTxMemPoolEntry::GetTxSize
size_t GetTxSize() const
Definition: txmempool.h:59
CTxMemPool::nTransactionsUpdated
unsigned int nTransactionsUpdated
Normally false, true if -checkmempool or -regtest.
Definition: txmempool.h:101
CTxMemPoolEntry::nHeight
unsigned int nHeight
Priority when entering the mempool.
Definition: txmempool.h:49
CTxMemPool::removeConflicts
void removeConflicts(const CTransaction &tx, std::list< CTransaction > &removed)
Definition: txmempool.cpp:493
CTxMemPoolEntry::CTxMemPoolEntry
CTxMemPoolEntry()
Definition: txmempool.cpp:20
amount.h
CTxMemPool::clear
void clear()
Definition: txmempool.cpp:531
CInPoint::ptx
const CTransaction * ptx
Definition: txmempool.h:70
CTxMemPool::estimatePriority
double estimatePriority(int nBlocks) const
Estimate priority needed to get into the next nBlocks.
Definition: txmempool.cpp:638
CTxMemPool::ClearPrioritisation
void ClearPrioritisation(const uint256 hash)
Definition: txmempool.cpp:697
CTxMemPoolEntry::GetTx
const CTransaction & GetTx() const
Definition: txmempool.h:56
CTxMemPool::fSanityCheck
bool fSanityCheck
Definition: txmempool.h:100