2013-08-27 07:51:57 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2013 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#ifndef BITCOIN_TXMEMPOOL_H
|
|
|
|
#define BITCOIN_TXMEMPOOL_H
|
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
#include "coins.h"
|
2013-08-27 07:51:57 +02:00
|
|
|
#include "core.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "sync.h"
|
2013-08-27 07:51:57 +02:00
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
/** Fake height value used in CCoins to signify they are only in the memory pool (since 0.8) */
|
|
|
|
static const unsigned int MEMPOOL_HEIGHT = 0x7FFFFFFF;
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
/*
|
|
|
|
* CTxMemPool stores these:
|
|
|
|
*/
|
|
|
|
class CTxMemPoolEntry
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CTransaction tx;
|
|
|
|
int64_t nFee; // Cached to avoid expensive parent-transaction lookups
|
|
|
|
size_t nTxSize; // ... and avoid recomputing tx size
|
|
|
|
int64_t nTime; // Local time when entering the mempool
|
|
|
|
double dPriority; // Priority when entering the mempool
|
|
|
|
unsigned int nHeight; // Chain height when entering the mempool
|
|
|
|
|
|
|
|
public:
|
|
|
|
CTxMemPoolEntry(const CTransaction& _tx, int64_t _nFee,
|
|
|
|
int64_t _nTime, double _dPriority, unsigned int _nHeight);
|
|
|
|
CTxMemPoolEntry();
|
|
|
|
CTxMemPoolEntry(const CTxMemPoolEntry& other);
|
|
|
|
|
|
|
|
const CTransaction& GetTx() const { return this->tx; }
|
|
|
|
double GetPriority(unsigned int currentHeight) const;
|
|
|
|
int64_t GetFee() const { return nFee; }
|
|
|
|
size_t GetTxSize() const { return nTxSize; }
|
|
|
|
int64_t GetTime() const { return nTime; }
|
|
|
|
unsigned int GetHeight() const { return nHeight; }
|
|
|
|
};
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
/*
|
|
|
|
* CTxMemPool stores valid-according-to-the-current-best-chain
|
|
|
|
* transactions that may be included in the next block.
|
|
|
|
*
|
|
|
|
* Transactions are added when they are seen on the network
|
|
|
|
* (or created by the local node), but not all transactions seen
|
|
|
|
* are added to the pool: if a new transaction double-spends
|
|
|
|
* an input of a transaction in the pool, it is dropped,
|
|
|
|
* as are non-standard transactions.
|
|
|
|
*/
|
|
|
|
class CTxMemPool
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool fSanityCheck; // Normally false, true if -checkmempool or -regtest
|
|
|
|
unsigned int nTransactionsUpdated;
|
|
|
|
|
|
|
|
public:
|
|
|
|
mutable CCriticalSection cs;
|
2013-11-11 08:35:14 +01:00
|
|
|
std::map<uint256, CTxMemPoolEntry> mapTx;
|
2013-08-27 07:51:57 +02:00
|
|
|
std::map<COutPoint, CInPoint> mapNextTx;
|
|
|
|
|
|
|
|
CTxMemPool();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If sanity-checking is turned on, check makes sure the pool is
|
|
|
|
* consistent (does not contain two transactions that spend the same inputs,
|
|
|
|
* all inputs are in the mapNextTx array). If sanity-checking is turned off,
|
|
|
|
* check does nothing.
|
|
|
|
*/
|
2013-11-05 02:47:07 +01:00
|
|
|
void check(CCoinsViewCache *pcoins) const;
|
2013-08-27 07:51:57 +02:00
|
|
|
void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
|
|
|
|
|
2013-11-11 08:35:14 +01:00
|
|
|
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
|
2013-08-27 07:51:57 +02:00
|
|
|
bool remove(const CTransaction &tx, bool fRecursive = false);
|
|
|
|
bool removeConflicts(const CTransaction &tx);
|
|
|
|
void clear();
|
|
|
|
void queryHashes(std::vector<uint256>& vtxid);
|
|
|
|
void pruneSpent(const uint256& hash, CCoins &coins);
|
|
|
|
unsigned int GetTransactionsUpdated() const;
|
|
|
|
void AddTransactionsUpdated(unsigned int n);
|
|
|
|
|
|
|
|
unsigned long size()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return mapTx.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exists(uint256 hash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
return (mapTx.count(hash) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lookup(uint256 hash, CTransaction& result) const;
|
|
|
|
};
|
|
|
|
|
2013-11-05 02:47:07 +01:00
|
|
|
/** CCoinsView that brings transactions from a memorypool into view.
|
|
|
|
It does not check for spendings by memory pool transactions. */
|
|
|
|
class CCoinsViewMemPool : public CCoinsViewBacked
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
CTxMemPool &mempool;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn);
|
|
|
|
bool GetCoins(const uint256 &txid, CCoins &coins);
|
|
|
|
bool HaveCoins(const uint256 &txid);
|
|
|
|
};
|
|
|
|
|
2013-08-27 07:51:57 +02:00
|
|
|
#endif /* BITCOIN_TXMEMPOOL_H */
|