Remove the p2p disconnect handling from dispatchMessages

This commit is contained in:
kdeme 2019-10-14 12:01:01 +02:00
parent 757ac1ab86
commit 02a6906c01
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
1 changed files with 10 additions and 17 deletions

View File

@ -23,7 +23,13 @@ type
ResponderWithoutId*[MsgType] = distinct Peer ResponderWithoutId*[MsgType] = distinct Peer
# We need these two types in rlpx/devp2p as no parameters or single parameters
# are not getting encoded in an rlp list.
# TODO: we could generalize this in the protocol dsl but it would need an
# `alwaysList` flag as not every protocol expects lists in these cases.
EmptyList = object EmptyList = object
DisconnectionReasonList = object
value: DisconnectionReason
const const
devp2pVersion* = 4 devp2pVersion* = 4
@ -519,18 +525,6 @@ proc dispatchMessages*(peer: Peer) {.async.} =
except PeerDisconnected: except PeerDisconnected:
return return
# TODO: is there a reason why we don't just allow `sendDisconnectMsg`
# to do its work here, with the usual thunk checks included?
if msgId == 1: # p2p.disconnect
if msgData.isList():
let reason = msgData.listElem(0).toInt(uint32).DisconnectionReason
trace "disconnect message received in dispatchMessages", reason, peer
await peer.disconnect(reason, false)
break
else:
debug "disconnect message with invalid rlp list", peer
await peer.disconnect(BreachOfProtocol, true)
try: try:
await peer.invokeThunk(msgId, msgData) await peer.invokeThunk(msgId, msgData)
except RlpError: except RlpError:
@ -794,13 +788,12 @@ p2pProtocol devp2p(version = 0, rlpxName = "p2p"):
listenPort: uint, listenPort: uint,
nodeId: array[RawPublicKeySize, byte]) nodeId: array[RawPublicKeySize, byte])
proc sendDisconnectMsg(peer: Peer, reason: DisconnectionReason) = proc sendDisconnectMsg(peer: Peer, reason: DisconnectionReasonList) =
trace "disconnect message received", reason, peer trace "disconnect message received", reason=reason.value, peer
await peer.disconnect(reason, false) await peer.disconnect(reason.value, false)
# Adding an empty RLP list as the spec defines. # Adding an empty RLP list as the spec defines.
# The parity client specifically checks if there is rlp data. # The parity client specifically checks if there is rlp data.
# TODO: can we do this in the macro instead?
proc ping(peer: Peer, emptyList: EmptyList) = proc ping(peer: Peer, emptyList: EmptyList) =
discard peer.pong(EmptyList()) discard peer.pong(EmptyList())
@ -845,7 +838,7 @@ proc disconnect*(peer: Peer, reason: DisconnectionReason, notifyOtherPeer = fals
traceAwaitErrors callDisconnectHandlers(peer, reason) traceAwaitErrors callDisconnectHandlers(peer, reason)
if notifyOtherPeer and not peer.transport.closed: if notifyOtherPeer and not peer.transport.closed:
var fut = peer.sendDisconnectMsg(reason) var fut = peer.sendDisconnectMsg(DisconnectionReasonList(value: reason))
yield fut yield fut
if fut.failed: if fut.failed:
debug "Failed to deliver disconnect message", peer debug "Failed to deliver disconnect message", peer