From 295e3c9c730892c2c5fc9fdb96cc4f11b6ddaf1c Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Fri, 15 Jan 2021 13:17:06 +0900 Subject: [PATCH] Topics validation and direct peers (#2237) * pick the right libp2p branch * add topics validation --- beacon_chain/eth2_network.nim | 11 ++++++++++- beacon_chain/nimbus_beacon_node.nim | 15 +++++++++++++++ vendor/nim-libp2p | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index b18239e7a..e83ee33e3 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -1,6 +1,6 @@ import # Std lib - std/[typetraits, strutils, os, algorithm, math, sets], + std/[typetraits, strutils, sequtils, os, algorithm, math, sets], std/options as stdOptions, # Status libs @@ -74,6 +74,7 @@ type forkId: ENRForkID rng*: ref BrHmacDrbgContext peers*: Table[PeerID, Peer] + validTopics: HashSet[string] EthereumNode = Eth2Node # needed for the definitions in p2p_backends_helpers @@ -1596,6 +1597,14 @@ proc subscribe*(node: Eth2Node, topic: string, enableTopicMetrics: bool = false) node.pubsub.subscribe(topicName, dummyMsgHandler) +proc setValidTopics*(node: Eth2Node, topics: openArray[string]) = + let topicsSnappy = topics.mapIt(it & "_snappy") + node.validTopics = topicsSnappy.toHashSet() + # there is a window of time where we got the switch open, we need this lazy update for now + node.pubsub.subscriptionValidator = + proc(topic: string): bool {.gcsafe, raises: [Defect].} = + topic in node.validTopics + proc addValidator*[MsgType](node: Eth2Node, topic: string, msgValidator: proc(msg: MsgType): diff --git a/beacon_chain/nimbus_beacon_node.nim b/beacon_chain/nimbus_beacon_node.nim index 61e5333f2..f56b17e2f 100644 --- a/beacon_chain/nimbus_beacon_node.nim +++ b/beacon_chain/nimbus_beacon_node.nim @@ -296,6 +296,21 @@ proc init*(T: type BeaconNode, topicAggregateAndProofs: topicAggregateAndProofs, ) + # set topic validation routine + network.setValidTopics( + block: + var + topics = @[ + topicBeaconBlocks, + getAttesterSlashingsTopic(enrForkId.forkDigest), + getProposerSlashingsTopic(enrForkId.forkDigest), + getVoluntaryExitsTopic(enrForkId.forkDigest), + getAggregateAndProofsTopic(enrForkId.forkDigest) + ] + for subnet in 0'u64 ..< ATTESTATION_SUBNET_COUNT: + topics &= getAttestationTopic(enrForkId.forkDigest, subnet) + topics) + info "Loading slashing protection database", path = conf.validatorsDir() res.attachedValidators = ValidatorPool.init( SlashingProtectionDB.init( diff --git a/vendor/nim-libp2p b/vendor/nim-libp2p index 3878a95b2..983003a52 160000 --- a/vendor/nim-libp2p +++ b/vendor/nim-libp2p @@ -1 +1 @@ -Subproject commit 3878a95b23306494b5ad1138383b78df7c7e42d0 +Subproject commit 983003a52b22926603f25bdf54c26e8a23c97254