Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit df36c4a

Browse files
committed
refactor: remove exit == destination
1 parent 70a17d1 commit df36c4a

File tree

9 files changed

+72
-368
lines changed

9 files changed

+72
-368
lines changed

examples/poc_gossipsub.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ proc makeMixConnCb(mixProto: MixProtocol): CustomConnCreationProc =
114114
destAddr: Option[MultiAddress], destPeerId: PeerId, codec: string
115115
): Connection {.gcsafe, raises: [].} =
116116
try:
117-
return mixProto.toConnection(destPeerId, codec).get()
117+
let dest = destAddr.valueOr:
118+
debug "No destination address available"
119+
return
120+
return mixProto.toConnection(MixDestination.init(destPeerId, dest), codec).get()
118121
except CatchableError as e:
119122
error "Error during execution of MixEntryConnection callback: ", err = e.msg
120123
return nil

examples/poc_gossipsub_repeated_runs.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ proc makeMixConnCb(mixProto: MixProtocol): CustomConnCreationProc =
116116
destAddr: Option[MultiAddress], destPeerId: PeerId, codec: string
117117
): Connection {.gcsafe, raises: [].} =
118118
try:
119-
return mixProto.toConnection(destPeerId, codec).get()
119+
let dest = destAddr.valueOr:
120+
debug "No destination address available"
121+
return
122+
return mixProto.toConnection(MixDestination.init(destPeerId, dest), codec).get()
120123
except CatchableError as e:
121124
error "Error during execution of MixEntryConnection callback: ", err = e.msg
122125
return nil

examples/poc_noresp_ping.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ proc mixnetSimulation() {.async: (raises: [Exception]).} =
105105
receiverIndex = senderIndex + 1
106106

107107
let conn = mixProto[senderIndex].toConnection(
108-
Destination.forwardToAddr(
108+
MixDestination.init(
109109
nodes[receiverIndex].peerInfo.peerId, nodes[receiverIndex].peerInfo.addrs[0]
110110
),
111111
NoRespPingCodec,

examples/poc_resp_ping.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ proc mixnetSimulation() {.async: (raises: [Exception]).} =
8585
return
8686

8787
# We'll fwd requests, so let's register how should the exit node will read responses
88-
proto.registerFwdReadBehavior(PingCodec, readExactly(32))
88+
proto.registerDestReadBehavior(PingCodec, readExactly(32))
8989

9090
mixProto.add(proto)
9191

@@ -105,7 +105,7 @@ proc mixnetSimulation() {.async: (raises: [Exception]).} =
105105
receiverIndex = senderIndex + 1
106106

107107
let conn = mixProto[senderIndex].toConnection(
108-
Destination.forwardToAddr(
108+
MixDestination.init(
109109
nodes[receiverIndex].peerInfo.peerId, nodes[receiverIndex].peerInfo.addrs[0]
110110
),
111111
PingCodec,

mix.nim

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,23 @@ export writeMixPubInfoToFile
1515
export writeMixNodeInfoToFile
1616
export getMixNodeInfo
1717
export `new`
18+
export init
1819
export getMaxMessageSizeForCodec
1920
export deleteNodeInfoFolder
2021
export deletePubInfoFolder
21-
22-
export Destination
23-
export DestinationType
24-
export forwardToAddr
25-
export mixNode
26-
22+
export MixDestination
2723
export MixParameters
28-
export fwdReadBehaviorCb
29-
export registerFwdReadBehavior
24+
export destReadBehaviorCb
25+
export registerDestReadBehavior
3026
export MixNodes
3127

32-
proc readLp*(maxSize: int): fwdReadBehaviorCb =
28+
proc readLp*(maxSize: int): destReadBehaviorCb =
3329
return proc(
3430
conn: Connection
3531
): Future[seq[byte]] {.async: (raises: [CancelledError, LPStreamError]).} =
3632
await conn.readLp(maxSize)
3733

38-
proc readExactly*(nBytes: int): fwdReadBehaviorCb =
34+
proc readExactly*(nBytes: int): destReadBehaviorCb =
3935
return proc(
4036
conn: Connection
4137
): Future[seq[byte]] {.async: (raises: [CancelledError, LPStreamError]).} =

mix/entry_connection.nim

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,16 @@ import ./mix_protocol
55
import ./config
66
from fragmentation import dataSize
77

8-
type
9-
DestinationType* = enum
10-
MixNode
11-
ForwardAddr
12-
13-
Destination* = object
14-
peerId*: PeerId
15-
case kind*: DestinationType
16-
of ForwardAddr:
17-
address*: MultiAddress
18-
else:
19-
discard
20-
21-
proc mixNode*(T: typedesc[Destination], p: PeerId): T =
22-
T(kind: DestinationType.MixNode, peerId: p)
23-
24-
proc forwardToAddr*(T: typedesc[Destination], p: PeerId, address: MultiAddress): T =
25-
T(kind: DestinationType.ForwardAddr, peerId: p, address: address)
26-
27-
proc `$`*(d: Destination): string =
28-
case d.kind
29-
of MixNode:
30-
"Destination[MixNode](" & $d.peerId & ")"
31-
of ForwardAddr:
32-
"Destination[ForwardAddr](" & $d.address & "/p2p/" & $d.peerId & ")"
33-
348
type MixDialer* = proc(
35-
msg: seq[byte], codec: string, destination: Destination
9+
msg: seq[byte], codec: string, destination: MixDestination
3610
): Future[void] {.async: (raises: [CancelledError, LPStreamError], raw: true).}
3711

3812
type MixParameters* = object
3913
expectReply*: Opt[bool]
4014
numSurbs*: Opt[uint8]
4115

4216
type MixEntryConnection* = ref object of Connection
43-
destination: Destination
17+
destination: MixDestination
4418
codec: string
4519
mixDialer: MixDialer
4620
params: Opt[MixParameters]
@@ -235,7 +209,7 @@ when defined(libp2p_agents_metrics):
235209
proc new*(
236210
T: typedesc[MixEntryConnection],
237211
srcMix: MixProtocol,
238-
destination: Destination,
212+
destination: MixDestination,
239213
codec: string,
240214
params: Opt[MixParameters],
241215
): T {.raises: [].} =
@@ -261,17 +235,11 @@ proc new*(
261235
instance.incomingFut = checkForIncoming()
262236

263237
instance.mixDialer = proc(
264-
msg: seq[byte], codec: string, dest: Destination
238+
msg: seq[byte], codec: string, dest: MixDestination
265239
): Future[void] {.async: (raises: [CancelledError, LPStreamError]).} =
266240
try:
267-
let (peerId, destination) =
268-
if dest.kind == DestinationType.MixNode:
269-
(Opt.some(dest.peerId), Opt.none(MixDestination))
270-
else:
271-
(Opt.none(PeerId), Opt.some(MixDestination.init(dest.peerId, dest.address)))
272-
273241
await srcMix.anonymizeLocalProtocolSend(
274-
instance.incoming, msg, codec, peerId, destination, numSurbs
242+
instance.incoming, msg, codec, dest, numSurbs
275243
)
276244
except CatchableError as e:
277245
error "Error during execution of anonymizeLocalProtocolSend: ", err = e.msg
@@ -284,19 +252,14 @@ proc new*(
284252

285253
proc toConnection*(
286254
srcMix: MixProtocol,
287-
destination: Destination | PeerId,
255+
destination: MixDestination,
288256
codec: string,
289257
params: Opt[MixParameters] = Opt.none(MixParameters),
290258
): Result[Connection, string] {.gcsafe, raises: [].} =
291-
let dest =
292-
when destination is PeerId:
293-
Destination.mixNode(destination)
259+
if not srcMix.hasDestReadBehavior(codec):
260+
if params.get(MixParameters()).expectReply.get(false):
261+
return err("no destination read behavior for codec")
294262
else:
295-
destination
296-
297-
if dest.kind == DestinationType.ForwardAddr and
298-
params.get(MixParameters()).expectReply.get(false) and
299-
not srcMix.hasFwdBehavior(codec):
300-
return err("no forward behavior for codec")
263+
warn "no destination read behavior for codec", codec
301264

302-
ok(MixEntryConnection.new(srcMix, dest, codec, params))
265+
ok(MixEntryConnection.new(srcMix, destination, codec, params))

mix/exit_connection.nim

Lines changed: 0 additions & 189 deletions
This file was deleted.

0 commit comments

Comments
 (0)