Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Fix failing Discovery tests #5581

Merged
merged 5 commits into from
Apr 30, 2019
Merged

Fix failing Discovery tests #5581

merged 5 commits into from
Apr 30, 2019

Conversation

gumb0
Copy link
Member

@gumb0 gumb0 commented Apr 29, 2019

Fixes network/net tests failures mentioned in #5544

The reason for some of the network test failires was incorrect implementation of NodeTable::nearestNodeEntries method (finding the nodes from the node table closest to some target node)

The old implementation tried to utilize the ordering of the nodes in the node table (they are ordered in buckets by distance to the host node) to efficiently find the nodes closest to target. It first looked for the bucket where the target node would fall, and then assumed the nodes in that bucket and around would be close to the target. However I don't think the idea was completely correct, and the implementation definitely was buggy and difficult to understand.

I ended up rewriting it in another, I think simpler, manner using std::multiset ordered by the distance to target and keeping only 16 closest nodes.

Go-ethereum uses the similar apporach of iterating over all nodes and putting them into a data structure which keeps only 16 nodes closest to target.
https://github.com/ethereum/go-ethereum/blob/4c90efdf57ce87edf0d855c8cec10525875a6ab1/p2p/discover/table.go#L521-L536
https://github.com/ethereum/go-ethereum/blob/4c90efdf57ce87edf0d855c8cec10525875a6ab1/p2p/discover/table.go#L757-L781

Parity uses another, much trickier (and presumably more efficient) approach, but I decided not to complicate it like this. Also it's very different from our original approach.
https://github.com/paritytech/parity-ethereum/blob/b30b54e446981dc57835b74d550b40b36385742a/util/network-devp2p/src/discovery.rs#L422-L471

@codecov-io
Copy link

codecov-io commented Apr 29, 2019

Codecov Report

Merging #5581 into master will increase coverage by 0.07%.
The diff coverage is 96.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5581      +/-   ##
==========================================
+ Coverage   62.15%   62.22%   +0.07%     
==========================================
  Files         347      347              
  Lines       29110    29136      +26     
  Branches     3303     3295       -8     
==========================================
+ Hits        18092    18130      +38     
+ Misses       9837     9826      -11     
+ Partials     1181     1180       -1

@gumb0 gumb0 force-pushed the fix-nearestnodes branch from 1aae456 to 3cfb4fe Compare April 30, 2019 14:40
@gumb0
Copy link
Member Author

gumb0 commented Apr 30, 2019

Also first I tried another approach using std::partial_sort 85fe2a4 but it required additional vector with all nodes, I decided it's too much of a memory footprint.

for (auto const& nodeWeakPtr : bucket.nodes)
if (auto node = nodeWeakPtr.lock())
{
nodesByDistanceToTarget.emplace(distance(_target, node->id()), node);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently very inefficient, because it calculates sha3(m_hostNodeID) many times in the loop and sha3(node->id()) every time nearestNodeEntries is called.

I will optimize it in the next PR by calculating hashes only once and making distance function get hashes as input.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BOOST_REQUIRE_EQUAL(nearest.front()->id(), nodeTableHost.testNodes.front().first);
}

unsigned xorDistance(h256 const& _h1, h256 const& _h2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move this function to NodeTable implementation in the next PR.

@gumb0 gumb0 removed the in progress label Apr 30, 2019
@gumb0 gumb0 requested review from halfalicious and chfast April 30, 2019 15:54
@gumb0 gumb0 force-pushed the fix-nearestnodes branch from e8f7d46 to 2888707 Compare April 30, 2019 15:59
@gumb0
Copy link
Member Author

gumb0 commented Apr 30, 2019

Rebased.

Copy link
Member

@chfast chfast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice.

@gumb0 gumb0 merged commit e80e197 into master Apr 30, 2019
@gumb0 gumb0 deleted the fix-nearestnodes branch April 30, 2019 18:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants