From 870d080972874df42881ebccd76afc0bc4154c1b Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Wed, 26 Jul 2023 14:39:52 +0200 Subject: [PATCH 1/4] fix: DiffCollectionsMigration hash function not used correctly --- testutil/collections.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testutil/collections.go b/testutil/collections.go index 2354943de77a..ceed1de8d80d 100644 --- a/testutil/collections.go +++ b/testutil/collections.go @@ -66,7 +66,7 @@ func DiffCollectionsMigration( h.Write(it.Value()) } - hash := sha256.Sum256(nil) + hash := h.Sum(nil) if hex.EncodeToString(hash[:]) != targetHash { return fmt.Errorf("hashes don't match: %s != %s", hex.EncodeToString(hash[:]), targetHash) } From b93c4af22c7652aea8cd7c41d1a863a7dbab223a Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Wed, 26 Jul 2023 14:50:10 +0200 Subject: [PATCH 2/4] add test --- testutil/collections_test.go | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 testutil/collections_test.go diff --git a/testutil/collections_test.go b/testutil/collections_test.go new file mode 100644 index 000000000000..02fa8d8e1429 --- /dev/null +++ b/testutil/collections_test.go @@ -0,0 +1,55 @@ +package testutil_test + +import ( + "testing" + + "cosmossdk.io/store/types" + storetypes "cosmossdk.io/store/types" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/testutil" +) + +func TestDiffCollectionsMigration(t *testing.T) { + key := storetypes.NewKVStoreKey("test") + ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient")) + + // First try with some invalid hash + err := testutil.DiffCollectionsMigration( + ctx, + key, + 5, + func(i int64) { + ctx.KVStore(key).Set([]byte{byte(i)}, []byte{byte(i)}) + }, + "abcdef0123456789", + ) + require.Error(t, err) + + // Now reset and try with the correct hash + ctx = testutil.DefaultContext(key, types.NewTransientStoreKey("transient")) + err = testutil.DiffCollectionsMigration( + ctx, + key, + 5, + func(i int64) { + ctx.KVStore(key).Set([]byte{byte(i)}, []byte{byte(i)}) + }, + "79541ed9da9c16cb7a1d43d5a3d5f6ee31a873c85a6cb4334fb99e021ee0e556", + ) + require.NoError(t, err) + + // Change the data a little and it will result in an error + ctx = testutil.DefaultContext(key, types.NewTransientStoreKey("transient")) + err = testutil.DiffCollectionsMigration( + ctx, + key, + 5, + func(i int64) { + ctx.KVStore(key).Set([]byte{byte(i)}, []byte{byte(i + 1)}) + }, + "79541ed9da9c16cb7a1d43d5a3d5f6ee31a873c85a6cb4334fb99e021ee0e556", + ) + require.Error(t, err) +} From 1e0737d4036089d1ad58d98fee8a2227503c2afd Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Wed, 26 Jul 2023 15:22:55 +0200 Subject: [PATCH 3/4] lint --- testutil/collections.go | 4 ++-- testutil/collections_test.go | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/testutil/collections.go b/testutil/collections.go index ceed1de8d80d..82ef1858d3bd 100644 --- a/testutil/collections.go +++ b/testutil/collections.go @@ -67,8 +67,8 @@ func DiffCollectionsMigration( } hash := h.Sum(nil) - if hex.EncodeToString(hash[:]) != targetHash { - return fmt.Errorf("hashes don't match: %s != %s", hex.EncodeToString(hash[:]), targetHash) + if hex.EncodeToString(hash) != targetHash { + return fmt.Errorf("hashes don't match: %s != %s", hex.EncodeToString(hash), targetHash) } return nil diff --git a/testutil/collections_test.go b/testutil/collections_test.go index 02fa8d8e1429..d77cb807cf69 100644 --- a/testutil/collections_test.go +++ b/testutil/collections_test.go @@ -3,7 +3,6 @@ package testutil_test import ( "testing" - "cosmossdk.io/store/types" storetypes "cosmossdk.io/store/types" "github.com/stretchr/testify/require" @@ -13,7 +12,7 @@ import ( func TestDiffCollectionsMigration(t *testing.T) { key := storetypes.NewKVStoreKey("test") - ctx := testutil.DefaultContext(key, types.NewTransientStoreKey("transient")) + ctx := testutil.DefaultContext(key, storetypes.NewTransientStoreKey("transient")) // First try with some invalid hash err := testutil.DiffCollectionsMigration( @@ -28,7 +27,7 @@ func TestDiffCollectionsMigration(t *testing.T) { require.Error(t, err) // Now reset and try with the correct hash - ctx = testutil.DefaultContext(key, types.NewTransientStoreKey("transient")) + ctx = testutil.DefaultContext(key, storetypes.NewTransientStoreKey("transient")) err = testutil.DiffCollectionsMigration( ctx, key, @@ -41,7 +40,7 @@ func TestDiffCollectionsMigration(t *testing.T) { require.NoError(t, err) // Change the data a little and it will result in an error - ctx = testutil.DefaultContext(key, types.NewTransientStoreKey("transient")) + ctx = testutil.DefaultContext(key, storetypes.NewTransientStoreKey("transient")) err = testutil.DiffCollectionsMigration( ctx, key, From f867b65347c52e4fe28858a7028f905c23e9de23 Mon Sep 17 00:00:00 2001 From: Facundo Medica Date: Wed, 26 Jul 2023 18:12:24 +0200 Subject: [PATCH 4/4] lint --- testutil/collections_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testutil/collections_test.go b/testutil/collections_test.go index d77cb807cf69..a35533539efc 100644 --- a/testutil/collections_test.go +++ b/testutil/collections_test.go @@ -3,10 +3,10 @@ package testutil_test import ( "testing" - storetypes "cosmossdk.io/store/types" - "github.com/stretchr/testify/require" + storetypes "cosmossdk.io/store/types" + "github.com/cosmos/cosmos-sdk/testutil" )