Skip to content

Test coverage of our networking code #14210

Open

Description

@sdaftuar

Our python functional testing framework is pretty limited for what kinds of p2p behaviors we can test. Basically, we can currently only make manual connections between bitcoind nodes (using the addnode rpc), which are treated differently in our code than outbound peers selected using addrman.

While we do have some unit-testing coverage of some of the components (like addrman, and parts of net_processing), I don't believe we currently are able to test the overall logic of how bitcoind uses those components (I recall this coming up when working on #11560, as a specific example).

Anyway I am just mentioning this here as a potential project idea, as this is a material gap in our testing that I think would be valuable to work towards improving, and I wasn't sure how well known this is.

Activity

  1. maflcko commented on Sep 12, 2018

    @maflcko
    Member

    Wouldn't it be possible to feed to node some addresses of python TestNodes (either via p2p or by putting them into the address database file when setting up the node).

    I haven't given it a try yet, but I'd be surprised if testing outbound connections was conceptually a hard problem to solve.

  2. sdaftuar commented on Sep 12, 2018

    @sdaftuar
    MemberAuthor

    I don't remember the entirety of the rabbit hole I went down trying to make this work, but I believe at least one of the issues I encountered had to do with special handling for IsLocal() addresses in our net code.

    One thing that occurs to me is that we have a couple options, we could try to rewrite our code to make it easier to test, or we could come up with a new testing environment that is able to exercise the logic. For instance maybe launching a test that fires up nodes on different hosts (whether a dedicated cluster or some containerized thing), with some appropriate IP address assignment, would do the trick.

  3. sdaftuar commented on Mar 1, 2019

    @sdaftuar
    MemberAuthor

    In case anyone is thinking about this -- I believe #15486 is another untestable code change in our current framework.

  4. Sjors commented on Mar 2, 2019

    @Sjors
    Member

    For background on what we should test, the paper by @EthanHeilman e.a. explains how the P2P networking works and the kinds of eclipse attack defences that were recently added:
    https://eprint.iacr.org/2015/263.pdf

    In addition to the problem @sdaftuar points to about the difference between manually adding nodes with addnode and spontaneous connections, there's two other problems.

    1. The regtest nodes are able to connect to each other via P2P on the same IP but with different ports. That's insufficient for thoroughly testing the behavior because net.cpp checks IsLocal, and because the connection buckets are IP based. There's also no DNS seed for regtest.

    2. The networking code relies on real time

    One approach could be a combination of regtest specific tweaks to the networking code and a few regtest specific RPC calls. The former is always scary, the latter probably fine.

    Regtest specific networking tweaks

    The test framework allows 5000 (PORT_RANGE) distinct port numbers. We could divide those in groups of 1000 to represent /16 IPv4 IPv4 ranges. Modulo 100 represents the IP address. That leaves 10 nodes max that a regtest node would still consider being local.

    The cryptographic hash for bucket selection would be hard coded for regtest.

    The DNS resolver code could have a regtest branch that fetches a fake result from a file (which tests can populate).

    Add SetTime() to set the time which GetTime() can read.

    Switch the DNS 11 second delay from sleep_for(std::chrono::seconds(11) to checking SetAdjusttedTime every 0.01 seconds until 11 "seconds" have passed.

    Ditto for CConnman::ThreadOpenConnections; instead of the thread sleeping std::chrono::milliseconds(500) it could sleep much shorter and check the "time".

    Regtest RPC calls

    • settime calls SetAdjusttedTime()
    • methods to get the various peer lists
  5. EthanHeilman commented on Mar 4, 2019

    @EthanHeilman
    Contributor

    I want to second this. Better test coverage and a network level mocking framework for testing Bitcoin's P2P network is something that would be immensely helpful. I started to work on it in 2015 as part of the eclipse attack paper but unfortunately I ran out of time to build it myself and no longer have the bandwidth.

  6. maflcko commented on Mar 4, 2019

    @maflcko
    Member

    Obviously I am lacking the background, but how hard would it be to copy a python-mininode and a python-regtest-dns-seeder to a couple of 1 EUR/month vps boxes?

  7. EthanHeilman commented on Mar 4, 2019

    @EthanHeilman
    Contributor

    Obviously whoever writes the code and does the work gets to decide, but my personal preference would be a way of replacing the network interface with a faked out network interface so that:

    1. A test could spin up a bunch of nodes,
    2. assign them fake IP addresses,
    3. and let them talk to each other without depending on any external systems or touching any network.

    It would allow mostly deterministic tests of all sorts of complex behavior and network failures.

  8. ariard commented on Mar 14, 2019

    @ariard
    Contributor

    Is there already someone working on this ?

    @EthanHeilman by faking out a network interface you mean something like swapping the system-level socket interface by one of our own, which would talk to MockNetworkRouter with a set of methods on this one to simulate network failures, configure topologies, broadcast random messages (or something lighter)?
    Obviously, would gladly take inputs from anyone !

  9. dongcarl commented on Mar 14, 2019

    @dongcarl
    Contributor

    I've chatted with @theuni about this a bit, and it would seem that getting CConnman to be self contained (as in, no reference to globals from within CConnman and removing the globals inside net.cpp) would allow us to achieve what @EthanHeilman is proposing.

    I would like to know what people's thoughts are on:

    1. @EthanHeilman's proposal above
    2. What's missing aside from getting CConnman to be self-contained

    If people think this is worthwhile to move ahead on, I believe it would be a great target to motivate and more importantly focus incremental net improvements.

  10. EthanHeilman commented on Mar 14, 2019

    @EthanHeilman
    Contributor

    @EthanHeilman Yes, I mean replacing the socket function with an interface with the same functions but that actually just routes between the local nodes. You could override the MockNetworkRouter to simulate botnet attacks, package failures, or really old nodes.

  11. dongcarl commented on Mar 14, 2019

    @dongcarl
    Contributor

    Upon further inspection, we might be able to use something like network namespaces thru pyroute2

  12. EthanHeilman commented on Mar 15, 2019

    @EthanHeilman
    Contributor

    I love to be able to do this in unittests for net.cpp. In those environments the MockNetworkRouter would just be a shared object injected into the components that would be talking to each other. Not saying this is the best way, but just the way I've always thought about it.

  13. ariard commented on Mar 15, 2019

    @ariard
    Contributor

    @dongcarl I've more digged into MockNetworkRouter way, if we use pyroute2 to attach fake-addresses to node instances each embbeded in its network namespace, are we gonna be able to simulate easily as much events (packets failures, ...) than with our crafted router ?

  14. 29 remaining items

  15. added a commit that references this issue on Feb 20, 2023
  16. added a commit that references this issue on Feb 28, 2023
  17. added a commit that references this issue on Jul 19, 2026
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