11import hashes, chronos, stew/ byteutils, results, chronicles
22import libp2p/ stream/ connection
33import ./ mix_protocol
4+ import ./ config
45from fragmentation import dataSize
56
67type
@@ -33,14 +34,20 @@ type MixDialer* = proc(
3334 msg: seq [byte ], codec: string , destination: Destination
3435): Future [void ] {.async : (raises: [CancelledError , LPStreamError ], raw: true ).}
3536
37+ type MixParameters * = object
38+ expectReply* : Opt [bool ]
39+ numSurbs* : Opt [uint8 ]
40+
3641type MixEntryConnection * = ref object of Connection
3742 destination: Destination
3843 codec: string
3944 mixDialer: MixDialer
45+ params: Opt [MixParameters ]
4046
4147method readExactly * (
4248 self: MixEntryConnection , pbytes: pointer , nbytes: int
4349): Future [void ] {.async : (raises: [CancelledError , LPStreamError ]), public .} =
50+ await sleepAsync (10 .minutes) # TODO : implement readExactly
4451 raise
4552 newException (LPStreamError , " readExactly not implemented for MixEntryConnection" )
4653
@@ -102,9 +109,6 @@ method writeLp*(
102109proc shortLog * (self: MixEntryConnection ): string {.raises : [].} =
103110 " [MixEntryConnection] Destination: " & $ self.destination
104111
105- method initStream * (self: MixEntryConnection ) =
106- discard
107-
108112method closeImpl * (
109113 self: MixEntryConnection
110114): Future [void ] {.async : (raises: [], raw: true ).} =
@@ -125,8 +129,10 @@ proc new*(
125129 destination: Destination ,
126130 codec: string ,
127131 mixDialer: MixDialer ,
132+ params: Opt [MixParameters ],
128133): T =
129- let instance = T (destination: destination, codec: codec, mixDialer: mixDialer)
134+ let instance =
135+ T (destination: destination, codec: codec, mixDialer: mixDialer, params: params)
130136
131137 when defined (libp2p_agents_metrics):
132138 instance.shortAgent = connection.shortAgent
@@ -138,7 +144,16 @@ proc new*(
138144 srcMix: MixProtocol ,
139145 destination: Destination ,
140146 codec: string ,
147+ params: Opt [MixParameters ],
141148): T {.raises : [].} =
149+ let params = params.get (MixParameters ())
150+ let expectReply = params.expectReply.get (false )
151+ let surbs =
152+ if expectReply:
153+ params.numSurbs.get (defaultSurbs)
154+ else :
155+ 0
156+
142157 var sendDialerFunc = proc (
143158 msg: seq [byte ], codec: string , dest: Destination
144159 ): Future [void ] {.async : (raises: [CancelledError , LPStreamError ]).} =
@@ -149,19 +164,28 @@ proc new*(
149164 else :
150165 (Opt .none (PeerId ), Opt .some (MixDestination .init (dest.peerId, dest.address)))
151166
152- await srcMix.anonymizeLocalProtocolSend (msg, codec, peerId, destination)
167+ await srcMix.anonymizeLocalProtocolSend (msg, codec, peerId, destination, surbs )
153168 except CatchableError as e:
154169 error " Error during execution of anonymizeLocalProtocolSend: " , err = e.msg
155170 return
156171
157- T.new (srcMix, destination, codec, sendDialerFunc)
172+ T.new (srcMix, destination, codec, sendDialerFunc, Opt . some (params) )
158173
159174proc toConnection * (
160- srcMix: MixProtocol , destination: Destination | PeerId , codec: string
161- ): Connection {.gcsafe , raises : [].} =
175+ srcMix: MixProtocol ,
176+ destination: Destination | PeerId ,
177+ codec: string ,
178+ params: Opt [MixParameters ] = Opt .none (MixParameters ),
179+ ): Result [Connection , string ] {.gcsafe , raises : [].} =
162180 let dest =
163181 when destination is PeerId :
164182 Destination .mixNode (destination)
165183 else :
166184 destination
167- MixEntryConnection .new (srcMix, dest, codec)
185+
186+ if dest.kind == DestinationType .ForwardAddr and
187+ params.get (MixParameters ()).expectReply.get (false ) and
188+ not srcMix.hasFwdBehavior (codec):
189+ return err (" no forward behavior for codec" )
190+
191+ ok (MixEntryConnection .new (srcMix, dest, codec, params))
0 commit comments