Skip to content

Commit

Permalink
[chore] Replaces multierr usage with go native errors package (#939)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
dylanlott and Olshansk authored Jul 31, 2023
1 parent fe12410 commit 4e0b03b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ require (
github.com/rs/zerolog v1.27.0
github.com/spf13/cobra v1.6.0
github.com/spf13/viper v1.13.0
go.uber.org/multierr v1.9.0
golang.org/x/term v0.5.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.26.1
Expand Down Expand Up @@ -197,6 +196,7 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions p2p/background/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ package background

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

dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
libp2pHost "github.com/libp2p/go-libp2p/core/host"
libp2pPeer "github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/multierr"
"google.golang.org/protobuf/proto"

"github.com/pokt-network/pocket/logger"
Expand Down Expand Up @@ -129,7 +129,7 @@ func (rtr *backgroundRouter) Close() error {
topicCloseErr = err
}

return multierr.Append(
return errors.Join(
topicCloseErr,
rtr.kadDHT.Close(),
)
Expand Down
18 changes: 9 additions & 9 deletions p2p/config/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config

import (
"errors"
"fmt"

"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/protocol"
"go.uber.org/multierr"

typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/shared/crypto"
Expand Down Expand Up @@ -56,39 +56,39 @@ type RainTreeConfig struct {
// IsValid implements the respective member of the `RouterConfig` interface.
func (cfg *baseConfig) IsValid() (err error) {
if cfg.Addr == nil {
err = multierr.Append(err, fmt.Errorf("pokt address not configured"))
err = errors.Join(err, fmt.Errorf("pokt address not configured"))
}

if cfg.Host == nil {
err = multierr.Append(err, fmt.Errorf("host not configured"))
err = errors.Join(err, fmt.Errorf("host not configured"))
}

if cfg.Handler == nil {
err = multierr.Append(err, fmt.Errorf("handler not configured"))
err = errors.Join(err, fmt.Errorf("handler not configured"))
}
return err
}

// IsValid implements the respective member of the `RouterConfig` interface.
func (cfg *UnicastRouterConfig) IsValid() (err error) {
if cfg.Logger == nil {
err = multierr.Append(err, fmt.Errorf("logger not configured"))
err = errors.Join(err, fmt.Errorf("logger not configured"))
}

if cfg.Host == nil {
err = multierr.Append(err, fmt.Errorf("host not configured"))
err = errors.Join(err, fmt.Errorf("host not configured"))
}

if cfg.ProtocolID == "" {
err = multierr.Append(err, fmt.Errorf("protocol id not configured"))
err = errors.Join(err, fmt.Errorf("protocol id not configured"))
}

if cfg.MessageHandler == nil {
err = multierr.Append(err, fmt.Errorf("message handler not configured"))
err = errors.Join(err, fmt.Errorf("message handler not configured"))
}

if cfg.PeerHandler == nil {
err = multierr.Append(err, fmt.Errorf("peer handler not configured"))
err = errors.Join(err, fmt.Errorf("peer handler not configured"))
}
return err
}
Expand Down
7 changes: 3 additions & 4 deletions p2p/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/libp2p/go-libp2p"
libp2pHost "github.com/libp2p/go-libp2p/core/host"
"github.com/multiformats/go-multiaddr"
"go.uber.org/multierr"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

Expand Down Expand Up @@ -182,12 +181,12 @@ func (m *p2pModule) Stop() error {
stakedActorRouterCloseErr = m.stakedActorRouter.Close()
}

routerCloseErrs := multierr.Append(
routerCloseErrs := errors.Join(
m.unstakedActorRouter.Close(),
stakedActorRouterCloseErr,
)

err := multierr.Append(
err := errors.Join(
routerCloseErrs,
m.host.Close(),
)
Expand Down Expand Up @@ -231,7 +230,7 @@ func (m *p2pModule) Broadcast(msg *anypb.Any) error {

unstakedBroadcastErr := m.unstakedActorRouter.Broadcast(poktEnvelopeBz)

return multierr.Append(stakedBroadcastErr, unstakedBroadcastErr)
return errors.Join(stakedBroadcastErr, unstakedBroadcastErr)
}

func (m *p2pModule) Send(addr cryptoPocket.Address, msg *anypb.Any) error {
Expand Down
7 changes: 4 additions & 3 deletions p2p/providers/peerstore_provider/peerstore_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package peerstore_provider
//go:generate mockgen -package=mock_types -destination=../../types/mocks/peerstore_provider_mock.go github.com/pokt-network/pocket/p2p/providers/peerstore_provider PeerstoreProvider

import (
"errors"

"github.com/pokt-network/pocket/logger"
typesP2P "github.com/pokt-network/pocket/p2p/types"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/modules"
"go.uber.org/multierr"
)

const PeerstoreProviderSubmoduleName = "peerstore_provider"
Expand All @@ -35,12 +36,12 @@ func ActorsToPeerstore(abp PeerstoreProvider, actors []*coreTypes.Actor) (pstore
logger.Global.Warn().Err(err).Msg("ignoring ErrResolvingAddr - peer unreachable, not adding it to peerstore")
continue
} else if err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
continue
}

if err = pstore.AddPeer(networkPeer); err != nil {
errs = multierr.Append(errs, err)
errs = errors.Join(errs, err)
}
}
return pstore, errs
Expand Down
6 changes: 3 additions & 3 deletions p2p/utils/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package utils

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

libp2pHost "github.com/libp2p/go-libp2p/core/host"
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
"go.uber.org/multierr"

"github.com/pokt-network/pocket/logger"
typesP2P "github.com/pokt-network/pocket/p2p/types"
Expand All @@ -26,7 +26,7 @@ const (
func PopulateLibp2pHost(host libp2pHost.Host, pstore typesP2P.Peerstore) (err error) {
for _, peer := range pstore.GetPeerList() {
if addErr := AddPeerToLibp2pHost(host, peer); addErr != nil {
err = multierr.Append(err, addErr)
err = errors.Join(err, addErr)
}
}
return err
Expand Down Expand Up @@ -101,7 +101,7 @@ func Libp2pSendToPeer(host libp2pHost.Host, protocolID libp2pProtocol.ID, data [
}

if n, err := stream.Write(data); err != nil {
return multierr.Append(
return errors.Join(
fmt.Errorf("writing to stream: %w", err),
stream.Reset(),
)
Expand Down
3 changes: 1 addition & 2 deletions persistence/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/pokt-network/pocket/persistence/indexer"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/modules"
"go.uber.org/multierr"
)

var _ modules.PersistenceRWContext = &PostgresContext{}
Expand Down Expand Up @@ -50,7 +49,7 @@ func (p *PostgresContext) RollbackToSavePoint() error {
ctx, _ := p.getCtxAndTx()
pgErr := p.tx.Rollback(ctx)
treesErr := p.stateTrees.Rollback()
return multierr.Combine(pgErr, treesErr)
return errors.Join(pgErr, treesErr)
}

// Full details in the thread from the PR review: https://github.com/pokt-network/pocket/pull/285#discussion_r1018471719
Expand Down
8 changes: 3 additions & 5 deletions shared/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package shared

import (
"context"
"errors"
"time"

"github.com/pokt-network/pocket/consensus"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/pokt-network/pocket/state_machine"
"github.com/pokt-network/pocket/telemetry"
"github.com/pokt-network/pocket/utility"
"go.uber.org/multierr"
)

const (
Expand Down Expand Up @@ -185,13 +185,11 @@ func (node *Node) handleEvent(message *messaging.PocketEnvelope) error {
case messaging.ConsensusNewHeightEventType:
err_p2p := node.GetBus().GetP2PModule().HandleEvent(message.Content)
err_ibc := node.GetBus().GetIBCModule().HandleEvent(message.Content)
// TODO: Remove this lib once we move to Go 1.2
return multierr.Combine(err_p2p, err_ibc)
return errors.Join(err_p2p, err_ibc)
case messaging.StateMachineTransitionEventType:
err_consensus := node.GetBus().GetConsensusModule().HandleEvent(message.Content)
err_p2p := node.GetBus().GetP2PModule().HandleEvent(message.Content)
// TODO: Remove this lib once we move to Go 1.2
return multierr.Combine(err_consensus, err_p2p)
return errors.Join(err_consensus, err_p2p)
default:
logger.Global.Warn().Msgf("Unsupported message content type: %s", contentType)
}
Expand Down
9 changes: 5 additions & 4 deletions utility/unit_of_work/module.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package unit_of_work

import (
"errors"

coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/modules/base_modules"
"go.uber.org/multierr"
)

const (
Expand Down Expand Up @@ -74,21 +75,21 @@ func (uow *baseUtilityUnitOfWork) ApplyBlock() error {
log.Debug().Msg("processing transactions from proposal block")
if err := uow.processProposalBlockTransactions(); err != nil {
rollErr := uow.revertToLastSavepoint()
return multierr.Combine(rollErr, err)
return errors.Join(rollErr, err)
}

// end block lifecycle phase calls endBlock and reverts to the last known savepoint if it encounters any errors
log.Debug().Msg("calling endBlock")
if err := uow.endBlock(uow.proposalProposerAddr); err != nil {
rollErr := uow.revertToLastSavepoint()
return multierr.Combine(rollErr, err)
return errors.Join(rollErr, err)
}

// return the app hash (consensus module will get the validator set directly)
stateHash, err := uow.persistenceRWContext.ComputeStateHash()
if err != nil {
rollErr := uow.persistenceRWContext.RollbackToSavePoint()
return coreTypes.ErrAppHash(multierr.Append(err, rollErr))
return coreTypes.ErrAppHash(errors.Join(err, rollErr))
}

// IMPROVE(#655): this acts as a feature flag to allow tests to ignore the check if needed, ideally the tests should have a way to determine
Expand Down

0 comments on commit 4e0b03b

Please sign in to comment.