cosmetics changes

This commit is contained in:
andri lim 2018-11-07 09:28:51 +07:00 committed by zah
parent c3a49d187d
commit 7787e27427
2 changed files with 13 additions and 10 deletions

View File

@ -46,10 +46,8 @@ proc newEthereumNode*(keys: KeyPair,
result.connectionState = ConnectionState.None result.connectionState = ConnectionState.None
when useSnappy: when useSnappy:
if useCompression: result.protocolVersion = if useCompression: devp2pSnappyVersion
result.protocolVersion = devp2pSnappyVersion else: devp2pVersion
else:
result.protocolVersion = devp2pVersion
if addAllCapabilities: if addAllCapabilities:
for p in rlpxProtocols: for p in rlpxProtocols:

View File

@ -261,8 +261,7 @@ proc linkSendFailureToReqFuture[S, R](sendFut: Future[S], resFut: Future[R]) =
template compressMsg(peer: Peer, data: Bytes): Bytes = template compressMsg(peer: Peer, data: Bytes): Bytes =
when useSnappy: when useSnappy:
if peer.snappyEnabled and if peer.snappyEnabled:
peer.network.protocolVersion == devp2pSnappyVersion:
snappy.compress(data) snappy.compress(data)
else: data else: data
else: else:
@ -424,8 +423,7 @@ proc recvMsg*(peer: Peer): Future[tuple[msgId: int, msgData: Rlp]] {.async.} =
decryptedBytes.setLen(decryptedBytesCount) decryptedBytes.setLen(decryptedBytesCount)
when useSnappy: when useSnappy:
if peer.network.protocolVersion == devp2pSnappyVersion and if peer.snappyEnabled:
peer.snappyEnabled:
decryptedBytes = snappy.uncompress(decryptedBytes) decryptedBytes = snappy.uncompress(decryptedBytes)
if decryptedBytes.len == 0: if decryptedBytes.len == 0:
await peer.disconnectAndRaise(BreachOfProtocol, await peer.disconnectAndRaise(BreachOfProtocol,
@ -1199,11 +1197,11 @@ template baseProtocolVersion(node: EthereumNode, peer: Peer): untyped =
else: else:
devp2pVersion devp2pVersion
template checkPeerProtocolVersion(peer: Peer, handshake: HandShake) = template checkPeerProtocolVersion(peer: Peer, handshake: Handshake) =
when useSnappy: when useSnappy:
peer.snappyEnabled = handshake.version >= devp2pSnappyVersion.uint peer.snappyEnabled = handshake.version >= devp2pSnappyVersion.uint
template getVersion(handshake: HandShake): uint = template getVersion(handshake: Handshake): uint =
when useSnappy: when useSnappy:
handshake.version handshake.version
else: else:
@ -1216,6 +1214,11 @@ template baseProtocolVersion(peer: Peer): uint =
else: else:
devp2pVersion devp2pVersion
template checkPeerNeedCompression(peer: Peer, node: EthereumNode) =
when useSnappy:
peer.snappyEnabled = peer.snappyEnabled and
node.protocolVersion >= devp2pSnappyVersion.uint
proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} = proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
new result new result
result.network = node result.network = node
@ -1264,6 +1267,7 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} =
warn "Remote nodeId is not its public key" # XXX: Do we care? warn "Remote nodeId is not its public key" # XXX: Do we care?
await postHelloSteps(result, response) await postHelloSteps(result, response)
result.checkPeerNeedCompression(node)
ok = true ok = true
except PeerDisconnected as e: except PeerDisconnected as e:
if e.reason != TooManyPeers: if e.reason != TooManyPeers:
@ -1335,6 +1339,7 @@ proc rlpxAccept*(node: EthereumNode,
result.remote = newNode(initEnode(handshake.remoteHPubkey, address)) result.remote = newNode(initEnode(handshake.remoteHPubkey, address))
await postHelloSteps(result, response) await postHelloSteps(result, response)
result.checkPeerNeedCompression(node)
except: except:
error "Exception in rlpxAccept", error "Exception in rlpxAccept",
err = getCurrentExceptionMsg(), err = getCurrentExceptionMsg(),