diff --git a/libp2p_dht/libp2p_dht.nimble b/libp2p_dht/libp2p_dht.nimble deleted file mode 100644 index 16fa06b..0000000 --- a/libp2p_dht/libp2p_dht.nimble +++ /dev/null @@ -1,12 +0,0 @@ -# Package - -version = "0.0.1" -author = "Status.im" -description = "DHT based on the libp2p Kademlia spec" -license = "MIT" -srcDir = "src" - - -# Dependencies - -requires "nim >= 1.6.2" diff --git a/libp2p_dht/src/libp2p_dht.nim b/libp2p_dht/src/libp2p_dht.nim deleted file mode 100644 index 4b2a270..0000000 --- a/libp2p_dht/src/libp2p_dht.nim +++ /dev/null @@ -1,7 +0,0 @@ -# This is just an example to get you started. A typical library package -# exports the main API in this file. Note that you cannot rename this file -# but you can remove it if you wish. - -proc add*(x, y: int): int = - ## Adds two files together. - return x + y diff --git a/libp2p_dht/src/libp2p_dht/submodule.nim b/libp2p_dht/src/libp2p_dht/submodule.nim deleted file mode 100644 index b75fc72..0000000 --- a/libp2p_dht/src/libp2p_dht/submodule.nim +++ /dev/null @@ -1,12 +0,0 @@ -# This is just an example to get you started. Users of your library will -# import this file by writing ``import libp2p_dht/submodule``. Feel free to rename or -# remove this file altogether. You may create additional modules alongside -# this file as required. - -type - Submodule* = object - name*: string - -proc initSubmodule*(): Submodule = - ## Initialises a new ``Submodule`` object. - Submodule(name: "Anonymous") diff --git a/libp2p_dht/tests/config.nims b/libp2p_dht/tests/config.nims deleted file mode 100644 index 3bb69f8..0000000 --- a/libp2p_dht/tests/config.nims +++ /dev/null @@ -1 +0,0 @@ -switch("path", "$projectDir/../src") \ No newline at end of file diff --git a/libp2p_dht/tests/test1.nim b/libp2p_dht/tests/test1.nim deleted file mode 100644 index a46434b..0000000 --- a/libp2p_dht/tests/test1.nim +++ /dev/null @@ -1,12 +0,0 @@ -# This is just an example to get you started. You may wish to put all of your -# tests into a single file, or separate them into multiple `test1`, `test2` -# etc. files (better names are recommended, just make sure the name starts with -# the letter 't'). -# -# To run these tests, simply execute `nimble test`. - -import unittest - -import libp2p_dht -test "can add": - check add(5, 5) == 10 diff --git a/libp2pdht.nim b/libp2pdht.nim new file mode 100644 index 0000000..a907dd1 --- /dev/null +++ b/libp2pdht.nim @@ -0,0 +1,5 @@ +import + ./libp2pdht/dht, + ./libp2pdht/discv5 + +export dht, discv5 diff --git a/libp2pdht.nimble b/libp2pdht.nimble new file mode 100644 index 0000000..fff176f --- /dev/null +++ b/libp2pdht.nimble @@ -0,0 +1,24 @@ +# Package + +version = "0.0.1" +author = "Status Research & Development GmbH" +description = "DHT based on the libp2p Kademlia spec" +license = "MIT" +skipDirs = @["tests"] + + +# Dependencies +requires "nim >= 1.2.0", + "nimcrypto >= 0.5.4 & < 0.6.0", + "bearssl >= 0.1.5 & < 0.2.0", + "chronicles >= 0.10.2 & < 0.11.0", + "chronos >= 3.0.11 & < 3.1.0", + "eth >= 1.0.0 & < 1.1.0", # to be removed in https://github.com/status-im/nim-libp2p-dht/issues/2 + "libp2p#unstable", + "metrics", + "protobufserialization >= 0.2.0 & < 0.3.0", + "secp256k1 >= 0.5.2 & < 0.6.0", + "stew#head", + "stint", + "asynctest#88ae00ea288c93b23d0111da4868127a1426f39d" # >= 0.3.0 & < 0.4.0" + # "testutils >= 0.4.2 & < 0.5.0" diff --git a/libp2pdht/dht.nim b/libp2pdht/dht.nim new file mode 100644 index 0000000..53da4f8 --- /dev/null +++ b/libp2pdht/dht.nim @@ -0,0 +1,4 @@ +import + ./dht/providers + +export providers \ No newline at end of file diff --git a/dht/providers.nim b/libp2pdht/dht/providers.nim similarity index 99% rename from dht/providers.nim rename to libp2pdht/dht/providers.nim index d038eed..c32438a 100644 --- a/dht/providers.nim +++ b/libp2pdht/dht/providers.nim @@ -10,7 +10,7 @@ import chronicles, std/tables, sequtils, stew/byteutils, # toBytes - ../eth/p2p/discoveryv5/[protocol, node], + ../discv5/[protocol, node], libp2p/routing_record, ./providers_messages, ./providers_encoding diff --git a/dht/providers_encoding.nim b/libp2pdht/dht/providers_encoding.nim similarity index 98% rename from dht/providers_encoding.nim rename to libp2pdht/dht/providers_encoding.nim index cb234f1..d9c422c 100644 --- a/dht/providers_encoding.nim +++ b/libp2pdht/dht/providers_encoding.nim @@ -1,5 +1,5 @@ import - ../eth/p2p/discoveryv5/[node], + ../discv5/[node], libp2p/routing_record, libp2p/protobuf/minprotobuf, ./providers_messages diff --git a/dht/providers_messages.nim b/libp2pdht/dht/providers_messages.nim similarity index 87% rename from dht/providers_messages.nim rename to libp2pdht/dht/providers_messages.nim index db59fd9..b55f865 100644 --- a/dht/providers_messages.nim +++ b/libp2pdht/dht/providers_messages.nim @@ -1,5 +1,5 @@ import - ../eth/p2p/discoveryv5/[node], + ../discv5/[node], libp2p/routing_record type diff --git a/libp2pdht/discv5.nim b/libp2pdht/discv5.nim new file mode 100644 index 0000000..61e011e --- /dev/null +++ b/libp2pdht/discv5.nim @@ -0,0 +1,4 @@ +import + ./discv5/[enr, encoding, messages, messages_encoding, node, nodes_verification, protocol, routing_table, sessions, transport] + +export enr, encoding, messages, messages_encoding, node, nodes_verification, protocol, routing_table, sessions, transport \ No newline at end of file diff --git a/libp2pdht/discv5/encoding.nim b/libp2pdht/discv5/encoding.nim new file mode 100644 index 0000000..bb82313 --- /dev/null +++ b/libp2pdht/discv5/encoding.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/encoding + +export encoding \ No newline at end of file diff --git a/libp2pdht/discv5/enr.nim b/libp2pdht/discv5/enr.nim new file mode 100644 index 0000000..594395a --- /dev/null +++ b/libp2pdht/discv5/enr.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/enr + +export enr \ No newline at end of file diff --git a/libp2pdht/discv5/messages.nim b/libp2pdht/discv5/messages.nim new file mode 100644 index 0000000..d2edb93 --- /dev/null +++ b/libp2pdht/discv5/messages.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/messages + +export messages \ No newline at end of file diff --git a/libp2pdht/discv5/messages_encoding.nim b/libp2pdht/discv5/messages_encoding.nim new file mode 100644 index 0000000..f4ef985 --- /dev/null +++ b/libp2pdht/discv5/messages_encoding.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/messages_encoding + +export messages_encoding \ No newline at end of file diff --git a/libp2pdht/discv5/node.nim b/libp2pdht/discv5/node.nim new file mode 100644 index 0000000..b96358a --- /dev/null +++ b/libp2pdht/discv5/node.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/node + +export node \ No newline at end of file diff --git a/libp2pdht/discv5/nodes_verification.nim b/libp2pdht/discv5/nodes_verification.nim new file mode 100644 index 0000000..1863159 --- /dev/null +++ b/libp2pdht/discv5/nodes_verification.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/nodes_verification + +export nodes_verification \ No newline at end of file diff --git a/libp2pdht/discv5/protocol.nim b/libp2pdht/discv5/protocol.nim new file mode 100644 index 0000000..35aa0fb --- /dev/null +++ b/libp2pdht/discv5/protocol.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/protocol + +export protocol \ No newline at end of file diff --git a/libp2pdht/discv5/routing_table.nim b/libp2pdht/discv5/routing_table.nim new file mode 100644 index 0000000..80fcfc2 --- /dev/null +++ b/libp2pdht/discv5/routing_table.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/routing_table + +export routing_table \ No newline at end of file diff --git a/libp2pdht/discv5/sessions.nim b/libp2pdht/discv5/sessions.nim new file mode 100644 index 0000000..071ee7f --- /dev/null +++ b/libp2pdht/discv5/sessions.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/sessions + +export sessions \ No newline at end of file diff --git a/libp2pdht/discv5/transport.nim b/libp2pdht/discv5/transport.nim new file mode 100644 index 0000000..55f3d5f --- /dev/null +++ b/libp2pdht/discv5/transport.nim @@ -0,0 +1,4 @@ +import + ../private/eth/p2p/discoveryv5/transport + +export transport \ No newline at end of file diff --git a/eth/p2p/discoveryv5/encoding.nim b/libp2pdht/private/eth/p2p/discoveryv5/encoding.nim similarity index 100% rename from eth/p2p/discoveryv5/encoding.nim rename to libp2pdht/private/eth/p2p/discoveryv5/encoding.nim diff --git a/eth/p2p/discoveryv5/enr.nim b/libp2pdht/private/eth/p2p/discoveryv5/enr.nim similarity index 100% rename from eth/p2p/discoveryv5/enr.nim rename to libp2pdht/private/eth/p2p/discoveryv5/enr.nim diff --git a/eth/p2p/discoveryv5/hkdf.nim b/libp2pdht/private/eth/p2p/discoveryv5/hkdf.nim similarity index 100% rename from eth/p2p/discoveryv5/hkdf.nim rename to libp2pdht/private/eth/p2p/discoveryv5/hkdf.nim diff --git a/eth/p2p/discoveryv5/ip_vote.nim b/libp2pdht/private/eth/p2p/discoveryv5/ip_vote.nim similarity index 100% rename from eth/p2p/discoveryv5/ip_vote.nim rename to libp2pdht/private/eth/p2p/discoveryv5/ip_vote.nim diff --git a/eth/p2p/discoveryv5/lru.nim b/libp2pdht/private/eth/p2p/discoveryv5/lru.nim similarity index 100% rename from eth/p2p/discoveryv5/lru.nim rename to libp2pdht/private/eth/p2p/discoveryv5/lru.nim diff --git a/eth/p2p/discoveryv5/messages.nim b/libp2pdht/private/eth/p2p/discoveryv5/messages.nim similarity index 100% rename from eth/p2p/discoveryv5/messages.nim rename to libp2pdht/private/eth/p2p/discoveryv5/messages.nim diff --git a/eth/p2p/discoveryv5/messages_encoding.nim b/libp2pdht/private/eth/p2p/discoveryv5/messages_encoding.nim similarity index 100% rename from eth/p2p/discoveryv5/messages_encoding.nim rename to libp2pdht/private/eth/p2p/discoveryv5/messages_encoding.nim diff --git a/eth/p2p/discoveryv5/node.nim b/libp2pdht/private/eth/p2p/discoveryv5/node.nim similarity index 100% rename from eth/p2p/discoveryv5/node.nim rename to libp2pdht/private/eth/p2p/discoveryv5/node.nim diff --git a/eth/p2p/discoveryv5/nodes_verification.nim b/libp2pdht/private/eth/p2p/discoveryv5/nodes_verification.nim similarity index 100% rename from eth/p2p/discoveryv5/nodes_verification.nim rename to libp2pdht/private/eth/p2p/discoveryv5/nodes_verification.nim diff --git a/eth/p2p/discoveryv5/protocol.nim b/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim similarity index 100% rename from eth/p2p/discoveryv5/protocol.nim rename to libp2pdht/private/eth/p2p/discoveryv5/protocol.nim diff --git a/eth/p2p/discoveryv5/random2.nim b/libp2pdht/private/eth/p2p/discoveryv5/random2.nim similarity index 100% rename from eth/p2p/discoveryv5/random2.nim rename to libp2pdht/private/eth/p2p/discoveryv5/random2.nim diff --git a/eth/p2p/discoveryv5/routing_table.nim b/libp2pdht/private/eth/p2p/discoveryv5/routing_table.nim similarity index 100% rename from eth/p2p/discoveryv5/routing_table.nim rename to libp2pdht/private/eth/p2p/discoveryv5/routing_table.nim diff --git a/eth/p2p/discoveryv5/sessions.nim b/libp2pdht/private/eth/p2p/discoveryv5/sessions.nim similarity index 100% rename from eth/p2p/discoveryv5/sessions.nim rename to libp2pdht/private/eth/p2p/discoveryv5/sessions.nim diff --git a/eth/p2p/discoveryv5/transport.nim b/libp2pdht/private/eth/p2p/discoveryv5/transport.nim similarity index 100% rename from eth/p2p/discoveryv5/transport.nim rename to libp2pdht/private/eth/p2p/discoveryv5/transport.nim diff --git a/tests/dht/test_helper.nim b/tests/dht/test_helper.nim index 421e32b..b62b79f 100644 --- a/tests/dht/test_helper.nim +++ b/tests/dht/test_helper.nim @@ -1,8 +1,8 @@ import stew/shims/net, bearssl, chronos, eth/keys, - ../../eth/p2p/discoveryv5/[enr, node, routing_table], - ../../eth/p2p/discoveryv5/protocol as discv5_protocol + libp2pdht/discv5/[enr, node, routing_table], + libp2pdht/discv5/protocol as discv5_protocol export net diff --git a/tests/dht/test_providers.nim b/tests/dht/test_providers.nim index 206a08d..0e561d6 100644 --- a/tests/dht/test_providers.nim +++ b/tests/dht/test_providers.nim @@ -11,16 +11,20 @@ import std/sequtils, - chronos, stew/byteutils, nimcrypto, testutils/unittests, + chronos, stew/byteutils, nimcrypto, asynctest, eth/keys, - ../../dht/providers, + libp2pdht/dht, chronicles, - ../../eth/p2p/discoveryv5/protocol as discv5_protocol, + libp2pdht/discv5/protocol as discv5_protocol, test_helper, libp2p/routing_record, libp2p/multihash, libp2p/multicodec + + +# suite "Providers Tests: node alone": + proc initProvidersNode( rng: ref BrHmacDrbgContext, privKey: keys.PrivateKey, @@ -35,7 +39,7 @@ proc toPeerRecord(p: ProvidersProtocol) : PeerRecord = ## hadle conversion between the two worlds #NodeId is a keccak-256 hash created by keccak256.digest and stored as UInt256 - let discNodeId = p.discovery.localNode.id + let discNodeId = p.discovery.localNode.id ## get it back to MDigest form var digest: MDigest[256] digest.data = discNodeId.toBytesBE @@ -65,123 +69,151 @@ proc bootstrapNetwork(nodecount: int, rng = keys.newRng()) : seq[ProvidersProtoc bootNode = initProvidersNode(rng, bootNodeKey, bootNodeAddr, @[]) # just a shortcut for new and open #waitFor bootNode.bootstrap() # immediate, since no bootnodes are defined above - + result = bootstrapNodes(nodecount - 1, @[bootnode.discovery.localNode.record], rng = rng) result.insert(bootNode, 0) + +# suite "Providers Tests": suite "Providers Tests: node alone": + var + rng: ref HmacDrbgContext + nodes: seq[ProvidersProtocol] + targetId: NodeId - asyncTest "node alone": - let - rng = keys.newRng() - nodes = bootstrapNetwork(nodecount=1) - targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) - - asyncTest "Node in isolation should store": - - debug "---- ADDING PROVIDERS ---" - let addedTo = await nodes[0].addProvider(targetId, nodes[0].toPeerRecord) - debug "Provider added to: ", addedTo - - debug "---- STARTING CHECKS ---" - check (addedTo.len == 1) - check (addedTo[0].id == nodes[0].discovery.localNode.id) - check (nodes[0].getProvidersLocal(targetId)[0].peerId == nodes[0].toPeerRecord.peerId) - - asyncTest "Node in isolation should retrieve": - - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[0].getProviders(targetId) - debug "Providers:", providers - - debug "---- STARTING CHECKS ---" - check (providers.len > 0 and providers[0].peerId == nodes[0].toPeerRecord.peerId) - - asyncTest "Should not retrieve bogus": - - let bogusId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) - - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[0].getProviders(bogusId) - debug "Providers:", providers - - debug "---- STARTING CHECKS ---" - check (providers.len == 0) + before: + debug "RUNNING BEFORE TESTS" + rng = keys.newRng() + nodes = bootstrapNetwork(nodecount=1) + targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) + after: + debug "RUNNING AFTER TESTS" for n in nodes: await n.discovery.closeWait() await sleepAsync(chronos.seconds(3)) - asyncTest "Providers Tests: two nodes": - let - rng = keys.newRng() - nodes = bootstrapNetwork(nodecount=2) - targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) - asyncTest "2 nodes, store and retieve from same": + test "Node in isolation should store": + debug "---- ADDING PROVIDERS ---", nodes = nodes.len + let addedTo = await nodes[0].addProvider(targetId, nodes[0].toPeerRecord) + debug "Provider added to: ", addedTo - debug "---- ADDING PROVIDERS ---" - let addedTo = await nodes[0].addProvider(targetId, nodes[0].toPeerRecord) - debug "Provider added to: ", addedTo + debug "---- STARTING CHECKS ---" + check (addedTo.len == 1) + check (addedTo[0].id == nodes[0].discovery.localNode.id) + check (nodes[0].getProvidersLocal(targetId)[0].peerId == nodes[0].toPeerRecord.peerId) - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[0].getProviders(targetId) - debug "Providers:", providers + test "Node in isolation should retrieve": - debug "---- STARTING CHECKS ---" - check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[0].getProviders(targetId) + debug "Providers:", providers - asyncTest "2 nodes, retieve from other": - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[1].getProviders(targetId) - debug "Providers:", providers + debug "---- STARTING CHECKS ---" + check (providers.len > 0 and providers[0].peerId == nodes[0].toPeerRecord.peerId) - debug "---- STARTING CHECKS ---" - check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + test "Should not retrieve bogus": + let bogusId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) + + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[0].getProviders(bogusId) + debug "Providers:", providers + + debug "---- STARTING CHECKS ---" + check (providers.len == 0) + + +suite "Providers Tests: two nodes": + + var + rng: ref HmacDrbgContext + nodes: seq[ProvidersProtocol] + targetId: NodeId + + before: + rng = keys.newRng() + nodes = bootstrapNetwork(nodecount=2) + targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) + + after: for n in nodes: await n.discovery.closeWait() await sleepAsync(chronos.seconds(3)) - asyncTest "Providers Tests: 20 nodes": - let - rng = keys.newRng() - nodes = bootstrapNetwork(nodecount=20) - targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) + test "2 nodes, store and retieve from same": + + debug "---- ADDING PROVIDERS ---" + let addedTo = await nodes[0].addProvider(targetId, nodes[0].toPeerRecord) + debug "Provider added to: ", addedTo + + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[0].getProviders(targetId) + debug "Providers:", providers + + debug "---- STARTING CHECKS ---" + check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + + test "2 nodes, retieve from other": + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[1].getProviders(targetId) + debug "Providers:", providers + + debug "---- STARTING CHECKS ---" + check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + + + +suite "Providers Tests: 20 nodes": + + var + rng: ref HmacDrbgContext + nodes: seq[ProvidersProtocol] + targetId: NodeId + + before: + rng = keys.newRng() + nodes = bootstrapNetwork(nodecount=20) + targetId = toNodeId(keys.PrivateKey.random(rng[]).toPublicKey) + await sleepAsync(chronos.seconds(15)) - asyncTest "20 nodes, store and retieve from same": - - debug "---- ADDING PROVIDERS ---" - let addedTo = await nodes[0].addProvider(targetId, nodes[0].toPeerRecord) - debug "Provider added to: ", addedTo - - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[0].getProviders(targetId) - debug "Providers:", providers - - debug "---- STARTING CHECKS ---" - check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) - - asyncTest "20 nodes, retieve from other": - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[^1].getProviders(targetId) - debug "Providers:", providers - - debug "---- STARTING CHECKS ---" - check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) - - asyncTest "20 nodes, retieve after bootnode dies": - debug "---- KILLING BOOTSTRAP NODE ---" - await nodes[0].discovery.closeWait() - - debug "---- STARTING PROVIDERS LOOKUP ---" - let providers = await nodes[^2].getProviders(targetId) - debug "Providers:", providers - - debug "---- STARTING CHECKS ---" - check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) - + after: for n in nodes[1..^1]: await n.discovery.closeWait() + + test "20 nodes, store and retieve from same": + + debug "---- ADDING PROVIDERS ---" + let addedTo = await nodes[0].addProvider(targetId, nodes[0].toPeerRecord) + debug "Provider added to: ", addedTo + + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[0].getProviders(targetId) + debug "Providers:", providers + + debug "---- STARTING CHECKS ---" + check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + + test "20 nodes, retieve from other": + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[^1].getProviders(targetId) + debug "Providers:", providers + + debug "---- STARTING CHECKS ---" + check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + + test "20 nodes, retieve after bootnode dies": + debug "---- KILLING BOOTSTRAP NODE ---" + await nodes[0].discovery.closeWait() + + debug "---- STARTING PROVIDERS LOOKUP ---" + let providers = await nodes[^2].getProviders(targetId) + debug "Providers:", providers + + debug "---- STARTING CHECKS ---" + check (providers.len == 1 and providers[0].peerId == nodes[0].toPeerRecord.peerId) + + diff --git a/tests/nim.cfg b/tests/nim.cfg new file mode 100644 index 0000000..cf48491 --- /dev/null +++ b/tests/nim.cfg @@ -0,0 +1,3 @@ +--path:".." +--threads:on +--tlsEmulation:off diff --git a/tests/p2p/discv5_test_helper.nim b/tests/p2p/discv5_test_helper.nim index bf8e6ee..0b4a9e4 100644 --- a/tests/p2p/discv5_test_helper.nim +++ b/tests/p2p/discv5_test_helper.nim @@ -1,8 +1,8 @@ import stew/shims/net, bearssl, chronos, eth/keys, - ../../eth/p2p/discoveryv5/[enr, node, routing_table], - ../../eth/p2p/discoveryv5/protocol as discv5_protocol + libp2pdht/discv5/[enr, node, routing_table], + libp2pdht/discv5/protocol as discv5_protocol export net diff --git a/tests/p2p/test_discoveryv5.nim b/tests/p2p/test_discoveryv5.nim index 8773d9f..277630d 100644 --- a/tests/p2p/test_discoveryv5.nim +++ b/tests/p2p/test_discoveryv5.nim @@ -2,18 +2,20 @@ import std/tables, - chronos, chronicles, stint, testutils/unittests, stew/shims/net, + chronos, chronicles, stint, asynctest, stew/shims/net, stew/byteutils, bearssl, eth/keys, - ../../eth/p2p/discoveryv5/[transport, enr, node, routing_table, encoding, sessions, messages, nodes_verification], - ../../eth/p2p/discoveryv5/protocol as discv5_protocol, + libp2pdht/discv5/[transport, enr, node, routing_table, encoding, sessions, messages, nodes_verification], + libp2pdht/discv5/protocol as discv5_protocol, ./discv5_test_helper suite "Discovery v5 Tests": - setup: - let rng = newRng() + var rng: ref HmacDrbgContext - asyncTest "GetNode": + setup: + rng = newRng() + + test "GetNode": # TODO: This could be tested in just a routing table only context let node = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20302)) @@ -30,7 +32,7 @@ suite "Discovery v5 Tests": await node.closeWait() - asyncTest "Node deletion": + test "Node deletion": let bootnode = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20301)) @@ -173,7 +175,7 @@ suite "Discovery v5 Tests": for (id, d) in testValues: check idAtDistance(targetId, d) == parse(id, UInt256, 16) - asyncTest "FindNode Test": + test "FindNode Test": const dist = 253'u16 let mainNodeKey = PrivateKey.fromHex( @@ -241,7 +243,7 @@ suite "Discovery v5 Tests": await mainNode.closeWait() await testNode.closeWait() - asyncTest "FindNode with test table": + test "FindNode with test table": let mainNode = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301)) @@ -270,7 +272,7 @@ suite "Discovery v5 Tests": await mainNode.closeWait() await testNode.closeWait() - asyncTest "Lookup targets": + test "Lookup targets": const nodeCount = 17 @@ -306,7 +308,7 @@ suite "Discovery v5 Tests": for node in nodes: await node.closeWait() - asyncTest "Resolve target": + test "Resolve target": let mainNode = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301)) @@ -389,7 +391,7 @@ suite "Discovery v5 Tests": await mainNode.closeWait() await lookupNode.closeWait() - asyncTest "Random nodes with enr field filter": + test "Random nodes with enr field filter": let lookupNode = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301)) targetFieldPair = toFieldPair("test", @[byte 1,2,3,4]) @@ -438,7 +440,7 @@ suite "Discovery v5 Tests": ip, some(port), some(port), bindPort = port, rng = rng, previousRecord = some(updatesNode.getRecord())) - asyncTest "Update node record with revalidate": + test "Update node record with revalidate": let mainNode = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301)) @@ -472,7 +474,7 @@ suite "Discovery v5 Tests": await mainNode.closeWait() await testNode.closeWait() - asyncTest "Update node record with handshake": + test "Update node record with handshake": let mainNode = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(20301)) @@ -588,7 +590,7 @@ suite "Discovery v5 Tests": let dist1 = lookupDistances(u256(0), u256(1)) check dist1 == @[1'u16, 2, 3] - asyncTest "Handshake cleanup: different ids": + test "Handshake cleanup: different ids": # Node to test the handshakes on. let receiveNode = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20302)) @@ -619,7 +621,7 @@ suite "Discovery v5 Tests": await receiveNode.closeWait() - asyncTest "Handshake cleanup: different ips": + test "Handshake cleanup: different ips": # Node to test the handshakes on. let receiveNode = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20302)) @@ -649,7 +651,7 @@ suite "Discovery v5 Tests": await receiveNode.closeWait() - asyncTest "Handshake duplicates": + test "Handshake duplicates": # Node to test the handshakes on. let receiveNode = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20302)) @@ -682,7 +684,7 @@ suite "Discovery v5 Tests": await receiveNode.closeWait() - asyncTest "Talkreq no protocol": + test "Talkreq no protocol": let node1 = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20302)) @@ -698,7 +700,7 @@ suite "Discovery v5 Tests": await node1.closeWait() await node2.closeWait() - asyncTest "Talkreq echo protocol": + test "Talkreq echo protocol": let node1 = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20302)) @@ -723,7 +725,7 @@ suite "Discovery v5 Tests": await node1.closeWait() await node2.closeWait() - asyncTest "Talkreq register protocols": + test "Talkreq register protocols": let node1 = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20302)) diff --git a/tests/p2p/test_discoveryv5_encoding.nim b/tests/p2p/test_discoveryv5_encoding.nim index cff6662..126d61e 100644 --- a/tests/p2p/test_discoveryv5_encoding.nim +++ b/tests/p2p/test_discoveryv5_encoding.nim @@ -2,12 +2,11 @@ import std/[options, sequtils, tables], - unittest2, + chronos, + asynctest/unittest2, stint, stew/byteutils, stew/shims/net, eth/[keys,rlp], - ../../eth/p2p/discoveryv5/[messages, messages_encoding, encoding, enr, node, sessions] - -let rng = newRng() + libp2pdht/discv5/[messages, messages_encoding, encoding, enr, node, sessions] suite "Discovery v5.1 Protocol Message Encodings": test "Ping Request": @@ -31,7 +30,7 @@ suite "Discovery v5.1 Protocol Message Encodings": test "Pong Response": let enrSeq = 1'u64 - ip = IpAddress(family: IPv4, address_v4: [127.byte, 0, 0, 1]) + ip = IpAddress(family: IpAddressFamily.IPv4, address_v4: [127.byte, 0, 0, 1]) port = 5000'u16 p = PongMessage(enrSeq: enrSeq, ip: ip, port: port) reqId = RequestId(id: @[1.byte]) @@ -251,26 +250,31 @@ suite "Discovery v5.1 Packet Encodings Test Vectors": const nodeAKey = "0xeef77acb6c6a6eebc5b363a475ac583ec7eccdb42b6481424c60f59aa326547f" nodeBKey = "0x66fb62bfbd66b9177a138c1e5cddbe4f7c30c343e94e68df8769459cb1cde628" - setup: - let - privKeyA = PrivateKey.fromHex(nodeAKey)[] # sender -> encode - privKeyB = PrivateKey.fromHex(nodeBKey)[] # receive -> decode + var + codecA, codecB: Codec + nodeA, nodeB: Node + privKeyA, privKeyB: PrivateKey + + setup: + privKeyA = PrivateKey.fromHex(nodeAKey)[] # sender -> encode + privKeyB = PrivateKey.fromHex(nodeBKey)[] # receive -> decode + + let enrRecA = enr.Record.init(1, privKeyA, some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(Port(9000))).expect("Properly intialized private key") - nodeA = newNode(enrRecA).expect("Properly initialized record") enrRecB = enr.Record.init(1, privKeyB, some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(Port(9000))).expect("Properly intialized private key") - nodeB = newNode(enrRecB).expect("Properly initialized record") - var - codecA {.used.} = Codec(localNode: nodeA, privKey: privKeyA, - sessions: Sessions.init(5)) - codecB = Codec(localNode: nodeB, privKey: privKeyB, - sessions: Sessions.init(5)) + nodeA = newNode(enrRecA).expect("Properly initialized record") + nodeB = newNode(enrRecB).expect("Properly initialized record") + codecA = Codec(localNode: nodeA, privKey: privKeyA, + sessions: Sessions.init(5)) + codecB = Codec(localNode: nodeB, privKey: privKeyB, + sessions: Sessions.init(5)) test "Ping Ordinary Message Packet": const @@ -420,6 +424,8 @@ suite "Discovery v5.1 Packet Encodings Test Vectors": hexToSeqByte(encodedPacket & "00")).isErr() suite "Discovery v5.1 Additional Encode/Decode": + var rng = newRng() + test "Encryption/Decryption": let encryptionKey = hexToByteArray[aesKeySize]("0x9f2d77db7004bf8a1a85107ac686990b") @@ -475,24 +481,28 @@ suite "Discovery v5.1 Additional Encode/Decode": check decoded.isOk() - setup: - let - privKeyA = PrivateKey.random(rng[]) # sender -> encode - privKeyB = PrivateKey.random(rng[]) # receiver -> decode + var + codecA, codecB: Codec + nodeA, nodeB: Node + privKeyA, privKeyB: PrivateKey + setup: + privKeyA = PrivateKey.random(rng[]) # sender -> encode + privKeyB = PrivateKey.random(rng[]) # receiver -> decode + + let enrRecA = enr.Record.init(1, privKeyA, some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(Port(9000))).expect("Properly intialized private key") - nodeA = newNode(enrRecA).expect("Properly initialized record") enrRecB = enr.Record.init(1, privKeyB, some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(Port(9000))).expect("Properly intialized private key") - nodeB = newNode(enrRecB).expect("Properly initialized record") - var - codecA = Codec(localNode: nodeA, privKey: privKeyA, sessions: Sessions.init(5)) - codecB = Codec(localNode: nodeB, privKey: privKeyB, sessions: Sessions.init(5)) + nodeA = newNode(enrRecA).expect("Properly initialized record") + nodeB = newNode(enrRecB).expect("Properly initialized record") + codecA = Codec(localNode: nodeA, privKey: privKeyA, sessions: Sessions.init(5)) + codecB = Codec(localNode: nodeB, privKey: privKeyB, sessions: Sessions.init(5)) test "Encode / Decode Ordinary Random Message Packet": let diff --git a/tests/testdht.nim b/tests/testdht.nim new file mode 100644 index 0000000..353a4cd --- /dev/null +++ b/tests/testdht.nim @@ -0,0 +1,2 @@ +import + ./dht/test_providers \ No newline at end of file diff --git a/tests/testp2p.nim b/tests/testp2p.nim new file mode 100644 index 0000000..e33dac7 --- /dev/null +++ b/tests/testp2p.nim @@ -0,0 +1,2 @@ +import + ./p2p/[test_discoveryv5, test_discoveryv5_encoding] \ No newline at end of file