2021-07-15 13:12:33 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
stew/shims/net,
|
|
|
|
eth/keys,
|
|
|
|
eth/p2p/discoveryv5/[enr, node, routing_table],
|
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol
|
|
|
|
|
|
|
|
proc localAddress*(port: int): Address =
|
|
|
|
Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port))
|
|
|
|
|
2022-06-24 13:35:31 +00:00
|
|
|
proc initDiscoveryNode*(
|
|
|
|
rng: ref BrHmacDrbgContext,
|
2021-07-15 13:12:33 +00:00
|
|
|
privKey: PrivateKey,
|
|
|
|
address: Address,
|
2021-12-13 08:06:29 +00:00
|
|
|
bootstrapRecords: openArray[Record] = [],
|
|
|
|
localEnrFields: openArray[(string, seq[byte])] = [],
|
2021-07-15 13:12:33 +00:00
|
|
|
previousRecord = none[enr.Record]()): discv5_protocol.Protocol =
|
|
|
|
# set bucketIpLimit to allow bucket split
|
2022-02-02 21:48:33 +00:00
|
|
|
let config = DiscoveryConfig.init(1000, 24, 5)
|
2021-07-15 13:12:33 +00:00
|
|
|
|
|
|
|
result = newProtocol(privKey,
|
|
|
|
some(address.ip),
|
|
|
|
some(address.port), some(address.port),
|
|
|
|
bindPort = address.port,
|
|
|
|
bootstrapRecords = bootstrapRecords,
|
|
|
|
localEnrFields = localEnrFields,
|
|
|
|
previousRecord = previousRecord,
|
2022-02-02 21:48:33 +00:00
|
|
|
config = config,
|
2021-07-15 13:12:33 +00:00
|
|
|
rng = rng)
|
|
|
|
|
|
|
|
result.open()
|
2022-05-12 16:04:37 +00:00
|
|
|
|
|
|
|
proc genByteSeq*(length: int): seq[byte] =
|
|
|
|
var i = 0
|
|
|
|
var resultSeq = newSeq[byte](length)
|
|
|
|
while i < length:
|
|
|
|
resultSeq[i] = byte(i)
|
|
|
|
inc i
|
|
|
|
return resultSeq
|