From 5803599768ab7ec9c135e048c4d27b83dd51ae5d Mon Sep 17 00:00:00 2001 From: Matthias Fasching <5011972+fasmat@users.noreply.github.com> Date: Wed, 5 Feb 2025 18:02:19 +0000 Subject: [PATCH 1/2] Remove fat context from activation builder (#6686) ## Motivation A context should not be stored inside a service but instead only passed to functions, who then block until they either finish work or the context is canceled. This fixes a code where we use context incorrectly. --- activation/activation.go | 21 ++++++++------------- node/node.go | 1 - 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/activation/activation.go b/activation/activation.go index 3695ad06e7..d2a07efe2c 100644 --- a/activation/activation.go +++ b/activation/activation.go @@ -93,7 +93,6 @@ type Builder struct { layerClock layerClock syncer syncer logger *zap.Logger - parentCtx context.Context poets []PoetService poetCfg PoetConfig poetRetryInterval time.Duration @@ -139,14 +138,6 @@ func WithPoetRetryInterval(interval time.Duration) BuilderOption { } } -// WithContext modifies parent context for background job. -func WithContext(ctx context.Context) BuilderOption { - return func(b *Builder) { - // TODO(mafa): fix this - b.parentCtx = ctx // nolint:fatcontext - } -} - // WithPoetConfig sets the poet config. func WithPoetConfig(c PoetConfig) BuilderOption { return func(b *Builder) { @@ -192,7 +183,6 @@ func NewBuilder( opts ...BuilderOption, ) *Builder { b := &Builder{ - parentCtx: context.Background(), signers: make(map[types.NodeID]*signing.EdSigner), conf: conf, db: db, @@ -228,7 +218,13 @@ func (b *Builder) Register(sig *signing.EdSigner) { b.postStates.Set(sig.NodeID(), types.PostStateIdle) if b.stop != nil { - b.startID(b.parentCtx, sig) + ctx, stop := context.WithCancel(context.Background()) + prevStop := b.stop + b.stop = func() { + prevStop() + stop() + } + b.startID(ctx, sig) } } @@ -268,9 +264,8 @@ func (b *Builder) StartSmeshing(coinbase types.Address) error { } b.coinbaseAccount = coinbase - ctx, stop := context.WithCancel(b.parentCtx) + ctx, stop := context.WithCancel(context.Background()) b.stop = stop - for _, sig := range b.signers { b.startID(ctx, sig) } diff --git a/node/node.go b/node/node.go index ff7ccda2e4..070b7a717d 100644 --- a/node/node.go +++ b/node/node.go @@ -1125,7 +1125,6 @@ func (app *App) initServices(ctx context.Context) error { app.clock, syncer, app.addLogger(ATXBuilderLogger, lg).Zap(), - activation.WithContext(ctx), activation.WithPoetConfig(app.Config.POET), // TODO(dshulyak) makes no sense. how we ended using it? activation.WithPoetRetryInterval(app.Config.HARE3.PreroundDelay), From 106070ffb16349d87eb400a2e3b6aedf89e8ee4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20R=C3=B3=C5=BCa=C5=84ski?= Date: Thu, 6 Feb 2025 11:19:36 +0000 Subject: [PATCH 2/2] don't query prod poets in node tests and add timeout (#6698) ## Motivation Unit tests in node_test.go query prod poets in `NewPoetServiceWithClient()` and might timeout if poet is not responding. --- activation/poet.go | 5 ++++- node/node_test.go | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/activation/poet.go b/activation/poet.go index 4d8530f5d2..9d6541eb3e 100644 --- a/activation/poet.go +++ b/activation/poet.go @@ -462,7 +462,10 @@ func NewPoetServiceWithClient( opt(service) } - err := service.verifyPhaseShiftConfiguration(context.Background()) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + err := service.verifyPhaseShiftConfiguration(ctx) switch { case errors.Is(err, errIncompatiblePhaseShift): logger.Fatal("failed to create poet service", zap.String("poet", client.Address())) diff --git a/node/node_test.go b/node/node_test.go index 782258446d..39f1e4307e 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -1253,6 +1253,8 @@ func getTestDefaultConfig(tb testing.TB) *config.Config { cfg.POST.LabelsPerUnit = 32 cfg.POST.K2 = 4 + cfg.BaseConfig.PoetServers = nil + cfg.SMESHING = config.DefaultSmeshingConfig() cfg.SMESHING.Start = false cfg.SMESHING.CoinbaseAccount = types.GenerateAddress([]byte{1}).String()