This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix failing Discovery tests #5581
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b3164d9
Increase number of generated random nodes for evictionWithOldNodeAnsw…
gumb0 08898aa
Improve NodeTable::nearestNodeEntries implementation
gumb0 fe865be
Get rid of an additional vector with all NodeEntries in nearestNodeEn…
gumb0 8f13098
Tests for NodeTable::nearestNodeEntries
gumb0 2888707
Add changelog item
gumb0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ struct TestNodeTable: public NodeTable | |
static vector<pair<Public, uint16_t>> createTestNodes(unsigned _count) | ||
{ | ||
vector<pair<Public, uint16_t>> ret; | ||
asserts(_count <= 1000); | ||
asserts(_count <= 2000); | ||
|
||
ret.clear(); | ||
for (unsigned i = 0; i < _count; i++) | ||
|
@@ -210,13 +210,14 @@ struct TestNodeTable: public NodeTable | |
concurrent_queue<bytes> packetsReceived; | ||
|
||
|
||
using NodeTable::m_hostNodeID; | ||
using NodeTable::m_allowLocalDiscovery; | ||
using NodeTable::m_hostNodeEndpoint; | ||
using NodeTable::m_hostNodeID; | ||
using NodeTable::m_socket; | ||
using NodeTable::nearestNodeEntries; | ||
using NodeTable::nodeEntry; | ||
using NodeTable::noteActiveNode; | ||
using NodeTable::setRequestTimeToLive; | ||
using NodeTable::nodeEntry; | ||
using NodeTable::m_allowLocalDiscovery; | ||
}; | ||
|
||
/** | ||
|
@@ -550,6 +551,93 @@ BOOST_AUTO_TEST_CASE(noteActiveNodeEvictsTheNodeWhenBucketIsFull) | |
BOOST_CHECK_EQUAL(evicted->replacementNodeEntry->id(), newNodeId); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(nearestNodeEntriesOneNode) | ||
{ | ||
TestNodeTableHost nodeTableHost(1); | ||
nodeTableHost.populate(1); | ||
|
||
auto& nodeTable = nodeTableHost.nodeTable; | ||
vector<shared_ptr<NodeEntry>> const nearest = nodeTable->nearestNodeEntries(NodeID::random()); | ||
|
||
BOOST_REQUIRE_EQUAL(nearest.size(), 1); | ||
BOOST_REQUIRE_EQUAL(nearest.front()->id(), nodeTableHost.testNodes.front().first); | ||
} | ||
|
||
unsigned xorDistance(h256 const& _h1, h256 const& _h2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will move this function to |
||
{ | ||
u256 d = _h1 ^ _h2; | ||
unsigned ret = 0; | ||
while (d >>= 1) | ||
++ret; | ||
return ret; | ||
}; | ||
|
||
BOOST_AUTO_TEST_CASE(nearestNodeEntriesOneDistantNode) | ||
{ | ||
// specific case that was failing - one node in bucket #252, target corresponding to bucket #253 | ||
unique_ptr<TestNodeTableHost> nodeTableHost; | ||
do | ||
{ | ||
nodeTableHost.reset(new TestNodeTableHost(1)); | ||
nodeTableHost->populate(1); | ||
} while (nodeTableHost->nodeTable->bucketSize(252) != 1); | ||
|
||
auto& nodeTable = nodeTableHost->nodeTable; | ||
|
||
h256 const hostNodeIDHash = sha3(nodeTable->m_hostNodeID); | ||
|
||
NodeID target = NodeID::random(); | ||
while (xorDistance(hostNodeIDHash, sha3(target)) != 254) | ||
target = NodeID::random(); | ||
|
||
vector<shared_ptr<NodeEntry>> const nearest = nodeTable->nearestNodeEntries(target); | ||
|
||
BOOST_REQUIRE_EQUAL(nearest.size(), 1); | ||
BOOST_REQUIRE_EQUAL(nearest.front()->id(), nodeTableHost->testNodes.front().first); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(nearestNodeEntriesManyNodes) | ||
{ | ||
unsigned constexpr nodeCount = 128; | ||
TestNodeTableHost nodeTableHost(nodeCount); | ||
nodeTableHost.populate(nodeCount); | ||
|
||
auto& nodeTable = nodeTableHost.nodeTable; | ||
|
||
NodeID const target = NodeID::random(); | ||
vector<shared_ptr<NodeEntry>> nearest = nodeTable->nearestNodeEntries(target); | ||
|
||
BOOST_REQUIRE_EQUAL(nearest.size(), 16); | ||
|
||
// get all nodes sorted by distance to target | ||
list<NodeEntry> const allNodeEntries = nodeTable->snapshot(); | ||
h256 const targetNodeIDHash = sha3(target); | ||
vector<pair<unsigned, NodeID>> nodesByDistanceToTarget; | ||
for (auto const& nodeEntry : allNodeEntries) | ||
{ | ||
NodeID const& nodeID = nodeEntry.id(); | ||
nodesByDistanceToTarget.emplace_back(xorDistance(targetNodeIDHash, sha3(nodeID)), nodeID); | ||
} | ||
// stable sort to keep them in the order as they are in buckets | ||
// (the same order they are iterated in nearestNodeEntries implementation) | ||
std::stable_sort(nodesByDistanceToTarget.begin(), nodesByDistanceToTarget.end(), | ||
[](pair<unsigned, NodeID> const& _n1, pair<unsigned, NodeID> const& _n2) { | ||
return _n1.first < _n2.first; | ||
}); | ||
// get 16 with lowest distance | ||
std::vector<NodeID> expectedNearestIDs; | ||
std::transform(nodesByDistanceToTarget.begin(), nodesByDistanceToTarget.begin() + 16, | ||
std::back_inserter(expectedNearestIDs), | ||
[](pair<unsigned, NodeID> const& _n) { return _n.second; }); | ||
|
||
|
||
vector<NodeID> nearestIDs; | ||
for (auto const& nodeEntry : nearest) | ||
nearestIDs.push_back(nodeEntry->id()); | ||
|
||
BOOST_REQUIRE(nearestIDs == expectedNearestIDs); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(unexpectedPong) | ||
{ | ||
// NodeTable receiving PONG | ||
|
@@ -829,7 +917,7 @@ BOOST_AUTO_TEST_CASE(unexpectedFindNode) | |
|
||
BOOST_AUTO_TEST_CASE(evictionWithOldNodeAnswering) | ||
{ | ||
TestNodeTableHost nodeTableHost(1000); | ||
TestNodeTableHost nodeTableHost(2000); | ||
auto& nodeTable = nodeTableHost.nodeTable; | ||
|
||
// socket receiving PING | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 andsha3(node->id())
every timenearestNodeEntries
is called.I will optimize it in the next PR by calculating hashes only once and making distance function get hashes as input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#5586