1 // Copyright (c) 2016 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "policy/rbf.h"
6 
SignalsOptInRBF(const CTransaction & tx)7 bool SignalsOptInRBF(const CTransaction &tx)
8 {
9     BOOST_FOREACH(const CTxIn &txin, tx.vin) {
10         if (txin.nSequence < std::numeric_limits<unsigned int>::max()-1) {
11             return true;
12         }
13     }
14     return false;
15 }
16 
IsRBFOptIn(const CTransaction & tx,CTxMemPool & pool)17 RBFTransactionState IsRBFOptIn(const CTransaction &tx, CTxMemPool &pool)
18 {
19     AssertLockHeld(pool.cs);
20 
21     CTxMemPool::setEntries setAncestors;
22 
23     // First check the transaction itself.
24     if (SignalsOptInRBF(tx)) {
25         return RBF_TRANSACTIONSTATE_REPLACEABLE_BIP125;
26     }
27 
28     // If this transaction is not in our mempool, then we can't be sure
29     // we will know about all its inputs.
30     if (!pool.exists(tx.GetHash())) {
31         return RBF_TRANSACTIONSTATE_UNKNOWN;
32     }
33 
34     // If all the inputs have nSequence >= maxint-1, it still might be
35     // signaled for RBF if any unconfirmed parents have signaled.
36     uint64_t noLimit = std::numeric_limits<uint64_t>::max();
37     std::string dummy;
38     CTxMemPoolEntry entry = *pool.mapTx.find(tx.GetHash());
39     pool.CalculateMemPoolAncestors(entry, setAncestors, noLimit, noLimit, noLimit, noLimit, dummy, false);
40 
41     BOOST_FOREACH(CTxMemPool::txiter it, setAncestors) {
42         if (SignalsOptInRBF(it->GetTx())) {
43             return RBF_TRANSACTIONSTATE_REPLACEABLE_BIP125;
44         }
45     }
46     return RBF_TRANSACTIONSTATE_FINAL;
47 }
48