mirror of
https://github.com/codex-storage/nim-codex-dht.git
synced 2025-01-10 11:55:49 +00:00
feat: reorg project to add nimble build system
Project has been updated to use nimble as a build system. All required dependencies have been added. All nim-eth mirrored files were added to a private folder in the libp2pdht module. A libp2pdht/discv5 module was added to alias the nim-eth modules (which will change over time). Test have been updated to use status-im/asynctest. This PR uses a branch of asynctest that supports async suite before/after. This seemed like the only the tests would work without throwing gcsafe errors. All tests working.
This commit is contained in:
parent
617a98bd01
commit
acd03ff831
@ -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"
|
@ -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
|
@ -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")
|
@ -1 +0,0 @@
|
||||
switch("path", "$projectDir/../src")
|
@ -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
|
5
libp2pdht.nim
Normal file
5
libp2pdht.nim
Normal file
@ -0,0 +1,5 @@
|
||||
import
|
||||
./libp2pdht/dht,
|
||||
./libp2pdht/discv5
|
||||
|
||||
export dht, discv5
|
24
libp2pdht.nimble
Normal file
24
libp2pdht.nimble
Normal file
@ -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"
|
4
libp2pdht/dht.nim
Normal file
4
libp2pdht/dht.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
./dht/providers
|
||||
|
||||
export providers
|
@ -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
|
@ -1,5 +1,5 @@
|
||||
import
|
||||
../eth/p2p/discoveryv5/[node],
|
||||
../discv5/[node],
|
||||
libp2p/routing_record,
|
||||
libp2p/protobuf/minprotobuf,
|
||||
./providers_messages
|
@ -1,5 +1,5 @@
|
||||
import
|
||||
../eth/p2p/discoveryv5/[node],
|
||||
../discv5/[node],
|
||||
libp2p/routing_record
|
||||
|
||||
type
|
4
libp2pdht/discv5.nim
Normal file
4
libp2pdht/discv5.nim
Normal file
@ -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
|
4
libp2pdht/discv5/encoding.nim
Normal file
4
libp2pdht/discv5/encoding.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/encoding
|
||||
|
||||
export encoding
|
4
libp2pdht/discv5/enr.nim
Normal file
4
libp2pdht/discv5/enr.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/enr
|
||||
|
||||
export enr
|
4
libp2pdht/discv5/messages.nim
Normal file
4
libp2pdht/discv5/messages.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/messages
|
||||
|
||||
export messages
|
4
libp2pdht/discv5/messages_encoding.nim
Normal file
4
libp2pdht/discv5/messages_encoding.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/messages_encoding
|
||||
|
||||
export messages_encoding
|
4
libp2pdht/discv5/node.nim
Normal file
4
libp2pdht/discv5/node.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/node
|
||||
|
||||
export node
|
4
libp2pdht/discv5/nodes_verification.nim
Normal file
4
libp2pdht/discv5/nodes_verification.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/nodes_verification
|
||||
|
||||
export nodes_verification
|
4
libp2pdht/discv5/protocol.nim
Normal file
4
libp2pdht/discv5/protocol.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/protocol
|
||||
|
||||
export protocol
|
4
libp2pdht/discv5/routing_table.nim
Normal file
4
libp2pdht/discv5/routing_table.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/routing_table
|
||||
|
||||
export routing_table
|
4
libp2pdht/discv5/sessions.nim
Normal file
4
libp2pdht/discv5/sessions.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/sessions
|
||||
|
||||
export sessions
|
4
libp2pdht/discv5/transport.nim
Normal file
4
libp2pdht/discv5/transport.nim
Normal file
@ -0,0 +1,4 @@
|
||||
import
|
||||
../private/eth/p2p/discoveryv5/transport
|
||||
|
||||
export transport
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
3
tests/nim.cfg
Normal file
3
tests/nim.cfg
Normal file
@ -0,0 +1,3 @@
|
||||
--path:".."
|
||||
--threads:on
|
||||
--tlsEmulation:off
|
@ -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
|
||||
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
|
2
tests/testdht.nim
Normal file
2
tests/testdht.nim
Normal file
@ -0,0 +1,2 @@
|
||||
import
|
||||
./dht/test_providers
|
2
tests/testp2p.nim
Normal file
2
tests/testp2p.nim
Normal file
@ -0,0 +1,2 @@
|
||||
import
|
||||
./p2p/[test_discoveryv5, test_discoveryv5_encoding]
|
Loading…
x
Reference in New Issue
Block a user