| 170 | } |
| 171 | |
| 172 | bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints) |
| 173 | { |
| 174 | AssertLockHeld(cs_main); |
| 175 | AssertLockHeld(mempool.cs); |
| 176 | |
| 177 | CBlockIndex* tip = chainActive.Tip(); |
| 178 | if(!tip) return false; |
| 179 | |
| 180 | CBlockIndex index; |
| 181 | index.pprev = tip; |
| 182 | // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate |
| 183 | // height based locks because when SequenceLocks() is called within |
| 184 | // ConnectBlock(), the height of the block *being* |
| 185 | // evaluated is what is used. |
| 186 | // Thus if we want to know if a transaction can be part of the |
| 187 | // *next* block, we need to use one more than chainActive.Height() |
| 188 | index.nHeight = tip->nHeight + 1; |
| 189 | |
| 190 | std::pair<int, int64_t> lockPair; |
| 191 | if (useExistingLockPoints) { |
| 192 | assert(lp); |
| 193 | lockPair.first = lp->height; |
| 194 | lockPair.second = lp->time; |
| 195 | } |
| 196 | else { |
| 197 | // pcoinsTip contains the UTXO set for chainActive.Tip() |
| 198 | CCoinsViewMemPool viewMemPool(pcoinsTip, mempool); |
| 199 | std::vector<int> prevheights; |
| 200 | prevheights.resize(tx.vin.size()); |
| 201 | for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) { |
| 202 | const CTxIn& txin = tx.vin[txinIndex]; |
| 203 | CCoins coin; |
| 204 | if (!viewMemPool.GetCoin(txin.prevout, coin)) { |
| 205 | return error("%s: Missing input", __func__); |
| 206 | } |
| 207 | if (coin.nHeight == MEMPOOL_HEIGHT) { |
| 208 | // Assume all mempool transaction confirm in the next block |
| 209 | prevheights[txinIndex] = tip->nHeight + 1; |
| 210 | } else { |
| 211 | prevheights[txinIndex] = coin.nHeight; |
| 212 | } |
| 213 | } |
| 214 | lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index); |
| 215 | if (lp) { |
| 216 | lp->height = lockPair.first; |
| 217 | lp->time = lockPair.second; |
| 218 | // Also store the hash of the block with the highest height of |
| 219 | // all the blocks which have sequence locked prevouts. |
| 220 | // This hash needs to still be on the chain |
| 221 | // for these LockPoint calculations to be valid |
| 222 | // Note: It is impossible to correctly calculate a maxInputBlock |
| 223 | // if any of the sequence locked inputs depend on unconfirmed txs, |
| 224 | // except in the special case where the relative lock time/height |
| 225 | // is 0, which is equivalent to no sequence lock. Since we assume |
| 226 | // input height of tip+1 for mempool txs and test the resulting |
| 227 | // lockPair from CalculateSequenceLocks against tip+1. We know |
| 228 | // EvaluateSequenceLocks will fail if there was a non-zero sequence |
| 229 | // lock on a mempool input, so we can use the return value of |