Skip to content

Commit 392b313

Browse files
Move engine startup into helper function (#2329)
1 parent fe72c5b commit 392b313

File tree

2 files changed

+83
-81
lines changed

2 files changed

+83
-81
lines changed

snow/engine/snowman/bootstrap/bootstrapper.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,42 @@ func (b *bootstrapper) Start(ctx context.Context, startReqID uint32) error {
149149
b.startingHeight = lastAccepted.Height()
150150
b.Config.SharedCfg.RequestID = startReqID
151151

152-
if !b.StartupTracker.ShouldStart() {
152+
return b.tryStartBootstrapping(ctx)
153+
}
154+
155+
func (b *bootstrapper) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
156+
if err := b.VM.Connected(ctx, nodeID, nodeVersion); err != nil {
157+
return err
158+
}
159+
160+
if err := b.StartupTracker.Connected(ctx, nodeID, nodeVersion); err != nil {
161+
return err
162+
}
163+
// Ensure fetchFrom reflects proper validator list
164+
if _, ok := b.Beacons.GetValidator(b.Ctx.SubnetID, nodeID); ok {
165+
b.fetchFrom.Add(nodeID)
166+
}
167+
168+
return b.tryStartBootstrapping(ctx)
169+
}
170+
171+
func (b *bootstrapper) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
172+
if err := b.VM.Disconnected(ctx, nodeID); err != nil {
173+
return err
174+
}
175+
176+
if err := b.StartupTracker.Disconnected(ctx, nodeID); err != nil {
177+
return err
178+
}
179+
180+
b.markUnavailable(nodeID)
181+
return nil
182+
}
183+
184+
// tryStartBootstrapping will start bootstrapping the first time it is called
185+
// while the startupTracker is reporting that the protocol should start.
186+
func (b *bootstrapper) tryStartBootstrapping(ctx context.Context) error {
187+
if b.started || !b.StartupTracker.ShouldStart() {
153188
return nil
154189
}
155190

@@ -246,40 +281,6 @@ func (b *bootstrapper) GetAncestorsFailed(ctx context.Context, nodeID ids.NodeID
246281
return b.fetch(ctx, blkID)
247282
}
248283

249-
func (b *bootstrapper) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
250-
if err := b.VM.Connected(ctx, nodeID, nodeVersion); err != nil {
251-
return err
252-
}
253-
254-
if err := b.StartupTracker.Connected(ctx, nodeID, nodeVersion); err != nil {
255-
return err
256-
}
257-
// Ensure fetchFrom reflects proper validator list
258-
if _, ok := b.Beacons.GetValidator(b.Ctx.SubnetID, nodeID); ok {
259-
b.fetchFrom.Add(nodeID)
260-
}
261-
262-
if b.started || !b.StartupTracker.ShouldStart() {
263-
return nil
264-
}
265-
266-
b.started = true
267-
return b.Startup(ctx)
268-
}
269-
270-
func (b *bootstrapper) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
271-
if err := b.VM.Disconnected(ctx, nodeID); err != nil {
272-
return err
273-
}
274-
275-
if err := b.StartupTracker.Disconnected(ctx, nodeID); err != nil {
276-
return err
277-
}
278-
279-
b.markUnavailable(nodeID)
280-
return nil
281-
}
282-
283284
func (b *bootstrapper) Timeout(ctx context.Context) error {
284285
if !b.awaitingTimeout {
285286
return errUnexpectedTimeout

snow/engine/snowman/syncer/state_syncer.go

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,53 @@ func (ss *stateSyncer) Context() *snow.ConsensusContext {
112112
return ss.Ctx
113113
}
114114

115+
func (ss *stateSyncer) Start(ctx context.Context, startReqID uint32) error {
116+
ss.Ctx.Log.Info("starting state sync")
117+
118+
ss.Ctx.State.Set(snow.EngineState{
119+
Type: p2p.EngineType_ENGINE_TYPE_SNOWMAN,
120+
State: snow.StateSyncing,
121+
})
122+
if err := ss.VM.SetState(ctx, snow.StateSyncing); err != nil {
123+
return fmt.Errorf("failed to notify VM that state syncing has started: %w", err)
124+
}
125+
126+
ss.requestID = startReqID
127+
128+
return ss.tryStartSyncing(ctx)
129+
}
130+
131+
func (ss *stateSyncer) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
132+
if err := ss.VM.Connected(ctx, nodeID, nodeVersion); err != nil {
133+
return err
134+
}
135+
136+
if err := ss.StartupTracker.Connected(ctx, nodeID, nodeVersion); err != nil {
137+
return err
138+
}
139+
140+
return ss.tryStartSyncing(ctx)
141+
}
142+
143+
func (ss *stateSyncer) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
144+
if err := ss.VM.Disconnected(ctx, nodeID); err != nil {
145+
return err
146+
}
147+
148+
return ss.StartupTracker.Disconnected(ctx, nodeID)
149+
}
150+
151+
// tryStartSyncing will start syncing the first time it is called while the
152+
// startupTracker is reporting that the protocol should start.
153+
func (ss *stateSyncer) tryStartSyncing(ctx context.Context) error {
154+
if ss.started || !ss.StartupTracker.ShouldStart() {
155+
return nil
156+
}
157+
158+
ss.started = true
159+
return ss.startup(ctx)
160+
}
161+
115162
func (ss *stateSyncer) StateSummaryFrontier(ctx context.Context, nodeID ids.NodeID, requestID uint32, summaryBytes []byte) error {
116163
// ignores any late responses
117164
if requestID != ss.requestID {
@@ -426,27 +473,6 @@ func (ss *stateSyncer) GetAcceptedStateSummaryFailed(ctx context.Context, nodeID
426473
return ss.AcceptedStateSummary(ctx, nodeID, requestID, nil)
427474
}
428475

429-
func (ss *stateSyncer) Start(ctx context.Context, startReqID uint32) error {
430-
ss.Ctx.Log.Info("starting state sync")
431-
432-
ss.Ctx.State.Set(snow.EngineState{
433-
Type: p2p.EngineType_ENGINE_TYPE_SNOWMAN,
434-
State: snow.StateSyncing,
435-
})
436-
if err := ss.VM.SetState(ctx, snow.StateSyncing); err != nil {
437-
return fmt.Errorf("failed to notify VM that state syncing has started: %w", err)
438-
}
439-
440-
ss.requestID = startReqID
441-
442-
if !ss.StartupTracker.ShouldStart() {
443-
return nil
444-
}
445-
446-
ss.started = true
447-
return ss.startup(ctx)
448-
}
449-
450476
// startup do start the whole state sync process by
451477
// sampling frontier seeders, listing state syncers to request votes to
452478
// and reaching out frontier seeders if any. Otherwise, it moves immediately
@@ -580,31 +606,6 @@ func (ss *stateSyncer) Notify(ctx context.Context, msg common.Message) error {
580606
return ss.onDoneStateSyncing(ctx, ss.requestID)
581607
}
582608

583-
func (ss *stateSyncer) Connected(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error {
584-
if err := ss.VM.Connected(ctx, nodeID, nodeVersion); err != nil {
585-
return err
586-
}
587-
588-
if err := ss.StartupTracker.Connected(ctx, nodeID, nodeVersion); err != nil {
589-
return err
590-
}
591-
592-
if ss.started || !ss.StartupTracker.ShouldStart() {
593-
return nil
594-
}
595-
596-
ss.started = true
597-
return ss.startup(ctx)
598-
}
599-
600-
func (ss *stateSyncer) Disconnected(ctx context.Context, nodeID ids.NodeID) error {
601-
if err := ss.VM.Disconnected(ctx, nodeID); err != nil {
602-
return err
603-
}
604-
605-
return ss.StartupTracker.Disconnected(ctx, nodeID)
606-
}
607-
608609
func (*stateSyncer) Gossip(context.Context) error {
609610
return nil
610611
}

0 commit comments

Comments
 (0)