txmempool.h 5.74 KB
Newer Older
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
3
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5

6
7
8
#ifndef BITCOIN_TXMEMPOOL_H
#define BITCOIN_TXMEMPOOL_H

Gavin Andresen's avatar
Gavin Andresen committed
9
10
#include <list>

11
#include "amount.h"
12
#include "coins.h"
Luke Dashjr's avatar
Luke Dashjr committed
13
#include "primitives/transaction.h"
14
#include "sync.h"
15

16
17
class CAutoFile;

Cozz Lovan's avatar
Cozz Lovan committed
18
19
inline double AllowFreeThreshold()
{
20
    return COIN * 144 / 250;
Cozz Lovan's avatar
Cozz Lovan committed
21
22
}

23
24
25
26
inline bool AllowFree(double dPriority)
{
    // Large (in bytes) low-priority (new, small-coin) transactions
    // need a fee.
Cozz Lovan's avatar
Cozz Lovan committed
27
    return dPriority > AllowFreeThreshold();
28
29
}

30

31
32
33
/** 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;

34
/**
35
36
37
38
39
40
 * CTxMemPool stores these:
 */
class CTxMemPoolEntry
{
private:
    CTransaction tx;
41
42
43
44
45
46
    CAmount nFee; //! Cached to avoid expensive parent-transaction lookups
    size_t nTxSize; //! ... and avoid recomputing tx size
    size_t nModSize; //! ... and modified size for priority
    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
47
48

public:
49
    CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
50
51
52
53
54
55
                    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;
56
    CAmount GetFee() const { return nFee; }
57
58
59
60
61
    size_t GetTxSize() const { return nTxSize; }
    int64_t GetTime() const { return nTime; }
    unsigned int GetHeight() const { return nHeight; }
};

62
63
class CMinerPolicyEstimator;

64
65
66
67
68
69
70
71
72
73
74
75
76
/** An inpoint - a combination of a transaction and an index n into its vin */
class CInPoint
{
public:
    const CTransaction* ptx;
    uint32_t n;

    CInPoint() { SetNull(); }
    CInPoint(const CTransaction* ptxIn, uint32_t nIn) { ptx = ptxIn; n = nIn; }
    void SetNull() { ptx = NULL; n = (uint32_t) -1; }
    bool IsNull() const { return (ptx == NULL && n == (uint32_t) -1); }
};

77
/**
78
79
80
81
82
83
84
85
86
87
88
89
 * 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:
90
    bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
91
    unsigned int nTransactionsUpdated;
92
    CMinerPolicyEstimator* minerPolicyEstimator;
93

94
95
    CFeeRate minRelayFee; //! Passed to constructor to avoid dependency on main
    uint64_t totalTxSize; //! sum of all mempool tx' byte sizes
96
97
98

public:
    mutable CCriticalSection cs;
99
    std::map<uint256, CTxMemPoolEntry> mapTx;
100
    std::map<COutPoint, CInPoint> mapNextTx;
101
    std::map<uint256, std::pair<double, CAmount> > mapDeltas;
102

Gavin Andresen's avatar
Gavin Andresen committed
103
    CTxMemPool(const CFeeRate& _minRelayFee);
104
    ~CTxMemPool();
105

106
    /**
107
108
109
110
111
     * 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.
     */
112
    void check(const CCoinsViewCache *pcoins) const;
113
114
    void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }

115
    bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
Gavin Andresen's avatar
Gavin Andresen committed
116
    void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
117
    void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
Gavin Andresen's avatar
Gavin Andresen committed
118
    void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
119
120
    void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
                        std::list<CTransaction>& conflicts);
121
122
123
124
125
126
    void clear();
    void queryHashes(std::vector<uint256>& vtxid);
    void pruneSpent(const uint256& hash, CCoins &coins);
    unsigned int GetTransactionsUpdated() const;
    void AddTransactionsUpdated(unsigned int n);

127
    /** Affect CreateNewBlock prioritisation of transactions */
128
129
    void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, const CAmount& nFeeDelta);
    void ApplyDeltas(const uint256 hash, double &dPriorityDelta, CAmount &nFeeDelta);
130
131
    void ClearPrioritisation(const uint256 hash);

132
133
134
135
136
    unsigned long size()
    {
        LOCK(cs);
        return mapTx.size();
    }
137
138
139
140
141
    uint64_t GetTotalTxSize()
    {
        LOCK(cs);
        return totalTxSize;
    }
142
143
144
145
146
147
148
149

    bool exists(uint256 hash)
    {
        LOCK(cs);
        return (mapTx.count(hash) != 0);
    }

    bool lookup(uint256 hash, CTransaction& result) const;
150

151
    /** Estimate fee rate needed to get into the next nBlocks */
152
    CFeeRate estimateFee(int nBlocks) const;
153
154

    /** Estimate priority needed to get into the next nBlocks */
155
    double estimatePriority(int nBlocks) const;
156
157
    
    /** Write/Read estimates to disk */
158
159
    bool WriteFeeEstimates(CAutoFile& fileout) const;
    bool ReadFeeEstimates(CAutoFile& filein);
160
161
};

162
163
164
165
/** 
 * CCoinsView that brings transactions from a memorypool into view.
 * It does not check for spendings by memory pool transactions.
 */
166
167
168
169
170
171
class CCoinsViewMemPool : public CCoinsViewBacked
{
protected:
    CTxMemPool &mempool;

public:
172
    CCoinsViewMemPool(CCoinsView *baseIn, CTxMemPool &mempoolIn);
173
174
    bool GetCoins(const uint256 &txid, CCoins &coins) const;
    bool HaveCoins(const uint256 &txid) const;
175
176
};

177
#endif // BITCOIN_TXMEMPOOL_H