| 387 | } |
| 388 | |
| 389 | void TaprootBuilder::Insert(TaprootBuilder::NodeInfo&& node, int depth) |
| 390 | { |
| 391 | assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT); |
| 392 | /* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing |
| 393 | * so would mean the Add() invocations do not correspond to a DFS traversal of a |
| 394 | * binary tree. */ |
| 395 | if ((size_t)depth + 1 < m_branch.size()) { |
| 396 | m_valid = false; |
| 397 | return; |
| 398 | } |
| 399 | /* As long as an entry in the branch exists at the specified depth, combine it and propagate up. |
| 400 | * The 'node' variable is overwritten here with the newly combined node. */ |
| 401 | while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) { |
| 402 | node = Combine(std::move(node), std::move(*m_branch[depth])); |
| 403 | m_branch.pop_back(); |
| 404 | if (depth == 0) m_valid = false; /* Can't propagate further up than the root */ |
| 405 | --depth; |
| 406 | } |
| 407 | if (m_valid) { |
| 408 | /* Make sure the branch is big enough to place the new node. */ |
| 409 | if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1); |
| 410 | assert(!m_branch[depth].has_value()); |
| 411 | m_branch[depth] = std::move(node); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths) |
| 416 | { |