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.} =
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".}

View File

@ -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:

View File

@ -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

View File

@ -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].} =

View File

@ -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:

View File

@ -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:

View File

@ -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