enabling holepunching and other libp2p goodies

This commit is contained in:
Dmitriy Ryajov 2024-03-29 18:01:09 -06:00
parent 9365aa4d51
commit db295501e2
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
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
let
switch = SwitchBuilder
.new()
##
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})
.build()
.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 = builder.build
var
cache: CacheStore = nil

View File

@ -166,6 +166,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

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