| 411 | } |
| 412 | |
| 413 | void TaprootBuilder::Insert(TaprootBuilder::NodeInfo&& node, int depth) |
| 414 | { |
| 415 | assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT); |
| 416 | /* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing |
| 417 | * so would mean the Add() invocations do not correspond to a DFS traversal of a |
| 418 | * binary tree. */ |
| 419 | if ((size_t)depth + 1 < m_branch.size()) { |
| 420 | m_valid = false; |
| 421 | return; |
| 422 | } |
| 423 | /* As long as an entry in the branch exists at the specified depth, combine it and propagate up. |
| 424 | * The 'node' variable is overwritten here with the newly combined node. */ |
| 425 | while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) { |
| 426 | node = Combine(std::move(node), std::move(*m_branch[depth])); |
| 427 | m_branch.pop_back(); |
| 428 | if (depth == 0) m_valid = false; /* Can't propagate further up than the root */ |
| 429 | --depth; |
| 430 | } |
| 431 | if (m_valid) { |
| 432 | /* Make sure the branch is big enough to place the new node. */ |
| 433 | if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1); |
| 434 | assert(!m_branch[depth].has_value()); |
| 435 | m_branch[depth] = std::move(node); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths) |
| 440 | { |