txmempool.h 4.9 KB
Newer Older
1
2
3
4
5
6
7
// 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

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

10
#include "coins.h"
11
#include "core.h"
12
#include "sync.h"
13

14
15
16
17
18
19
20
inline bool AllowFree(double dPriority)
{
    // Large (in bytes) low-priority (new, small-coin) transactions
    // need a fee.
    return dPriority > COIN * 144 / 250;
}

21
22
23
/** 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;

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * 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; }
};

51
52
class CMinerPolicyEstimator;

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * 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;
68
    CMinerPolicyEstimator* minerPolicyEstimator;
69

Gavin Andresen's avatar
Gavin Andresen committed
70
71
    CFeeRate minRelayFee; // Passed to constructor to avoid dependency on main

72
73
public:
    mutable CCriticalSection cs;
74
    std::map<uint256, CTxMemPoolEntry> mapTx;
75
    std::map<COutPoint, CInPoint> mapNextTx;
76
    std::map<uint256, std::pair<double, int64_t> > mapDeltas;
77

Gavin Andresen's avatar
Gavin Andresen committed
78
    CTxMemPool(const CFeeRate& _minRelayFee);
79
    ~CTxMemPool();
80
81
82
83
84
85
86

    /*
     * 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.
     */
87
    void check(CCoinsViewCache *pcoins) const;
88
89
    void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }

90
    bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry);
Gavin Andresen's avatar
Gavin Andresen committed
91
92
    void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
    void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
93
94
    void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
                        std::list<CTransaction>& conflicts);
95
96
97
98
99
100
    void clear();
    void queryHashes(std::vector<uint256>& vtxid);
    void pruneSpent(const uint256& hash, CCoins &coins);
    unsigned int GetTransactionsUpdated() const;
    void AddTransactionsUpdated(unsigned int n);

101
102
103
104
105
    /** Affect CreateNewBlock prioritisation of transactions */
    void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, int64_t nFeeDelta);
    void ApplyDeltas(const uint256 hash, double &dPriorityDelta, int64_t &nFeeDelta);
    void ClearPrioritisation(const uint256 hash);

106
107
108
109
110
111
112
113
114
115
116
117
118
    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;
119
120
121
122
123
124
125
126
127
128

    // Estimate fee rate needed to get into the next
    // nBlocks
    CFeeRate estimateFee(int nBlocks) const;
    // Estimate priority needed to get into the next
    // nBlocks
    double estimatePriority(int nBlocks) const;
    // Write/Read estimates to disk
    bool WriteFeeEstimates(CAutoFile& fileout) const;
    bool ReadFeeEstimates(CAutoFile& filein);
129
130
};

131
132
133
134
135
136
137
138
139
140
141
142
143
/** 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);
};

144
#endif /* BITCOIN_TXMEMPOOL_H */