From d09e9f1aaf13aaacce55aec929136e84e085daf2 Mon Sep 17 00:00:00 2001 From: tersec Date: Tue, 30 Jun 2020 09:17:49 +0000 Subject: [PATCH] remove non-snappy support from inspector; switch some procs to funcs (#1261) * remove non-snappy support from inspector; switch some procs to funcs * use mapIt instead of explicit for loop --- beacon_chain/inspector.nim | 58 ++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/beacon_chain/inspector.nim b/beacon_chain/inspector.nim index a25d24ec0..3a7d6cb8a 100644 --- a/beacon_chain/inspector.nim +++ b/beacon_chain/inspector.nim @@ -4,7 +4,7 @@ # * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # at your option. This file may not be copied, modified, or distributed except according to those terms. -import strutils, os, tables, options +import sequtils, strutils, os, tables, options import confutils, chronicles, chronos import libp2p/[switch, standard_setup, multiaddress, multicodec, peer, peerinfo, peer] @@ -156,15 +156,15 @@ type StrRes[T] = Result[T, string] -proc `==`*(a, b: ENRFieldPair): bool {.inline.} = +func `==`*(a, b: ENRFieldPair): bool {.inline.} = result = (a.eth2 == b.eth2) -proc hasTCP(a: PeerInfo): bool = +func hasTCP(a: PeerInfo): bool = for ma in a.addrs: if TCP.match(ma): return true -proc toNodeId(a: PeerID): Option[NodeId] = +func toNodeId(a: PeerID): Option[NodeId] = var buffer: array[64, byte] if a.hasPublicKey(): var pubkey: lcrypto.PublicKey @@ -185,24 +185,20 @@ func getTopics(forkDigest: ForkDigest, case filter of TopicFilter.Blocks: let topic = getBeaconBlocksTopic(forkDigest) - @[topic, topic & "_snappy"] + @[topic & "_snappy"] of TopicFilter.Exits: let topic = getVoluntaryExitsTopic(forkDigest) - @[topic, topic & "_snappy"] + @[topic & "_snappy"] of TopicFilter.ProposerSlashing: let topic = getProposerSlashingsTopic(forkDigest) - @[topic, topic & "_snappy"] + @[topic & "_snappy"] of TopicFilter.AttesterSlashings: let topic = getAttesterSlashingsTopic(forkDigest) - @[topic, topic & "_snappy"] + @[topic & "_snappy"] of TopicFilter.Attestations: - var topics = newSeq[string](ATTESTATION_SUBNET_COUNT * 2) - var offset = 0 - for i in 0'u64 ..< ATTESTATION_SUBNET_COUNT.uint64: - topics[offset] = getAttestationTopic(forkDigest, i) - topics[offset + 1] = getAttestationTopic(forkDigest, i) & "_snappy" - offset += 2 - topics + mapIt( + 0'u64 ..< ATTESTATION_SUBNET_COUNT.uint64, + getAttestationTopic(forkDigest, it) & "_snappy") proc loadBootFile(name: string): seq[string] = try: @@ -210,7 +206,7 @@ proc loadBootFile(name: string): seq[string] = except: discard -proc unpackYmlLine(line: string): string = +func unpackYmlLine(line: string): string = result = line let stripped = line.strip() var parts = stripped.split({'"'}) @@ -244,7 +240,7 @@ proc getBootstrapAddress(bootnode: string): Option[BootstrapAddress] = except CatchableError as exc: warn "Incorrect bootstrap address", address = bootnode, errMsg = exc.msg -proc tryGetForkDigest(bootnode: enr.Record): Option[ForkDigest] = +func tryGetForkDigest(bootnode: enr.Record): Option[ForkDigest] = var forkId: ENRForkID var sszForkData = bootnode.tryGet("eth2", seq[byte]) if sszForkData.isSome(): @@ -254,14 +250,14 @@ proc tryGetForkDigest(bootnode: enr.Record): Option[ForkDigest] = except CatchableError: discard -proc tryGetFieldPairs(bootnode: enr.Record): Option[ENRFieldPair] = +func tryGetFieldPairs(bootnode: enr.Record): Option[ENRFieldPair] = var sszEth2 = bootnode.tryGet("eth2", seq[byte]) var sszAttnets = bootnode.tryGet("attnets", seq[byte]) if sszEth2.isSome() and sszAttnets.isSome(): result = some(ENRFieldPair(eth2: sszEth2.get(), attnets: sszAttnets.get())) -proc tryGetForkDigest(hexdigest: string): Option[ForkDigest] = +func tryGetForkDigest(hexdigest: string): Option[ForkDigest] = var res: ForkDigest if len(hexdigest) > 0: try: @@ -270,7 +266,7 @@ proc tryGetForkDigest(hexdigest: string): Option[ForkDigest] = except CatchableError: discard -proc tryGetMultiAddress(address: string): Option[MultiAddress] = +func tryGetMultiAddress(address: string): Option[MultiAddress] = let maRes = MultiAddress.init(address) let ma = if maRes.isOk: maRes.get else: return @@ -389,7 +385,7 @@ proc connectLoop*(switch: Switch, for item in infos: peerTable[item.peerId] = item -proc toIpAddress*(ma: MultiAddress): Option[ValidIpAddress] = +func toIpAddress*(ma: MultiAddress): Option[ValidIpAddress] = if IP4.match(ma): let addressRes = ma.protoAddress() let address = if addressRes.isOk: addressRes.get @@ -486,7 +482,7 @@ proc logEnrAddress(address: string) = else: info "ENR bootstrap address is wrong or incomplete", enr_uri = address -proc init*(p: typedesc[PeerInfo], +func init*(p: typedesc[PeerInfo], enruri: EnrUri): Option[PeerInfo] {.inline.} = var rec: enr.Record if fromURI(rec, enruri): @@ -510,24 +506,18 @@ proc pubsubLogger(conf: InspectorConf, switch: Switch, buffer = data try: - if topic.endsWith(topicBeaconBlocksSuffix) or - topic.endsWith(topicBeaconBlocksSuffix & "_snappy"): + if topic.endsWith(topicBeaconBlocksSuffix & "_snappy"): info "SignedBeaconBlock", msg = SSZ.decode(buffer, SignedBeaconBlock) - elif topic.endsWith(topicMainnetAttestationsSuffix) or - topic.endsWith(topicMainnetAttestationsSuffix & "_snappy"): + elif topic.endsWith(topicMainnetAttestationsSuffix & "_snappy"): info "Attestation", msg = SSZ.decode(buffer, Attestation) - elif topic.endsWith(topicVoluntaryExitsSuffix) or - topic.endsWith(topicVoluntaryExitsSuffix & "_snappy"): + elif topic.endsWith(topicVoluntaryExitsSuffix & "_snappy"): info "SignedVoluntaryExit", msg = SSZ.decode(buffer, SignedVoluntaryExit) - elif topic.endsWith(topicProposerSlashingsSuffix) or - topic.endsWith(topicProposerSlashingsSuffix & "_snappy"): + elif topic.endsWith(topicProposerSlashingsSuffix & "_snappy"): info "ProposerSlashing", msg = SSZ.decode(buffer, ProposerSlashing) - elif topic.endsWith(topicAttesterSlashingsSuffix) or - topic.endsWith(topicAttesterSlashingsSuffix & "_snappy"): + elif topic.endsWith(topicAttesterSlashingsSuffix & "_snappy"): info "AttesterSlashing", msg = SSZ.decode(buffer, AttesterSlashing) - elif topic.endsWith(topicAggregateAndProofsSuffix) or - topic.endsWith(topicAggregateAndProofsSuffix & "_snappy"): + elif topic.endsWith(topicAggregateAndProofsSuffix & "_snappy"): info "AggregateAndProof", msg = SSZ.decode(buffer, AggregateAndProof) except CatchableError as exc: