-
Notifications
You must be signed in to change notification settings - Fork 108
/
portal_protocol_config.nim
92 lines (77 loc) · 2.51 KB
/
portal_protocol_config.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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: [].}
import
std/strutils,
confutils,
eth/p2p/discoveryv5/routing_table
type
RadiusConfigKind* = enum
Static, Dynamic
RadiusConfig* = object
case kind*: RadiusConfigKind
of Static:
logRadius*: uint16
of Dynamic:
discard
PortalProtocolConfig* = object
tableIpLimits*: TableIpLimits
bitsPerHop*: int
radiusConfig*: RadiusConfig
const
defaultRadiusConfig* = RadiusConfig(kind: Dynamic)
defaultRadiusConfigDesc* = $defaultRadiusConfig.kind
defaultPortalProtocolConfig* = PortalProtocolConfig(
tableIpLimits: DefaultTableIpLimits,
bitsPerHop: DefaultBitsPerHop,
radiusConfig: defaultRadiusConfig
)
proc init*(
T: type PortalProtocolConfig,
tableIpLimit: uint,
bucketIpLimit: uint,
bitsPerHop: int,
radiusConfig: RadiusConfig): T =
PortalProtocolConfig(
tableIpLimits: TableIpLimits(
tableIpLimit: tableIpLimit,
bucketIpLimit: bucketIpLimit),
bitsPerHop: bitsPerHop,
radiusConfig: radiusConfig
)
proc parseCmdArg*(T: type RadiusConfig, p: string): T
{.raises: [ValueError].} =
if p.startsWith("dynamic") and len(p) == 7:
RadiusConfig(kind: Dynamic)
elif p.startsWith("static:"):
let num = p[7..^1]
let parsed =
try:
uint16.parseCmdArg(num)
except ValueError:
let msg = "Provided logRadius: " & num & " is not a valid number"
raise newException(ValueError, msg)
if parsed > 256:
raise newException(
ValueError, "Provided logRadius should be <= 256"
)
RadiusConfig(kind: Static, logRadius: parsed)
else:
let parsed =
try:
uint16.parseCmdArg(p)
except ValueError:
let msg =
"Not supported radius config option: " & p & " . " &
"Supported options: dynamic and static:logRadius"
raise newException(ValueError, msg)
if parsed > 256:
raise newException(
ValueError, "Provided logRadius should be <= 256")
RadiusConfig(kind: Static, logRadius: parsed)
proc completeCmdArg*(T: type RadiusConfig, val: string): seq[string] =
return @[]