Improve the discv5 initialization logs (#552)

- Unify the debug log and info log into one. Removing the
redundant information
- Log the custom ENR fields more pretty
- Make the JSON format logging more pretty for several types
This commit is contained in:
Kim De Mey 2022-11-10 10:52:24 +01:00 committed by GitHub
parent 86b37bf72e
commit 70b83a4efb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 12 deletions

View File

@ -13,6 +13,7 @@
import import
std/[strutils, macros, algorithm, options], std/[strutils, macros, algorithm, options],
nimcrypto/[keccak, utils], stew/shims/net, stew/[base64, results], nimcrypto/[keccak, utils], stew/shims/net, stew/[base64, results],
chronicles,
".."/../[rlp, keys] ".."/../[rlp, keys]
export options, results, keys export options, results, keys
@ -489,6 +490,9 @@ proc `$`(f: Field): string =
of kList: of kList:
"(Raw RLP list) " & "0x" & f.listRaw.toHex "(Raw RLP list) " & "0x" & f.listRaw.toHex
func `$`*(fp: FieldPair): string =
fp[0] & ":" & $fp[1]
proc `$`*(r: Record): string = proc `$`*(r: Record): string =
result = "(" result = "("
result &= $r.seqNum result &= $r.seqNum
@ -526,3 +530,5 @@ proc read*(rlp: var Rlp, T: typedesc[Record]):
proc append*(rlpWriter: var RlpWriter, value: Record) = proc append*(rlpWriter: var RlpWriter, value: Record) =
rlpWriter.appendRawBytes(value.raw) rlpWriter.appendRawBytes(value.raw)
chronicles.formatIt(seq[FieldPair]): $it

View File

@ -104,7 +104,6 @@ func shortLog*(id: NodeId): string =
result.add("*") result.add("*")
for i in (len(sid) - 6)..sid.high: for i in (len(sid) - 6)..sid.high:
result.add(sid[i]) result.add(sid[i])
chronicles.formatIt(NodeId): shortLog(it)
func hash*(a: Address): hashes.Hash = func hash*(a: Address): hashes.Hash =
let res = a.ip.hash !& a.port.hash let res = a.ip.hash !& a.port.hash
@ -121,7 +120,6 @@ func shortLog*(n: Node): string =
shortLog(n.id) & ":unaddressable" shortLog(n.id) & ":unaddressable"
else: else:
shortLog(n.id) & ":" & $n.address.get() shortLog(n.id) & ":" & $n.address.get()
chronicles.formatIt(Node): shortLog(it)
func shortLog*(nodes: seq[Node]): string = func shortLog*(nodes: seq[Node]): string =
result = "[" result = "["
@ -135,4 +133,8 @@ func shortLog*(nodes: seq[Node]): string =
result.add(shortLog(n)) result.add(shortLog(n))
result.add("]") result.add("]")
chronicles.formatIt(NodeId): shortLog(it)
chronicles.formatIt(Address): $it
chronicles.formatIt(Node): shortLog(it)
chronicles.formatIt(seq[Node]): shortLog(it) chronicles.formatIt(seq[Node]): shortLog(it)

View File

@ -173,6 +173,9 @@ const
responseTimeout: defaultResponseTimeout responseTimeout: defaultResponseTimeout
) )
chronicles.formatIt(Option[Port]): $it
chronicles.formatIt(Option[ValidIpAddress]): $it
proc addNode*(d: Protocol, node: Node): bool = proc addNode*(d: Protocol, node: Node): bool =
## Add `Node` to discovery routing table. ## Add `Node` to discovery routing table.
## ##
@ -967,7 +970,7 @@ proc newProtocol*(
# Anyhow, nim-beacon-chain would also require some changes to support port # Anyhow, nim-beacon-chain would also require some changes to support port
# remapping through NAT and this API is also subject to change once we # remapping through NAT and this API is also subject to change once we
# introduce support for ipv4 + ipv6 binding/listening. # introduce support for ipv4 + ipv6 binding/listening.
let extraFields = mapIt(localEnrFields, toFieldPair(it[0], it[1])) let customEnrFields = mapIt(localEnrFields, toFieldPair(it[0], it[1]))
# TODO: # TODO:
# - Defect as is now or return a result for enr errors? # - Defect as is now or return a result for enr errors?
# - In case incorrect key, allow for new enr based on new key (new node id)? # - In case incorrect key, allow for new enr based on new key (new node id)?
@ -975,18 +978,14 @@ proc newProtocol*(
if previousRecord.isSome(): if previousRecord.isSome():
record = previousRecord.get() record = previousRecord.get()
record.update(privKey, enrIp, enrTcpPort, enrUdpPort, record.update(privKey, enrIp, enrTcpPort, enrUdpPort,
extraFields).expect("Record within size limits and correct key") customEnrFields).expect("Record within size limits and correct key")
else: else:
record = enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort, record = enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort,
extraFields).expect("Record within size limits") customEnrFields).expect("Record within size limits")
debug "Initializing discovery v5", info "Discovery ENR initialized", enrAutoUpdate, seqNum = record.seqNum,
enrIp, enrTcpPort, enrUdpPort, enrAutoUpdate, ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort,
bootstrapEnrs = bootstrapRecords, localEnrFields, customEnrFields, uri = toURI(record)
bindPort, bindIp
info "ENR initialized", ip = enrIp, tcp = enrTcpPort, udp = enrUdpPort,
seqNum = record.seqNum, uri = toURI(record)
if enrIp.isNone(): if enrIp.isNone():
if enrAutoUpdate: if enrAutoUpdate:
notice "No external IP provided for the ENR, this node will not be " & notice "No external IP provided for the ENR, this node will not be " &