Skip to content

Commit

Permalink
remove pkgerrors dependency from common pkg (#11479)
Browse files Browse the repository at this point in the history
* remove pkgerrors dependency from common pkg

* fix naming inconsistencies

* clean up error return

* clean up error return and remove use of Unwrap
  • Loading branch information
poopoothegorilla authored Dec 5, 2023
1 parent 7d92740 commit 1032ba3
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 99 deletions.
7 changes: 3 additions & 4 deletions common/client/multi_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand Down Expand Up @@ -166,12 +165,12 @@ func NewMultiNode[
func (c *multiNode[CHAIN_ID, SEQ, ADDR, BLOCK_HASH, TX, TX_HASH, EVENT, EVENT_OPS, TX_RECEIPT, FEE, HEAD, RPC_CLIENT]) Dial(ctx context.Context) error {
return c.StartOnce("MultiNode", func() (merr error) {
if len(c.nodes) == 0 {
return errors.Errorf("no available nodes for chain %s", c.chainID.String())
return fmt.Errorf("no available nodes for chain %s", c.chainID.String())
}
var ms services.MultiStart
for _, n := range c.nodes {
if n.ConfiguredChainID().String() != c.chainID.String() {
return ms.CloseBecause(errors.Errorf("node %s has configured chain ID %s which does not match multinode configured chain ID of %s", n.String(), n.ConfiguredChainID().String(), c.chainID.String()))
return ms.CloseBecause(fmt.Errorf("node %s has configured chain ID %s which does not match multinode configured chain ID of %s", n.String(), n.ConfiguredChainID().String(), c.chainID.String()))
}
rawNode, ok := n.(*node[CHAIN_ID, HEAD, RPC_CLIENT])
if ok {
Expand All @@ -188,7 +187,7 @@ func (c *multiNode[CHAIN_ID, SEQ, ADDR, BLOCK_HASH, TX, TX_HASH, EVENT, EVENT_OP
}
for _, s := range c.sendonlys {
if s.ConfiguredChainID().String() != c.chainID.String() {
return ms.CloseBecause(errors.Errorf("sendonly node %s has configured chain ID %s which does not match multinode configured chain ID of %s", s.String(), s.ConfiguredChainID().String(), c.chainID.String()))
return ms.CloseBecause(fmt.Errorf("sendonly node %s has configured chain ID %s which does not match multinode configured chain ID of %s", s.String(), s.ConfiguredChainID().String(), c.chainID.String()))
}
if err := ms.Start(ctx, s); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion common/client/multi_node_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package client

import (
"errors"
"fmt"
big "math/big"
"math/rand"
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down
10 changes: 5 additions & 5 deletions common/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package client

import (
"context"
"errors"
"fmt"
"math/big"
"net/url"
"sync"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand Down Expand Up @@ -256,15 +256,15 @@ func (n *node[CHAIN_ID, HEAD, RPC]) verify(callerCtx context.Context) (err error
var chainID CHAIN_ID
if chainID, err = n.rpc.ChainID(callerCtx); err != nil {
promFailed()
return errors.Wrapf(err, "failed to verify chain ID for node %s", n.name)
return fmt.Errorf("failed to verify chain ID for node %s: %w", n.name, err)
} else if chainID.String() != n.chainID.String() {
promFailed()
return errors.Wrapf(
errInvalidChainID,
"rpc ChainID doesn't match local chain ID: RPC ID=%s, local ID=%s, node name=%s",
return fmt.Errorf(
"rpc ChainID doesn't match local chain ID: RPC ID=%s, local ID=%s, node name=%s: %w",
chainID.String(),
n.chainID.String(),
n.name,
errInvalidChainID,
)
}

Expand Down
2 changes: 1 addition & 1 deletion common/client/node_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package client

import (
"context"
"errors"
"fmt"
"math"
"math/big"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand Down
2 changes: 1 addition & 1 deletion common/client/node_lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package client

import (
"errors"
"fmt"
big "math/big"
"sync/atomic"
"testing"

"github.com/cometbft/cometbft/libs/rand"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.uber.org/zap"
Expand Down
2 changes: 1 addition & 1 deletion common/client/send_only_node_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package client

import (
"errors"
"fmt"
"net/url"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down
12 changes: 6 additions & 6 deletions common/fee/models.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fee

import (
"errors"
"fmt"
"math/big"

"github.com/pkg/errors"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
bigmath "github.com/smartcontractkit/chainlink-common/pkg/utils/big_math"
"github.com/smartcontractkit/chainlink/v2/common/chains/label"
Expand Down Expand Up @@ -47,15 +47,15 @@ func CalculateBumpedFee(
bumpedFeePrice = maxFee(lggr, currentfeePrice, bumpedFeePrice, maxFeePrice, "fee price", toChainUnit)

if bumpedFeePrice.Cmp(maxFeePrice) > 0 {
return maxFeePrice, errors.Wrapf(ErrBumpFeeExceedsLimit, "bumped fee price of %s would exceed configured max fee price of %s (original price was %s). %s",
toChainUnit(bumpedFeePrice), toChainUnit(maxFeePrice), toChainUnit(originalfeePrice), label.NodeConnectivityProblemWarning)
return maxFeePrice, fmt.Errorf("bumped fee price of %s would exceed configured max fee price of %s (original price was %s). %s: %w",
toChainUnit(bumpedFeePrice), toChainUnit(maxFeePrice), toChainUnit(originalfeePrice), label.NodeConnectivityProblemWarning, ErrBumpFeeExceedsLimit)
} else if bumpedFeePrice.Cmp(originalfeePrice) == 0 {
// NOTE: This really shouldn't happen since we enforce minimums for
// FeeEstimator.BumpPercent and FeeEstimator.BumpMin in the config validation,
// but it's here anyway for a "belts and braces" approach
return bumpedFeePrice, errors.Wrapf(ErrBump, "bumped fee price of %s is equal to original fee price of %s."+
return bumpedFeePrice, fmt.Errorf("bumped fee price of %s is equal to original fee price of %s."+
" ACTION REQUIRED: This is a configuration error, you must increase either "+
"FeeEstimator.BumpPercent or FeeEstimator.BumpMin", toChainUnit(bumpedFeePrice), toChainUnit(bumpedFeePrice))
"FeeEstimator.BumpPercent or FeeEstimator.BumpMin: %w", toChainUnit(bumpedFeePrice), toChainUnit(bumpedFeePrice), ErrBump)
}
return bumpedFeePrice, nil
}
Expand Down
4 changes: 2 additions & 2 deletions common/fee/utils.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package fee

import (
"fmt"
"math"
"math/big"

"github.com/pkg/errors"
"github.com/shopspring/decimal"
)

func ApplyMultiplier(feeLimit uint32, multiplier float32) (uint32, error) {
result := decimal.NewFromBigInt(big.NewInt(0).SetUint64(uint64(feeLimit)), 0).Mul(decimal.NewFromFloat32(multiplier)).IntPart()

if result > math.MaxUint32 {
return 0, errors.Errorf("integer overflow when applying multiplier of %f to fee limit of %d", multiplier, feeLimit)
return 0, fmt.Errorf("integer overflow when applying multiplier of %f to fee limit of %d", multiplier, feeLimit)
}
return uint32(result), nil
}
Expand Down
5 changes: 3 additions & 2 deletions common/headtracker/head_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package headtracker

import (
"context"
"errors"
"fmt"
"sync/atomic"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand Down Expand Up @@ -202,7 +203,7 @@ func (hl *HeadListener[HTH, S, ID, BLOCK_HASH]) subscribeToHead(ctx context.Cont
hl.headSubscription, err = hl.client.SubscribeNewHead(ctx, hl.chHeaders)
if err != nil {
close(hl.chHeaders)
return errors.Wrap(err, "Client#SubscribeNewHead")
return fmt.Errorf("Client#SubscribeNewHead: %w", err)
}

hl.connected.Store(true)
Expand Down
12 changes: 6 additions & 6 deletions common/headtracker/head_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package headtracker

import (
"context"
"errors"
"fmt"
"sync"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

Expand Down Expand Up @@ -123,7 +123,7 @@ func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) Start(ctx context.Context) error
ht.log.Errorw("Error getting initial head", "err", err)
} else if initialHead.IsValid() {
if err := ht.handleNewHead(ctx, initialHead); err != nil {
return errors.Wrap(err, "error handling initial head")
return fmt.Errorf("error handling initial head: %w", err)
}
} else {
ht.log.Debug("Got nil initial head")
Expand Down Expand Up @@ -179,7 +179,7 @@ func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) LatestChain() HTH {
func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) getInitialHead(ctx context.Context) (HTH, error) {
head, err := ht.client.HeadByNumber(ctx, nil)
if err != nil {
return ht.getNilHead(), errors.Wrap(err, "failed to fetch initial head")
return ht.getNilHead(), fmt.Errorf("failed to fetch initial head: %w", err)
}
loggerFields := []interface{}{"head", head}
if head.IsValid() {
Expand All @@ -204,15 +204,15 @@ func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) handleNewHead(ctx context.Context
if ctx.Err() != nil {
return nil
} else if err != nil {
return errors.Wrapf(err, "failed to save head: %#v", head)
return fmt.Errorf("failed to save head: %#v: %w", head, err)
}

if !prevHead.IsValid() || head.BlockNumber() > prevHead.BlockNumber() {
promCurrentHead.WithLabelValues(ht.chainID.String()).Set(float64(head.BlockNumber()))

headWithChain := ht.headSaver.Chain(head.BlockHash())
if !headWithChain.IsValid() {
return errors.Errorf("HeadTracker#handleNewHighestHead headWithChain was unexpectedly nil")
return fmt.Errorf("HeadTracker#handleNewHighestHead headWithChain was unexpectedly nil")
}
ht.backfillMB.Deliver(headWithChain)
ht.broadcastMB.Deliver(headWithChain)
Expand Down Expand Up @@ -339,7 +339,7 @@ func (ht *HeadTracker[HTH, S, ID, BLOCK_HASH]) backfill(ctx context.Context, hea
ht.log.Debugw("context canceled, aborting backfill", "err", err, "ctx.Err", ctx.Err())
break
} else if err != nil {
return errors.Wrap(err, "fetchAndSaveHead failed")
return fmt.Errorf("fetchAndSaveHead failed: %w", err)
}
}
return
Expand Down
Loading

0 comments on commit 1032ba3

Please sign in to comment.