From a48687d277ebfb6b97d9ab4a4eed7d495f58aa85 Mon Sep 17 00:00:00 2001 From: Jamie Lokier Date: Tue, 6 Apr 2021 14:43:15 +0100 Subject: [PATCH] discv4: Add test associated with fix to Kademlia crash (#341) It's been confirmed that the test fails if the Kademlia crash workaround isn't in `findNode`, and passes with it there. Signed-off-by: Jamie Lokier --- eth.nimble | 3 +++ tests/p2p/test_discovery.nim | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/eth.nimble b/eth.nimble index 6a1c7d8..8dcd49f 100644 --- a/eth.nimble +++ b/eth.nimble @@ -43,6 +43,9 @@ task test_keys, "Run keys tests": task test_discv5, "Run discovery v5 tests": runTest("tests/p2p/all_discv5_tests") +task test_discv4, "Run discovery v4 tests": + runTest("tests/p2p/test_discovery", chronosStrict = false) + task test_p2p, "Run p2p tests": test_discv5_task() diff --git a/tests/p2p/test_discovery.nim b/tests/p2p/test_discovery.nim index 564e712..95569c2 100644 --- a/tests/p2p/test_discovery.nim +++ b/tests/p2p/test_discovery.nim @@ -93,8 +93,33 @@ proc test() {.async.} = expect DiscProtocolError: bootNode.receive(address, packData(hexToSeqByte(data), nodeKey)) - # empty msg id and payload, doesn't raise, just fails abd prints wrong + # empty msg id and payload, doesn't raise, just fails and prints wrong # msg mac bootNode.receive(address, packData(@[], nodeKey)) + test "Two findNode calls for the same peer in rapid succession": + let targetKey = PrivateKey.fromHex( + "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a618")[] + let peerKey = PrivateKey.fromHex( + "a2b50376a79b1a8c8a3296485572bdfbf54708bb46d3c25d73d2723aaaf6a619")[] + + let targetNodeId = kademlia.toNodeId(targetKey.toPublicKey) + let peerNode = kademlia.newNode(peerKey.toPublicKey, localAddress(20302)) + let nodesSeen = new(HashSet[Node]) + + # Start `findNode` but don't `await` yet, so the reply can't be processed yet. + let neighbours1Future = bootNode.kademlia.findNode(nodesSeen, targetNodeId, peerNode) + + # This will raise an assertion error if `findNode` doesn't check for and ignore + # this second call to the same target and peer in rapid successfion. + let neighbours2Future = bootNode.kademlia.findNode(nodesSeen, targetNodeId, peerNode) + + # Just for completness, verify the result is empty from the second call. + let neighbours2 = await neighbours2Future + check(neighbours2.len == 0) + + # Just for completeness, wait for the first result out of order. + # Max delay 5 seconds. + discard await neighbours1Future + waitFor test()