Previously, the addrman_tried_collisions test behaved in the following way:
- add an address to addrman
- attempt to move the new address to the tried table (using
AddrMan.Good()) - verify that
num_addrsmatchedsize()to check for collisions in the new table
AddrMan.size(), however, returns the number of unique address in addrman, regardless of whether they are in new or tried. This means the test would still pass for addresses where a collision did occur in the tried table. After 3 collisions in the tried table, there would eventually be a collision in the new table when trying to add a new address, which was then detected by checking num_addrs - collisions == size().
While the collision in the new table was caused by a collision in the tried table, the test is misleading as it's not directly testing for collisions in the tried table and misses 3 collisions before identifying a collision in the new table.
solution
To more directly test the tried table, I refactored AddrMan::Good() to return a boolean after successfully adding an address to the tried table. This makes the test much cleaner by first adding an address to new, calling Good to move it to the tried table, and checking if it was successful or not. It is worth noting there are other reasons, aside from collisions, which will cause Good to return false. That being said, this is an improvement over the previous testing methodology.
Additionally, having Good() return a boolean is useful outside of testing as it allows the caller to handle the case where Good is unable to move the entry to the tried table (e.g https://github.com/bitcoin/bitcoin/blob/a06364741358feae04813050e4225eb43fc386e3/src/rpc/net.cpp#L945).
followup
As a follow up to this PR, I plan to look at the following places Good() is called and see if it makes sense to handle the case where it is unable to add an entry to tried:
- https://github.com/bitcoin/bitcoin/blob/a06364741358feae04813050e4225eb43fc386e3/src/rpc/net.cpp#L945
- https://github.com/bitcoin/bitcoin/blob/a06364741358feae04813050e4225eb43fc386e3/src/net.cpp#L2067
- https://github.com/bitcoin/bitcoin/blob/a06364741358feae04813050e4225eb43fc386e3/src/net_processing.cpp#L2708