Skip to content

Commit

Permalink
Style fixes and comment improvements on uTP code (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme authored Jun 22, 2023
1 parent 6dacb2c commit d74dc40
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 204 deletions.
29 changes: 18 additions & 11 deletions eth/utp/utp_discv5_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ proc init*(T: type NodeAddress, nodeId: NodeId, address: Address): NodeAddress =
NodeAddress(nodeId: nodeId, address: address)

proc init*(T: type NodeAddress, node: Node): Option[NodeAddress] =
node.address.map((address: Address) => NodeAddress(nodeId: node.id, address: address))
node.address.map((address: Address) =>
NodeAddress(nodeId: node.id, address: address))

proc hash(x: NodeAddress): Hash =
var h = 0
Expand All @@ -51,25 +52,31 @@ func `$`*(x: UtpSocketKey[NodeAddress]): string =
", rcvId: " & $x.rcvId &
")"

proc talkReqDirect(p: protocol.Protocol, n: NodeAddress, protocol, request: seq[byte]): Future[void] =
proc talkReqDirect(
p: protocol.Protocol, n: NodeAddress, protocol, request: seq[byte]):
Future[void] =
let
reqId = RequestId.init(p.rng[])
message = encodeMessage(TalkReqMessage(protocol: protocol, request: request), reqId)
message = encodeMessage(
TalkReqMessage(protocol: protocol, request: request), reqId)
(data, nonce) = encodeMessagePacket(
p.rng[], p.codec, n.nodeId, n.address, message)

(data, nonce) = encodeMessagePacket(p.rng[], p.codec, n.nodeId, n.address, message)

trace "Send message packet", dstId = n.nodeId, address = n.address, kind = MessageKind.talkreq
trace "Send message packet",
dstId = n.nodeId, address = n.address, kind = MessageKind.talkreq
p.send(n.address, data)

proc initSendCallback(
t: protocol.Protocol, subProtocolName: seq[byte]): SendCallback[NodeAddress] =
t: protocol.Protocol, subProtocolName: seq[byte]):
SendCallback[NodeAddress] =
return (
proc (to: NodeAddress, data: seq[byte]): Future[void] =
let fut = newFuture[void]()
# hidden assumption here is that nodes already have established discv5 session
# between each other. In our use case this should be true as opening stream
# is only done after successful OFFER/ACCEPT or FINDCONTENT/CONTENT exchange
# which forces nodes to establish session between each other.
# hidden assumption here is that nodes already have established discv5
# session between each other. In our use case this should be true as
# opening stream is only done after successful OFFER/ACCEPT or
# FINDCONTENT/CONTENT exchange which forces nodes to establish session
# between each other.
discard t.talkReqDirect(to, subProtocolName, data)
fut.complete()
return fut
Expand Down
2 changes: 1 addition & 1 deletion eth/utp/utp_router.nim
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ proc processPacket[A](r: UtpRouter[A], p: Packet, sender: A) {.async.}=
else:
# TODO: add keeping track of recently send reset packets and do not send
# reset to peers which we recently send reset to.
debug "Received FIN/DATA/ACK on not known socket sending reset"
debug "Received FIN/DATA/ACK on unknown socket, sending reset"
let rstPacket = resetPacket(
randUint16(r.rng[]), p.header.connectionId, p.header.seqNr)
await r.sendCb(sender, encodePacket(rstPacket))
Expand Down
Loading

0 comments on commit d74dc40

Please sign in to comment.