Skip to content

Commit

Permalink
simplify code further, clarify comments
Browse files Browse the repository at this point in the history
  • Loading branch information
iurii-ssv committed Nov 21, 2024
1 parent 5b17837 commit 6c379d0
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 113 deletions.
7 changes: 3 additions & 4 deletions beacon/goclient/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import (
"github.com/attestantio/go-eth2-client/spec/phase0"
ssz "github.com/ferranbt/fastssz"
spectypes "github.com/ssvlabs/ssv-spec/types"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/logging/fields"
"github.com/ssvlabs/ssv/operator/slotticker"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -270,8 +269,8 @@ func (gc *GoClient) registrationSubmitter(slotTickerProvider slotticker.Provider
select {
case <-gc.ctx.Done():
return
case <-ticker.Next():
gc.submitRegistrationsFromCache(ticker.Slot(), operatorID)
case <-ticker.NextWait():
gc.submitRegistrationsFromCache(ticker.NextSlot(), operatorID)
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions operator/duties/attester.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (

eth2apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"go.uber.org/zap"

genesisspectypes "github.com/ssvlabs/ssv-spec-pre-cc/types"
spectypes "github.com/ssvlabs/ssv-spec/types"
"github.com/ssvlabs/ssv/logging/fields"
"github.com/ssvlabs/ssv/operator/duties/dutystore"
"go.uber.org/zap"
)

type AttesterHandler struct {
Expand Down Expand Up @@ -66,15 +65,15 @@ func (h *AttesterHandler) HandleDuties(ctx context.Context) {

h.fetchNextEpoch = true

next := h.ticker.Next()
next := h.ticker.NextWait()
for {
select {
case <-ctx.Done():
return

case <-next:
slot := h.ticker.Slot()
next = h.ticker.Next()
slot := h.ticker.NextSlot()
next = h.ticker.NextWait()
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(slot)
buildStr := fmt.Sprintf("e%v-s%v-#%v", currentEpoch, slot, slot%32+1)
h.logger.Debug("🛠 ticker event", zap.String("epoch_slot_pos", buildStr))
Expand Down
6 changes: 3 additions & 3 deletions operator/duties/committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func (h *CommitteeHandler) HandleDuties(ctx context.Context) {
h.logger.Info("starting duty handler")
defer h.logger.Info("duty handler exited")

next := h.ticker.Next()
next := h.ticker.NextWait()
for {
select {
case <-ctx.Done():
return

case <-next:
slot := h.ticker.Slot()
next = h.ticker.Next()
slot := h.ticker.NextSlot()
next = h.ticker.NextWait()
epoch := h.network.Beacon.EstimatedEpochAtSlot(slot)
period := h.network.Beacon.EstimatedSyncCommitteePeriodAtEpoch(epoch)
buildStr := fmt.Sprintf("p%v-e%v-s%v-#%v", period, epoch, slot, slot%32+1)
Expand Down
9 changes: 4 additions & 5 deletions operator/duties/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (

eth2apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"go.uber.org/zap"

genesisspectypes "github.com/ssvlabs/ssv-spec-pre-cc/types"
spectypes "github.com/ssvlabs/ssv-spec/types"
"github.com/ssvlabs/ssv/logging/fields"
"github.com/ssvlabs/ssv/operator/duties/dutystore"
"go.uber.org/zap"
)

type ProposerHandler struct {
Expand Down Expand Up @@ -55,15 +54,15 @@ func (h *ProposerHandler) HandleDuties(ctx context.Context) {
h.logger.Info("starting duty handler")
defer h.logger.Info("duty handler exited")

next := h.ticker.Next()
next := h.ticker.NextWait()
for {
select {
case <-ctx.Done():
return

case <-next:
slot := h.ticker.Slot()
next = h.ticker.Next()
slot := h.ticker.NextSlot()
next = h.ticker.NextWait()
currentEpoch := h.network.Beacon.EstimatedEpochAtSlot(slot)
buildStr := fmt.Sprintf("e%v-s%v-#%v", currentEpoch, slot, slot%32+1)
h.logger.Debug("🛠 ticker event", zap.String("epoch_slot_pos", buildStr))
Expand Down
4 changes: 2 additions & 2 deletions operator/duties/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func (s *Scheduler) SlotTicker(ctx context.Context) {
select {
case <-ctx.Done():
return
case <-s.ticker.Next():
slot := s.ticker.Slot()
case <-s.ticker.NextWait():
slot := s.ticker.NextSlot()

delay := s.network.SlotDurationSec() / casts.DurationFromUint64(goclient.IntervalsPerSlot) /* a third of the slot duration */
finalTime := s.network.Beacon.GetSlotStartTime(slot).Add(delay)
Expand Down
15 changes: 7 additions & 8 deletions operator/duties/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ import (
"github.com/sourcegraph/conc/pool"
genesisspectypes "github.com/ssvlabs/ssv-spec-pre-cc/types"
spectypes "github.com/ssvlabs/ssv-spec/types"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/logging"
"github.com/ssvlabs/ssv/networkconfig"
"github.com/ssvlabs/ssv/operator/slotticker"
mockslotticker "github.com/ssvlabs/ssv/operator/slotticker/mocks"
mocknetwork "github.com/ssvlabs/ssv/protocol/v2/blockchain/beacon/mocks"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap"
)

type MockSlotTicker interface {
Next() <-chan time.Time
Slot() phase0.Slot
NextWait() <-chan time.Time
NextSlot() phase0.Slot
Subscribe() chan phase0.Slot
}

Expand Down Expand Up @@ -55,11 +54,11 @@ func (m *mockSlotTicker) start() {
}()
}

func (m *mockSlotTicker) Next() <-chan time.Time {
func (m *mockSlotTicker) NextWait() <-chan time.Time {
return m.timeChan
}

func (m *mockSlotTicker) Slot() phase0.Slot {
func (m *mockSlotTicker) NextSlot() phase0.Slot {
m.mu.Lock()
defer m.mu.Unlock()
return m.slot
Expand Down
9 changes: 4 additions & 5 deletions operator/duties/sync_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (

eth2apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"go.uber.org/zap"

genesisspectypes "github.com/ssvlabs/ssv-spec-pre-cc/types"
spectypes "github.com/ssvlabs/ssv-spec/types"
"github.com/ssvlabs/ssv/logging/fields"
"github.com/ssvlabs/ssv/operator/duties/dutystore"
"go.uber.org/zap"
)

type SyncCommitteeHandler struct {
Expand Down Expand Up @@ -72,15 +71,15 @@ func (h *SyncCommitteeHandler) HandleDuties(ctx context.Context) {
h.fetchNextPeriod = true
}

next := h.ticker.Next()
next := h.ticker.NextWait()
for {
select {
case <-ctx.Done():
return

case <-next:
slot := h.ticker.Slot()
next = h.ticker.Next()
slot := h.ticker.NextSlot()
next = h.ticker.NextWait()
epoch := h.network.Beacon.EstimatedEpochAtSlot(slot)
period := h.network.Beacon.EstimatedSyncCommitteePeriodAtEpoch(epoch)
buildStr := fmt.Sprintf("p%v-e%v-s%v-#%v", period, epoch, slot, slot%32+1)
Expand Down
6 changes: 3 additions & 3 deletions operator/duties/validatorregistration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func (h *ValidatorRegistrationHandler) HandleDuties(ctx context.Context) {
// should be registered within validatorRegistrationEpochInterval epochs time in a corresponding slot
registrationSlotInterval := h.network.SlotsPerEpoch() * validatorRegistrationEpochInterval

next := h.ticker.Next()
next := h.ticker.NextWait()
for {
select {
case <-ctx.Done():
return

case <-next:
slot := h.ticker.Slot()
next = h.ticker.Next()
slot := h.ticker.NextSlot()
next = h.ticker.NextWait()
epoch := h.network.Beacon.EstimatedEpochAtSlot(slot)
shares := h.validatorProvider.SelfParticipatingValidators(epoch + phase0.Epoch(validatorRegistrationEpochInterval))

Expand Down
6 changes: 3 additions & 3 deletions operator/duties/voluntary_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func (h *VoluntaryExitHandler) HandleDuties(ctx context.Context) {
h.logger.Info("starting duty handler")
defer h.logger.Info("duty handler exited")

next := h.ticker.Next()
next := h.ticker.NextWait()
for {
select {
case <-ctx.Done():
return

case <-next:
currentSlot := h.ticker.Slot()
next = h.ticker.Next()
currentSlot := h.ticker.NextSlot()
next = h.ticker.NextWait()

h.logger.Debug("🛠 ticker event", fields.Slot(currentSlot))
h.processExecution(ctx, currentSlot)
Expand Down
7 changes: 3 additions & 4 deletions operator/fee_recipient/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/ssvlabs/ssv/networkconfig"
operatordatastore "github.com/ssvlabs/ssv/operator/datastore"
"github.com/ssvlabs/ssv/operator/slotticker"
beaconprotocol "github.com/ssvlabs/ssv/protocol/v2/blockchain/beacon"
"github.com/ssvlabs/ssv/protocol/v2/types"
"github.com/ssvlabs/ssv/registry/storage"
"go.uber.org/zap"
)

//go:generate mockgen -package=mocks -destination=./mocks/controller.go -source=./controller.go
Expand Down Expand Up @@ -71,8 +70,8 @@ func (rc *recipientController) listenToTicker(logger *zap.Logger) {
firstTimeSubmitted := false
ticker := rc.slotTickerProvider()
for {
<-ticker.Next()
slot := ticker.Slot()
<-ticker.NextWait()
slot := ticker.NextSlot()
// submit if first time or if first slot in epoch
if firstTimeSubmitted && uint64(slot)%rc.network.SlotsPerEpoch() != (rc.network.SlotsPerEpoch()/2) {
continue
Expand Down
8 changes: 4 additions & 4 deletions operator/slotticker/mocks/slotticker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6c379d0

Please sign in to comment.