MCPcopy Create free account
hub / github.com/ElementsProject/elements / PromoteCandidateReady

Method PromoteCandidateReady

src/txrequest.cpp:400–425  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

GetStateMethod · 0.80
SetStateMethod · 0.80
endMethod · 0.45

Tested by

no test coverage detected