Convert a CANDIDATE_DELAYED announcement into a CANDIDATE_READY. If this makes it the new best CANDIDATE_READY (and no REQUESTED exists) and better than the CANDIDATE_BEST (if any), it becomes the new CANDIDATE_BEST.
| 388 | //! CANDIDATE_READY (and no REQUESTED exists) and better than the CANDIDATE_BEST (if any), it becomes the new |
| 389 | //! CANDIDATE_BEST. |
| 390 | void PromoteCandidateReady(Iter<ByTxHash> it) |
| 391 | { |
| 392 | assert(it != m_index.get<ByTxHash>().end()); |
| 393 | assert(it->GetState() == State::CANDIDATE_DELAYED); |
| 394 | // Convert CANDIDATE_DELAYED to CANDIDATE_READY first. |
| 395 | Modify<ByTxHash>(it, [](Announcement& ann){ ann.SetState(State::CANDIDATE_READY); }); |
| 396 | // The following code relies on the fact that the ByTxHash is sorted by txhash, and then by state (first |
| 397 | // _DELAYED, then _READY, then _BEST/REQUESTED). Within the _READY announcements, the best one (highest |
| 398 | // priority) comes last. Thus, if an existing _BEST exists for the same txhash that this announcement may |
| 399 | // be preferred over, it must immediately follow the newly created _READY. |
| 400 | auto it_next = std::next(it); |
| 401 | if (it_next == m_index.get<ByTxHash>().end() || it_next->m_gtxid.ToUint256() != it->m_gtxid.ToUint256() || |
| 402 | it_next->GetState() == State::COMPLETED) { |
| 403 | // This is the new best CANDIDATE_READY, and there is no IsSelected() announcement for this txhash |
| 404 | // already. |
| 405 | Modify<ByTxHash>(it, [](Announcement& ann){ ann.SetState(State::CANDIDATE_BEST); }); |
| 406 | } else if (it_next->GetState() == State::CANDIDATE_BEST) { |
| 407 | Priority priority_old = m_computer(*it_next); |
| 408 | Priority priority_new = m_computer(*it); |
| 409 | if (priority_new > priority_old) { |
| 410 | // There is a CANDIDATE_BEST announcement already, but this one is better. |
| 411 | Modify<ByTxHash>(it_next, [](Announcement& ann){ ann.SetState(State::CANDIDATE_READY); }); |
| 412 | Modify<ByTxHash>(it, [](Announcement& ann){ ann.SetState(State::CANDIDATE_BEST); }); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | //! Change the state of an announcement to something non-IsSelected(). If it was IsSelected(), the next best |
| 418 | //! announcement will be marked CANDIDATE_BEST. |