2023-11-21 16:16:15 +01:00
|
|
|
# Fluffy
|
2024-02-28 18:31:45 +01:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2023-01-31 13:38:08 +01: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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-02-28 18:31:45 +01:00
|
|
|
import std/strutils, confutils, chronos, stint, eth/p2p/discoveryv5/routing_table
|
2022-01-18 09:01:22 +01:00
|
|
|
|
|
|
|
type
|
2024-06-18 15:32:57 +08:00
|
|
|
PortalNetwork* = enum
|
|
|
|
none
|
|
|
|
mainnet
|
|
|
|
angelfood
|
|
|
|
|
|
|
|
# The Portal sub-protocols
|
|
|
|
PortalSubnetwork* = enum
|
|
|
|
state
|
|
|
|
history
|
|
|
|
beacon
|
|
|
|
transactionIndex
|
|
|
|
verkleState
|
|
|
|
transactionGossip
|
|
|
|
|
2022-05-12 18:04:37 +02:00
|
|
|
RadiusConfigKind* = enum
|
2024-02-28 18:31:45 +01:00
|
|
|
Static
|
|
|
|
Dynamic
|
2022-05-12 18:04:37 +02:00
|
|
|
|
|
|
|
RadiusConfig* = object
|
|
|
|
case kind*: RadiusConfigKind
|
|
|
|
of Static:
|
|
|
|
logRadius*: uint16
|
|
|
|
of Dynamic:
|
|
|
|
discard
|
|
|
|
|
2022-01-18 09:01:22 +01:00
|
|
|
PortalProtocolConfig* = object
|
|
|
|
tableIpLimits*: TableIpLimits
|
|
|
|
bitsPerHop*: int
|
2022-05-12 18:04:37 +02:00
|
|
|
radiusConfig*: RadiusConfig
|
2023-08-30 11:01:00 +03:00
|
|
|
disablePoke*: bool
|
2024-09-25 15:15:20 +08:00
|
|
|
maxGossipNodes*: int
|
2024-10-16 21:05:39 +08:00
|
|
|
contentCacheSize*: int
|
|
|
|
disableContentCache*: bool
|
2022-01-18 09:01:22 +01:00
|
|
|
|
|
|
|
const
|
2022-05-12 18:04:37 +02:00
|
|
|
defaultRadiusConfig* = RadiusConfig(kind: Dynamic)
|
2022-05-23 23:23:24 +02:00
|
|
|
defaultRadiusConfigDesc* = $defaultRadiusConfig.kind
|
2023-08-30 11:01:00 +03:00
|
|
|
defaultDisablePoke* = false
|
2024-10-16 21:05:39 +08:00
|
|
|
defaultMaxGossipNodes* = 4
|
|
|
|
defaultContentCacheSize* = 100
|
|
|
|
defaultDisableContentCache* = false
|
2023-10-05 19:29:39 +02:00
|
|
|
revalidationTimeout* = chronos.seconds(30)
|
2022-05-12 18:04:37 +02:00
|
|
|
|
2022-01-18 09:01:22 +01:00
|
|
|
defaultPortalProtocolConfig* = PortalProtocolConfig(
|
2024-07-10 17:26:30 +02:00
|
|
|
tableIpLimits: DefaultTableIpLimits,
|
2022-05-12 18:04:37 +02:00
|
|
|
bitsPerHop: DefaultBitsPerHop,
|
2024-02-28 18:31:45 +01:00
|
|
|
radiusConfig: defaultRadiusConfig,
|
2024-09-25 15:15:20 +08:00
|
|
|
disablePoke: defaultDisablePoke,
|
|
|
|
maxGossipNodes: defaultMaxGossipNodes,
|
2024-10-16 21:05:39 +08:00
|
|
|
contentCacheSize: defaultContentCacheSize,
|
|
|
|
disableContentCache: defaultDisableContentCache,
|
2022-05-12 18:04:37 +02:00
|
|
|
)
|
2022-01-18 09:01:22 +01:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type PortalProtocolConfig,
|
|
|
|
tableIpLimit: uint,
|
|
|
|
bucketIpLimit: uint,
|
2022-05-12 18:04:37 +02:00
|
|
|
bitsPerHop: int,
|
2023-08-30 11:01:00 +03:00
|
|
|
radiusConfig: RadiusConfig,
|
2024-02-28 18:31:45 +01:00
|
|
|
disablePoke: bool,
|
2024-09-25 15:15:20 +08:00
|
|
|
maxGossipNodes: int,
|
2024-10-16 21:05:39 +08:00
|
|
|
contentCacheSize: int,
|
|
|
|
disableContentCache: bool,
|
2024-02-28 18:31:45 +01:00
|
|
|
): T =
|
2022-01-18 09:01:22 +01:00
|
|
|
PortalProtocolConfig(
|
2024-02-28 18:31:45 +01:00
|
|
|
tableIpLimits:
|
|
|
|
TableIpLimits(tableIpLimit: tableIpLimit, bucketIpLimit: bucketIpLimit),
|
2022-05-12 18:04:37 +02:00
|
|
|
bitsPerHop: bitsPerHop,
|
2023-08-30 11:01:00 +03:00
|
|
|
radiusConfig: radiusConfig,
|
2024-02-28 18:31:45 +01:00
|
|
|
disablePoke: disablePoke,
|
2024-09-25 15:15:20 +08:00
|
|
|
maxGossipNodes: maxGossipNodes,
|
2024-10-16 21:05:39 +08:00
|
|
|
contentCacheSize: contentCacheSize,
|
|
|
|
disableContentCache: disableContentCache,
|
2022-01-18 09:01:22 +01:00
|
|
|
)
|
|
|
|
|
2023-11-23 18:49:15 +01:00
|
|
|
func fromLogRadius*(T: type UInt256, logRadius: uint16): T =
|
2023-11-21 16:16:15 +01:00
|
|
|
# Get the max value of the logRadius range
|
|
|
|
pow((2).stuint(256), logRadius) - 1
|
|
|
|
|
|
|
|
## Confutils parsers
|
|
|
|
|
2024-02-28 18:31:45 +01:00
|
|
|
proc parseCmdArg*(T: type RadiusConfig, p: string): T {.raises: [ValueError].} =
|
2022-05-12 18:04:37 +02:00
|
|
|
if p.startsWith("dynamic") and len(p) == 7:
|
2022-05-23 23:23:24 +02:00
|
|
|
RadiusConfig(kind: Dynamic)
|
2022-05-12 18:04:37 +02:00
|
|
|
elif p.startsWith("static:"):
|
2024-02-28 18:31:45 +01:00
|
|
|
let num = p[7 ..^ 1]
|
2022-05-23 23:23:24 +02:00
|
|
|
let parsed =
|
|
|
|
try:
|
|
|
|
uint16.parseCmdArg(num)
|
|
|
|
except ValueError:
|
|
|
|
let msg = "Provided logRadius: " & num & " is not a valid number"
|
2023-08-04 19:43:30 +07:00
|
|
|
raise newException(ValueError, msg)
|
2022-05-12 18:04:37 +02:00
|
|
|
|
2022-05-23 23:23:24 +02:00
|
|
|
if parsed > 256:
|
2024-02-28 18:31:45 +01:00
|
|
|
raise newException(ValueError, "Provided logRadius should be <= 256")
|
2022-05-23 23:23:24 +02:00
|
|
|
|
|
|
|
RadiusConfig(kind: Static, logRadius: parsed)
|
2022-05-12 18:04:37 +02:00
|
|
|
else:
|
2022-05-23 23:23:24 +02:00
|
|
|
let parsed =
|
|
|
|
try:
|
|
|
|
uint16.parseCmdArg(p)
|
|
|
|
except ValueError:
|
|
|
|
let msg =
|
|
|
|
"Not supported radius config option: " & p & " . " &
|
|
|
|
"Supported options: dynamic and static:logRadius"
|
2023-08-04 19:43:30 +07:00
|
|
|
raise newException(ValueError, msg)
|
2022-05-23 23:23:24 +02:00
|
|
|
|
|
|
|
if parsed > 256:
|
2024-02-28 18:31:45 +01:00
|
|
|
raise newException(ValueError, "Provided logRadius should be <= 256")
|
2022-05-23 23:23:24 +02:00
|
|
|
|
|
|
|
RadiusConfig(kind: Static, logRadius: parsed)
|
2022-05-12 18:04:37 +02:00
|
|
|
|
2023-01-31 13:38:08 +01:00
|
|
|
proc completeCmdArg*(T: type RadiusConfig, val: string): seq[string] =
|
2022-05-12 18:04:37 +02:00
|
|
|
return @[]
|