Skip to content

Commit

Permalink
chore: Fix errors.As usage for PoetSvcUnstableError (#6217)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
fasmat committed Aug 6, 2024
1 parent dc5fcaf commit 6b39ce4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
8 changes: 6 additions & 2 deletions activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
7 changes: 0 additions & 7 deletions activation/activation_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions activation/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b39ce4

Please sign in to comment.