From 142880b04679ec4794d6fe7ac5909807460b06be Mon Sep 17 00:00:00 2001 From: leoflalv <47084241+leoflalv@users.noreply.github.com> Date: Wed, 26 Oct 2022 17:42:19 +0200 Subject: [PATCH] refactor: add `sdk.LogDeferred` to report errors in defers (#13619) ## Description Closes: #11785 A new helper function called `LogDeferred` was added. This function it's used to report errors in `defer` section. It was added in `defer` statements of `x/bank` folder. ### Author Checklist I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- CHANGELOG.md | 1 + internal/util.go | 4 +++- types/utils.go | 8 ++++++++ x/bank/keeper/keeper.go | 4 ++-- x/bank/keeper/send.go | 2 +- x/bank/keeper/view.go | 2 +- x/bank/migrations/v2/store.go | 7 ++++--- x/bank/migrations/v3/store.go | 13 +++++++------ 8 files changed, 27 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32999e46f2a6..fe6ee1984b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements +* [#13619](https://github.com/cosmos/cosmos-sdk/pull/13619) Add new function called LogDeferred to report errors in defers. Use the function in x/bank files. * (tools) [#13603](https://github.com/cosmos/cosmos-sdk/pull/13603) Rename cosmovisor package name to `cosmossdk.io/tools/cosmovisor`. The new tool directory contains Cosmos SDK tools. * (deps) [#13397](https://github.com/cosmos/cosmos-sdk/pull/13397) Bump Go version minimum requirement to `1.19`. * [#13070](https://github.com/cosmos/cosmos-sdk/pull/13070) Migrate from `gogo/protobuf` to `cosmos/gogoproto`. diff --git a/internal/util.go b/internal/util.go index fc67461bd489..bcbcf821638d 100644 --- a/internal/util.go +++ b/internal/util.go @@ -1,6 +1,8 @@ package util -import "fmt" +import ( + "fmt" +) func CombineErrors(ret error, also error, desc string) error { if also != nil { diff --git a/types/utils.go b/types/utils.go index b3a1efebba60..88c19f8d0069 100644 --- a/types/utils.go +++ b/types/utils.go @@ -7,6 +7,7 @@ import ( "time" "github.com/cosmos/cosmos-sdk/types/kv" + log "github.com/tendermint/tendermint/libs/log" ) // SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces @@ -133,3 +134,10 @@ func ParseLengthPrefixedBytes(key []byte, startIndex int, sliceLength int) ([]by return byteSlice, endIndex } + +// LogDeferred logs an error in a deferred function call if the returned error is non-nil. +func LogDeferred(logger log.Logger, f func() error) { + if err := f(); err != nil { + logger.Error(err.Error()) + } +} diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index f8ce411b4cbe..4fbb622ad9e6 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -288,7 +288,7 @@ func (k BaseKeeper) IterateAllDenomMetaData(ctx sdk.Context, cb func(types.Metad denomMetaDataStore := prefix.NewStore(store, types.DenomMetadataPrefix) iterator := denomMetaDataStore.Iterator(nil, nil) - defer iterator.Close() + defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { var metadata types.Metadata @@ -527,7 +527,7 @@ func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bo supplyStore := prefix.NewStore(store, types.SupplyKey) iterator := supplyStore.Iterator(nil, nil) - defer iterator.Close() + defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { var amount math.Int diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index 6fa89f9cc52c..afaf44b4d15a 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -445,7 +445,7 @@ func (k BaseSendKeeper) IterateSendEnabledEntries(ctx sdk.Context, cb func(denom seStore := k.getSendEnabledPrefixStore(ctx) iterator := seStore.Iterator(nil, nil) - defer iterator.Close() + defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { denom := string(iterator.Key()) diff --git a/x/bank/keeper/view.go b/x/bank/keeper/view.go index 865b6e1eda4e..c761163beab2 100644 --- a/x/bank/keeper/view.go +++ b/x/bank/keeper/view.go @@ -116,7 +116,7 @@ func (k BaseViewKeeper) IterateAccountBalances(ctx sdk.Context, addr sdk.AccAddr accountStore := k.getAccountStore(ctx, addr) iterator := accountStore.Iterator(nil, nil) - defer iterator.Close() + defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) for ; iterator.Valid(); iterator.Next() { denom := string(iterator.Key()) diff --git a/x/bank/migrations/v2/store.go b/x/bank/migrations/v2/store.go index 84a882d3d40b..7b9331bdfbdf 100644 --- a/x/bank/migrations/v2/store.go +++ b/x/bank/migrations/v2/store.go @@ -9,6 +9,7 @@ import ( v042auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v042" v1 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v1" "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/tendermint/tendermint/libs/log" ) // migrateSupply migrates the supply to be stored by denom key instead in a @@ -50,7 +51,7 @@ func migrateSupply(store sdk.KVStore, cdc codec.BinaryCodec) error { // migrateBalanceKeys migrate the balances keys to cater for variable-length // addresses. -func migrateBalanceKeys(store sdk.KVStore) { +func migrateBalanceKeys(store sdk.KVStore, logger log.Logger) { // old key is of format: // prefix ("balances") || addrBytes (20 bytes) || denomBytes // new key is of format @@ -58,7 +59,7 @@ func migrateBalanceKeys(store sdk.KVStore) { oldStore := prefix.NewStore(store, v1.BalancesPrefix) oldStoreIter := oldStore.Iterator(nil, nil) - defer oldStoreIter.Close() + defer sdk.LogDeferred(logger, func() error { return oldStoreIter.Close() }) for ; oldStoreIter.Valid(); oldStoreIter.Next() { addr := v1.AddressFromBalancesStore(oldStoreIter.Key()) @@ -80,7 +81,7 @@ func migrateBalanceKeys(store sdk.KVStore) { // - Prune balances & supply with zero coins (ref: https://github.com/cosmos/cosmos-sdk/pull/9229) func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error { store := ctx.KVStore(storeKey) - migrateBalanceKeys(store) + migrateBalanceKeys(store, ctx.Logger()) if err := pruneZeroBalances(store, cdc); err != nil { return err diff --git a/x/bank/migrations/v3/store.go b/x/bank/migrations/v3/store.go index 1ac18373f5c8..5525f0015bff 100644 --- a/x/bank/migrations/v3/store.go +++ b/x/bank/migrations/v3/store.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/address" v2 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v2" "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/tendermint/tendermint/libs/log" ) // MigrateStore performs in-place store migrations from v0.43 to v0.45. The @@ -18,19 +19,19 @@ import ( // - Remove duplicate denom from denom metadata store key. func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error { store := ctx.KVStore(storeKey) - err := addDenomReverseIndex(store, cdc) + err := addDenomReverseIndex(store, cdc, ctx.Logger()) if err != nil { return err } - return migrateDenomMetadata(store) + return migrateDenomMetadata(store, ctx.Logger()) } -func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { +func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec, logger log.Logger) error { oldBalancesStore := prefix.NewStore(store, v2.BalancesPrefix) oldBalancesIter := oldBalancesStore.Iterator(nil, nil) - defer oldBalancesIter.Close() + defer sdk.LogDeferred(logger, func() error { return oldBalancesIter.Close() }) denomPrefixStores := make(map[string]prefix.Store) // memoize prefix stores @@ -72,11 +73,11 @@ func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { return nil } -func migrateDenomMetadata(store sdk.KVStore) error { +func migrateDenomMetadata(store sdk.KVStore, logger log.Logger) error { oldDenomMetaDataStore := prefix.NewStore(store, v2.DenomMetadataPrefix) oldDenomMetaDataIter := oldDenomMetaDataStore.Iterator(nil, nil) - defer oldDenomMetaDataIter.Close() + defer sdk.LogDeferred(logger, func() error { return oldDenomMetaDataIter.Close() }) for ; oldDenomMetaDataIter.Valid(); oldDenomMetaDataIter.Next() { oldKey := oldDenomMetaDataIter.Key()