Skip to content

Connection eviction logic (AttemptToEvictConnection) is not covered by our test suite #16660

Closed

Description

@practicalswift

From what I can tell the connection eviction logic is not covered by our test suite (functional and unit tests).

More specifically the following is never executed with nInbound >= nMaxInbound:

bitcoin/src/net.cpp

Lines 962 to 970 in ed9a2a3

if (nInbound >= nMaxInbound)
{
if (!AttemptToEvictConnection()) {
// No connection to evict, disconnect the new connection
LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
CloseSocket(hSocket);
return;
}
}

Context:

bitcoin/src/net.cpp

Lines 800 to 808 in ed9a2a3

/** Try to find a connection to evict when the node is full.
* Extreme care must be taken to avoid opening the node to attacker
* triggered network partitioning.
* The strategy used here is to protect a small number of peers
* for each of several distinct characteristics which are difficult
* to forge. In order to partition a node the attacker must be
* simultaneously better at all of them than honest peers.
*/
bool CConnman::AttemptToEvictConnection()

A test would be nice here :-)

Due to AttemptToEvictConnection not being tested the following net.cpp functions -- which are only called directly or indirectly via AttemptToEvictConnection -- are currently untested:

static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
static bool CompareNetGroupKeyed(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
static bool CompareNodeBlockTime(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
static bool CompareNodeTXTime(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
static void EraseLastKElements(std::vector<T>& elements, Comparator comparator, size_t k)

Activity

  1. maflcko commented on Aug 21, 2019

    @maflcko
    Member

    Closing as duplicate of #14210

  2. practicalswift commented on Aug 21, 2019

    @practicalswift
    ContributorAuthor

    @MarcoFalke

    Do you know if we have any tests for the eviction logic? Perhaps my measurements are incorrect.

    AFAICT the eviction logic is by far the most critical code path not covered at all by any test. I mean "at all" in the most literal sense: the code path it isn't even entered (if my measurements are correct) when running the full test suite :-)

    I'm not super convinced that this very specific observation/issue is a duplicate of the general observation/issue "we could do better network testing in general" (#14210). Fixing the former can be done quickly - fixing the latter will take quite some time. Don't you think? :-)

  3. maflcko commented on Aug 21, 2019

    @maflcko
    Member

    Fixing #14210 is a requirement for fixing this issue. This issue is also mentioned as motivating example in #14210.

  4. practicalswift commented on Aug 21, 2019

    @practicalswift
    ContributorAuthor

    I can see the dependency on #14210 for functional testing, but unit tests could easily be written to cover these functions AFAICT:

    static bool ReverseCompareNodeMinPingTime(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
    static bool ReverseCompareNodeTimeConnected(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
    static bool CompareNetGroupKeyed(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
    static bool CompareNodeBlockTime(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
    static bool CompareNodeTXTime(const NodeEvictionCandidate& a, const NodeEvictionCandidate& b)
    static void EraseLastKElements(std::vector<T>& elements, Comparator comparator, size_t k)
    

    That would be good even if #14210 takes time to resolve :-)

  5. mzumsande commented on Aug 23, 2019

    @mzumsande
    Contributor

    I think that parts of the eviction logic could be tested in a functional test also prior to #14210 being solved.

    The eviction logic applies to inbound peers and ignores outbound peers - we can simulate inbound connections with P2PInterface, and don't need outbound peers to actually exist.

    I don't see how the logic based on address/netgroup could be tested, but other parts can.
    There is a draft version of a functional test that partially covers the logic of CConnman::AttemptToEvictConnection in mzumsande/bitcoin@56d38d0

    I intend to open a PR based on this in the next days, but for that PR I will also add a commit with unit tests (unless @practicalswift you are working on that?).

  6. practicalswift commented on Aug 23, 2019

    @practicalswift
    ContributorAuthor

    @mzumsande Oh, that's excellent news! Very glad to hear that you're working on this! Thanks!

    […] (unless @practicalswift you are working on that?).

    No, I'm just sharing my observation: I was quite surprised to see that the code eviction code path wasn't exercised at all when running the full test suite given how important it is that we get this logic correct. An error in this code could lead to attacker triggered network partitioning.

    @MarcoFalke Would you mind re-opening this ticket?

  7. practicalswift commented on May 19, 2020

    @practicalswift
    ContributorAuthor

    Closing due to lack of progress/interest :)

  8. jnewbery commented on May 31, 2020

    @jnewbery
    Contributor

    Closing due to lack of progress/interest :)

    #16756 has received some good review over the last two weeks, and we're covering it in review club next week (https://bitcoincore.reviews/16756.html)

    I don't think we need to re-open this issue, but it's certainly not true that there isn't progress/interest in this :)

  9. practicalswift commented on May 31, 2020

    @practicalswift
    ContributorAuthor

    Great news! :)

  10. 15 remaining items

  11. added a commit that references this issue on Feb 20, 2023
    0de5f70
  12. added a commit that references this issue on Feb 28, 2023
    434420f
  13. added a commit that references this issue on Jul 19, 2026
    1fd85cd
  14. added a commit that references this issue on Aug 30, 2026
    332b2df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions