diff --git a/discv5/README.md b/discv5/README.md new file mode 100644 index 0000000..45e7391 --- /dev/null +++ b/discv5/README.md @@ -0,0 +1,4 @@ +# discv5 feasibility study + +This contains the study as documented by the issue [vacp2p/research#15](https://github.com/vacp2p/research/issues/15). +Semi-inspired by [zilm13/discv5](https://github.com/zilm13/discv5). diff --git a/discv5/discv5.nimble b/discv5/discv5.nimble new file mode 100644 index 0000000..b3ce5a1 --- /dev/null +++ b/discv5/discv5.nimble @@ -0,0 +1,16 @@ +# Package + +version = "0.1.0" +author = "decanus" +description = "A new awesome nimble package" +license = "MIT" +srcDir = "src" +bin = @["discv5"] + + + +# Dependencies + +requires "nim >= 1.0.6", + "eth", + "confutils" diff --git a/discv5/src/discv5.nim b/discv5/src/discv5.nim new file mode 100644 index 0000000..7e8d321 --- /dev/null +++ b/discv5/src/discv5.nim @@ -0,0 +1,208 @@ +import + random, chronos, sequtils, chronicles, tables, stint, options, std/bitops, sequtils, + eth/[keys, rlp, async_utils], eth/p2p/enode, eth/trie/db, + eth/p2p/discoveryv5/[discovery_db, enr, node, types, routing_table, encoding], + eth/p2p/discoveryv5/protocol as discv5_protocol, + ./utils + +const + # the amount of nodes + N = 100 + + MAX_LOOKUPS = 100 + RUNS = 100 + + # the cooldown period between runs. + COOLDOWN = 0 + + # the sleep period before starting our runs. + SLEEP = 600 + VERBOSE = true + + # if true, nodes are randomly added to other nodes using the `addNode` function. + # otherwise we use discv5s native paring functionality letting each node find peers using the boostrap. + USE_MANUAL_PAIRING = false + + # when manual pairing is enabled this indicates the amount of nodes to pair with. + PEERS_PER_NODE = 16 + + # True if looking for a node with field rather than a specific node + LOOK_FOR_FIELD = true + + # The amount of nodes that will have our specific field to look for + LOOKUP_FIELD_DISTRIBUTION = 5 + +proc write(str: string) = + if VERBOSE: + echo str + +proc runWith(node: discv5_protocol.Protocol, nodes: seq[discv5_protocol.Protocol]) {.async.} = + randomize() + + let target = sample(nodes).localNode + let tid = recordToNodeID(target.record) + + var peer: Node + while true: + randomize() + peer = sample(nodes).localNode + if peer.record.toUri() != target.record.toUri(): + break + + var distance = logDist(recordToNodeID(peer.record), tid) + + var called = newSeq[string](0) + + for i in 0..= 1: + echo i + 1 + return + + if lookup.len == 0: + write("Lookup from node " & $((get peer.record.toTypedRecord()).udp.get()) & " found no results at 256") + return + + peer = sample(lookup) + + echo "Not found in max iterations" + +proc runWithRandom(node: discv5_protocol.Protocol, nodes: seq[discv5_protocol.Protocol]) {.async.} = + randomize() + + let target = sample(nodes).localNode + let tid = recordToNodeID(target.record) + + var peer: Node + while true: + randomize() + peer = sample(nodes).localNode + if peer.record.toUri() != target.record.toUri(): + break + + var called = newSeq[string](0) + + for i in 0.. 0: @[nodes[0].localNode.record] else: @[], + if i mod divisor == 0: 1 else: 0 + ) + nodes.add(node) + + if (USE_MANUAL_PAIRING and i == 0) or not USE_MANUAL_PAIRING: + node.start() + + if USE_MANUAL_PAIRING: + for n in nodes: + pair(n, nodes) + + if not USE_MANUAL_PAIRING: + echo "Sleeping for ", SLEEP, " seconds" + await sleepAsync(SLEEP.seconds) + + let node = initDiscoveryNode(PrivateKey.random().get, localAddress(20300 + N), @[nodes[0].localNode.record], 0) + + for i in 0..