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
This commit is contained in:
tersec 2020-06-30 09:17:49 +00:00 committed by GitHub
parent 76a9708dff
commit d09e9f1aaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 34 deletions

View File

@ -4,7 +4,7 @@
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). # * 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). # * 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. # 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 confutils, chronicles, chronos
import libp2p/[switch, standard_setup, multiaddress, multicodec, import libp2p/[switch, standard_setup, multiaddress, multicodec,
peer, peerinfo, peer] peer, peerinfo, peer]
@ -156,15 +156,15 @@ type
StrRes[T] = Result[T, string] StrRes[T] = Result[T, string]
proc `==`*(a, b: ENRFieldPair): bool {.inline.} = func `==`*(a, b: ENRFieldPair): bool {.inline.} =
result = (a.eth2 == b.eth2) result = (a.eth2 == b.eth2)
proc hasTCP(a: PeerInfo): bool = func hasTCP(a: PeerInfo): bool =
for ma in a.addrs: for ma in a.addrs:
if TCP.match(ma): if TCP.match(ma):
return true return true
proc toNodeId(a: PeerID): Option[NodeId] = func toNodeId(a: PeerID): Option[NodeId] =
var buffer: array[64, byte] var buffer: array[64, byte]
if a.hasPublicKey(): if a.hasPublicKey():
var pubkey: lcrypto.PublicKey var pubkey: lcrypto.PublicKey
@ -185,24 +185,20 @@ func getTopics(forkDigest: ForkDigest,
case filter case filter
of TopicFilter.Blocks: of TopicFilter.Blocks:
let topic = getBeaconBlocksTopic(forkDigest) let topic = getBeaconBlocksTopic(forkDigest)
@[topic, topic & "_snappy"] @[topic & "_snappy"]
of TopicFilter.Exits: of TopicFilter.Exits:
let topic = getVoluntaryExitsTopic(forkDigest) let topic = getVoluntaryExitsTopic(forkDigest)
@[topic, topic & "_snappy"] @[topic & "_snappy"]
of TopicFilter.ProposerSlashing: of TopicFilter.ProposerSlashing:
let topic = getProposerSlashingsTopic(forkDigest) let topic = getProposerSlashingsTopic(forkDigest)
@[topic, topic & "_snappy"] @[topic & "_snappy"]
of TopicFilter.AttesterSlashings: of TopicFilter.AttesterSlashings:
let topic = getAttesterSlashingsTopic(forkDigest) let topic = getAttesterSlashingsTopic(forkDigest)
@[topic, topic & "_snappy"] @[topic & "_snappy"]
of TopicFilter.Attestations: of TopicFilter.Attestations:
var topics = newSeq[string](ATTESTATION_SUBNET_COUNT * 2) mapIt(
var offset = 0 0'u64 ..< ATTESTATION_SUBNET_COUNT.uint64,
for i in 0'u64 ..< ATTESTATION_SUBNET_COUNT.uint64: getAttestationTopic(forkDigest, it) & "_snappy")
topics[offset] = getAttestationTopic(forkDigest, i)
topics[offset + 1] = getAttestationTopic(forkDigest, i) & "_snappy"
offset += 2
topics
proc loadBootFile(name: string): seq[string] = proc loadBootFile(name: string): seq[string] =
try: try:
@ -210,7 +206,7 @@ proc loadBootFile(name: string): seq[string] =
except: except:
discard discard
proc unpackYmlLine(line: string): string = func unpackYmlLine(line: string): string =
result = line result = line
let stripped = line.strip() let stripped = line.strip()
var parts = stripped.split({'"'}) var parts = stripped.split({'"'})
@ -244,7 +240,7 @@ proc getBootstrapAddress(bootnode: string): Option[BootstrapAddress] =
except CatchableError as exc: except CatchableError as exc:
warn "Incorrect bootstrap address", address = bootnode, errMsg = exc.msg 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 forkId: ENRForkID
var sszForkData = bootnode.tryGet("eth2", seq[byte]) var sszForkData = bootnode.tryGet("eth2", seq[byte])
if sszForkData.isSome(): if sszForkData.isSome():
@ -254,14 +250,14 @@ proc tryGetForkDigest(bootnode: enr.Record): Option[ForkDigest] =
except CatchableError: except CatchableError:
discard discard
proc tryGetFieldPairs(bootnode: enr.Record): Option[ENRFieldPair] = func tryGetFieldPairs(bootnode: enr.Record): Option[ENRFieldPair] =
var sszEth2 = bootnode.tryGet("eth2", seq[byte]) var sszEth2 = bootnode.tryGet("eth2", seq[byte])
var sszAttnets = bootnode.tryGet("attnets", seq[byte]) var sszAttnets = bootnode.tryGet("attnets", seq[byte])
if sszEth2.isSome() and sszAttnets.isSome(): if sszEth2.isSome() and sszAttnets.isSome():
result = some(ENRFieldPair(eth2: sszEth2.get(), result = some(ENRFieldPair(eth2: sszEth2.get(),
attnets: sszAttnets.get())) attnets: sszAttnets.get()))
proc tryGetForkDigest(hexdigest: string): Option[ForkDigest] = func tryGetForkDigest(hexdigest: string): Option[ForkDigest] =
var res: ForkDigest var res: ForkDigest
if len(hexdigest) > 0: if len(hexdigest) > 0:
try: try:
@ -270,7 +266,7 @@ proc tryGetForkDigest(hexdigest: string): Option[ForkDigest] =
except CatchableError: except CatchableError:
discard discard
proc tryGetMultiAddress(address: string): Option[MultiAddress] = func tryGetMultiAddress(address: string): Option[MultiAddress] =
let maRes = MultiAddress.init(address) let maRes = MultiAddress.init(address)
let ma = if maRes.isOk: maRes.get let ma = if maRes.isOk: maRes.get
else: return else: return
@ -389,7 +385,7 @@ proc connectLoop*(switch: Switch,
for item in infos: for item in infos:
peerTable[item.peerId] = item peerTable[item.peerId] = item
proc toIpAddress*(ma: MultiAddress): Option[ValidIpAddress] = func toIpAddress*(ma: MultiAddress): Option[ValidIpAddress] =
if IP4.match(ma): if IP4.match(ma):
let addressRes = ma.protoAddress() let addressRes = ma.protoAddress()
let address = if addressRes.isOk: addressRes.get let address = if addressRes.isOk: addressRes.get
@ -486,7 +482,7 @@ proc logEnrAddress(address: string) =
else: else:
info "ENR bootstrap address is wrong or incomplete", enr_uri = address 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.} = enruri: EnrUri): Option[PeerInfo] {.inline.} =
var rec: enr.Record var rec: enr.Record
if fromURI(rec, enruri): if fromURI(rec, enruri):
@ -510,24 +506,18 @@ proc pubsubLogger(conf: InspectorConf, switch: Switch,
buffer = data buffer = data
try: try:
if topic.endsWith(topicBeaconBlocksSuffix) or if topic.endsWith(topicBeaconBlocksSuffix & "_snappy"):
topic.endsWith(topicBeaconBlocksSuffix & "_snappy"):
info "SignedBeaconBlock", msg = SSZ.decode(buffer, SignedBeaconBlock) info "SignedBeaconBlock", msg = SSZ.decode(buffer, SignedBeaconBlock)
elif topic.endsWith(topicMainnetAttestationsSuffix) or elif topic.endsWith(topicMainnetAttestationsSuffix & "_snappy"):
topic.endsWith(topicMainnetAttestationsSuffix & "_snappy"):
info "Attestation", msg = SSZ.decode(buffer, Attestation) info "Attestation", msg = SSZ.decode(buffer, Attestation)
elif topic.endsWith(topicVoluntaryExitsSuffix) or elif topic.endsWith(topicVoluntaryExitsSuffix & "_snappy"):
topic.endsWith(topicVoluntaryExitsSuffix & "_snappy"):
info "SignedVoluntaryExit", msg = SSZ.decode(buffer, info "SignedVoluntaryExit", msg = SSZ.decode(buffer,
SignedVoluntaryExit) SignedVoluntaryExit)
elif topic.endsWith(topicProposerSlashingsSuffix) or elif topic.endsWith(topicProposerSlashingsSuffix & "_snappy"):
topic.endsWith(topicProposerSlashingsSuffix & "_snappy"):
info "ProposerSlashing", msg = SSZ.decode(buffer, ProposerSlashing) info "ProposerSlashing", msg = SSZ.decode(buffer, ProposerSlashing)
elif topic.endsWith(topicAttesterSlashingsSuffix) or elif topic.endsWith(topicAttesterSlashingsSuffix & "_snappy"):
topic.endsWith(topicAttesterSlashingsSuffix & "_snappy"):
info "AttesterSlashing", msg = SSZ.decode(buffer, AttesterSlashing) info "AttesterSlashing", msg = SSZ.decode(buffer, AttesterSlashing)
elif topic.endsWith(topicAggregateAndProofsSuffix) or elif topic.endsWith(topicAggregateAndProofsSuffix & "_snappy"):
topic.endsWith(topicAggregateAndProofsSuffix & "_snappy"):
info "AggregateAndProof", msg = SSZ.decode(buffer, AggregateAndProof) info "AggregateAndProof", msg = SSZ.decode(buffer, AggregateAndProof)
except CatchableError as exc: except CatchableError as exc: