From d0c777c8ef310ea86d2ee3cf14f3071d0c122bdb Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Mon, 12 Jun 2023 23:00:07 +0200 Subject: [PATCH] making this work with nimble - moving das.nim to root directory nimble dependencies work strange .... - adding das target in .nimble file - adding missing tostring - copy test-helper to main folder (otherwise compilation errors) Signed-off-by: Csaba Kiraly --- das/das.nim => das.nim | 2 +- libp2pdht.nimble | 1 + .../private/eth/p2p/discoveryv5/protocol.nim | 3 + test_helper.nim | 131 ++++++++++++++++++ 4 files changed, 136 insertions(+), 1 deletion(-) rename das/das.nim => das.nim (99%) create mode 100644 test_helper.nim diff --git a/das/das.nim b/das.nim similarity index 99% rename from das/das.nim rename to das.nim index fee698c..946d742 100644 --- a/das/das.nim +++ b/das.nim @@ -5,7 +5,7 @@ import libp2pdht/dht, libp2pdht/discv5/crypto as dhtcrypto, libp2pdht/discv5/protocol as discv5_protocol, - tests/dht/test_helper + test_helper logScope: topics = "DAS emulator" diff --git a/libp2pdht.nimble b/libp2pdht.nimble index 952e1d4..9d403a5 100644 --- a/libp2pdht.nimble +++ b/libp2pdht.nimble @@ -6,6 +6,7 @@ description = "DHT based on the libp2p Kademlia spec" license = "MIT" skipDirs = @["tests"] +bin = @["das"] # Dependencies requires "nim >= 1.2.0", diff --git a/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim b/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim index c11578a..ca6133b 100644 --- a/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim +++ b/libp2pdht/private/eth/p2p/discoveryv5/protocol.nim @@ -184,6 +184,9 @@ type DiscResult*[T] = Result[T, cstring] +func `$`*(d: Protocol): string = + $d.localNode.id + const defaultDiscoveryConfig* = DiscoveryConfig( tableIpLimits: DefaultTableIpLimits, diff --git a/test_helper.nim b/test_helper.nim new file mode 100644 index 0000000..89f5797 --- /dev/null +++ b/test_helper.nim @@ -0,0 +1,131 @@ +import + bearssl/rand, + chronos, + libp2p/crypto/[crypto, secp], + libp2p/multiaddress, + libp2pdht/discv5/[node, routing_table, spr], + libp2pdht/discv5/crypto as dhtcrypto, + libp2pdht/discv5/protocol as discv5_protocol, + stew/shims/net + +export net + +proc localAddress*(port: int): Address = + Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port)) + +proc example*(T: type PrivateKey, rng: ref HmacDrbgContext): PrivateKey = + PrivateKey.random(rng[]).expect("Valid rng for private key") + +proc example*(T: type NodeId, rng: ref HmacDrbgContext): NodeId = + let + privKey = PrivateKey.example(rng) + pubKey = privKey.getPublicKey.expect("Valid private key for public key") + pubKey.toNodeId().expect("Public key valid for node id") + +proc initDiscoveryNode*( + rng: ref HmacDrbgContext, + privKey: PrivateKey, + address: Address, + bootstrapRecords: openArray[SignedPeerRecord] = [], + localEnrFields: openArray[(string, seq[byte])] = [], + previousRecord = none[SignedPeerRecord]()): + discv5_protocol.Protocol = + # set bucketIpLimit to allow bucket split + let config = DiscoveryConfig.init(1000, 24, 5) + + let protocol = newProtocol( + privKey, + some(address.ip), + some(address.port), + some(address.port), + bindPort = address.port, + bootstrapRecords = bootstrapRecords, + localEnrFields = localEnrFields, + previousRecord = previousRecord, + config = config, + rng = rng) + + protocol.open() + + protocol + +proc nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool = + for n in nodes: + if id == n.id: return true + +proc generateNode*(privKey: PrivateKey, port: int = 20302, + ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node = + + let + port = Port(port) + spr = SignedPeerRecord.init(1, privKey, some(ip), some(port), some(port)) + .expect("Properly intialized private key") + result = newNode(spr).expect("Properly initialized node") + +proc generateNRandomNodes*(rng: ref HmacDrbgContext, n: int): seq[Node] = + var res = newSeq[Node]() + for i in 1..n: + let + privKey = PrivateKey.example(rng) + node = privKey.generateNode() + res.add(node) + res + +proc nodeAndPrivKeyAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32, + ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): (Node, PrivateKey) = + while true: + let + privKey = PrivateKey.random(rng).expect("Valid rng for private key") + node = privKey.generateNode(ip = ip) + if logDistance(n.id, node.id) == d: + return (node, privKey) + +proc nodeAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32, + ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node = + let (node, _) = n.nodeAndPrivKeyAtDistance(rng, d, ip) + node + +proc nodesAtDistance*( + n: Node, rng: var HmacDrbgContext, d: uint32, amount: int, + ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] = + for i in 0..