From 248ad760b89198a6700807487461b87f867d739f Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:14:29 +0700 Subject: [PATCH 01/25] refactor defer in loop, and add error handling for Close function --- store/commitment/store.go | 11 +++++++++-- store/migration/manager.go | 6 +++++- store/snapshots/store.go | 1 - store/storage/storage_test_suite.go | 14 ++++++++++---- tests/integration/auth/client/cli/suite_test.go | 3 ++- x/group/keeper/invariants.go | 12 ++++++++++-- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/store/commitment/store.go b/store/commitment/store.go index 4b2756fec206..44eeaae9c855 100644 --- a/store/commitment/store.go +++ b/store/commitment/store.go @@ -399,7 +399,10 @@ loop: if err := importer.Commit(); err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err) } - importer.Close() + err := importer.Close() + if err != nil { + return snapshotstypes.SnapshotItem{}, err + } } storeKey = item.Store.Name @@ -411,7 +414,6 @@ loop: if err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to import tree for version %d: %w", version, err) } - defer importer.Close() case *snapshotstypes.SnapshotItem_IAVL: if importer == nil { @@ -460,6 +462,11 @@ loop: } } + err := importer.Close() + if err != nil { + return snapshotstypes.SnapshotItem{}, err + } + return snapshotItem, c.LoadVersion(version) } diff --git a/store/migration/manager.go b/store/migration/manager.go index 43dc7dffc033..098fbb72997a 100644 --- a/store/migration/manager.go +++ b/store/migration/manager.go @@ -134,7 +134,6 @@ func (m *Manager) writeChangeset() error { } batch := m.db.NewBatch() - defer batch.Close() if err := batch.Set(csKey, csBytes); err != nil { return fmt.Errorf("failed to write changeset to db.Batch: %w", err) @@ -142,6 +141,11 @@ func (m *Manager) writeChangeset() error { if err := batch.Write(); err != nil { return fmt.Errorf("failed to write changeset to db: %w", err) } + + err = batch.Close() + if err != nil { + return err + } } return nil diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 5c82a095e6dd..5ad6caf14248 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -172,7 +172,6 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R _ = pw.CloseWithError(err) return } - defer chunk.Close() _, err = io.Copy(pw, chunk) if err != nil { _ = pw.CloseWithError(err) diff --git a/store/storage/storage_test_suite.go b/store/storage/storage_test_suite.go index b7d748386e2b..563b20a659c2 100644 --- a/store/storage/storage_test_suite.go +++ b/store/storage/storage_test_suite.go @@ -256,8 +256,6 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { itr, err := db.Iterator(storeKey1Bytes, v, []byte("key000"), nil) s.Require().NoError(err) - defer itr.Close() - var i, count int for ; itr.Valid(); itr.Next() { s.Require().Equal([]byte(fmt.Sprintf("key%03d", i)), itr.Key(), string(itr.Key())) @@ -271,6 +269,11 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { // seek past domain, which should make the iterator invalid and produce an error s.Require().False(itr.Valid()) + + err = itr.Close() + if err != nil { + return + } } // iterator with a start and end domain over multiple versions @@ -278,8 +281,6 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { itr2, err := db.Iterator(storeKey1Bytes, v, []byte("key010"), []byte("key019")) s.Require().NoError(err) - defer itr2.Close() - i, count := 10, 0 for ; itr2.Valid(); itr2.Next() { s.Require().Equal([]byte(fmt.Sprintf("key%03d", i)), itr2.Key()) @@ -293,6 +294,11 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { // seek past domain, which should make the iterator invalid and produce an error s.Require().False(itr2.Valid()) + + err = itr2.Close() + if err != nil { + return + } } // start must be <= end diff --git a/tests/integration/auth/client/cli/suite_test.go b/tests/integration/auth/client/cli/suite_test.go index 963a313045c5..df085942995c 100644 --- a/tests/integration/auth/client/cli/suite_test.go +++ b/tests/integration/auth/client/cli/suite_test.go @@ -207,7 +207,6 @@ func (s *CLITestSuite) TestCLISignBatchTotalFees() { sdk.NewCoins(sendTokens), clitestutil.TestTxConfig{GenOnly: true}) s.Require().NoError(err) txFile := testutil.WriteToNewTempFile(s.T(), tx.String()+"\n") - defer txFile.Close() txFiles[i] = txFile.Name() unsignedTx, err := txCfg.TxJSONDecoder()(tx.Bytes()) @@ -215,6 +214,8 @@ func (s *CLITestSuite) TestCLISignBatchTotalFees() { txBuilder, err := txCfg.WrapTxBuilder(unsignedTx) s.Require().NoError(err) expectedBatchedTotalFee += txBuilder.GetTx().GetFee().AmountOf(tc.denom).Int64() + err = txFile.Close() + s.NoError(err) } // Test batch sign diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index a88f6d10699b..72d77d0d66aa 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -42,7 +42,12 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("PrefixScan failure on group table\n%v\n", err) return msg, broken } - defer groupIt.Close() + defer func(groupIt orm.Iterator) { + err := groupIt.Close() + if err != nil { + return + } + }(groupIt) groups := make(map[uint64]group.GroupInfo) for { @@ -76,7 +81,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("error while returning group member iterator for group with ID %d\n%v\n", groupInfo.Id, err) return msg, broken } - defer memIt.Close() for { var groupMember group.GroupMember @@ -113,6 +117,10 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("group's TotalWeight must be equal to the sum of its members' weights\ngroup weight: %s\nSum of group members weights: %s\n", groupWeight.String(), membersWeight.String()) break } + err = memIt.Close() + if err != nil { + return "", broken + } } return msg, broken From 306cf5bee68780884c1d92f29af7f915710ae240 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:26:28 +0700 Subject: [PATCH 02/25] Update store/storage/storage_test_suite.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/storage/storage_test_suite.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/store/storage/storage_test_suite.go b/store/storage/storage_test_suite.go index 563b20a659c2..be5ea003f8e1 100644 --- a/store/storage/storage_test_suite.go +++ b/store/storage/storage_test_suite.go @@ -271,9 +271,7 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { s.Require().False(itr.Valid()) err = itr.Close() - if err != nil { - return - } + s.Require().NoError(err, "Failed to close iterator") } // iterator with a start and end domain over multiple versions From 7b0116e1090cb5e90cc0cc38f6911d0881e673ec Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:26:54 +0700 Subject: [PATCH 03/25] Update x/group/keeper/invariants.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- x/group/keeper/invariants.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index 72d77d0d66aa..9435d859f7b6 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -42,12 +42,7 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("PrefixScan failure on group table\n%v\n", err) return msg, broken } - defer func(groupIt orm.Iterator) { - err := groupIt.Close() - if err != nil { - return - } - }(groupIt) + defer groupIt.Close() groups := make(map[uint64]group.GroupInfo) for { From e45b71f6568099ecd2b2c33b671a226c0c63781e Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:27:09 +0700 Subject: [PATCH 04/25] Update store/commitment/store.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/commitment/store.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/store/commitment/store.go b/store/commitment/store.go index 44eeaae9c855..3c5e9f0a0a73 100644 --- a/store/commitment/store.go +++ b/store/commitment/store.go @@ -399,9 +399,8 @@ loop: if err := importer.Commit(); err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err) } - err := importer.Close() - if err != nil { - return snapshotstypes.SnapshotItem{}, err + if err := importer.Close(); err != nil { + return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err) } } From 023247eaaddb28bd73d626d2b14757237f6d48b6 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:27:22 +0700 Subject: [PATCH 05/25] Update store/commitment/store.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/commitment/store.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/store/commitment/store.go b/store/commitment/store.go index 3c5e9f0a0a73..d345bfd16826 100644 --- a/store/commitment/store.go +++ b/store/commitment/store.go @@ -461,9 +461,8 @@ loop: } } - err := importer.Close() - if err != nil { - return snapshotstypes.SnapshotItem{}, err + if err := importer.Close(); err != nil { + return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err) } return snapshotItem, c.LoadVersion(version) From 93c36dc0dbacc46702d39abccb305a2934870912 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:27:36 +0700 Subject: [PATCH 06/25] Update store/migration/manager.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/migration/manager.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/store/migration/manager.go b/store/migration/manager.go index 098fbb72997a..a52b6a7bd050 100644 --- a/store/migration/manager.go +++ b/store/migration/manager.go @@ -141,10 +141,8 @@ func (m *Manager) writeChangeset() error { if err := batch.Write(); err != nil { return fmt.Errorf("failed to write changeset to db: %w", err) } - - err = batch.Close() - if err != nil { - return err + if err := batch.Close(); err != nil { + return fmt.Errorf("failed to close batch: %w", err) } } From b0c99b44fd7e2131fef88e533760ed08cb8ea9b7 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 20:59:19 +0700 Subject: [PATCH 07/25] refactor to use anonymous closure --- store/migration/manager.go | 19 ++++++---- store/snapshots/store.go | 13 +++++-- x/group/keeper/invariants.go | 68 ++++++++++++++++-------------------- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/store/migration/manager.go b/store/migration/manager.go index 098fbb72997a..43a3cd35c303 100644 --- a/store/migration/manager.go +++ b/store/migration/manager.go @@ -135,14 +135,19 @@ func (m *Manager) writeChangeset() error { batch := m.db.NewBatch() - if err := batch.Set(csKey, csBytes); err != nil { - return fmt.Errorf("failed to write changeset to db.Batch: %w", err) - } - if err := batch.Write(); err != nil { - return fmt.Errorf("failed to write changeset to db: %w", err) - } + // Invoking this code in a closure so that defer is called immediately on return + // yet not in the for-loop which can leave resource lingering. + err = func() error { + defer batch.Close() - err = batch.Close() + if err := batch.Set(csKey, csBytes); err != nil { + return fmt.Errorf("failed to write changeset to db.Batch: %w", err) + } + if err := batch.Write(); err != nil { + return fmt.Errorf("failed to write changeset to db: %w", err) + } + return nil + }() if err != nil { return err } diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 5ad6caf14248..e3a5ff738b23 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -162,6 +162,7 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R } ch := make(chan io.ReadCloser) + go func() { defer close(ch) for i := uint32(0); i < snapshot.Chunks; i++ { @@ -170,15 +171,21 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R chunk, err := s.loadChunkFile(height, format, i) if err != nil { _ = pw.CloseWithError(err) + _ = chunk.Close() return } - _, err = io.Copy(pw, chunk) + err = func() error { + defer chunk.Close() + if _, err := io.Copy(pw, chunk); err != nil { + return fmt.Errorf("failed to copy chunk %d: %w", i, err) + } + return nil + }() if err != nil { _ = pw.CloseWithError(err) return } - chunk.Close() - pw.Close() + _ = pw.Close() } }() diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index 72d77d0d66aa..3715f1bc3276 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -30,7 +30,6 @@ func GroupTotalWeightInvariant(keeper Keeper) sdk.Invariant { return sdk.FormatInvariant(group.ModuleName, weightInvariant, msg), broken } } - func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KVStoreService, groupTable orm.AutoUInt64Table, groupMemberByGroupIndex orm.Index) (string, bool) { var msg string var broken bool @@ -42,12 +41,7 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("PrefixScan failure on group table\n%v\n", err) return msg, broken } - defer func(groupIt orm.Iterator) { - err := groupIt.Close() - if err != nil { - return - } - }(groupIt) + defer groupIt.Close() groups := make(map[uint64]group.GroupInfo) for { @@ -60,7 +54,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("LoadNext failure on group table iterator\n%v\n", err) return msg, broken } - groups[groupInfo.Id] = groupInfo } @@ -68,6 +61,7 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV sort.Slice(groupByIDs, func(i, j int) bool { return groupByIDs[i] < groupByIDs[j] }) + for _, groupID := range groupByIDs { groupInfo := groups[groupID] membersWeight, err := groupmath.NewNonNegativeDecFromString("0") @@ -76,34 +70,38 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV return msg, broken } - memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id) - if err != nil { - msg += fmt.Sprintf("error while returning group member iterator for group with ID %d\n%v\n", groupInfo.Id, err) - return msg, broken - } - - for { - var groupMember group.GroupMember - _, err = memIt.LoadNext(&groupMember) - if errors.ErrORMIteratorDone.Is(err) { - break - } + err = func() error { + memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id) if err != nil { - msg += fmt.Sprintf("LoadNext failure on member table iterator\n%v\n", err) - return msg, broken + return fmt.Errorf("error while returning group member iterator for group with ID %d\n%v", groupInfo.Id, err) } - - curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight()) - if err != nil { - msg += fmt.Sprintf("error while parsing non-nengative decimal for group member %s\n%v\n", groupMember.Member.Address, err) - return msg, broken - } - - membersWeight, err = groupmath.Add(membersWeight, curMemWeight) - if err != nil { - msg += fmt.Sprintf("decimal addition error while adding group member voting weight to total voting weight\n%v\n", err) - return msg, broken + defer memIt.Close() + + for { + var groupMember group.GroupMember + _, err = memIt.LoadNext(&groupMember) + if errors.ErrORMIteratorDone.Is(err) { + break + } + if err != nil { + return fmt.Errorf("LoadNext failure on member table iterator\n%v", err) + } + + curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight()) + if err != nil { + return fmt.Errorf("error while parsing non-nengative decimal for group member %s\n%v", groupMember.Member.Address, err) + } + + membersWeight, err = groupmath.Add(membersWeight, curMemWeight) + if err != nil { + return fmt.Errorf("decimal addition error while adding group member voting weight to total voting weight\n%v", err) + } } + return nil + }() + if err != nil { + msg += err.Error() + "\n" + return msg, broken } groupWeight, err := groupmath.NewNonNegativeDecFromString(groupInfo.GetTotalWeight()) @@ -117,10 +115,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("group's TotalWeight must be equal to the sum of its members' weights\ngroup weight: %s\nSum of group members weights: %s\n", groupWeight.String(), membersWeight.String()) break } - err = memIt.Close() - if err != nil { - return "", broken - } } return msg, broken From 99953564ac9133b0637c6a555dab8ac46b4c88bb Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 21:08:03 +0700 Subject: [PATCH 08/25] linting --- x/group/keeper/invariants.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index 3715f1bc3276..09f73a970e18 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -30,6 +30,7 @@ func GroupTotalWeightInvariant(keeper Keeper) sdk.Invariant { return sdk.FormatInvariant(group.ModuleName, weightInvariant, msg), broken } } + func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KVStoreService, groupTable orm.AutoUInt64Table, groupMemberByGroupIndex orm.Index) (string, bool) { var msg string var broken bool From b6c8dbec978f5e668598689d68dd1a4b40413391 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 21:21:11 +0700 Subject: [PATCH 09/25] rollback importer defer, bring pw.Close() to closure --- store/commitment/store.go | 5 +---- store/snapshots/store.go | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/store/commitment/store.go b/store/commitment/store.go index d345bfd16826..c5c6e46f0019 100644 --- a/store/commitment/store.go +++ b/store/commitment/store.go @@ -413,6 +413,7 @@ loop: if err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to import tree for version %d: %w", version, err) } + defer importer.Close() case *snapshotstypes.SnapshotItem_IAVL: if importer == nil { @@ -461,10 +462,6 @@ loop: } } - if err := importer.Close(); err != nil { - return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err) - } - return snapshotItem, c.LoadVersion(version) } diff --git a/store/snapshots/store.go b/store/snapshots/store.go index e3a5ff738b23..2a23c6e66ffd 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -179,13 +179,13 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R if _, err := io.Copy(pw, chunk); err != nil { return fmt.Errorf("failed to copy chunk %d: %w", i, err) } + _ = pw.Close() return nil }() if err != nil { _ = pw.CloseWithError(err) return } - _ = pw.Close() } }() From 097384b4f22718be814ff297ad910a32b865d13d Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 21:57:47 +0700 Subject: [PATCH 10/25] remove incorrect Close() --- store/snapshots/store.go | 1 - 1 file changed, 1 deletion(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 2a23c6e66ffd..1e3aec936429 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -171,7 +171,6 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R chunk, err := s.loadChunkFile(height, format, i) if err != nil { _ = pw.CloseWithError(err) - _ = chunk.Close() return } err = func() error { From e7b1a17eeea0d92ac24ae9ebdad2346ce41e546a Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 2 May 2024 08:36:44 +0700 Subject: [PATCH 11/25] refactor --- store/snapshots/store.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 1e3aec936429..ddb1ca1a1885 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -178,11 +178,13 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R if _, err := io.Copy(pw, chunk); err != nil { return fmt.Errorf("failed to copy chunk %d: %w", i, err) } - _ = pw.Close() - return nil + if err != nil { + _ = pw.CloseWithError(err) + return err + } + return pw.Close() }() if err != nil { - _ = pw.CloseWithError(err) return } } From b6a63be0cfe94c7d383c6eac2325da2ac2f9dc26 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 2 May 2024 08:46:02 +0700 Subject: [PATCH 12/25] fix redundant err check --- store/snapshots/store.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index ddb1ca1a1885..9863e02d34a6 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -175,13 +175,12 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R } err = func() error { defer chunk.Close() + if _, err := io.Copy(pw, chunk); err != nil { - return fmt.Errorf("failed to copy chunk %d: %w", i, err) - } - if err != nil { _ = pw.CloseWithError(err) - return err + return fmt.Errorf("failed to copy chunk %d: %w", i, err) } + return pw.Close() }() if err != nil { From 95fc4a7ed502438c4f4d3ba3a1464e85478cae3d Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:14:29 +0700 Subject: [PATCH 13/25] refactor defer in loop, and add error handling for Close function --- store/snapshots/store.go | 1 - store/v2/commitment/store.go | 11 +++++++++-- store/v2/migration/manager.go | 6 +++++- store/v2/storage/storage_test_suite.go | 14 ++++++++++---- tests/integration/auth/client/cli/suite_test.go | 3 ++- x/group/keeper/invariants.go | 12 ++++++++++-- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 2f08a6e6c463..a02b9fb75740 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -150,7 +150,6 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R _ = pw.CloseWithError(err) return } - defer chunk.Close() _, err = io.Copy(pw, chunk) if err != nil { _ = pw.CloseWithError(err) diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index 7a15ff48a2f0..a447add1a565 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -399,7 +399,10 @@ loop: if err := importer.Commit(); err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err) } - importer.Close() + err := importer.Close() + if err != nil { + return snapshotstypes.SnapshotItem{}, err + } } storeKey = []byte(item.Store.Name) @@ -411,7 +414,6 @@ loop: if err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to import tree for version %d: %w", version, err) } - defer importer.Close() case *snapshotstypes.SnapshotItem_IAVL: if importer == nil { @@ -458,6 +460,11 @@ loop: } } + err := importer.Close() + if err != nil { + return snapshotstypes.SnapshotItem{}, err + } + return snapshotItem, c.LoadVersion(version) } diff --git a/store/v2/migration/manager.go b/store/v2/migration/manager.go index bae585b1e503..14f251c0d5f2 100644 --- a/store/v2/migration/manager.go +++ b/store/v2/migration/manager.go @@ -182,7 +182,6 @@ func (m *Manager) writeChangeset() error { } batch := m.db.NewBatch() - defer batch.Close() if err := batch.Set(csKey, csBytes); err != nil { return fmt.Errorf("failed to write changeset to db.Batch: %w", err) @@ -190,6 +189,11 @@ func (m *Manager) writeChangeset() error { if err := batch.Write(); err != nil { return fmt.Errorf("failed to write changeset to db: %w", err) } + + err = batch.Close() + if err != nil { + return err + } } return nil diff --git a/store/v2/storage/storage_test_suite.go b/store/v2/storage/storage_test_suite.go index b7d748386e2b..563b20a659c2 100644 --- a/store/v2/storage/storage_test_suite.go +++ b/store/v2/storage/storage_test_suite.go @@ -256,8 +256,6 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { itr, err := db.Iterator(storeKey1Bytes, v, []byte("key000"), nil) s.Require().NoError(err) - defer itr.Close() - var i, count int for ; itr.Valid(); itr.Next() { s.Require().Equal([]byte(fmt.Sprintf("key%03d", i)), itr.Key(), string(itr.Key())) @@ -271,6 +269,11 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { // seek past domain, which should make the iterator invalid and produce an error s.Require().False(itr.Valid()) + + err = itr.Close() + if err != nil { + return + } } // iterator with a start and end domain over multiple versions @@ -278,8 +281,6 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { itr2, err := db.Iterator(storeKey1Bytes, v, []byte("key010"), []byte("key019")) s.Require().NoError(err) - defer itr2.Close() - i, count := 10, 0 for ; itr2.Valid(); itr2.Next() { s.Require().Equal([]byte(fmt.Sprintf("key%03d", i)), itr2.Key()) @@ -293,6 +294,11 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { // seek past domain, which should make the iterator invalid and produce an error s.Require().False(itr2.Valid()) + + err = itr2.Close() + if err != nil { + return + } } // start must be <= end diff --git a/tests/integration/auth/client/cli/suite_test.go b/tests/integration/auth/client/cli/suite_test.go index 15eeb31e4218..8c87e6b27ce0 100644 --- a/tests/integration/auth/client/cli/suite_test.go +++ b/tests/integration/auth/client/cli/suite_test.go @@ -207,7 +207,6 @@ func (s *CLITestSuite) TestCLISignBatchTotalFees() { sdk.NewCoins(sendTokens), clitestutil.TestTxConfig{GenOnly: true}) s.Require().NoError(err) txFile := testutil.WriteToNewTempFile(s.T(), tx.String()+"\n") - defer txFile.Close() txFiles[i] = txFile.Name() unsignedTx, err := txCfg.TxJSONDecoder()(tx.Bytes()) @@ -215,6 +214,8 @@ func (s *CLITestSuite) TestCLISignBatchTotalFees() { txBuilder, err := txCfg.WrapTxBuilder(unsignedTx) s.Require().NoError(err) expectedBatchedTotalFee += txBuilder.GetTx().GetFee().AmountOf(tc.denom).Int64() + err = txFile.Close() + s.NoError(err) } // Test batch sign diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index a88f6d10699b..72d77d0d66aa 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -42,7 +42,12 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("PrefixScan failure on group table\n%v\n", err) return msg, broken } - defer groupIt.Close() + defer func(groupIt orm.Iterator) { + err := groupIt.Close() + if err != nil { + return + } + }(groupIt) groups := make(map[uint64]group.GroupInfo) for { @@ -76,7 +81,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("error while returning group member iterator for group with ID %d\n%v\n", groupInfo.Id, err) return msg, broken } - defer memIt.Close() for { var groupMember group.GroupMember @@ -113,6 +117,10 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("group's TotalWeight must be equal to the sum of its members' weights\ngroup weight: %s\nSum of group members weights: %s\n", groupWeight.String(), membersWeight.String()) break } + err = memIt.Close() + if err != nil { + return "", broken + } } return msg, broken From 869ec3e38c03c11b2666748aaea06016086f143e Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 20:59:19 +0700 Subject: [PATCH 14/25] refactor to use anonymous closure --- store/snapshots/store.go | 13 +++++-- store/v2/migration/manager.go | 19 ++++++---- x/group/keeper/invariants.go | 68 ++++++++++++++++------------------- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index a02b9fb75740..cdb488ff0ada 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -140,6 +140,7 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R } ch := make(chan io.ReadCloser) + go func() { defer close(ch) for i := uint32(0); i < snapshot.Chunks; i++ { @@ -148,15 +149,21 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R chunk, err := s.loadChunkFile(height, format, i) if err != nil { _ = pw.CloseWithError(err) + _ = chunk.Close() return } - _, err = io.Copy(pw, chunk) + err = func() error { + defer chunk.Close() + if _, err := io.Copy(pw, chunk); err != nil { + return fmt.Errorf("failed to copy chunk %d: %w", i, err) + } + return nil + }() if err != nil { _ = pw.CloseWithError(err) return } - chunk.Close() - pw.Close() + _ = pw.Close() } }() diff --git a/store/v2/migration/manager.go b/store/v2/migration/manager.go index 14f251c0d5f2..fd11863c5bdd 100644 --- a/store/v2/migration/manager.go +++ b/store/v2/migration/manager.go @@ -183,14 +183,19 @@ func (m *Manager) writeChangeset() error { batch := m.db.NewBatch() - if err := batch.Set(csKey, csBytes); err != nil { - return fmt.Errorf("failed to write changeset to db.Batch: %w", err) - } - if err := batch.Write(); err != nil { - return fmt.Errorf("failed to write changeset to db: %w", err) - } + // Invoking this code in a closure so that defer is called immediately on return + // yet not in the for-loop which can leave resource lingering. + err = func() error { + defer batch.Close() - err = batch.Close() + if err := batch.Set(csKey, csBytes); err != nil { + return fmt.Errorf("failed to write changeset to db.Batch: %w", err) + } + if err := batch.Write(); err != nil { + return fmt.Errorf("failed to write changeset to db: %w", err) + } + return nil + }() if err != nil { return err } diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index 72d77d0d66aa..3715f1bc3276 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -30,7 +30,6 @@ func GroupTotalWeightInvariant(keeper Keeper) sdk.Invariant { return sdk.FormatInvariant(group.ModuleName, weightInvariant, msg), broken } } - func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KVStoreService, groupTable orm.AutoUInt64Table, groupMemberByGroupIndex orm.Index) (string, bool) { var msg string var broken bool @@ -42,12 +41,7 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("PrefixScan failure on group table\n%v\n", err) return msg, broken } - defer func(groupIt orm.Iterator) { - err := groupIt.Close() - if err != nil { - return - } - }(groupIt) + defer groupIt.Close() groups := make(map[uint64]group.GroupInfo) for { @@ -60,7 +54,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("LoadNext failure on group table iterator\n%v\n", err) return msg, broken } - groups[groupInfo.Id] = groupInfo } @@ -68,6 +61,7 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV sort.Slice(groupByIDs, func(i, j int) bool { return groupByIDs[i] < groupByIDs[j] }) + for _, groupID := range groupByIDs { groupInfo := groups[groupID] membersWeight, err := groupmath.NewNonNegativeDecFromString("0") @@ -76,34 +70,38 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV return msg, broken } - memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id) - if err != nil { - msg += fmt.Sprintf("error while returning group member iterator for group with ID %d\n%v\n", groupInfo.Id, err) - return msg, broken - } - - for { - var groupMember group.GroupMember - _, err = memIt.LoadNext(&groupMember) - if errors.ErrORMIteratorDone.Is(err) { - break - } + err = func() error { + memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id) if err != nil { - msg += fmt.Sprintf("LoadNext failure on member table iterator\n%v\n", err) - return msg, broken + return fmt.Errorf("error while returning group member iterator for group with ID %d\n%v", groupInfo.Id, err) } - - curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight()) - if err != nil { - msg += fmt.Sprintf("error while parsing non-nengative decimal for group member %s\n%v\n", groupMember.Member.Address, err) - return msg, broken - } - - membersWeight, err = groupmath.Add(membersWeight, curMemWeight) - if err != nil { - msg += fmt.Sprintf("decimal addition error while adding group member voting weight to total voting weight\n%v\n", err) - return msg, broken + defer memIt.Close() + + for { + var groupMember group.GroupMember + _, err = memIt.LoadNext(&groupMember) + if errors.ErrORMIteratorDone.Is(err) { + break + } + if err != nil { + return fmt.Errorf("LoadNext failure on member table iterator\n%v", err) + } + + curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight()) + if err != nil { + return fmt.Errorf("error while parsing non-nengative decimal for group member %s\n%v", groupMember.Member.Address, err) + } + + membersWeight, err = groupmath.Add(membersWeight, curMemWeight) + if err != nil { + return fmt.Errorf("decimal addition error while adding group member voting weight to total voting weight\n%v", err) + } } + return nil + }() + if err != nil { + msg += err.Error() + "\n" + return msg, broken } groupWeight, err := groupmath.NewNonNegativeDecFromString(groupInfo.GetTotalWeight()) @@ -117,10 +115,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV msg += fmt.Sprintf("group's TotalWeight must be equal to the sum of its members' weights\ngroup weight: %s\nSum of group members weights: %s\n", groupWeight.String(), membersWeight.String()) break } - err = memIt.Close() - if err != nil { - return "", broken - } } return msg, broken From d819ddd1ee8efbb3c708190cc913484221e15a67 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:26:28 +0700 Subject: [PATCH 15/25] Update store/storage/storage_test_suite.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/v2/storage/storage_test_suite.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/store/v2/storage/storage_test_suite.go b/store/v2/storage/storage_test_suite.go index 563b20a659c2..be5ea003f8e1 100644 --- a/store/v2/storage/storage_test_suite.go +++ b/store/v2/storage/storage_test_suite.go @@ -271,9 +271,7 @@ func (s *StorageTestSuite) TestDatabase_Iterator() { s.Require().False(itr.Valid()) err = itr.Close() - if err != nil { - return - } + s.Require().NoError(err, "Failed to close iterator") } // iterator with a start and end domain over multiple versions From 4543e6765998763c65deef3966f4c8928b163f57 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:27:09 +0700 Subject: [PATCH 16/25] Update store/commitment/store.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/v2/commitment/store.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index a447add1a565..1b18b4fe69a2 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -399,9 +399,8 @@ loop: if err := importer.Commit(); err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err) } - err := importer.Close() - if err != nil { - return snapshotstypes.SnapshotItem{}, err + if err := importer.Close(); err != nil { + return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err) } } From fce217ab2b841bf6cba8de7662277523c7af47d1 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 00:27:22 +0700 Subject: [PATCH 17/25] Update store/commitment/store.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- store/v2/commitment/store.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index 1b18b4fe69a2..525e40c1f2f0 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -459,9 +459,8 @@ loop: } } - err := importer.Close() - if err != nil { - return snapshotstypes.SnapshotItem{}, err + if err := importer.Close(); err != nil { + return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err) } return snapshotItem, c.LoadVersion(version) From fbaef03701e0a76ee6732bdfda5ca7393143fa84 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 21:08:03 +0700 Subject: [PATCH 18/25] linting --- x/group/keeper/invariants.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index 3715f1bc3276..09f73a970e18 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -30,6 +30,7 @@ func GroupTotalWeightInvariant(keeper Keeper) sdk.Invariant { return sdk.FormatInvariant(group.ModuleName, weightInvariant, msg), broken } } + func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KVStoreService, groupTable orm.AutoUInt64Table, groupMemberByGroupIndex orm.Index) (string, bool) { var msg string var broken bool From 953cfac8cb2cf816a9d6e2589f9aac5339a5f760 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 21:21:11 +0700 Subject: [PATCH 19/25] rollback importer defer, bring pw.Close() to closure --- store/snapshots/store.go | 2 +- store/v2/commitment/store.go | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index cdb488ff0ada..7d2ec1aa501b 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -157,13 +157,13 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R if _, err := io.Copy(pw, chunk); err != nil { return fmt.Errorf("failed to copy chunk %d: %w", i, err) } + _ = pw.Close() return nil }() if err != nil { _ = pw.CloseWithError(err) return } - _ = pw.Close() } }() diff --git a/store/v2/commitment/store.go b/store/v2/commitment/store.go index 525e40c1f2f0..6cf9b8bc989d 100644 --- a/store/v2/commitment/store.go +++ b/store/v2/commitment/store.go @@ -413,6 +413,7 @@ loop: if err != nil { return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to import tree for version %d: %w", version, err) } + defer importer.Close() case *snapshotstypes.SnapshotItem_IAVL: if importer == nil { @@ -459,10 +460,6 @@ loop: } } - if err := importer.Close(); err != nil { - return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err) - } - return snapshotItem, c.LoadVersion(version) } From 879999d9cef412aeda02073ac9d3bae13265b32a Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Wed, 1 May 2024 21:57:47 +0700 Subject: [PATCH 20/25] remove incorrect Close() --- store/snapshots/store.go | 1 - 1 file changed, 1 deletion(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 7d2ec1aa501b..9d1ba831bc00 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -149,7 +149,6 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R chunk, err := s.loadChunkFile(height, format, i) if err != nil { _ = pw.CloseWithError(err) - _ = chunk.Close() return } err = func() error { From e9df29ba63942228c53d73c8fa0e97717f9489db Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 2 May 2024 08:36:44 +0700 Subject: [PATCH 21/25] refactor --- store/snapshots/store.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index 9d1ba831bc00..ec1d70c700f2 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -156,11 +156,13 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R if _, err := io.Copy(pw, chunk); err != nil { return fmt.Errorf("failed to copy chunk %d: %w", i, err) } - _ = pw.Close() - return nil + if err != nil { + _ = pw.CloseWithError(err) + return err + } + return pw.Close() }() if err != nil { - _ = pw.CloseWithError(err) return } } From 870112717355b0f77ce99dc2b11935319eb70335 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 2 May 2024 08:46:02 +0700 Subject: [PATCH 22/25] fix redundant err check --- store/snapshots/store.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index ec1d70c700f2..f5dd345cb4b1 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -153,13 +153,12 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R } err = func() error { defer chunk.Close() + if _, err := io.Copy(pw, chunk); err != nil { - return fmt.Errorf("failed to copy chunk %d: %w", i, err) - } - if err != nil { _ = pw.CloseWithError(err) - return err + return fmt.Errorf("failed to copy chunk %d: %w", i, err) } + return pw.Close() }() if err != nil { From 0415482c88280b4acaf4d023a4f115aa3013514c Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 9 May 2024 21:43:58 +0700 Subject: [PATCH 23/25] add missing import --- store/snapshots/store.go | 1 + 1 file changed, 1 insertion(+) diff --git a/store/snapshots/store.go b/store/snapshots/store.go index f5dd345cb4b1..aa593f5bac61 100644 --- a/store/snapshots/store.go +++ b/store/snapshots/store.go @@ -3,6 +3,7 @@ package snapshots import ( "crypto/sha256" "encoding/binary" + "fmt" "hash" "io" "math" From a737df00766a5ed4ffc39fca6dd08fbf300f5e80 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Thu, 9 May 2024 23:45:03 +0700 Subject: [PATCH 24/25] add err handling --- x/group/keeper/msg_server.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/group/keeper/msg_server.go b/x/group/keeper/msg_server.go index f861cd111ca2..d9fc902aca5b 100644 --- a/x/group/keeper/msg_server.go +++ b/x/group/keeper/msg_server.go @@ -631,7 +631,10 @@ func (k Keeper) SubmitProposal(ctx context.Context, msg *group.MsgSubmitProposal if msg.Exec == group.Exec_EXEC_TRY { // Consider proposers as Yes votes for _, proposer := range msg.Proposers { - k.GasService.GasMeter(ctx).Consume(gasCostPerIteration, "vote on proposal") + err = k.GasService.GasMeter(ctx).Consume(gasCostPerIteration, "vote on proposal") + if err != nil { + return &group.MsgSubmitProposalResponse{ProposalId: id}, errorsmod.Wrap(err, "the proposal was created but failed on gas consumption for vote") + } _, err = k.Vote(ctx, &group.MsgVote{ ProposalId: id, Voter: proposer, From 101706bf2cb90fb1d05b300404ea73e3f9ba0747 Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Fri, 17 May 2024 22:50:52 +0700 Subject: [PATCH 25/25] lint errorf --- x/group/keeper/invariants.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/group/keeper/invariants.go b/x/group/keeper/invariants.go index 09f73a970e18..d3f47201a208 100644 --- a/x/group/keeper/invariants.go +++ b/x/group/keeper/invariants.go @@ -74,7 +74,7 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV err = func() error { memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id) if err != nil { - return fmt.Errorf("error while returning group member iterator for group with ID %d\n%v", groupInfo.Id, err) + return fmt.Errorf("error while returning group member iterator for group with ID %d\n%w", groupInfo.Id, err) } defer memIt.Close() @@ -85,17 +85,17 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV break } if err != nil { - return fmt.Errorf("LoadNext failure on member table iterator\n%v", err) + return fmt.Errorf("LoadNext failure on member table iterator\n%w", err) } curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight()) if err != nil { - return fmt.Errorf("error while parsing non-nengative decimal for group member %s\n%v", groupMember.Member.Address, err) + return fmt.Errorf("error while parsing non-nengative decimal for group member %s\n%w", groupMember.Member.Address, err) } membersWeight, err = groupmath.Add(membersWeight, curMemWeight) if err != nil { - return fmt.Errorf("decimal addition error while adding group member voting weight to total voting weight\n%v", err) + return fmt.Errorf("decimal addition error while adding group member voting weight to total voting weight\n%w", err) } } return nil