This commit is contained in:
Dmitriy Ryajov 2024-06-27 16:38:36 -06:00 committed by GitHub
commit 2e8ef1fd44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 16 deletions

View File

@ -16,6 +16,10 @@ import std/cpuinfo
import pkg/chronos
import pkg/presto
import pkg/libp2p
import pkg/libp2p/protocols/connectivity/autonat/client
import pkg/libp2p/protocols/connectivity/autonat/service
import pkg/libp2p/protocols/connectivity/relay/client
import pkg/libp2p/services/[autorelayservice, hpservice]
import pkg/confutils
import pkg/confutils/defs
import pkg/nitro
@ -196,24 +200,73 @@ proc stop*(s: CodexServer) {.async.} =
s.repoStore.stop(),
s.maintenance.stop())
proc getAutonatService*(rng: ref HmacDrbgContext): AutonatService =
## AutonatService request other peers to dial us back
## flagging us as Reachable or NotReachable.
## minConfidence is used as threshold to determine the state.
## If maxQueueSize > numPeersToAsk past samples are considered
## in the calculation.
##
let
autonatService = AutonatService.new(
autonatClient = AutonatClient.new(),
rng = rng,
scheduleInterval = Opt.some(chronos.seconds(120)),
askNewConnectedPeers = false,
numPeersToAsk = 3,
maxQueueSize = 3,
minConfidence = 0.7)
proc statusAndConfidenceHandler(
networkReachability: NetworkReachability,
confidence: Opt[float]): Future[void] {.gcsafe, async.} =
if confidence.isSome():
info "Peer reachability status",
networkReachability = networkReachability, confidence = confidence.get()
autonatService.statusAndConfidenceHandler(statusAndConfidenceHandler)
return autonatService
proc new*(
T: type CodexServer,
config: CodexConf,
privateKey: CodexPrivateKey): CodexServer =
## create CodexServer including setting up datastore, repostore, etc
##
var
builder = SwitchBuilder.new()
.withPrivateKey(privateKey)
.withAddresses(config.listenAddrs)
.withRng(Rng.instance())
.withNoise()
.withYamux()
.withMplex(5.minutes, 5.minutes)
.withMaxConnections(config.maxPeers)
.withAgentVersion(config.agentString)
.withSignedPeerRecord(true)
.withTcpTransport({ServerFlags.ReuseAddr})
.withAutonat()
.withRendezVous()
# .withObservedAddrManager()
builder = if config.lpRelay:
builder.withCircuitRelay()
else:
let
relayClient = RelayClient.new()
autoRelayService = AutoRelayService.new(1, relayClient, nil, Rng.instance())
autonatService = getAutonatService(Rng.instance())
hpservice = HPService.new(autonatService, autoRelayService)
builder
.withCircuitRelay(relayClient)
.withServices(@[Service(hpservice)])
let
switch = SwitchBuilder
.new()
.withPrivateKey(privateKey)
.withAddresses(config.listenAddrs)
.withRng(Rng.instance())
.withNoise()
.withMplex(5.minutes, 5.minutes)
.withMaxConnections(config.maxPeers)
.withAgentVersion(config.agentString)
.withSignedPeerRecord(true)
.withTcpTransport({ServerFlags.ReuseAddr})
.build()
switch = builder.build
var
cache: CacheStore = nil

View File

@ -167,6 +167,10 @@ type
abbr: "b"
name: "bootstrap-node" }: seq[SignedPeerRecord]
lpRelay* {.
desc: "Should this node be a circuit relay"
name: "libp2p-relay" }: bool
maxPeers* {.
desc: "The maximum number of peers to connect to"
defaultValue: 160

View File

@ -17,10 +17,9 @@ import pkg/libp2p
import pkg/stew/shims/net
func remapAddr*(
address: MultiAddress,
ip: Option[ValidIpAddress] = ValidIpAddress.none,
port: Option[Port] = Port.none
): MultiAddress =
address: MultiAddress,
ip: Option[ValidIpAddress] = ValidIpAddress.none,
port: Option[Port] = Port.none): MultiAddress =
## Remap addresses to new IP and/or Port
##