From 9b0f054b0451dce8c25d9953b48f2ebc3aabe45c Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Thu, 10 Nov 2022 16:32:57 +0100 Subject: [PATCH] Tackle some Nim warnings new since Nim 1.4 & 1.6 (#553) --- eth/net/nat.nim | 4 ++-- eth/p2p/discoveryv5/dcli.nim | 12 ++++++------ eth/p2p/discoveryv5/enr.nim | 2 +- eth/p2p/discoveryv5/protocol.nim | 2 +- eth/p2p/kademlia.nim | 2 +- eth/p2p/peer_pool.nim | 2 +- eth/p2p/private/p2p_types.nim | 1 - eth/p2p/rlpx.nim | 2 +- eth/p2p/rlpxcrypt.nim | 16 ++++++---------- 9 files changed, 19 insertions(+), 24 deletions(-) diff --git a/eth/net/nat.nim b/eth/net/nat.nim index 8901af8..e1aa790 100644 --- a/eth/net/nat.nim +++ b/eth/net/nat.nim @@ -321,7 +321,7 @@ type of true: extIp*: ValidIpAddress of false: nat*: NatStrategy -func parseCmdArg*(T: type NatConfig, p: TaintedString): T {.raises: [Defect, ConfigurationError].} = +func parseCmdArg*(T: type NatConfig, p: string): T {.raises: [Defect, ConfigurationError].} = case p.toLowerAscii: of "any": NatConfig(hasExtIp: false, nat: NatAny) @@ -343,7 +343,7 @@ func parseCmdArg*(T: type NatConfig, p: TaintedString): T {.raises: [Defect, Con let error = "Not a valid NAT option: " & p raise newException(ConfigurationError, error) -func completeCmdArg*(T: type NatConfig, val: TaintedString): seq[string] = +func completeCmdArg*(T: type NatConfig, val: string): seq[string] = return @[] proc setupAddress*(natConfig: NatConfig, bindIp: ValidIpAddress, diff --git a/eth/p2p/discoveryv5/dcli.nim b/eth/p2p/discoveryv5/dcli.nim index 4479ce6..6e46814 100644 --- a/eth/p2p/discoveryv5/dcli.nim +++ b/eth/p2p/discoveryv5/dcli.nim @@ -99,14 +99,14 @@ func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress = func defaultAdminListenAddress*(conf: DiscoveryConf): ValidIpAddress = (static ValidIpAddress.init("127.0.0.1")) -proc parseCmdArg*(T: type enr.Record, p: TaintedString): T = +proc parseCmdArg*(T: type enr.Record, p: string): T = if not fromURI(result, p): raise newException(ConfigurationError, "Invalid ENR") -proc completeCmdArg*(T: type enr.Record, val: TaintedString): seq[string] = +proc completeCmdArg*(T: type enr.Record, val: string): seq[string] = return @[] -proc parseCmdArg*(T: type Node, p: TaintedString): T = +proc parseCmdArg*(T: type Node, p: string): T = var record: enr.Record if not fromURI(record, p): raise newException(ConfigurationError, "Invalid ENR") @@ -120,16 +120,16 @@ proc parseCmdArg*(T: type Node, p: TaintedString): T = n[] -proc completeCmdArg*(T: type Node, val: TaintedString): seq[string] = +proc completeCmdArg*(T: type Node, val: string): seq[string] = return @[] -proc parseCmdArg*(T: type PrivateKey, p: TaintedString): T = +proc parseCmdArg*(T: type PrivateKey, p: string): T = try: result = PrivateKey.fromHex(string(p)).tryGet() except CatchableError: raise newException(ConfigurationError, "Invalid private key") -proc completeCmdArg*(T: type PrivateKey, val: TaintedString): seq[string] = +proc completeCmdArg*(T: type PrivateKey, val: string): seq[string] = return @[] proc discover(d: discv5_protocol.Protocol) {.async.} = diff --git a/eth/p2p/discoveryv5/enr.nim b/eth/p2p/discoveryv5/enr.nim index bb5c575..ba75522 100644 --- a/eth/p2p/discoveryv5/enr.nim +++ b/eth/p2p/discoveryv5/enr.nim @@ -300,7 +300,7 @@ proc update*(record: var Record, pk: PrivateKey, updated = true if updated: - if r.seqNum == high(r.seqNum): # highly unlikely + if r.seqNum == high(type r.seqNum): # highly unlikely return err("Maximum sequence number reached") r.seqNum.inc() r.raw = ? makeEnrRaw(r.seqNum, pk, r.pairs) diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index 69e6a40..6254092 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -83,7 +83,7 @@ import std/[tables, sets, options, math, sequtils, algorithm], stew/shims/net as stewNet, json_serialization/std/net, - stew/[endians2, results], chronicles, chronos, stint, metrics, + stew/results, chronicles, chronos, stint, metrics, ".."/../[rlp, keys], "."/[messages_encoding, encoding, node, routing_table, enr, random2, sessions, ip_vote, nodes_verification] diff --git a/eth/p2p/kademlia.nim b/eth/p2p/kademlia.nim index 038b30c..4f89f18 100644 --- a/eth/p2p/kademlia.nim +++ b/eth/p2p/kademlia.nim @@ -597,7 +597,7 @@ proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) = future.complete(true) k.updateLastPongReceived(n, getTime()) -proc recvPing*(k: KademliaProtocol, n: Node, msgHash: any) +proc recvPing*(k: KademliaProtocol, n: Node, msgHash: auto) {.raises: [ValueError, Defect].} = trace "<<< ping from ", n k.wire.sendPong(n, msgHash) diff --git a/eth/p2p/peer_pool.nim b/eth/p2p/peer_pool.nim index 9a5fb18..714c2bd 100644 --- a/eth/p2p/peer_pool.nim +++ b/eth/p2p/peer_pool.nim @@ -13,7 +13,7 @@ import std/[os, tables, times, random, sequtils, options], chronos, chronicles, - ".."/[rlp, keys, common], + ".."/[keys, common], ./private/p2p_types, "."/[discovery, kademlia, rlpx, enode] const diff --git a/eth/p2p/private/p2p_types.nim b/eth/p2p/private/p2p_types.nim index 1477d6e..488fdfe 100644 --- a/eth/p2p/private/p2p_types.nim +++ b/eth/p2p/private/p2p_types.nim @@ -8,7 +8,6 @@ import std/[deques, tables], chronos, - stew/results, ".."/../[rlp, keys], ../../common/eth_types, ".."/[enode, kademlia, discovery, rlpxcrypt] diff --git a/eth/p2p/rlpx.nim b/eth/p2p/rlpx.nim index 676ef5d..2b2e05f 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -378,8 +378,8 @@ template compressMsg(peer: Peer, data: seq[byte]): seq[byte] = data proc sendMsg*(peer: Peer, data: seq[byte]) {.gcsafe, async.} = + var cipherText = encryptMsg(peer.compressMsg(data), peer.secretsState) try: - var cipherText = encryptMsg(peer.compressMsg(data), peer.secretsState) var res = await peer.transport.write(cipherText) if res != len(cipherText): # This is ECONNRESET or EPIPE case when remote peer disconnected. diff --git a/eth/p2p/rlpxcrypt.nim b/eth/p2p/rlpxcrypt.nim index 8545f3b..abfaa80 100644 --- a/eth/p2p/rlpxcrypt.nim +++ b/eth/p2p/rlpxcrypt.nim @@ -137,16 +137,13 @@ proc encrypt*(c: var SecretState, header: openArray[byte], ok() proc encryptMsg*(msg: openArray[byte], secrets: var SecretState): seq[byte] = + doAssert(uint32(msg.len) <= maxUInt24, "RLPx message size exceeds limit") + var header: RlpxHeader - - if uint32(msg.len) > maxUInt24: - raise newException(OverflowError, "RLPx message size exceeds limit") - # write the frame size in the first 3 bytes of the header header[0] = byte((msg.len shr 16) and 0xFF) header[1] = byte((msg.len shr 8) and 0xFF) header[2] = byte(msg.len and 0xFF) - # This is the [capability-id, context-id] in header-data # While not really used, this is checked in the Parity client. # Same as rlp.encode((0, 0)) @@ -154,11 +151,10 @@ proc encryptMsg*(msg: openArray[byte], secrets: var SecretState): seq[byte] = header[4] = 0x80 header[5] = 0x80 - # XXX: - # This would be safer if we use a thread-local sequ for the temporary buffer - result = newSeq[byte](encryptedLength(msg.len)) - let s = encrypt(secrets, header, msg, result) - s.expect("always succeeds because we call with correct buffer") + var res = newSeq[byte](encryptedLength(msg.len)) + encrypt(secrets, header, msg, res).expect( + "always succeeds because we call with correct buffer") + res proc getBodySize*(a: RlpxHeader): int = (int(a[0]) shl 16) or (int(a[1]) shl 8) or int(a[2])