You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by Mara van der Laan, Ethan Heilman and Jon Atack
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.
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.
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.
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.
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".
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.
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?
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:
A test could spin up a bunch of nodes,
assign them fake IP addresses,
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.
@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 !
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:
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.
@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.
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.
@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 ?
Activity
maflcko commentedon Sep 12, 2018
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.
sdaftuar commentedon Sep 12, 2018
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.
sdaftuar commentedon Mar 1, 2019
In case anyone is thinking about this -- I believe #15486 is another untestable code change in our current framework.
Sjors commentedon Mar 2, 2019
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
addnodeand spontaneous connections, there's two other problems.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.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 IPv4IPv4 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 whichGetTime()can read.Switch the DNS 11 second delay from
sleep_for(std::chrono::seconds(11)to checkingSetAdjusttedTimeevery 0.01 seconds until 11 "seconds" have passed.Ditto for
CConnman::ThreadOpenConnections; instead of the thread sleepingstd::chrono::milliseconds(500)it could sleep much shorter and check the "time".Regtest RPC calls
settimecallsSetAdjusttedTime()EthanHeilman commentedon Mar 4, 2019
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.
maflcko commentedon Mar 4, 2019
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?
EthanHeilman commentedon Mar 4, 2019
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:
It would allow mostly deterministic tests of all sorts of complex behavior and network failures.
ariard commentedon Mar 14, 2019
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 !
dongcarl commentedon Mar 14, 2019
I've chatted with @theuni about this a bit, and it would seem that getting
CConnmanto be self contained (as in, no reference to globals from withinCConnmanand removing the globals insidenet.cpp) would allow us to achieve what @EthanHeilman is proposing.I would like to know what people's thoughts are on:
CConnmanto be self-containedIf 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.
EthanHeilman commentedon Mar 14, 2019
@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.
dongcarl commentedon Mar 14, 2019
Upon further inspection, we might be able to use something like network namespaces thru pyroute2
EthanHeilman commentedon Mar 15, 2019
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.
ariard commentedon Mar 15, 2019
@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 ?
29 remaining items
Merge bitcoin#16756: test: Connection eviction logic tests
Merge bitcoin#16756: test: Connection eviction logic tests
Merge #19315: [tests] Allow outbound & block-relay-only connections i…
Merge #19315: [tests] Allow outbound & block-relay-only connections i…
Merge bitcoin#16756: test: Connection eviction logic tests
Merge #19315: [tests] Allow outbound & block-relay-only connections i…
Merge #16756: test: Connection eviction logic tests