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
Handle receiving Discovery packets with changed endpoint #5519
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9343c0b
m_sentPings maps pinged udp::endpoint to NodeValidation
gumb0 a1d3b8d
Check whether node's endpoint changed when handling discovery packets
gumb0 5719e7d
Minor improvements
gumb0 bca8cb6
Add changelog message
gumb0 28984b4
Fix build after bad rebase
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 |
---|---|---|
|
@@ -79,7 +79,7 @@ struct TestNodeTable: public NodeTable | |
auto entry = make_shared<NodeEntry>(m_hostNodeID, n.first, | ||
NodeIPEndpoint(ourIp, n.second, n.second), | ||
RLPXDatagramFace::secondsSinceEpoch(), RLPXDatagramFace::secondsSinceEpoch()); | ||
noteActiveNode(move(entry), bi::udp::endpoint(ourIp, n.second)); | ||
noteActiveNode(move(entry)); | ||
} | ||
else | ||
break; | ||
|
@@ -100,7 +100,7 @@ struct TestNodeTable: public NodeTable | |
NodeIPEndpoint(ourIp, testNode->second, testNode->second), | ||
RLPXDatagramFace::secondsSinceEpoch(), RLPXDatagramFace::secondsSinceEpoch())); | ||
auto distance = node->distance; | ||
noteActiveNode(move(node), bi::udp::endpoint(ourIp, testNode->second)); | ||
noteActiveNode(move(node)); | ||
|
||
{ | ||
Guard stateGuard(x_state); | ||
|
@@ -138,7 +138,7 @@ struct TestNodeTable: public NodeTable | |
auto entry = make_shared<NodeEntry>(m_hostNodeID, testNode->first, | ||
NodeIPEndpoint(ourIp, testNode->second, testNode->second), | ||
RLPXDatagramFace::secondsSinceEpoch(), RLPXDatagramFace::secondsSinceEpoch()); | ||
noteActiveNode(move(entry), bi::udp::endpoint(ourIp, testNode->second)); | ||
noteActiveNode(move(entry)); | ||
|
||
++testNode; | ||
} | ||
|
@@ -500,7 +500,7 @@ BOOST_AUTO_TEST_CASE(noteActiveNodeUpdatesKnownNode) | |
auto& nodeTable = nodeTableHost.nodeTable; | ||
auto knownNode = nodeTable->bucketFirstNode(bucketIndex); | ||
|
||
nodeTable->noteActiveNode(knownNode, knownNode->endpoint()); | ||
nodeTable->noteActiveNode(knownNode); | ||
|
||
// check that node was moved to the back of the bucket | ||
BOOST_CHECK_NE(nodeTable->bucketFirstNode(bucketIndex), knownNode); | ||
|
@@ -550,32 +550,6 @@ BOOST_AUTO_TEST_CASE(noteActiveNodeEvictsTheNodeWhenBucketIsFull) | |
BOOST_CHECK_EQUAL(evicted->replacementNodeEntry->id(), newNodeId); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(noteActiveNodeReplacesNodeInFullBucketWhenEndpointChanged) | ||
{ | ||
TestNodeTableHost nodeTableHost(512); | ||
int const bucketIndex = nodeTableHost.populateUntilBucketSize(16); | ||
BOOST_REQUIRE(bucketIndex >= 0); | ||
|
||
auto& nodeTable = nodeTableHost.nodeTable; | ||
auto leastRecentlySeenNode = nodeTable->bucketFirstNode(bucketIndex); | ||
|
||
// addNode will replace the node in the m_allNodes map, because it's the same id with enother | ||
// endpoint | ||
auto const port = randomPortNumber(); | ||
NodeIPEndpoint newEndpoint{bi::address::from_string(c_localhostIp), port, port }; | ||
nodeTable->noteActiveNode(leastRecentlySeenNode, newEndpoint); | ||
|
||
// the bucket is still max size | ||
BOOST_CHECK_EQUAL(nodeTable->bucketSize(bucketIndex), 16); | ||
// least recently seen node removed | ||
BOOST_CHECK_NE(nodeTable->bucketFirstNode(bucketIndex)->id(), leastRecentlySeenNode->id()); | ||
// but added as most recently seen with new endpoint | ||
auto mostRecentNodeEntry = nodeTable->bucketLastNode(bucketIndex); | ||
BOOST_CHECK_EQUAL(mostRecentNodeEntry->id(), leastRecentlySeenNode->id()); | ||
BOOST_CHECK_EQUAL(mostRecentNodeEntry->endpoint().address(), newEndpoint.address()); | ||
BOOST_CHECK_EQUAL(mostRecentNodeEntry->endpoint().udpPort(), newEndpoint.udpPort()); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(unexpectedPong) | ||
{ | ||
// NodeTable receiving PONG | ||
|
@@ -1173,6 +1147,140 @@ BOOST_AUTO_TEST_CASE(addNodePingsNodeOnlyOnce) | |
BOOST_REQUIRE_EQUAL(sentPing->pingHash, sentPing2->pingHash); | ||
} | ||
|
||
class PacketsWithChangedEndpointFixture : public TestOutputHelperFixture | ||
{ | ||
public: | ||
PacketsWithChangedEndpointFixture() | ||
{ | ||
nodeTableHost.start(); | ||
nodeSocketHost1.start(); | ||
nodePort1 = nodeSocketHost1.port; | ||
nodeSocketHost2.start(); | ||
nodePort2 = nodeSocketHost2.port; | ||
|
||
nodeEndpoint1 = | ||
NodeIPEndpoint{bi::address::from_string(c_localhostIp), nodePort1, nodePort1}; | ||
nodeEndpoint2 = | ||
NodeIPEndpoint{bi::address::from_string(c_localhostIp), nodePort2, nodePort2}; | ||
|
||
// add a node to node table, initiating PING | ||
nodeTable->addNode(Node{nodePubKey, nodeEndpoint1}); | ||
|
||
// handle received PING | ||
auto pingDataReceived = nodeSocketHost1.packetsReceived.pop(chrono::seconds(5)); | ||
auto pingDatagram = | ||
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(pingDataReceived)); | ||
BOOST_REQUIRE_EQUAL(pingDatagram->typeName(), "Ping"); | ||
auto ping = dynamic_cast<PingNode const&>(*pingDatagram); | ||
halfalicious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// send PONG | ||
Pong pong{nodeTable->m_hostNodeEndpoint}; | ||
pong.echo = ping.echo; | ||
pong.sign(nodeKeyPair.secret()); | ||
nodeSocketHost1.socket->send(pong); | ||
|
||
// wait for PONG to be received and handled | ||
nodeTable->packetsReceived.pop(chrono::seconds(5)); | ||
|
||
nodeEntry = nodeTable->nodeEntry(nodePubKey); | ||
} | ||
|
||
TestNodeTableHost nodeTableHost{0}; | ||
shared_ptr<TestNodeTable>& nodeTable = nodeTableHost.nodeTable; | ||
|
||
// socket representing initial peer node | ||
TestUDPSocketHost nodeSocketHost1; | ||
uint16_t nodePort1 = 0; | ||
|
||
// socket representing peer with changed endpoint | ||
TestUDPSocketHost nodeSocketHost2; | ||
uint16_t nodePort2 = 0; | ||
|
||
NodeIPEndpoint nodeEndpoint1; | ||
NodeIPEndpoint nodeEndpoint2; | ||
KeyPair nodeKeyPair = KeyPair::create(); | ||
NodeID nodePubKey = nodeKeyPair.pub(); | ||
|
||
shared_ptr<NodeEntry> nodeEntry; | ||
}; | ||
|
||
BOOST_FIXTURE_TEST_SUITE(packetsWithChangedEndpointSuite, PacketsWithChangedEndpointFixture) | ||
|
||
BOOST_AUTO_TEST_CASE(addNode) | ||
halfalicious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// this should initiate Ping to endpoint 2 | ||
nodeTable->addNode(Node{nodePubKey, nodeEndpoint2}); | ||
|
||
// handle received PING | ||
auto pingDataReceived = nodeSocketHost2.packetsReceived.pop(); | ||
auto pingDatagram = | ||
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(pingDataReceived)); | ||
BOOST_REQUIRE_EQUAL(pingDatagram->typeName(), "Ping"); | ||
auto ping = dynamic_cast<PingNode const&>(*pingDatagram); | ||
halfalicious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// send PONG | ||
Pong pong{nodeTable->m_hostNodeEndpoint}; | ||
pong.echo = ping.echo; | ||
pong.sign(nodeKeyPair.secret()); | ||
nodeSocketHost2.socket->send(pong); | ||
|
||
// wait for PONG to be received and handled | ||
halfalicious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto pongDataReceived = nodeTable->packetsReceived.pop(chrono::seconds(5)); | ||
auto pongDatagram = | ||
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(pongDataReceived)); | ||
BOOST_REQUIRE_EQUAL(pongDatagram->typeName(), "Pong"); | ||
|
||
BOOST_REQUIRE_EQUAL(nodeEntry->endpoint(), nodeEndpoint2); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(findNode) | ||
{ | ||
// Create and send the FindNode packet through endpoint 2 | ||
FindNode findNode{nodeTable->m_hostNodeEndpoint, KeyPair::create().pub() /* target */}; | ||
findNode.sign(nodeKeyPair.secret()); | ||
nodeSocketHost2.socket->send(findNode); | ||
|
||
// Wait for FindNode to be received | ||
auto findNodeDataReceived = nodeTable->packetsReceived.pop(chrono::seconds(5)); | ||
auto findNodeDatagram = | ||
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(findNodeDataReceived)); | ||
BOOST_REQUIRE_EQUAL(findNodeDatagram->typeName(), "FindNode"); | ||
|
||
// Verify that no neighbours response is received | ||
BOOST_CHECK_THROW(nodeSocketHost2.packetsReceived.pop(chrono::seconds(5)), WaitTimeout); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(neighbours) | ||
{ | ||
// Wait for FindNode arriving to endpoint 1 | ||
halfalicious marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto findNodeDataReceived = nodeSocketHost1.packetsReceived.pop(chrono::seconds(10)); | ||
auto findNodeDatagram = | ||
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(findNodeDataReceived)); | ||
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. Can we validate the packet name? |
||
BOOST_REQUIRE_EQUAL(findNodeDatagram->typeName(), "FindNode"); | ||
auto findNode = dynamic_cast<FindNode const&>(*findNodeDatagram); | ||
|
||
// send Neighbours through endpoint 2 | ||
NodeIPEndpoint neighbourEndpoint{boost::asio::ip::address::from_string("200.200.200.200"), | ||
c_defaultListenPort, c_defaultListenPort}; | ||
vector<shared_ptr<NodeEntry>> nearest{ | ||
make_shared<NodeEntry>(nodeTable->m_hostNodeID, KeyPair::create().pub(), neighbourEndpoint, | ||
RLPXDatagramFace::secondsSinceEpoch(), 0 /* pongSentTime */)}; | ||
Neighbours neighbours{nodeTable->m_hostNodeEndpoint, nearest}; | ||
neighbours.sign(nodeKeyPair.secret()); | ||
nodeSocketHost2.socket->send(neighbours); | ||
|
||
// Wait for Neighbours to be received | ||
auto neighboursDataReceived = nodeTable->packetsReceived.pop(chrono::seconds(5)); | ||
auto neighboursDatagram = | ||
DiscoveryDatagram::interpretUDP(bi::udp::endpoint{}, dev::ref(neighboursDataReceived)); | ||
BOOST_REQUIRE_EQUAL(neighboursDatagram->typeName(), "Neighbours"); | ||
|
||
// no Ping is sent to neighbour | ||
auto sentPing = nodeTable->nodeValidation(neighbourEndpoint); | ||
BOOST_REQUIRE(!sentPing.is_initialized()); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() | ||
BOOST_AUTO_TEST_SUITE_END() | ||
|
||
BOOST_FIXTURE_TEST_SUITE(netTypes, TestOutputHelperFixture) | ||
|
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.
I know this is good practice but do you know if we ever actually hit this case? It seems like we wouldn't given that pongs have to be received within 1 minute of the ping being sent to be considered valid?
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.
Not sure how it is related to Pong timeout, this is the case when there is a record in the bucket of the node table (maybe added long ago), but we receive a valid Pong with the same NodeID but from another endpoint.