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

[Merged by Bors] - Execute tortoise beacon round only if node is synced at the start of the epoch #2715

Closed
3 changes: 2 additions & 1 deletion cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ func (app *App) initServices(ctx context.Context,
AlwaysListen: app.Config.AlwaysListen,
}
syncer := syncer.NewSyncer(ctx, syncerConf, clock, msh, layerFetch, app.addLogger(SyncLogger, lg))

// TODO(dshulyak) this needs to be improved, but dependency graph is a bit complicated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try RegisterChForSynced in syncer.go?

tBeacon.SetSyncState(syncer)
nkryuchkov marked this conversation as resolved.
Show resolved Hide resolved
blockOracle := blocks.NewMinerBlockOracle(layerSize, layersPerEpoch, atxDB, tBeacon, vrfSigner, nodeID, syncer.ListenToGossip, app.addLogger(BlockOracle, lg))

// TODO: we should probably decouple the apptest and the node (and duplicate as necessary) (#1926)
Expand Down
23 changes: 22 additions & 1 deletion tortoisebeacon/tortoise_beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ type layerClock interface {
LayerToTime(id types.LayerID) time.Time
}

// SyncState interface to check the state the sync.
type SyncState interface {
IsSynced(context.Context) bool
}

// New returns a new TortoiseBeacon.
func New(
conf Config,
Expand Down Expand Up @@ -120,6 +125,7 @@ type TortoiseBeacon struct {
layerDuration time.Duration
nodeID types.NodeID

sync SyncState
net broadcaster
atxDB activationDB
tortoiseBeaconDB tortoiseBeaconDB
Expand Down Expand Up @@ -156,13 +162,24 @@ type TortoiseBeacon struct {
proposalChans map[types.EpochID]chan *proposalMessageWithReceiptData
}

// SetSyncState updates sync state provider. Must be executed only once.
func (tb *TortoiseBeacon) SetSyncState(sync SyncState) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename SetSyncStateProvider? or it sounds like you are changing the state

if tb.sync != nil {
tb.Log.Panic("sync state provider can be updated only once")
}
tb.sync = sync
}

// Start starts listening for layers and outputs.
func (tb *TortoiseBeacon) Start(ctx context.Context) error {
if !atomic.CompareAndSwapUint64(&tb.closed, 0, 1) {
tb.Log.Warning("attempt to start tortoise beacon more than once")
return nil
}
tb.Log.Info("Starting %v with the following config: %+v", protoName, tb.config)
tb.Log.Info("starting %v with the following config: %+v", protoName, tb.config)
if tb.sync == nil {
tb.Log.Panic("update sync state provider can't be nil")
}

ctx, cancel := context.WithCancel(ctx)
tb.tg = taskgroup.New(taskgroup.WithContext(ctx))
Expand Down Expand Up @@ -359,6 +376,10 @@ func (tb *TortoiseBeacon) handleEpoch(ctx context.Context, epoch types.EpochID)

return
}
if !tb.sync.IsSynced(ctx) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you can register a channel to be notified when the node turned synced. see RegisterChForSynced.
tho this does not notify you again if the node goes out of sync, i think it serves your purpose here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would be better. but if node goes out of sync again i will need to re-register. and for that, i still need a pointer

tb.Log.With().Info("tortoise beacon protocol is skipped while node is not synced", epoch)
return
}

tb.Log.With().Info("Handling epoch",
log.Uint32("epoch_id", uint32(epoch)))
Expand Down
7 changes: 7 additions & 0 deletions tortoisebeacon/tortoise_beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func (*validatorMock) ValidatePost([]byte, *types.Post, *types.PostMetadata, uin
return nil
}

type testSyncState bool

func (ss testSyncState) IsSynced(context.Context) bool {
return bool(ss)
}

func TestTortoiseBeacon(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -75,6 +81,7 @@ func TestTortoiseBeacon(t *testing.T) {

tb := New(conf, ld, minerID, n1, mockDB, nil, edSgn, signing.NewEDVerifier(), vrfSigner, signing.VRFVerifier{}, mwc, clock, logger)
requirer.NotNil(tb)
tb.SetSyncState(testSyncState(true))

err = tb.Start(context.TODO())
requirer.NoError(err)
Expand Down