Skip to content

Commit

Permalink
Circuit relay v1
Browse files Browse the repository at this point in the history
  • Loading branch information
lchenut committed May 9, 2022
1 parent 9ba5c06 commit 8e050d3
Show file tree
Hide file tree
Showing 9 changed files with 1,032 additions and 8 deletions.
17 changes: 15 additions & 2 deletions libp2p/builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import
switch, peerid, peerinfo, stream/connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex],
protocols/[identify, secure/secure, secure/noise],
protocols/[identify, secure/secure, secure/noise, relay],
connmanager, upgrademngrs/muxedupgrade,
nameresolving/nameresolver,
errors
Expand Down Expand Up @@ -48,6 +48,8 @@ type
protoVersion: string
agentVersion: string
nameResolver: NameResolver
isCircuitRelay: bool
circuitRelayCanHop: bool

proc new*(T: type[SwitchBuilder]): T =

Expand All @@ -64,7 +66,8 @@ proc new*(T: type[SwitchBuilder]): T =
maxOut: -1,
maxConnsPerPeer: MaxConnectionsPerPeer,
protoVersion: ProtoVersion,
agentVersion: AgentVersion)
agentVersion: AgentVersion,
isCircuitRelay: false)

proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder =
b.privKey = some(privateKey)
Expand Down Expand Up @@ -139,6 +142,11 @@ proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuil
b.nameResolver = nameResolver
b

proc withRelayTransport*(b: SwitchBuilder, canHop: bool): SwitchBuilder =
b.isCircuitRelay = true
b.circuitRelayCanHop = canHop
b

proc build*(b: SwitchBuilder): Switch
{.raises: [Defect, LPError].} =

Expand Down Expand Up @@ -197,6 +205,11 @@ proc build*(b: SwitchBuilder): Switch
ms = ms,
nameResolver = b.nameResolver)

if b.isCircuitRelay:
let relay = Relay.new(switch, b.circuitRelayCanHop)
switch.mount(relay)
switch.addTransport(RelayTransport.new(relay, muxedUpgrade))

return switch

proc newStandardSwitch*(
Expand Down
8 changes: 7 additions & 1 deletion libp2p/dial.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

import chronos
import peerid,
stream/connection
stream/connection,
transports/transport

type
Dial* = ref object of RootObj
Expand Down Expand Up @@ -49,3 +50,8 @@ method dial*(
##

doAssert(false, "Not implemented!")

method addTransport*(
self: Dial,
transport: Transport) {.base.} =
doAssert(false, "Not implemented!")
3 changes: 3 additions & 0 deletions libp2p/dialer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ method dial*(
await cleanup()
raise exc

method addTransport*(self: Dialer, t: Transport) =
self.transports &= t

proc new*(
T: type Dialer,
localPeerId: PeerId,
Expand Down
6 changes: 5 additions & 1 deletion libp2p/multiaddress.nim
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ const

Reliable* = mapOr(TCP, UTP, QUIC, WebSockets)

IPFS* = mapAnd(Reliable, mapEq("p2p"))
P2PPattern* = mapEq("p2p")

IPFS* = mapAnd(Reliable, P2PPattern)

HTTP* = mapOr(
mapAnd(TCP, mapEq("http")),
Expand All @@ -447,6 +449,8 @@ const
mapAnd(HTTPS, mapEq("p2p-webrtc-direct"))
)

CircuitRelay* = mapEq("p2p-circuit")

proc initMultiAddressCodeTable(): Table[MultiCodec,
MAProtocol] {.compileTime.} =
for item in ProtocolsList:
Expand Down
Loading

0 comments on commit 8e050d3

Please sign in to comment.