| 218 | } |
| 219 | |
| 220 | bool CheckSequenceLocks(CBlockIndex* tip, |
| 221 | const CCoinsView& coins_view, |
| 222 | const CTransaction& tx, |
| 223 | int flags, |
| 224 | LockPoints* lp, |
| 225 | bool useExistingLockPoints) |
| 226 | { |
| 227 | assert(tip != nullptr); |
| 228 | |
| 229 | CBlockIndex index; |
| 230 | index.pprev = tip; |
| 231 | // CheckSequenceLocks() uses active_chainstate.m_chain.Height()+1 to evaluate |
| 232 | // height based locks because when SequenceLocks() is called within |
| 233 | // ConnectBlock(), the height of the block *being* |
| 234 | // evaluated is what is used. |
| 235 | // Thus if we want to know if a transaction can be part of the |
| 236 | // *next* block, we need to use one more than active_chainstate.m_chain.Height() |
| 237 | index.nHeight = tip->nHeight + 1; |
| 238 | |
| 239 | std::pair<int, int64_t> lockPair; |
| 240 | if (useExistingLockPoints) { |
| 241 | assert(lp); |
| 242 | lockPair.first = lp->height; |
| 243 | lockPair.second = lp->time; |
| 244 | } |
| 245 | else { |
| 246 | std::vector<int> prevheights; |
| 247 | prevheights.resize(tx.vin.size()); |
| 248 | for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) { |
| 249 | const CTxIn& txin = tx.vin[txinIndex]; |
| 250 | // pegins should not restrict validity of sequence locks |
| 251 | if (txin.m_is_pegin) { |
| 252 | prevheights[txinIndex] = -1; |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | Coin coin; |
| 257 | if (!coins_view.GetCoin(txin.prevout, coin)) { |
| 258 | return error("%s: Missing input", __func__); |
| 259 | } |
| 260 | if (coin.nHeight == MEMPOOL_HEIGHT) { |
| 261 | // Assume all mempool transaction confirm in the next block |
| 262 | prevheights[txinIndex] = tip->nHeight + 1; |
| 263 | } else { |
| 264 | prevheights[txinIndex] = coin.nHeight; |
| 265 | } |
| 266 | } |
| 267 | lockPair = CalculateSequenceLocks(tx, flags, prevheights, index); |
| 268 | if (lp) { |
| 269 | lp->height = lockPair.first; |
| 270 | lp->time = lockPair.second; |
| 271 | // Also store the hash of the block with the highest height of |
| 272 | // all the blocks which have sequence locked prevouts. |
| 273 | // This hash needs to still be on the chain |
| 274 | // for these LockPoint calculations to be valid |
| 275 | // Note: It is impossible to correctly calculate a maxInputBlock |
| 276 | // if any of the sequence locked inputs depend on unconfirmed txs, |
| 277 | // except in the special case where the relative lock time/height |