| 996 | } |
| 997 | |
| 998 | bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints) |
| 999 | { |
| 1000 | AssertLockHeld(cs_main); |
| 1001 | AssertLockHeld(mempool.cs); |
| 1002 | |
| 1003 | CBlockIndex* tip = chainActive.Tip(); |
| 1004 | CBlockIndex index; |
| 1005 | index.pprev = tip; |
| 1006 | // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate |
| 1007 | // height based locks because when SequenceLocks() is called within |
| 1008 | // ConnectBlock(), the height of the block *being* |
| 1009 | // evaluated is what is used. |
| 1010 | // Thus if we want to know if a transaction can be part of the |
| 1011 | // *next* block, we need to use one more than chainActive.Height() |
| 1012 | index.nHeight = tip->nHeight + 1; |
| 1013 | |
| 1014 | std::pair<int, int64_t> lockPair; |
| 1015 | if (useExistingLockPoints) { |
| 1016 | assert(lp); |
| 1017 | lockPair.first = lp->height; |
| 1018 | lockPair.second = lp->time; |
| 1019 | } |
| 1020 | else { |
| 1021 | // pcoinsTip contains the UTXO set for chainActive.Tip() |
| 1022 | CCoinsViewMemPool viewMemPool(pcoinsTip, mempool); |
| 1023 | std::vector<int> prevheights; |
| 1024 | prevheights.resize(tx.vin.size()); |
| 1025 | for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) { |
| 1026 | const CTxIn& txin = tx.vin[txinIndex]; |
| 1027 | CCoins coins; |
| 1028 | if (!viewMemPool.GetCoins(txin.prevout.hash, coins)) { |
| 1029 | return error("%s: Missing input", __func__); |
| 1030 | } |
| 1031 | if (coins.nHeight == MEMPOOL_HEIGHT) { |
| 1032 | // Assume all mempool transaction confirm in the next block |
| 1033 | prevheights[txinIndex] = tip->nHeight + 1; |
| 1034 | } else { |
| 1035 | prevheights[txinIndex] = coins.nHeight; |
| 1036 | } |
| 1037 | } |
| 1038 | lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index); |
| 1039 | if (lp) { |
| 1040 | lp->height = lockPair.first; |
| 1041 | lp->time = lockPair.second; |
| 1042 | // Also store the hash of the block with the highest height of |
| 1043 | // all the blocks which have sequence locked prevouts. |
| 1044 | // This hash needs to still be on the chain |
| 1045 | // for these LockPoint calculations to be valid |
| 1046 | // Note: It is impossible to correctly calculate a maxInputBlock |
| 1047 | // if any of the sequence locked inputs depend on unconfirmed txs, |
| 1048 | // except in the special case where the relative lock time/height |
| 1049 | // is 0, which is equivalent to no sequence lock. Since we assume |
| 1050 | // input height of tip+1 for mempool txs and test the resulting |
| 1051 | // lockPair from CalculateSequenceLocks against tip+1. We know |
| 1052 | // EvaluateSequenceLocks will fail if there was a non-zero sequence |
| 1053 | // lock on a mempool input, so we can use the return value of |
| 1054 | // CheckSequenceLocks to indicate the LockPoints validity |
| 1055 | int maxInputHeight = 0; |