Add DiscoveryConfig to tune routing table ip limits and bitPerHops (#471)

This commit is contained in:
Kim De Mey 2022-02-02 15:29:45 +01:00 committed by GitHub
parent cf95b688e6
commit 41edd4a3f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 55 deletions

View File

@ -116,6 +116,10 @@ const
## call ## call
type type
DiscoveryConfig* = object
tableIpLimits*: TableIpLimits
bitsPerHop*: int
Protocol* = ref object Protocol* = ref object
transp: DatagramTransport transp: DatagramTransport
localNode*: Node localNode*: Node
@ -148,6 +152,11 @@ type
DiscResult*[T] = Result[T, cstring] DiscResult*[T] = Result[T, cstring]
const
defaultDiscoveryConfig* = DiscoveryConfig(
tableIpLimits: DefaultTableIpLimits,
bitsPerHop: DefaultBitsPerHop)
proc addNode*(d: Protocol, node: Node): bool = proc addNode*(d: Protocol, node: Node): bool =
## Add `Node` to discovery routing table. ## Add `Node` to discovery routing table.
## ##
@ -880,18 +889,32 @@ proc ipMajorityLoop(d: Protocol) {.async.} =
except CancelledError: except CancelledError:
trace "ipMajorityLoop canceled" trace "ipMajorityLoop canceled"
proc newProtocol*(privKey: PrivateKey, func init*(
enrIp: Option[ValidIpAddress], T: type DiscoveryConfig,
enrTcpPort, enrUdpPort: Option[Port], tableIpLimit: uint,
localEnrFields: openArray[(string, seq[byte])] = [], bucketIpLimit: uint,
bootstrapRecords: openArray[Record] = [], bitsPerHop: int): T =
previousRecord = none[enr.Record](),
bindPort: Port, DiscoveryConfig(
bindIp = IPv4_any(), tableIpLimits: TableIpLimits(
enrAutoUpdate = false, tableIpLimit: tableIpLimit,
tableIpLimits = DefaultTableIpLimits, bucketIpLimit: bucketIpLimit),
rng = newRng()): bitsPerHop: bitsPerHop
Protocol = )
proc newProtocol*(
privKey: PrivateKey,
enrIp: Option[ValidIpAddress],
enrTcpPort, enrUdpPort: Option[Port],
localEnrFields: openArray[(string, seq[byte])] = [],
bootstrapRecords: openArray[Record] = [],
previousRecord = none[enr.Record](),
bindPort: Port,
bindIp = IPv4_any(),
enrAutoUpdate = false,
config = defaultDiscoveryConfig,
rng = newRng()):
Protocol =
# TODO: Tried adding bindPort = udpPort as parameter but that gave # TODO: Tried adding bindPort = udpPort as parameter but that gave
# "Error: internal error: environment misses: udpPort" in nim-beacon-chain. # "Error: internal error: environment misses: udpPort" in nim-beacon-chain.
# Anyhow, nim-beacon-chain would also require some changes to support port # Anyhow, nim-beacon-chain would also require some changes to support port
@ -933,7 +956,8 @@ proc newProtocol*(privKey: PrivateKey,
bootstrapRecords: @bootstrapRecords, bootstrapRecords: @bootstrapRecords,
ipVote: IpVote.init(), ipVote: IpVote.init(),
enrAutoUpdate: enrAutoUpdate, enrAutoUpdate: enrAutoUpdate,
routingTable: RoutingTable.init(node, DefaultBitsPerHop, tableIpLimits, rng), routingTable: RoutingTable.init(
node, config.bitsPerHop, config.tableIpLimits, rng),
rng: rng) rng: rng)
template listeningAddress*(p: Protocol): Address = template listeningAddress*(p: Protocol): Address =

View File

@ -9,26 +9,31 @@ export net
proc localAddress*(port: int): Address = proc localAddress*(port: int): Address =
Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port)) Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port))
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext, privKey: PrivateKey, proc initDiscoveryNode*(
address: Address, rng: ref BrHmacDrbgContext,
bootstrapRecords: openArray[Record] = [], privKey: PrivateKey,
localEnrFields: openArray[(string, seq[byte])] = [], address: Address,
previousRecord = none[enr.Record]()): bootstrapRecords: openArray[Record] = [],
discv5_protocol.Protocol = localEnrFields: openArray[(string, seq[byte])] = [],
previousRecord = none[enr.Record]()):
discv5_protocol.Protocol =
# set bucketIpLimit to allow bucket split # set bucketIpLimit to allow bucket split
let tableIpLimits = TableIpLimits(tableIpLimit: 1000, bucketIpLimit: 24) let config = DiscoveryConfig.init(1000, 24, 5)
result = newProtocol(privKey, let protocol = newProtocol(
some(address.ip), privKey,
some(address.port), some(address.port), some(address.ip),
bindPort = address.port, some(address.port), some(address.port),
bootstrapRecords = bootstrapRecords, bindPort = address.port,
localEnrFields = localEnrFields, bootstrapRecords = bootstrapRecords,
previousRecord = previousRecord, localEnrFields = localEnrFields,
tableIpLimits = tableIpLimits, previousRecord = previousRecord,
rng = rng) config = config,
rng = rng)
result.open() protocol.open()
protocol
proc nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool = proc nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool =
for n in nodes: for n in nodes:

View File

@ -14,31 +14,8 @@ import
../../eth/p2p/discoveryv5/[enr, node, routing_table], ../../eth/p2p/discoveryv5/[enr, node, routing_table],
../../eth/p2p/discoveryv5/protocol as discv5_protocol, ../../eth/p2p/discoveryv5/protocol as discv5_protocol,
../../eth/utp/utp_discv5_protocol, ../../eth/utp/utp_discv5_protocol,
../../eth/keys ../../eth/keys,
../p2p/discv5_test_helper
proc localAddress*(port: int): Address =
Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port))
proc initDiscoveryNode*(rng: ref BrHmacDrbgContext,
privKey: PrivateKey,
address: Address,
bootstrapRecords: openArray[Record] = [],
localEnrFields: openArray[(string, seq[byte])] = [],
previousRecord = none[enr.Record]()): discv5_protocol.Protocol =
# set bucketIpLimit to allow bucket split
let tableIpLimits = TableIpLimits(tableIpLimit: 1000, bucketIpLimit: 24)
result = newProtocol(privKey,
some(address.ip),
some(address.port), some(address.port),
bindPort = address.port,
bootstrapRecords = bootstrapRecords,
localEnrFields = localEnrFields,
previousRecord = previousRecord,
tableIpLimits = tableIpLimits,
rng = rng)
result.open()
proc generateByteArray(rng: var BrHmacDrbgContext, length: int): seq[byte] = proc generateByteArray(rng: var BrHmacDrbgContext, length: int): seq[byte] =
var bytes = newSeq[byte](length) var bytes = newSeq[byte](length)