2023-01-31 12:38:08 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021-2023 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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-01-18 08:01:22 +00:00
|
|
|
import
|
2022-05-12 16:04:37 +00:00
|
|
|
std/strutils,
|
|
|
|
confutils,
|
2022-01-18 08:01:22 +00:00
|
|
|
eth/p2p/discoveryv5/routing_table
|
|
|
|
|
|
|
|
type
|
2022-05-12 16:04:37 +00:00
|
|
|
RadiusConfigKind* = enum
|
|
|
|
Static, Dynamic
|
|
|
|
|
|
|
|
RadiusConfig* = object
|
|
|
|
case kind*: RadiusConfigKind
|
|
|
|
of Static:
|
|
|
|
logRadius*: uint16
|
|
|
|
of Dynamic:
|
|
|
|
discard
|
|
|
|
|
2022-01-18 08:01:22 +00:00
|
|
|
PortalProtocolConfig* = object
|
|
|
|
tableIpLimits*: TableIpLimits
|
|
|
|
bitsPerHop*: int
|
2022-05-12 16:04:37 +00:00
|
|
|
radiusConfig*: RadiusConfig
|
2022-01-18 08:01:22 +00:00
|
|
|
|
|
|
|
const
|
2022-05-12 16:04:37 +00:00
|
|
|
defaultRadiusConfig* = RadiusConfig(kind: Dynamic)
|
2022-05-23 21:23:24 +00:00
|
|
|
defaultRadiusConfigDesc* = $defaultRadiusConfig.kind
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2022-01-18 08:01:22 +00:00
|
|
|
defaultPortalProtocolConfig* = PortalProtocolConfig(
|
|
|
|
tableIpLimits: DefaultTableIpLimits,
|
2022-05-12 16:04:37 +00:00
|
|
|
bitsPerHop: DefaultBitsPerHop,
|
|
|
|
radiusConfig: defaultRadiusConfig
|
|
|
|
)
|
2022-01-18 08:01:22 +00:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type PortalProtocolConfig,
|
|
|
|
tableIpLimit: uint,
|
|
|
|
bucketIpLimit: uint,
|
2022-05-12 16:04:37 +00:00
|
|
|
bitsPerHop: int,
|
|
|
|
radiusConfig: RadiusConfig): T =
|
2022-01-18 08:01:22 +00:00
|
|
|
|
|
|
|
PortalProtocolConfig(
|
|
|
|
tableIpLimits: TableIpLimits(
|
|
|
|
tableIpLimit: tableIpLimit,
|
|
|
|
bucketIpLimit: bucketIpLimit),
|
2022-05-12 16:04:37 +00:00
|
|
|
bitsPerHop: bitsPerHop,
|
|
|
|
radiusConfig: radiusConfig
|
2022-01-18 08:01:22 +00:00
|
|
|
)
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc parseCmdArg*(T: type RadiusConfig, p: string): T
|
2023-05-10 16:04:35 +00:00
|
|
|
{.raises: [ConfigurationError].} =
|
2022-05-12 16:04:37 +00:00
|
|
|
if p.startsWith("dynamic") and len(p) == 7:
|
2022-05-23 21:23:24 +00:00
|
|
|
RadiusConfig(kind: Dynamic)
|
2022-05-12 16:04:37 +00:00
|
|
|
elif p.startsWith("static:"):
|
|
|
|
let num = p[7..^1]
|
2022-05-23 21:23:24 +00:00
|
|
|
let parsed =
|
|
|
|
try:
|
|
|
|
uint16.parseCmdArg(num)
|
|
|
|
except ValueError:
|
|
|
|
let msg = "Provided logRadius: " & num & " is not a valid number"
|
|
|
|
raise newException(ConfigurationError, msg)
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2022-05-23 21:23:24 +00:00
|
|
|
if parsed > 256:
|
2022-05-12 16:04:37 +00:00
|
|
|
raise newException(
|
2022-05-23 21:23:24 +00:00
|
|
|
ConfigurationError, "Provided logRadius should be <= 256"
|
2022-05-12 16:04:37 +00:00
|
|
|
)
|
2022-05-23 21:23:24 +00:00
|
|
|
|
|
|
|
RadiusConfig(kind: Static, logRadius: parsed)
|
2022-05-12 16:04:37 +00:00
|
|
|
else:
|
2022-05-23 21:23:24 +00:00
|
|
|
let parsed =
|
|
|
|
try:
|
|
|
|
uint16.parseCmdArg(p)
|
|
|
|
except ValueError:
|
|
|
|
let msg =
|
|
|
|
"Not supported radius config option: " & p & " . " &
|
|
|
|
"Supported options: dynamic and static:logRadius"
|
|
|
|
raise newException(ConfigurationError, msg)
|
|
|
|
|
|
|
|
if parsed > 256:
|
|
|
|
raise newException(
|
|
|
|
ConfigurationError, "Provided logRadius should be <= 256")
|
|
|
|
|
|
|
|
RadiusConfig(kind: Static, logRadius: parsed)
|
2022-05-12 16:04:37 +00:00
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
proc completeCmdArg*(T: type RadiusConfig, val: string): seq[string] =
|
2022-05-12 16:04:37 +00:00
|
|
|
return @[]
|