Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions beacon_chain/eth2_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,20 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
p.historyLength = 6
p.historyGossip = 3
p.seenTTL = 385.seconds
p.gossipThreshold = -4000
p.publishThreshold = -8000
p.graylistThreshold = -16000
p.decayInterval = 12.seconds
p.decayToZero = 0.01
p.retainScore = 385.seconds
p.ipColocationFactorWeight = -53.75
p.ipColocationFactorThreshold = 3
p.behaviourPenaltyWeight = -15.9
p.behaviourPenaltyDecay = 0.986
p.floodPublish = true
p.gossipFactor = 0.05
p.disconnectBadPeers = true
p.validateParameters().tryGet()
p
pubsub = GossipSub.init(
switch = switch,
Expand All @@ -1578,14 +1591,6 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext,
anonymize = true,
parameters = params)

# disable any extra scoring weight
# do this after validation (done inside init())
# this is a scoring violation for validation
# but we don't want to use scores for now
pubsub.parameters.behaviourPenaltyWeight = 0.0
pubsub.parameters.appSpecificWeight = 0.0
pubsub.parameters.ipColocationFactorWeight = 0.0

switch.mount(pubsub)

result = Eth2Node.init(conf, enrForkId, switch, pubsub,
Expand All @@ -1601,7 +1606,7 @@ proc announcedENR*(node: Eth2Node): enr.Record =
proc shortForm*(id: KeyPair): string =
$PeerID.init(id.pubkey)

proc subscribe*(node: Eth2Node, topic: string, enableTopicMetrics: bool = false) =
proc subscribe*(node: Eth2Node, topic: string, topicParams: TopicParams, enableTopicMetrics: bool = false) =
proc dummyMsgHandler(topic: string, data: seq[byte]) {.async.} =
discard

Expand All @@ -1611,6 +1616,7 @@ proc subscribe*(node: Eth2Node, topic: string, enableTopicMetrics: bool = false)
if enableTopicMetrics:
node.pubsub.knownTopics.incl(topicName)

node.pubsub.topicParams[topicName] = topicParams
node.pubsub.subscribe(topicName, dummyMsgHandler)

proc setValidTopics*(node: Eth2Node, topics: openArray[string]) =
Expand Down
71 changes: 65 additions & 6 deletions beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ import

from eth/common/eth_types import BlockHashOrNumber

from
libp2p/protocols/pubsub/gossipsub
import
TopicParams, validateParameters, init

const
hasPrompt = not defined(withoutPrompt)

Expand Down Expand Up @@ -368,10 +373,15 @@ func verifyFinalization(node: BeaconNode, slot: Slot) =
# finalization occurs every slot, to 4 slots vs scheduledSlot.
doAssert finalizedEpoch + 4 >= epoch

proc getTotalValidators(node: BeaconNode): uint64 =
let epochRef = getEpochRef(node.chainDag, node.chainDag.head, node.chainDag.head.slot.epoch)
epochRef.shuffled_active_validator_indices.lenu64()

proc installAttestationSubnetHandlers(node: BeaconNode, subnets: set[uint8]) =
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.0/specs/phase0/p2p-interface.md#attestations-and-aggregation
# nimbus won't score attestation subnets for now, we just rely on block and aggregate which are more stabe and reliable
for subnet in subnets:
node.network.subscribe(getAttestationTopic(node.forkDigest, subnet))
node.network.subscribe(getAttestationTopic(node.forkDigest, subnet), TopicParams.init()) # don't score attestation subnets for now

proc updateStabilitySubnetMetadata(
node: BeaconNode, stabilitySubnets: set[uint8]) =
Expand Down Expand Up @@ -671,11 +681,60 @@ proc getAttestationSubnetHandlers(node: BeaconNode) =
node.attestationSubnets.subscribedSubnets + initialStabilitySubnets)

proc addMessageHandlers(node: BeaconNode) =
node.network.subscribe(node.topicBeaconBlocks, enableTopicMetrics = true)
node.network.subscribe(getAttesterSlashingsTopic(node.forkDigest))
node.network.subscribe(getProposerSlashingsTopic(node.forkDigest))
node.network.subscribe(getVoluntaryExitsTopic(node.forkDigest))
node.network.subscribe(getAggregateAndProofsTopic(node.forkDigest), enableTopicMetrics = true)
# inspired by lighthouse research here
# https://gist.github.com/blacktemplar/5c1862cb3f0e32a1a7fb0b25e79e6e2c#file-generate-scoring-params-py
const
blocksTopicParams = TopicParams(
topicWeight: 0.5,
timeInMeshWeight: 0.03333333333333333,
timeInMeshQuantum: chronos.seconds(12),
timeInMeshCap: 300,
firstMessageDeliveriesWeight: 1.1471603557060206,
firstMessageDeliveriesDecay: 0.9928302477768374,
firstMessageDeliveriesCap: 34.86870846001471,
meshMessageDeliveriesWeight: -458.31054878249114,
meshMessageDeliveriesDecay: 0.9716279515771061,
meshMessageDeliveriesThreshold: 0.6849191409056553,
meshMessageDeliveriesCap: 2.054757422716966,
meshMessageDeliveriesActivation: chronos.seconds(384),
meshMessageDeliveriesWindow: chronos.seconds(2),
meshFailurePenaltyWeight: -458.31054878249114 ,
meshFailurePenaltyDecay: 0.9716279515771061,
invalidMessageDeliveriesWeight: -214.99999999999994,
invalidMessageDeliveriesDecay: 0.9971259067705325
)
aggregateTopicParams = TopicParams(
topicWeight: 0.5,
timeInMeshWeight: 0.03333333333333333,
timeInMeshQuantum: chronos.seconds(12),
timeInMeshCap: 300,
firstMessageDeliveriesWeight: 0.10764904539552399,
firstMessageDeliveriesDecay: 0.8659643233600653,
firstMessageDeliveriesCap: 371.5778421725158,
meshMessageDeliveriesWeight: -0.07538533073670682,
meshMessageDeliveriesDecay: 0.930572040929699,
meshMessageDeliveriesThreshold: 53.404248450179836,
meshMessageDeliveriesCap: 213.61699380071934,
meshMessageDeliveriesActivation: chronos.seconds(384),
meshMessageDeliveriesWindow: chronos.seconds(2),
meshFailurePenaltyWeight: -0.07538533073670682 ,
meshFailurePenaltyDecay: 0.930572040929699,
invalidMessageDeliveriesWeight: -214.99999999999994,
invalidMessageDeliveriesDecay: 0.9971259067705325
)
basicParams = TopicParams.init()

static:
# compile time validation
blocksTopicParams.validateParameters().tryGet()
aggregateTopicParams.validateParameters().tryGet()
basicParams.validateParameters.tryGet()

node.network.subscribe(node.topicBeaconBlocks, blocksTopicParams, enableTopicMetrics = true)
node.network.subscribe(getAttesterSlashingsTopic(node.forkDigest), basicParams)
node.network.subscribe(getProposerSlashingsTopic(node.forkDigest), basicParams)
node.network.subscribe(getVoluntaryExitsTopic(node.forkDigest), basicParams)
node.network.subscribe(getAggregateAndProofsTopic(node.forkDigest), aggregateTopicParams, enableTopicMetrics = true)
node.getAttestationSubnetHandlers()

func getTopicSubscriptionEnabled(node: BeaconNode): bool =
Expand Down