From 8e8a284b91b9773fa751327b39bd99c1f8e5fc4e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 02:19:33 +0000 Subject: [PATCH 1/5] fix: remove map iteration non-determinism with keys (backport #1302) (#1305) * fix: remove map iteration non-determinism with keys (#1302) * remove map iteration non-determinism with keys * add CHANGELOG (cherry picked from commit c462f368628c092dafe48c254114143dbf596b35) # Conflicts: # CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: Jayden Lee <41176085+tkxkd0159@users.noreply.github.com> Co-authored-by: Youngtaek Yoon --- CHANGELOG.md | 1 + baseapp/baseapp.go | 7 ++++++- store/rootmulti/store.go | 25 +++++++++++++++++++++---- types/module/module.go | 6 +++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb197ca3e7..77f7aef24b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/collection) [\#1282](https://github.com/Finschia/finschia-sdk/pull/1282) eliminates potential risk for Insufficient Sanity Check of tokenID in Genesis (backport #1276) * (x/collection) [\#1290](https://github.com/Finschia/finschia-sdk/pull/1290) export x/collection params into genesis (backport #1268) * (x/foundation) [\#1295](https://github.com/Finschia/finschia-sdk/pull/1295) add missing error handling for migration +* (sec) [\#1302](https://github.com/Finschia/finschia-sdk/pull/1302) remove map iteration non-determinism with keys + sorting ### Removed diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index cd0798dcc7..422b63db43 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "reflect" + "sort" "strings" "sync" @@ -16,6 +17,7 @@ import ( "github.com/Finschia/ostracon/crypto/tmhash" "github.com/Finschia/ostracon/libs/log" dbm "github.com/tendermint/tm-db" + "golang.org/x/exp/maps" "github.com/Finschia/finschia-sdk/codec/types" "github.com/Finschia/finschia-sdk/server/config" @@ -272,7 +274,10 @@ func (app *BaseApp) MountKVStores(keys map[string]*sdk.KVStoreKey) { // MountMemoryStores mounts all in-memory KVStores with the BaseApp's internal // commit multi-store. func (app *BaseApp) MountMemoryStores(keys map[string]*sdk.MemoryStoreKey) { - for _, memKey := range keys { + skeys := maps.Keys(keys) + sort.Strings(skeys) + for _, key := range skeys { + memKey := keys[key] app.MountStore(memKey, sdk.StoreTypeMemory) } } diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 8165debd66..703cc1b6e5 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -41,6 +41,19 @@ const ( const iavlDisablefastNodeDefault = true +// keysFromStoreKeyMap returns a slice of keys for the provided map lexically sorted by StoreKey.Name() +func keysFromStoreKeyMap[V any](m map[types.StoreKey]V) []types.StoreKey { + keys := make([]types.StoreKey, 0, len(m)) + for key := range m { + keys = append(keys, key) + } + sort.Slice(keys, func(i, j int) bool { + ki, kj := keys[i], keys[j] + return ki.Name() < kj.Name() + }) + return keys +} + // Store is composed of many CommitStores. Name contrasts with // cacheMultiStore which is used for branching other MultiStores. It implements // the CommitMultiStore interface. @@ -691,8 +704,9 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error { *iavl.Store name string } - stores := []namedStore{} - for key := range rs.stores { + stores := make([]namedStore, 0) + keys := keysFromStoreKeyMap(rs.stores) + for _, key := range keys { switch store := rs.GetCommitKVStore(key).(type) { case *iavl.Store: stores = append(stores, namedStore{name: key.Name(), Store: store}) @@ -900,9 +914,12 @@ func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID } func (rs *Store) buildCommitInfo(version int64) *types.CommitInfo { + keys := keysFromStoreKeyMap(rs.stores) storeInfos := []types.StoreInfo{} - for key, store := range rs.stores { - if store.GetStoreType() == types.StoreTypeTransient { + for _, key := range keys { + store := rs.stores[key] + storeType := store.GetStoreType() + if storeType == types.StoreTypeTransient || storeType == types.StoreTypeMemory { continue } storeInfos = append(storeInfos, types.StoreInfo{ diff --git a/types/module/module.go b/types/module/module.go index 527d3bcd02..8d932455f9 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -38,6 +38,7 @@ import ( ocabci "github.com/Finschia/ostracon/abci/types" abci "github.com/tendermint/tendermint/abci/types" + "golang.org/x/exp/maps" "github.com/Finschia/finschia-sdk/client" "github.com/Finschia/finschia-sdk/codec" @@ -351,13 +352,16 @@ func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames [] for _, m := range moduleNames { ms[m] = true } + + allKeys := maps.Keys(m.Modules) var missing []string - for m := range m.Modules { + for _, m := range allKeys { if !ms[m] { missing = append(missing, m) } } if len(missing) != 0 { + sort.Strings(missing) panic(fmt.Sprintf( "%s: all modules must be defined when setting %s, missing: %v", setOrderFnName, setOrderFnName, missing)) } From c5fbc749abf65ac8906c735f48031c2334acdeb4 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 02:34:55 +0000 Subject: [PATCH 2/5] fix: possible overflow in BuildUnsignedTx (backport #1303) (#1307) * fix: possible overflow in BuildUnsignedTx (#1303) * fix: possible overflow * chore: update changelog * chore: add comment (cherry picked from commit b1c09cf6c82b9db4600176512ecf5e1b14679385) # Conflicts: # CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: jaeseung-bae <119839167+jaeseung-bae@users.noreply.github.com> Co-authored-by: Youngtaek Yoon --- CHANGELOG.md | 1 + client/tx/factory.go | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77f7aef24b..63e69f702e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/collection) [\#1290](https://github.com/Finschia/finschia-sdk/pull/1290) export x/collection params into genesis (backport #1268) * (x/foundation) [\#1295](https://github.com/Finschia/finschia-sdk/pull/1295) add missing error handling for migration * (sec) [\#1302](https://github.com/Finschia/finschia-sdk/pull/1302) remove map iteration non-determinism with keys + sorting +* (client) [\#1303](https://github.com/Finschia/finschia-sdk/pull/1303) fix possible overflow in BuildUnsignedTx ### Removed diff --git a/client/tx/factory.go b/client/tx/factory.go index 709782d68a..ae773f4438 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -3,6 +3,7 @@ package tx import ( "errors" "fmt" + "math/big" "os" "github.com/spf13/pflag" @@ -212,7 +213,9 @@ func (f Factory) BuildUnsignedTx(msgs ...sdk.Msg) (client.TxBuilder, error) { return nil, errors.New("cannot provide both fees and gas prices") } - glDec := sdk.NewDec(int64(f.gas)) + // f.gas is a uint64 and we should convert to LegacyDec + // without the risk of under/overflow via uint64->int64. + glDec := sdk.NewDecFromBigInt(new(big.Int).SetUint64(f.gas)) // Derive the fees based on the provided gas prices, where // fee = ceil(gasPrice * gasLimit). From a496d32f9132f89cd82440881b4e4739359c6e83 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 04:01:25 +0000 Subject: [PATCH 3/5] fix: add missing nil checks (backport #1299) (#1308) * fix: add missing nil checks (#1299) * fix: types: ensure .Amount is non-nil in Coin.Validate() (#15691) This change fixes a scenario in which Coin.Validate() would panic when given a nil Amount. While here, added a fuzz test along with unit/regression tests. Fixes #15690 * fix: change math.NewInt to sdk.NewInt Signed-off-by: 170210 * fix: change to finschia-sdk Signed-off-by: 170210 * fix: prevent nil DecCoin creation when converting Coins to DecCoins (#12903) Closes: #12902 --- *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed *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) * chore: update CHANGELOG.md Signed-off-by: 170210 * fix: fix lint Signed-off-by: 170210 --------- Signed-off-by: 170210 Co-authored-by: Emmanuel T Odeke Co-authored-by: yys (cherry picked from commit a7a39e57060621afb0c346240d204d3cf0687fdc) # Conflicts: # CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: 170210 <85928898+170210@users.noreply.github.com> Co-authored-by: Youngtaek Yoon --- CHANGELOG.md | 1 + types/coin.go | 5 +++++ types/coin_test.go | 33 +++++++++++++++++++++++++++++++++ types/dec_coin.go | 10 +++++++--- types/dec_coin_test.go | 23 +++++++++++++++++++++++ types/fuzz_test.go | 24 ++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 types/fuzz_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 63e69f702e..36980f7335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/foundation) [\#1295](https://github.com/Finschia/finschia-sdk/pull/1295) add missing error handling for migration * (sec) [\#1302](https://github.com/Finschia/finschia-sdk/pull/1302) remove map iteration non-determinism with keys + sorting * (client) [\#1303](https://github.com/Finschia/finschia-sdk/pull/1303) fix possible overflow in BuildUnsignedTx +* (types) [\#1299](https://github.com/Finschia/finschia-sdk/pull/1299) add missing nil checks ### Removed diff --git a/types/coin.go b/types/coin.go index 01358f414b..85d4c405da 100644 --- a/types/coin.go +++ b/types/coin.go @@ -2,6 +2,7 @@ package types import ( "encoding/json" + "errors" "fmt" "regexp" "sort" @@ -44,6 +45,10 @@ func (coin Coin) Validate() error { return err } + if coin.Amount.IsNil() { + return errors.New("amount is nil") + } + if coin.Amount.IsNegative() { return fmt.Errorf("negative coin amount: %v", coin.Amount) } diff --git a/types/coin_test.go b/types/coin_test.go index 8f48e89fec..bb5eee2cc6 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -1034,6 +1034,39 @@ func (s *coinTestSuite) TestMarshalJSONCoins() { } } +func (s *coinTestSuite) TestCoinValidate() { + testCases := []struct { + name string + coin sdk.Coin + wantErr string + }{ + {"nil coin: nil Amount", sdk.Coin{}, "invalid denom"}, + {"non-blank coin, nil Amount", sdk.Coin{Denom: "atom"}, "amount is nil"}, + {"valid coin", sdk.Coin{Denom: "atom", Amount: sdk.NewInt(100)}, ""}, + {"negative coin", sdk.Coin{Denom: "atom", Amount: sdk.NewInt(-999)}, "negative coin amount"}, + } + + for _, tc := range testCases { + tc := tc + t := s.T() + t.Run(tc.name, func(t *testing.T) { + err := tc.coin.Validate() + if tc.wantErr == "" { + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + return + } else { + if err == nil { + t.Error("Expected an error") + } else if !strings.Contains(err.Error(), tc.wantErr) { + t.Errorf("Error mismatch\n\tGot: %q\nWant: %q", err, tc.wantErr) + } + } + }) + } +} + func (s *coinTestSuite) TestCoinAminoEncoding() { cdc := codec.NewLegacyAmino() c := sdk.NewInt64Coin(testDenom1, 5) diff --git a/types/dec_coin.go b/types/dec_coin.go index 68fe3b9f95..10856d0fe2 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -182,10 +182,14 @@ func sanitizeDecCoins(decCoins []DecCoin) DecCoins { // NewDecCoinsFromCoins constructs a new coin set with decimal values // from regular Coins. func NewDecCoinsFromCoins(coins ...Coin) DecCoins { - decCoins := make(DecCoins, len(coins)) + if len(coins) == 0 { + return DecCoins{} + } + + decCoins := make([]DecCoin, 0, len(coins)) newCoins := NewCoins(coins...) - for i, coin := range newCoins { - decCoins[i] = NewDecCoinFromCoin(coin) + for _, coin := range newCoins { + decCoins = append(decCoins, NewDecCoinFromCoin(coin)) } return decCoins diff --git a/types/dec_coin_test.go b/types/dec_coin_test.go index 9017bda27c..e2c3ba9028 100644 --- a/types/dec_coin_test.go +++ b/types/dec_coin_test.go @@ -546,6 +546,29 @@ func (s *decCoinTestSuite) TestNewDecCoinsWithIsValid() { } } +func (s *decCoinTestSuite) TestNewDecCoinsWithZeroCoins() { + zeroCoins := append(sdk.NewCoins(sdk.NewCoin("mytoken", sdk.NewInt(0))), sdk.Coin{Denom: "wbtc", Amount: sdk.NewInt(10)}) + + tests := []struct { + coins sdk.Coins + expectLength int + }{ + { + sdk.NewCoins(sdk.NewCoin("mytoken", sdk.NewInt(10)), sdk.NewCoin("wbtc", sdk.NewInt(10))), + 2, + }, + { + zeroCoins, + 1, + }, + } + + for _, tc := range tests { + tc := tc + s.Require().Equal(sdk.NewDecCoinsFromCoins(tc.coins...).Len(), tc.expectLength) + } +} + func (s *decCoinTestSuite) TestDecCoins_AddDecCoinWithIsValid() { lengthTestDecCoins := sdk.NewDecCoins().Add(sdk.NewDecCoin("mytoken", sdk.NewInt(10))).Add(sdk.DecCoin{Denom: "BTC", Amount: sdk.NewDec(10)}) s.Require().Equal(2, len(lengthTestDecCoins), "should be 2") diff --git a/types/fuzz_test.go b/types/fuzz_test.go new file mode 100644 index 0000000000..99b80be29a --- /dev/null +++ b/types/fuzz_test.go @@ -0,0 +1,24 @@ +package types + +import ( + "testing" + + "github.com/Finschia/finschia-sdk/codec" +) + +func FuzzCoinUnmarshalJSON(f *testing.F) { + if testing.Short() { + f.Skip() + } + + cdc := codec.NewLegacyAmino() + f.Add(`{"denom":"atom","amount":"1000"}`) + f.Add(`{"denom":"atom","amount":"-1000"}`) + f.Add(`{"denom":"uatom","amount":"1000111111111111111111111"}`) + f.Add(`{"denom":"mu","amount":"0"}`) + + f.Fuzz(func(t *testing.T, jsonBlob string) { + var c Coin + _ = cdc.UnmarshalJSON([]byte(jsonBlob), &c) + }) +} From 506f8ffface3ebbcf881c73c992a81736758d554 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 04:14:14 +0000 Subject: [PATCH 4/5] fix: Use bytes instead of string comparison in delete validator queue (backport cosmos/cosmos-sdk#12303) (backport #1301) (#1309) * fix: Use bytes instead of string comparison in delete validator queue (backport cosmos/cosmos-sdk#12303) (#1301) * fix : Use bytes instead of string comparison in delete validator queue (#12303) * Update CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: William Chong <6198816+williamchong@users.noreply.github.com> (cherry picked from commit 1038a394c9cbb7b21740b438cdda8b40b4db4c1a) # Conflicts: # CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: Youngtaek Yoon --- CHANGELOG.md | 1 + x/staking/keeper/validator.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36980f7335..e2cb976818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (sec) [\#1302](https://github.com/Finschia/finschia-sdk/pull/1302) remove map iteration non-determinism with keys + sorting * (client) [\#1303](https://github.com/Finschia/finschia-sdk/pull/1303) fix possible overflow in BuildUnsignedTx * (types) [\#1299](https://github.com/Finschia/finschia-sdk/pull/1299) add missing nil checks +* (x/staking) [\#1301](https://github.com/Finschia/finschia-sdk/pull/1301) Use bytes instead of string comparison in delete validator queue (backport cosmos/cosmos-sdk#12303) ### Removed diff --git a/x/staking/keeper/validator.go b/x/staking/keeper/validator.go index a10de577d8..9c5e5fd003 100644 --- a/x/staking/keeper/validator.go +++ b/x/staking/keeper/validator.go @@ -375,9 +375,21 @@ func (k Keeper) DeleteValidatorQueue(ctx sdk.Context, val types.Validator) { addrs := k.GetUnbondingValidators(ctx, val.UnbondingTime, val.UnbondingHeight) newAddrs := []string{} + // since address string may change due to Bech32 prefix change, we parse the addresses into bytes + // format for normalization + deletingAddr, err := sdk.ValAddressFromBech32(val.OperatorAddress) + if err != nil { + panic(err) + } + for _, addr := range addrs { - if addr != val.OperatorAddress { - newAddrs = append(newAddrs, addr) + storedAddr, err := sdk.ValAddressFromBech32(addr) + if err != nil { + // even if we don't panic here, it will panic in UnbondAllMatureValidators at unbond time + panic(err) + } + if !storedAddr.Equals(deletingAddr) { + newAddrs = append(newAddrs, storedAddr.String()) } } From 0c0e2c24374e64a06b43a4957391d890e0a72ea1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 04:28:01 +0000 Subject: [PATCH 5/5] fix: ignore error when key not found in keys delete (backport #1312) (#1315) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: ignore error when key not found in keys delete (#1312) * fix(client/keys): don't return when key not found in keys delete (#18562) * chore: update CHANGLOG.md Signed-off-by: 170210 --------- Signed-off-by: 170210 Co-authored-by: Julián Toledano (cherry picked from commit e5709f3324a4b766faa56fc51aed4a9d6b62d4c6) # Conflicts: # CHANGELOG.md * Update CHANGELOG.md --------- Co-authored-by: 170210 <85928898+170210@users.noreply.github.com> Co-authored-by: Youngtaek Yoon --- CHANGELOG.md | 1 + client/keys/delete.go | 3 ++- client/keys/delete_test.go | 3 +-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2cb976818..3e6d979bdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (client) [\#1303](https://github.com/Finschia/finschia-sdk/pull/1303) fix possible overflow in BuildUnsignedTx * (types) [\#1299](https://github.com/Finschia/finschia-sdk/pull/1299) add missing nil checks * (x/staking) [\#1301](https://github.com/Finschia/finschia-sdk/pull/1301) Use bytes instead of string comparison in delete validator queue (backport cosmos/cosmos-sdk#12303) +* (client/keys) [#1312](https://github.com/Finschia/finschia-sdk/pull/1312) ignore error when key not found in `keys delete` ### Removed diff --git a/client/keys/delete.go b/client/keys/delete.go index 8f5c9e695e..a7fb35158d 100644 --- a/client/keys/delete.go +++ b/client/keys/delete.go @@ -37,7 +37,8 @@ private keys stored in a ledger device cannot be deleted with the CLI. for _, name := range args { info, err := clientCtx.Keyring.Key(name) if err != nil { - return err + cmd.PrintErrf("key %s not found\n", name) + continue } // confirm deletion, unless -y is passed diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index afb1c0fb0f..2a4bf397fd 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -51,8 +51,7 @@ func Test_runDeleteCmd(t *testing.T) { ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) err = cmd.ExecuteContext(ctx) - require.Error(t, err) - require.EqualError(t, err, "blah.info: key not found") + require.NoError(t, err) // User confirmation missing cmd.SetArgs([]string{