Skip to content

Commit

Permalink
More --styleCheck fixes for discoveryv5 and eth/common (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme authored Feb 3, 2022
1 parent 41edd4a commit 05ef9a8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 54 deletions.
2 changes: 1 addition & 1 deletion eth/common/eth_types_json_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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".}
Expand Down
23 changes: 12 additions & 11 deletions eth/p2p/discoveryv5/dcli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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* {.
Expand Down Expand Up @@ -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"
Expand All @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -171,16 +172,16 @@ 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:"
for node in nodes[]:
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:
Expand Down
8 changes: 4 additions & 4 deletions eth/p2p/discoveryv5/encoding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions eth/p2p/discoveryv5/messages.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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].} =
Expand Down
18 changes: 9 additions & 9 deletions eth/p2p/discoveryv5/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
##
Expand All @@ -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"])
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/p2p/test_discoveryv5.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions tests/p2p/test_discoveryv5_encoding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 05ef9a8

Please sign in to comment.