2021-07-15 13:12:33 +00:00
|
|
|
# Nimbus - Portal Network
|
2024-01-24 15:28:03 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-07-15 13:12:33 +00:00
|
|
|
# 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.
|
|
|
|
|
2024-01-09 15:09:02 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2021-07-15 13:12:33 +00:00
|
|
|
import
|
2024-01-24 15:28:03 +00:00
|
|
|
std/net,
|
2024-09-29 12:37:09 +00:00
|
|
|
eth/common/keys,
|
2021-07-15 13:12:33 +00:00
|
|
|
eth/p2p/discoveryv5/[enr, node, routing_table],
|
2024-09-24 11:07:20 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol
|
2021-07-15 13:12:33 +00:00
|
|
|
|
2024-01-09 15:09:02 +00:00
|
|
|
proc localAddress*(port: int): Address {.raises: [ValueError].} =
|
2023-11-10 18:38:11 +00:00
|
|
|
Address(ip: parseIpAddress("127.0.0.1"), port: Port(port))
|
2021-07-15 13:12:33 +00:00
|
|
|
|
2022-06-24 13:35:31 +00:00
|
|
|
proc initDiscoveryNode*(
|
2022-07-04 07:38:02 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
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])] = [],
|
2024-06-19 01:57:45 +00:00
|
|
|
previousRecord = Opt.none(enr.Record),
|
2024-02-28 17:31:45 +00:00
|
|
|
): discv5_protocol.Protocol {.raises: [CatchableError].} =
|
2021-07-15 13:12:33 +00:00
|
|
|
# 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
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
result = newProtocol(
|
|
|
|
privKey,
|
2024-06-19 01:57:45 +00:00
|
|
|
Opt.some(address.ip),
|
|
|
|
Opt.some(address.port),
|
|
|
|
Opt.some(address.port),
|
2021-07-15 13:12:33 +00:00
|
|
|
bindPort = address.port,
|
|
|
|
bootstrapRecords = bootstrapRecords,
|
|
|
|
localEnrFields = localEnrFields,
|
|
|
|
previousRecord = previousRecord,
|
2022-02-02 21:48:33 +00:00
|
|
|
config = config,
|
2024-02-28 17:31:45 +00:00
|
|
|
rng = rng,
|
|
|
|
)
|
2021-07-15 13:12:33 +00:00
|
|
|
|
|
|
|
result.open()
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2022-07-04 07:38:02 +00:00
|
|
|
proc genByteSeq*(length: int): seq[byte] =
|
2022-05-12 16:04:37 +00:00
|
|
|
var i = 0
|
|
|
|
var resultSeq = newSeq[byte](length)
|
|
|
|
while i < length:
|
|
|
|
resultSeq[i] = byte(i)
|
|
|
|
inc i
|
|
|
|
return resultSeq
|