Skip to content

Commit

Permalink
make AddShareToCommittee atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel committed Jan 8, 2025
1 parent c3de306 commit 04b6a9c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions operator/validator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func (c *controller) UpdateValidatorsMetadata(data map[spectypes.ValidatorPK]*be
c.logger.Warn("could not start validator", zap.Error(err))
}

_, found = c.validatorsMap.AddShareToCommittee(share)
_, found = c.validatorsMap.AddShareToCommittee(share, nil)

if !found {
c.logger.Warn("committee not found", fields.PubKey(share.ValidatorPubKey[:]))
Expand Down Expand Up @@ -828,9 +828,7 @@ func (c *controller) onShareInit(share *ssvtypes.SSVShare) (*validator.Validator
}

// Start a committee validator.
vc, found := c.validatorsMap.AddShareToCommittee(share)

if !found {
vc, found := c.validatorsMap.AddShareToCommittee(share, func() {
// Share context with both the validator and the runners,
// so that when the validator is stopped, the runners are stopped as well.
ctx, cancel := context.WithCancel(c.ctx)
Expand All @@ -847,7 +845,7 @@ func (c *controller) onShareInit(share *ssvtypes.SSVShare) (*validator.Validator

committeeRunnerFunc := SetupCommitteeRunners(ctx, opts)

vc = validator.NewCommittee(
comittee := validator.NewCommittee(
ctx,
cancel,
logger,
Expand All @@ -857,9 +855,11 @@ func (c *controller) onShareInit(share *ssvtypes.SSVShare) (*validator.Validator
nil,
c.dutyGuard,
)
vc.AddShare(&share.Share)
c.validatorsMap.PutCommittee(operator.CommitteeID, vc)
comittee.AddShare(&share.Share)
c.validatorsMap.PutCommittee(operator.CommitteeID, comittee)
})

if !found {
c.printShare(share, "setup committee done")
} else {
c.printShare(share, "added share to committee")
Expand Down
4 changes: 2 additions & 2 deletions operator/validators/validators_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ func (vm *ValidatorsMap) RemoveShareFromCommittee(share *types.SSVShare) (*valid
}

// AddShareToCommittee adds share to its committee
func (vm *ValidatorsMap) AddShareToCommittee(share *types.SSVShare) (*validator.Committee, bool) {
func (vm *ValidatorsMap) AddShareToCommittee(share *types.SSVShare, onMissingCommittee func()) (*validator.Committee, bool) {
vm.mlock.Lock()
defer vm.mlock.Unlock()

vc, found := vm.committees[share.CommitteeID()]
if !found || vc.Stopped() {
onMissingCommittee()
return nil, false
}

vc.AddShare(&share.Share)

return vc, true
}

Expand Down
4 changes: 2 additions & 2 deletions operator/validators/validators_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAddShareToCommittee(t *testing.T) {
},
}

vc, added := vm.AddShareToCommittee(share)
vc, added := vm.AddShareToCommittee(share, nil)

assert.Nil(t, vc)
assert.False(t, added)
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestAddShareToCommittee(t *testing.T) {
vm.PutCommittee(cmtID, cmt)
assert.Equal(t, 1, vm.SizeCommittees())

vc, added := vm.AddShareToCommittee(share)
vc, added := vm.AddShareToCommittee(share, nil)

assert.Equal(t, cmt, vc)
assert.True(t, added)
Expand Down

0 comments on commit 04b6a9c

Please sign in to comment.