From 05ef9a8e008bfda4739dff72b6e17f5c39fc3c84 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Thu, 3 Feb 2022 15:51:08 +0100 Subject: [PATCH] More --styleCheck fixes for discoveryv5 and eth/common (#473) --- eth/common/eth_types_json_serialization.nim | 2 +- eth/p2p/discoveryv5/dcli.nim | 23 ++++++------ eth/p2p/discoveryv5/encoding.nim | 8 ++--- eth/p2p/discoveryv5/messages.nim | 40 ++++++++++----------- eth/p2p/discoveryv5/protocol.nim | 18 +++++----- tests/p2p/test_discoveryv5.nim | 4 +-- tests/p2p/test_discoveryv5_encoding.nim | 14 ++++---- 7 files changed, 55 insertions(+), 54 deletions(-) diff --git a/eth/common/eth_types_json_serialization.nim b/eth/common/eth_types_json_serialization.nim index 992c7c4..4ecfb13 100644 --- a/eth/common/eth_types_json_serialization.nim +++ b/eth/common/eth_types_json_serialization.nim @@ -23,7 +23,7 @@ proc writeValue*(w: var JsonWriter, value: StUint) {.inline.} = proc readValue*(r: var JsonReader, value: var StUint) {.inline.} = value = parse(r.readValue(string), type(value)) -proc writeValue*(w: var JsonWriter, value: Stint) = +proc writeValue*(w: var JsonWriter, value: StInt) = # The Ethereum Yellow Paper defines the RLP serialization only # for unsigned integers: {.error: "RLP serialization of signed integers is not allowed".} diff --git a/eth/p2p/discoveryv5/dcli.nim b/eth/p2p/discoveryv5/dcli.nim index c038f7b..4479ce6 100644 --- a/eth/p2p/discoveryv5/dcli.nim +++ b/eth/p2p/discoveryv5/dcli.nim @@ -3,14 +3,15 @@ import confutils, confutils/std/net, chronicles, chronicles/topics_registry, chronos, metrics, metrics/chronos_httpserver, stew/byteutils, ../../keys, ../../net/nat, - "."/[enr, node, protocol] + "."/[enr, node], + "."/protocol as discv5_protocol type DiscoveryCmd* = enum noCommand ping - findnode - talkreq + findNode + talkReq DiscoveryConf* = object logLevel* {. @@ -75,7 +76,7 @@ type argument desc: "ENR URI of the node to a send ping message" name: "node" .}: Node - of findnode: + of findNode: distance* {. defaultValue: 255 desc: "Distance parameter for the findNode message" @@ -86,10 +87,10 @@ type argument desc: "ENR URI of the node to send a findNode message" name: "node" .}: Node - of talkreq: - talkreqTarget* {. + of talkReq: + talkReqTarget* {. argument - desc: "ENR URI of the node to send a talkreq message" + desc: "ENR URI of the node to send a talkReq message" name: "node" .}: Node func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress = @@ -131,7 +132,7 @@ proc parseCmdArg*(T: type PrivateKey, p: TaintedString): T = proc completeCmdArg*(T: type PrivateKey, val: TaintedString): seq[string] = return @[] -proc discover(d: protocol.Protocol) {.async.} = +proc discover(d: discv5_protocol.Protocol) {.async.} = while true: let discovered = await d.queryRandom() info "Lookup finished", nodes = discovered.len @@ -171,7 +172,7 @@ proc run(config: DiscoveryConf) = echo pong[] else: echo "No Pong message returned" - of findnode: + of findNode: let nodes = waitFor d.findNode(config.findNodeTarget, @[config.distance]) if nodes.isOk(): echo "Received valid records:" @@ -179,8 +180,8 @@ proc run(config: DiscoveryConf) = echo $node.record & " - " & shortLog(node) else: echo "No Nodes message returned" - of talkreq: - let talkresp = waitFor d.talkreq(config.talkreqTarget, @[], @[]) + of talkReq: + let talkresp = waitFor d.talkReq(config.talkReqTarget, @[], @[]) if talkresp.isOk(): echo talkresp[] else: diff --git a/eth/p2p/discoveryv5/encoding.nim b/eth/p2p/discoveryv5/encoding.nim index 726887b..ef22522 100644 --- a/eth/p2p/discoveryv5/encoding.nim +++ b/eth/p2p/discoveryv5/encoding.nim @@ -394,11 +394,11 @@ proc decodeMessage*(body: openArray[byte]): DecodeResult[Message] = of unused: return err("Invalid message type") of ping: rlp.decode(message.ping) of pong: rlp.decode(message.pong) - of findnode: rlp.decode(message.findNode) + of findNode: rlp.decode(message.findNode) of nodes: rlp.decode(message.nodes) - of talkreq: rlp.decode(message.talkreq) - of talkresp: rlp.decode(message.talkresp) - of regtopic, ticket, regconfirmation, topicquery: + of talkReq: rlp.decode(message.talkReq) + of talkResp: rlp.decode(message.talkResp) + of regTopic, ticket, regConfirmation, topicQuery: # We just pass the empty type of this message without attempting to # decode, so that the protocol knows what was received. # But we ignore the message as per specification as "the content and diff --git a/eth/p2p/discoveryv5/messages.nim b/eth/p2p/discoveryv5/messages.nim index ef31759..8d29bc3 100644 --- a/eth/p2p/discoveryv5/messages.nim +++ b/eth/p2p/discoveryv5/messages.nim @@ -26,14 +26,14 @@ type ping = 0x01 pong = 0x02 - findnode = 0x03 + findNode = 0x03 nodes = 0x04 - talkreq = 0x05 - talkresp = 0x06 - regtopic = 0x07 + talkReq = 0x05 + talkResp = 0x06 + regTopic = 0x07 ticket = 0x08 - regconfirmation = 0x09 - topicquery = 0x0A + regConfirmation = 0x09 + topicQuery = 0x0A RequestId* = object id*: seq[byte] @@ -76,32 +76,32 @@ type ping*: PingMessage of pong: pong*: PongMessage - of findnode: - findnode*: FindNodeMessage + of findNode: + findNode*: FindNodeMessage of nodes: nodes*: NodesMessage - of talkreq: - talkreq*: TalkReqMessage - of talkresp: - talkresp*: TalkRespMessage - of regtopic: + of talkReq: + talkReq*: TalkReqMessage + of talkResp: + talkResp*: TalkRespMessage + of regTopic: regtopic*: RegTopicMessage of ticket: ticket*: TicketMessage - of regconfirmation: - regconfirmation*: RegConfirmationMessage - of topicquery: - topicquery*: TopicQueryMessage + of regConfirmation: + regConfirmation*: RegConfirmationMessage + of topicQuery: + topicQuery*: TopicQueryMessage else: discard template messageKind*(T: typedesc[SomeMessage]): MessageKind = when T is PingMessage: ping elif T is PongMessage: pong - elif T is FindNodeMessage: findnode + elif T is FindNodeMessage: findNode elif T is NodesMessage: nodes - elif T is TalkReqMessage: talkreq - elif T is TalkRespMessage: talkresp + elif T is TalkReqMessage: talkReq + elif T is TalkRespMessage: talkResp proc read*(rlp: var Rlp, T: type RequestId): T {.raises: [ValueError, RlpError, Defect].} = diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index b5508bb..2582727 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -341,13 +341,13 @@ proc handleMessage(d: Protocol, srcId: NodeId, fromAddr: Address, of ping: discovery_message_requests_incoming.inc() d.handlePing(srcId, fromAddr, message.ping, message.reqId) - of findnode: + of findNode: discovery_message_requests_incoming.inc() - d.handleFindNode(srcId, fromAddr, message.findnode, message.reqId) - of talkreq: + d.handleFindNode(srcId, fromAddr, message.findNode, message.reqId) + of talkReq: discovery_message_requests_incoming.inc() - d.handleTalkReq(srcId, fromAddr, message.talkreq, message.reqId) - of regtopic, topicquery: + d.handleTalkReq(srcId, fromAddr, message.talkReq, message.reqId) + of regTopic, topicQuery: discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc(labelValues = ["no_response"]) trace "Received unimplemented message kind", kind = message.kind, @@ -574,7 +574,7 @@ proc findNode*(d: Protocol, toNode: Node, distances: seq[uint16]): d.replaceNode(toNode) return err(nodes.error) -proc talkreq*(d: Protocol, toNode: Node, protocol, request: seq[byte]): +proc talkReq*(d: Protocol, toNode: Node, protocol, request: seq[byte]): Future[DiscResult[seq[byte]]] {.async.} = ## Send a discovery talkreq message. ## @@ -584,9 +584,9 @@ proc talkreq*(d: Protocol, toNode: Node, protocol, request: seq[byte]): let resp = await d.waitMessage(toNode, reqId) if resp.isSome(): - if resp.get().kind == talkresp: + if resp.get().kind == talkResp: d.routingTable.setJustSeen(toNode) - return ok(resp.get().talkresp.response) + return ok(resp.get().talkResp.response) else: d.replaceNode(toNode) discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"]) @@ -612,7 +612,7 @@ proc lookupWorker(d: Protocol, destNode: Node, target: NodeId): Future[seq[Node]] {.async.} = let dists = lookupDistances(target, destNode.id) - # Instead of doing max `lookupRequestLimit` findNode requests, make use + # Instead of doing max `lookupRequestLimit` findNode requests, make use # of the discv5.1 functionality to request nodes for multiple distances. let r = await d.findNode(destNode, dists) if r.isOk: diff --git a/tests/p2p/test_discoveryv5.nim b/tests/p2p/test_discoveryv5.nim index 9ef308c..1e0d204 100644 --- a/tests/p2p/test_discoveryv5.nim +++ b/tests/p2p/test_discoveryv5.nim @@ -688,7 +688,7 @@ suite "Discovery v5 Tests": rng, PrivateKey.random(rng[]), localAddress(20302)) node2 = initDiscoveryNode( rng, PrivateKey.random(rng[]), localAddress(20303)) - talkresp = await discv5_protocol.talkreq(node1, node2.localNode, + talkresp = await discv5_protocol.talkReq(node1, node2.localNode, @[byte 0x01], @[]) check: @@ -713,7 +713,7 @@ suite "Discovery v5 Tests": let echoProtocol = TalkProtocol(protocolHandler: handler) check node2.registerTalkProtocol(talkProtocol, echoProtocol).isOk() - let talkresp = await discv5_protocol.talkreq(node1, node2.localNode, + let talkresp = await discv5_protocol.talkReq(node1, node2.localNode, talkProtocol, "hello".toBytes()) check: diff --git a/tests/p2p/test_discoveryv5_encoding.nim b/tests/p2p/test_discoveryv5_encoding.nim index ffcda9c..4ab089e 100644 --- a/tests/p2p/test_discoveryv5_encoding.nim +++ b/tests/p2p/test_discoveryv5_encoding.nim @@ -65,8 +65,8 @@ suite "Discovery v5.1 Protocol Message Encodings": let message = decoded.get() check: message.reqId == reqId - message.kind == findnode - message.findnode.distances == distances + message.kind == findNode + message.findNode.distances == distances test "Nodes Response (empty)": let @@ -125,9 +125,9 @@ suite "Discovery v5.1 Protocol Message Encodings": let message = decoded.get() check: message.reqId == reqId - message.kind == talkreq - message.talkreq.protocol == "echo".toBytes() - message.talkreq.request == "hi".toBytes() + message.kind == talkReq + message.talkReq.protocol == "echo".toBytes() + message.talkReq.request == "hi".toBytes() test "Talk Response": let @@ -143,8 +143,8 @@ suite "Discovery v5.1 Protocol Message Encodings": let message = decoded.get() check: message.reqId == reqId - message.kind == talkresp - message.talkresp.response == "hi".toBytes() + message.kind == talkResp + message.talkResp.response == "hi".toBytes() test "Ping with too large RequestId": let