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()