Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rotation): fixed race condition for last block #1244

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 1 addition & 5 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ func (m *Manager) Start(ctx context.Context) error {

// Start the settlement sync loop in the background
uerrors.ErrGroupGoLog(eg, m.logger, func() error {
err := m.SettlementSyncLoop(ctx)
if err != nil {
m.freezeNode(err)
}
return nil
return m.SettlementSyncLoop(ctx)
})

// Monitor sequencer set updates
Expand Down
15 changes: 13 additions & 2 deletions block/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package block

import (
"context"
"errors"
"fmt"

"github.com/dymensionxyz/dymint/p2p"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (m *Manager) runAsProposer(ctx context.Context, eg *errgroup.Group) error {
return fmt.Errorf("checking should rotate: %w", err)
}
if shouldRotate {
m.rotate(ctx)
m.rotate(ctx) // panics afterwards
}

// populate the bytes produced channel
Expand All @@ -85,7 +86,17 @@ func (m *Manager) runAsProposer(ctx context.Context, eg *errgroup.Group) error {
})

// Monitor and handling of the rotation
go m.MonitorProposerRotation(ctx)
uerrors.ErrGroupGoLog(eg, m.logger, func() error {
return m.MonitorProposerRotation(ctx)
})

go func() {
err = eg.Wait()
// Check if loops exited due to sequencer rotation signal
if errors.Is(err, errRotationRequested) {
m.rotate(ctx)
}
}()

return nil
}
Expand Down
19 changes: 13 additions & 6 deletions block/sequencers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ const (
ProposerMonitorInterval = 3 * time.Minute
)

func (m *Manager) MonitorProposerRotation(ctx context.Context) {
var errRotationRequested = fmt.Errorf("sequencer rotation started. signal to stop production")

func (m *Manager) MonitorProposerRotation(ctx context.Context) error {
ticker := time.NewTicker(ProposerMonitorInterval) // TODO: make this configurable
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
return nil
case <-ticker.C:
next, err := m.SLClient.GetNextProposer()
nextProposer, err := m.SLClient.GetNextProposer()
if err != nil {
m.logger.Error("Check rotation in progress", "err", err)
continue
}
if next != nil {
m.rotate(ctx)
// no rotation in progress
if nextProposer == nil {
continue
}

// we get here once a sequencer rotation signal is received
m.logger.Info("Sequencer rotation started.", "nextSeqAddr", nextProposer.SettlementAddress)
return errRotationRequested
}
}
}
Expand Down Expand Up @@ -103,7 +110,7 @@ func (m *Manager) ShouldRotate() (bool, error) {
func (m *Manager) rotate(ctx context.Context) {
// Get Next Proposer from SL. We assume such exists (even if empty proposer) otherwise function wouldn't be called.
nextProposer, err := m.SLClient.GetNextProposer()
if err != nil {
if err != nil || nextProposer == nil {
panic(fmt.Sprintf("rotate: fetch next proposer set from Hub: %v", err))
}

Expand Down
4 changes: 2 additions & 2 deletions block/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ func (m *Manager) SettlementSyncLoop(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
return nil
case <-m.settlementSyncingC:
m.logger.Info("syncing to target height", "targetHeight", m.LastSettlementHeight.Load())

for currH := m.State.NextHeight(); currH <= m.LastSettlementHeight.Load(); currH = m.State.NextHeight() {
// if context has been cancelled, stop syncing
if ctx.Err() != nil {
return ctx.Err()
return nil
}
// if we have the block locally, we don't need to fetch it from the DA.
// it will only happen in case of rollback.
Expand Down
Loading