refactor(peerfinder): use LogicError in PeerFinder::Logic (#4562)

It might be possible for the server code to indirect through certain
`end()` iterators. While a debug build would catch this problem with
`assert()`s, a release build would crash. If there are problems in this
area in the future, it is best to get a definitive indication of the
nature of the error regardless of whether it's a debug or release build.
To accomplish this, these `assert`s are converted into `LogicError`s
that will produce a reasonable error message when they fire.
This commit is contained in:
Scott Schurr
2023-10-17 22:52:33 -07:00
committed by tequ
parent c1c2a9b3a6
commit 31b61b5b94

View File

@@ -416,10 +416,9 @@ public:
// assert later when erasing the key. // assert later when erasing the key.
slot->public_key(key); slot->public_key(key);
{ {
auto const result = keys_.insert(key); [[maybe_unused]] bool const inserted = keys_.insert(key).second;
// Public key must not already exist // Public key must not already exist
assert(result.second); assert(inserted);
(void)result.second;
} }
// Change state and update counts // Change state and update counts
@@ -434,7 +433,11 @@ public:
if (slot->fixed() && !slot->inbound()) if (slot->fixed() && !slot->inbound())
{ {
auto iter(fixed_.find(slot->remote_endpoint())); auto iter(fixed_.find(slot->remote_endpoint()));
assert(iter != fixed_.end()); if (iter == fixed_.end())
LogicError(
"PeerFinder::Logic::activate(): remote_endpoint "
"missing from fixed_");
iter->second.success(m_clock.now()); iter->second.success(m_clock.now());
JLOG(m_journal.trace()) << beast::leftw(18) << "Logic fixed " JLOG(m_journal.trace()) << beast::leftw(18) << "Logic fixed "
<< slot->remote_endpoint() << " success"; << slot->remote_endpoint() << " success";
@@ -858,7 +861,11 @@ public:
{ {
auto const iter = slots_.find(slot->remote_endpoint()); auto const iter = slots_.find(slot->remote_endpoint());
// The slot must exist in the table // The slot must exist in the table
assert(iter != slots_.end()); if (iter == slots_.end())
LogicError(
"PeerFinder::Logic::remove(): remote_endpoint "
"missing from slots_");
// Remove from slot by IP table // Remove from slot by IP table
slots_.erase(iter); slots_.erase(iter);
} }
@@ -867,7 +874,11 @@ public:
{ {
auto const iter = keys_.find(*slot->public_key()); auto const iter = keys_.find(*slot->public_key());
// Key must exist // Key must exist
assert(iter != keys_.end()); if (iter == keys_.end())
LogicError(
"PeerFinder::Logic::remove(): public_key missing "
"from keys_");
keys_.erase(iter); keys_.erase(iter);
} }
// Remove from connected address table // Remove from connected address table
@@ -875,7 +886,11 @@ public:
auto const iter( auto const iter(
connectedAddresses_.find(slot->remote_endpoint().address())); connectedAddresses_.find(slot->remote_endpoint().address()));
// Address must exist // Address must exist
assert(iter != connectedAddresses_.end()); if (iter == connectedAddresses_.end())
LogicError(
"PeerFinder::Logic::remove(): remote_endpont "
"address missing from connectedAddresses_");
connectedAddresses_.erase(iter); connectedAddresses_.erase(iter);
} }
@@ -894,7 +909,11 @@ public:
if (slot->fixed() && !slot->inbound() && slot->state() != Slot::active) if (slot->fixed() && !slot->inbound() && slot->state() != Slot::active)
{ {
auto iter(fixed_.find(slot->remote_endpoint())); auto iter(fixed_.find(slot->remote_endpoint()));
assert(iter != fixed_.end()); if (iter == fixed_.end())
LogicError(
"PeerFinder::Logic::on_closed(): remote_endpont "
"missing from fixed_");
iter->second.failure(m_clock.now()); iter->second.failure(m_clock.now());
JLOG(m_journal.debug()) << beast::leftw(18) << "Logic fixed " JLOG(m_journal.debug()) << beast::leftw(18) << "Logic fixed "
<< slot->remote_endpoint() << " failed"; << slot->remote_endpoint() << " failed";