From 4e0b03b7a83ce1d97ad5c88f044e98fa2422ee58 Mon Sep 17 00:00:00 2001 From: d7t Date: Mon, 31 Jul 2023 12:25:09 -0600 Subject: [PATCH] [chore] Replaces multierr usage with go native errors package (#939) Co-authored-by: Daniel Olshansky --- go.mod | 2 +- p2p/background/router.go | 4 ++-- p2p/config/config.go | 18 +++++++++--------- p2p/module.go | 7 +++---- .../peerstore_provider/peerstore_provider.go | 7 ++++--- p2p/utils/host.go | 6 +++--- persistence/context.go | 3 +-- shared/node.go | 8 +++----- utility/unit_of_work/module.go | 9 +++++---- 9 files changed, 31 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index e9772889b..b5029a9ce 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/p2p/background/router.go b/p2p/background/router.go index 7899f7817..e79c9c79e 100644 --- a/p2p/background/router.go +++ b/p2p/background/router.go @@ -4,6 +4,7 @@ package background import ( "context" + "errors" "fmt" "time" @@ -11,7 +12,6 @@ import ( 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" @@ -129,7 +129,7 @@ func (rtr *backgroundRouter) Close() error { topicCloseErr = err } - return multierr.Append( + return errors.Join( topicCloseErr, rtr.kadDHT.Close(), ) diff --git a/p2p/config/config.go b/p2p/config/config.go index 90350444b..690a72be0 100644 --- a/p2p/config/config.go +++ b/p2p/config/config.go @@ -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" @@ -56,15 +56,15 @@ 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 } @@ -72,23 +72,23 @@ func (cfg *baseConfig) IsValid() (err error) { // 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 } diff --git a/p2p/module.go b/p2p/module.go index 6bb8f479a..4e130bb10 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -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" @@ -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(), ) @@ -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 { diff --git a/p2p/providers/peerstore_provider/peerstore_provider.go b/p2p/providers/peerstore_provider/peerstore_provider.go index bbf57746a..e7bb686d7 100644 --- a/p2p/providers/peerstore_provider/peerstore_provider.go +++ b/p2p/providers/peerstore_provider/peerstore_provider.go @@ -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" @@ -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 diff --git a/p2p/utils/host.go b/p2p/utils/host.go index 3597856b7..e53e0d246 100644 --- a/p2p/utils/host.go +++ b/p2p/utils/host.go @@ -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" @@ -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 @@ -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(), ) diff --git a/persistence/context.go b/persistence/context.go index 15a89dfe1..c91560086 100644 --- a/persistence/context.go +++ b/persistence/context.go @@ -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{} @@ -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 diff --git a/shared/node.go b/shared/node.go index f1e842382..39b905e38 100644 --- a/shared/node.go +++ b/shared/node.go @@ -2,6 +2,7 @@ package shared import ( "context" + "errors" "time" "github.com/pokt-network/pocket/consensus" @@ -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 ( @@ -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) } diff --git a/utility/unit_of_work/module.go b/utility/unit_of_work/module.go index 22547e090..a654218ac 100644 --- a/utility/unit_of_work/module.go +++ b/utility/unit_of_work/module.go @@ -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 ( @@ -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