Skip to content

Commit

Permalink
Stop using p2pNetwork.UpdateSubnets and use a new function solely for…
Browse files Browse the repository at this point in the history
… updating the score params
  • Loading branch information
MatheusFranco99 committed Jul 16, 2024
1 parent 2bf42d4 commit b665ab3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
2 changes: 2 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type P2PNetwork interface {
SubscribeAll(logger *zap.Logger) error
// SubscribeRandoms subscribes to random subnets
SubscribeRandoms(logger *zap.Logger, numSubnets int) error
// UpdateScoreParams will update the scoring parameters of GossipSub
UpdateScoreParams(logger *zap.Logger)
}

// GetValidatorStats returns stats of validators, including the following:
Expand Down
50 changes: 20 additions & 30 deletions network/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,6 @@ func (n *p2pNetwork) UpdateSubnets(logger *zap.Logger) {
registeredSubnets := make([]byte, commons.Subnets())
defer ticker.Stop()

// Holds participating validators to detect changes and to trigger updates
pariticipatingValidators := make(map[int]struct{})

// Run immediately and then every second.
for ; true; <-ticker.C {
start := time.Now()
Expand Down Expand Up @@ -313,38 +310,31 @@ func (n *p2pNetwork) UpdateSubnets(logger *zap.Logger) {
zap.Int("total_subnets", len(subnetsList)),
zap.Duration("took", time.Since(start)),
)

// Update topics score parameters upon change
// TODO: use an appropriate trigger (outside this function) for the UpdateScoreParams function to called by the event of an addition or exit of a validator
newParticipatingValidators, changed := getUpdatedParticipatingValidators(n, pariticipatingValidators)
if changed {
n.topicsCtrl.UpdateScoreParams(logger)
}
pariticipatingValidators = newParticipatingValidators
}
}

// Returns the updated set of participating validators. Also, compares to a previous set and returns whether it changed or not.
// If performance is an issue, the comparison could be done in a more efficient way with a high probability of detecting the change
// (for example, by using a state with the list's XOR and checksum values)
func getUpdatedParticipatingValidators(p2pnet *p2pNetwork, previousValidators map[int]struct{}) (map[int]struct{}, bool) {
// Gret updated list of shares
shares := p2pnet.nodeStorage.ValidatorStore().ParticipatingValidators(p2pnet.cfg.Network.Beacon.EstimatedCurrentEpoch())
// Create updated set
newValidators := make(map[int]struct{})
for _, share := range shares {
newValidators[int(share.ValidatorIndex)] = struct{}{}
}
// Compare it to the previous set
if len(previousValidators) != len(newValidators) {
return newValidators, true
}
for validatorIndex := range newValidators {
if _, exists := previousValidators[validatorIndex]; !exists {
return newValidators, true
// UpdateScoreParams updates the scoring parameters once per epoch through the call of n.topicsCtrl.UpdateScoreParams
func (n *p2pNetwork) UpdateScoreParams(logger *zap.Logger) {
// TODO: this is a temporary solution to update the score parameters periodically.
// But, we should use an appropriate trigger for the UpdateScoreParams function that should be
// called once a validator is added or removed from the network

logger = logger.Named(logging.NameP2PNetwork)

// Create ticker
oneEpochDuration := n.cfg.Network.Beacon.SlotDurationSec() * time.Duration(n.cfg.Network.Beacon.SlotsPerEpoch())
ticker := time.NewTicker(oneEpochDuration)
defer ticker.Stop()

// Run immediately and then once every epoch
for ; true; <-ticker.C {
err := n.topicsCtrl.UpdateScoreParams(logger)
if err != nil {
logger.Debug("score parameters update failed", zap.Error(err))
} else {
logger.Debug("updated score parameters successfully")
}
}
return newValidators, false
}

// getMaxPeers returns max peers of the given topic.
Expand Down
1 change: 1 addition & 0 deletions operator/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (n *operatorNode) Start(logger *zap.Logger) error {
}
}
go n.net.UpdateSubnets(logger)
go n.net.UpdateScoreParams(logger)
n.validatorsCtrl.StartValidators()
go n.reportOperators(logger)

Expand Down

0 comments on commit b665ab3

Please sign in to comment.