More --styleCheck fixes for discoveryv5 and eth/common (#473)

This commit is contained in:
Kim De Mey 2022-02-03 15:51:08 +01:00 committed by GitHub
parent 41edd4a3f2
commit 05ef9a8e00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 54 deletions

View File

@ -23,7 +23,7 @@ proc writeValue*(w: var JsonWriter, value: StUint) {.inline.} =
proc readValue*(r: var JsonReader, value: var StUint) {.inline.} = proc readValue*(r: var JsonReader, value: var StUint) {.inline.} =
value = parse(r.readValue(string), type(value)) 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 # The Ethereum Yellow Paper defines the RLP serialization only
# for unsigned integers: # for unsigned integers:
{.error: "RLP serialization of signed integers is not allowed".} {.error: "RLP serialization of signed integers is not allowed".}

View File

@ -3,14 +3,15 @@ import
confutils, confutils/std/net, chronicles, chronicles/topics_registry, confutils, confutils/std/net, chronicles, chronicles/topics_registry,
chronos, metrics, metrics/chronos_httpserver, stew/byteutils, chronos, metrics, metrics/chronos_httpserver, stew/byteutils,
../../keys, ../../net/nat, ../../keys, ../../net/nat,
"."/[enr, node, protocol] "."/[enr, node],
"."/protocol as discv5_protocol
type type
DiscoveryCmd* = enum DiscoveryCmd* = enum
noCommand noCommand
ping ping
findnode findNode
talkreq talkReq
DiscoveryConf* = object DiscoveryConf* = object
logLevel* {. logLevel* {.
@ -75,7 +76,7 @@ type
argument argument
desc: "ENR URI of the node to a send ping message" desc: "ENR URI of the node to a send ping message"
name: "node" .}: Node name: "node" .}: Node
of findnode: of findNode:
distance* {. distance* {.
defaultValue: 255 defaultValue: 255
desc: "Distance parameter for the findNode message" desc: "Distance parameter for the findNode message"
@ -86,10 +87,10 @@ type
argument argument
desc: "ENR URI of the node to send a findNode message" desc: "ENR URI of the node to send a findNode message"
name: "node" .}: Node name: "node" .}: Node
of talkreq: of talkReq:
talkreqTarget* {. talkReqTarget* {.
argument 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 name: "node" .}: Node
func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress = 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] = proc completeCmdArg*(T: type PrivateKey, val: TaintedString): seq[string] =
return @[] return @[]
proc discover(d: protocol.Protocol) {.async.} = proc discover(d: discv5_protocol.Protocol) {.async.} =
while true: while true:
let discovered = await d.queryRandom() let discovered = await d.queryRandom()
info "Lookup finished", nodes = discovered.len info "Lookup finished", nodes = discovered.len
@ -171,7 +172,7 @@ proc run(config: DiscoveryConf) =
echo pong[] echo pong[]
else: else:
echo "No Pong message returned" echo "No Pong message returned"
of findnode: of findNode:
let nodes = waitFor d.findNode(config.findNodeTarget, @[config.distance]) let nodes = waitFor d.findNode(config.findNodeTarget, @[config.distance])
if nodes.isOk(): if nodes.isOk():
echo "Received valid records:" echo "Received valid records:"
@ -179,8 +180,8 @@ proc run(config: DiscoveryConf) =
echo $node.record & " - " & shortLog(node) echo $node.record & " - " & shortLog(node)
else: else:
echo "No Nodes message returned" echo "No Nodes message returned"
of talkreq: of talkReq:
let talkresp = waitFor d.talkreq(config.talkreqTarget, @[], @[]) let talkresp = waitFor d.talkReq(config.talkReqTarget, @[], @[])
if talkresp.isOk(): if talkresp.isOk():
echo talkresp[] echo talkresp[]
else: else:

View File

@ -394,11 +394,11 @@ proc decodeMessage*(body: openArray[byte]): DecodeResult[Message] =
of unused: return err("Invalid message type") of unused: return err("Invalid message type")
of ping: rlp.decode(message.ping) of ping: rlp.decode(message.ping)
of pong: rlp.decode(message.pong) 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 nodes: rlp.decode(message.nodes)
of talkreq: rlp.decode(message.talkreq) of talkReq: rlp.decode(message.talkReq)
of talkresp: rlp.decode(message.talkresp) of talkResp: rlp.decode(message.talkResp)
of regtopic, ticket, regconfirmation, topicquery: of regTopic, ticket, regConfirmation, topicQuery:
# We just pass the empty type of this message without attempting to # We just pass the empty type of this message without attempting to
# decode, so that the protocol knows what was received. # decode, so that the protocol knows what was received.
# But we ignore the message as per specification as "the content and # But we ignore the message as per specification as "the content and

View File

@ -26,14 +26,14 @@ type
ping = 0x01 ping = 0x01
pong = 0x02 pong = 0x02
findnode = 0x03 findNode = 0x03
nodes = 0x04 nodes = 0x04
talkreq = 0x05 talkReq = 0x05
talkresp = 0x06 talkResp = 0x06
regtopic = 0x07 regTopic = 0x07
ticket = 0x08 ticket = 0x08
regconfirmation = 0x09 regConfirmation = 0x09
topicquery = 0x0A topicQuery = 0x0A
RequestId* = object RequestId* = object
id*: seq[byte] id*: seq[byte]
@ -76,32 +76,32 @@ type
ping*: PingMessage ping*: PingMessage
of pong: of pong:
pong*: PongMessage pong*: PongMessage
of findnode: of findNode:
findnode*: FindNodeMessage findNode*: FindNodeMessage
of nodes: of nodes:
nodes*: NodesMessage nodes*: NodesMessage
of talkreq: of talkReq:
talkreq*: TalkReqMessage talkReq*: TalkReqMessage
of talkresp: of talkResp:
talkresp*: TalkRespMessage talkResp*: TalkRespMessage
of regtopic: of regTopic:
regtopic*: RegTopicMessage regtopic*: RegTopicMessage
of ticket: of ticket:
ticket*: TicketMessage ticket*: TicketMessage
of regconfirmation: of regConfirmation:
regconfirmation*: RegConfirmationMessage regConfirmation*: RegConfirmationMessage
of topicquery: of topicQuery:
topicquery*: TopicQueryMessage topicQuery*: TopicQueryMessage
else: else:
discard discard
template messageKind*(T: typedesc[SomeMessage]): MessageKind = template messageKind*(T: typedesc[SomeMessage]): MessageKind =
when T is PingMessage: ping when T is PingMessage: ping
elif T is PongMessage: pong elif T is PongMessage: pong
elif T is FindNodeMessage: findnode elif T is FindNodeMessage: findNode
elif T is NodesMessage: nodes elif T is NodesMessage: nodes
elif T is TalkReqMessage: talkreq elif T is TalkReqMessage: talkReq
elif T is TalkRespMessage: talkresp elif T is TalkRespMessage: talkResp
proc read*(rlp: var Rlp, T: type RequestId): T proc read*(rlp: var Rlp, T: type RequestId): T
{.raises: [ValueError, RlpError, Defect].} = {.raises: [ValueError, RlpError, Defect].} =

View File

@ -341,13 +341,13 @@ proc handleMessage(d: Protocol, srcId: NodeId, fromAddr: Address,
of ping: of ping:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
d.handlePing(srcId, fromAddr, message.ping, message.reqId) d.handlePing(srcId, fromAddr, message.ping, message.reqId)
of findnode: of findNode:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
d.handleFindNode(srcId, fromAddr, message.findnode, message.reqId) d.handleFindNode(srcId, fromAddr, message.findNode, message.reqId)
of talkreq: of talkReq:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
d.handleTalkReq(srcId, fromAddr, message.talkreq, message.reqId) d.handleTalkReq(srcId, fromAddr, message.talkReq, message.reqId)
of regtopic, topicquery: of regTopic, topicQuery:
discovery_message_requests_incoming.inc() discovery_message_requests_incoming.inc()
discovery_message_requests_incoming.inc(labelValues = ["no_response"]) discovery_message_requests_incoming.inc(labelValues = ["no_response"])
trace "Received unimplemented message kind", kind = message.kind, trace "Received unimplemented message kind", kind = message.kind,
@ -574,7 +574,7 @@ proc findNode*(d: Protocol, toNode: Node, distances: seq[uint16]):
d.replaceNode(toNode) d.replaceNode(toNode)
return err(nodes.error) 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.} = Future[DiscResult[seq[byte]]] {.async.} =
## Send a discovery talkreq message. ## 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) let resp = await d.waitMessage(toNode, reqId)
if resp.isSome(): if resp.isSome():
if resp.get().kind == talkresp: if resp.get().kind == talkResp:
d.routingTable.setJustSeen(toNode) d.routingTable.setJustSeen(toNode)
return ok(resp.get().talkresp.response) return ok(resp.get().talkResp.response)
else: else:
d.replaceNode(toNode) d.replaceNode(toNode)
discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"]) discovery_message_requests_outgoing.inc(labelValues = ["invalid_response"])

View File

@ -688,7 +688,7 @@ suite "Discovery v5 Tests":
rng, PrivateKey.random(rng[]), localAddress(20302)) rng, PrivateKey.random(rng[]), localAddress(20302))
node2 = initDiscoveryNode( node2 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20303)) rng, PrivateKey.random(rng[]), localAddress(20303))
talkresp = await discv5_protocol.talkreq(node1, node2.localNode, talkresp = await discv5_protocol.talkReq(node1, node2.localNode,
@[byte 0x01], @[]) @[byte 0x01], @[])
check: check:
@ -713,7 +713,7 @@ suite "Discovery v5 Tests":
let echoProtocol = TalkProtocol(protocolHandler: handler) let echoProtocol = TalkProtocol(protocolHandler: handler)
check node2.registerTalkProtocol(talkProtocol, echoProtocol).isOk() 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()) talkProtocol, "hello".toBytes())
check: check:

View File

@ -65,8 +65,8 @@ suite "Discovery v5.1 Protocol Message Encodings":
let message = decoded.get() let message = decoded.get()
check: check:
message.reqId == reqId message.reqId == reqId
message.kind == findnode message.kind == findNode
message.findnode.distances == distances message.findNode.distances == distances
test "Nodes Response (empty)": test "Nodes Response (empty)":
let let
@ -125,9 +125,9 @@ suite "Discovery v5.1 Protocol Message Encodings":
let message = decoded.get() let message = decoded.get()
check: check:
message.reqId == reqId message.reqId == reqId
message.kind == talkreq message.kind == talkReq
message.talkreq.protocol == "echo".toBytes() message.talkReq.protocol == "echo".toBytes()
message.talkreq.request == "hi".toBytes() message.talkReq.request == "hi".toBytes()
test "Talk Response": test "Talk Response":
let let
@ -143,8 +143,8 @@ suite "Discovery v5.1 Protocol Message Encodings":
let message = decoded.get() let message = decoded.get()
check: check:
message.reqId == reqId message.reqId == reqId
message.kind == talkresp message.kind == talkResp
message.talkresp.response == "hi".toBytes() message.talkResp.response == "hi".toBytes()
test "Ping with too large RequestId": test "Ping with too large RequestId":
let let