From 6b39ce42c5844e58da81dcdad7cf282c40723453 Mon Sep 17 00:00:00 2001 From: Matthias Fasching <5011972+fasmat@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:11:15 +0000 Subject: [PATCH] chore: Fix errors.As usage for PoetSvcUnstableError (#6217) ## Motivation `errors.Is` is generally used to compare returned errors to a concrete instance of an error like `fs.ErrNotExists` or `io.EOF`, while `errors.As` is used to assign generic errors to an instance of a specific type if needed. --- activation/activation.go | 8 ++++++-- activation/activation_errors.go | 7 ------- activation/activation_test.go | 2 +- activation/nipost_test.go | 22 ++++++++++++++++------ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/activation/activation.go b/activation/activation.go index 1903d14a97..567332e36e 100644 --- a/activation/activation.go +++ b/activation/activation.go @@ -443,6 +443,7 @@ func (b *Builder) run(ctx context.Context, sig *signing.EdSigner) { b.logger.Warn("failed to publish atx", zap.Error(err)) + poetErr := &PoetSvcUnstableError{} switch { case errors.Is(err, ErrATXChallengeExpired): b.logger.Debug("retrying with new challenge after waiting for a layer") @@ -459,8 +460,11 @@ func (b *Builder) run(ctx context.Context, sig *signing.EdSigner) { return case <-b.layerClock.AwaitLayer(currentLayer.Add(1)): } - case errors.Is(err, ErrPoetServiceUnstable): - b.logger.Warn("retrying after poet retry interval", zap.Duration("interval", b.poetRetryInterval)) + case errors.As(err, &poetErr): + b.logger.Warn("retrying after poet retry interval", + zap.Duration("interval", b.poetRetryInterval), + zap.Error(poetErr.source), + ) select { case <-ctx.Done(): return diff --git a/activation/activation_errors.go b/activation/activation_errors.go index 601541e83c..63027b0c81 100644 --- a/activation/activation_errors.go +++ b/activation/activation_errors.go @@ -8,8 +8,6 @@ import ( var ( // ErrATXChallengeExpired is returned when atx missed its publication window and needs to be regenerated. ErrATXChallengeExpired = errors.New("builder: atx expired") - // ErrPoetServiceUnstable is returned when poet quality of service is low. - ErrPoetServiceUnstable = &PoetSvcUnstableError{} // ErrPoetProofNotReceived is returned when no poet proof was received. ErrPoetProofNotReceived = errors.New("builder: didn't receive any poet proof") ) @@ -28,8 +26,3 @@ func (e *PoetSvcUnstableError) Error() string { } func (e *PoetSvcUnstableError) Unwrap() error { return e.source } - -func (e *PoetSvcUnstableError) Is(target error) bool { - _, ok := target.(*PoetSvcUnstableError) - return ok -} diff --git a/activation/activation_test.go b/activation/activation_test.go index a3c696cb24..ba9acfa0b7 100644 --- a/activation/activation_test.go +++ b/activation/activation_test.go @@ -1076,7 +1076,7 @@ func TestBuilder_RetryPublishActivationTx(t *testing.T) { tries++ t.Logf("try %d: %s", tries, now) if tries < expectedTries { - return nil, ErrPoetServiceUnstable + return nil, &PoetSvcUnstableError{} } close(builderConfirmation) return newNIPostWithPoet(t, []byte("66666")), nil diff --git a/activation/nipost_test.go b/activation/nipost_test.go index a351345d9f..deb0fe588a 100644 --- a/activation/nipost_test.go +++ b/activation/nipost_test.go @@ -713,9 +713,14 @@ func TestNIPSTBuilder_PoetUnstable(t *testing.T) { ) require.NoError(t, err) - nipst, err := nb.BuildNIPost(context.Background(), sig, challenge, - &types.NIPostChallenge{PublishEpoch: postGenesisEpoch + 2}) - require.ErrorIs(t, err, ErrPoetServiceUnstable) + nipst, err := nb.BuildNIPost( + context.Background(), + sig, + challenge, + &types.NIPostChallenge{PublishEpoch: postGenesisEpoch + 2}, + ) + poetErr := &PoetSvcUnstableError{} + require.ErrorAs(t, err, &poetErr) require.Nil(t, nipst) }) t.Run("Submit hangs", func(t *testing.T) { @@ -750,9 +755,14 @@ func TestNIPSTBuilder_PoetUnstable(t *testing.T) { ) require.NoError(t, err) - nipst, err := nb.BuildNIPost(context.Background(), sig, challenge, - &types.NIPostChallenge{PublishEpoch: postGenesisEpoch + 2}) - require.ErrorIs(t, err, ErrPoetServiceUnstable) + nipst, err := nb.BuildNIPost( + context.Background(), + sig, + challenge, + &types.NIPostChallenge{PublishEpoch: postGenesisEpoch + 2}, + ) + poetErr := &PoetSvcUnstableError{} + require.ErrorAs(t, err, &poetErr) require.Nil(t, nipst) }) t.Run("GetProof fails", func(t *testing.T) {