From ab6cc27a8d8c609427d19ef0a4c81028641a3001 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Tue, 2 Apr 2024 21:36:34 +0300 Subject: [PATCH 1/8] write unit test first --- x/authz/keeper/keeper.go | 11 +++++++++++ x/authz/keeper/keys.go | 17 +++++++++++++++++ x/authz/keeper/keys_test.go | 15 +++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index a4347e6132d2..20c967369087 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -217,6 +217,17 @@ func (k Keeper) SaveGrant(ctx context.Context, grantee, granter sdk.AccAddress, // by the granter. func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress, msgType string) error { store := k.environment.KVStoreService.OpenKVStore(ctx) + fmt.Println(grantee.Bytes()) //[165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 1] + fmt.Println(granter.Bytes()) // [165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 0] + + fmt.Println(grantee.String()) + fmt.Println(granter.String()) + + //[165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 1] + // [165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 0] + // cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj + // cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq + skey := grantStoreKey(grantee, granter, msgType) grant, found := k.getGrant(ctx, skey) if !found { diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index c268a83e6079..defd449aa659 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -1,6 +1,7 @@ package keeper import ( + "fmt" "time" "cosmossdk.io/x/authz" @@ -98,3 +99,19 @@ func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress { addrLen := key[0] return sdk.AccAddress(key[1 : 1+addrLen]) } + +func grantKeyToString(skey []byte) string { + prefix := skey[0] + granterAddressLen := skey[1] + var ll = int(granterAddressLen) + granterAddressBytes := skey[2 : 2+ll] + granteeAddressLen := skey[2+ll+1] + var ll2 = int(granteeAddressLen) + granteeAddressBytes := skey[2+ll+1 : 2+ll+1+ll2] + msgTypeBytes := skey[2+ll+1+ll2:] + + granterAddr := sdk.AccAddress(granterAddressBytes) + granteeAddr := sdk.AccAddress(granteeAddressBytes) + + return fmt.Sprintf("%d %d %s %d %s %s", prefix, ll, granterAddr.String(), ll2, granteeAddr.String(), msgTypeBytes) +} diff --git a/x/authz/keeper/keys_test.go b/x/authz/keeper/keys_test.go index 9438f7c98b12..3b1bb3d829ee 100644 --- a/x/authz/keeper/keys_test.go +++ b/x/authz/keeper/keys_test.go @@ -40,3 +40,18 @@ func TestGrantQueueKey(t *testing.T) { require.Equal(t, granter, granter1) require.Equal(t, grantee, grantee1) } + +func TestParseGrantStoreKey(t *testing.T) { + // SetUp + granteeAddr, _ := sdk.AccAddressFromBech32("cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj") + granterAddr, _ := sdk.AccAddressFromBech32("cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq") + msg := "/cosmos.bank.v1beta1.MsgSend" + skey := grantStoreKey(granteeAddr, granterAddr, msg) + + // Action + actual := grantKeyToString(skey) + + // Assert + expected := "1|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj|/cosmos.bank.v1beta1.MsgSend" + require.Equal(t, expected, actual) +} From fdbe314b487b11c3eec93684212d6855021992a2 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Tue, 2 Apr 2024 21:47:14 +0300 Subject: [PATCH 2/8] implement parsing function that convert grant key to UTF-8 valid string --- x/authz/keeper/keys.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index defd449aa659..8bbc0fff9a80 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -105,7 +105,7 @@ func grantKeyToString(skey []byte) string { granterAddressLen := skey[1] var ll = int(granterAddressLen) granterAddressBytes := skey[2 : 2+ll] - granteeAddressLen := skey[2+ll+1] + granteeAddressLen := skey[2+ll] var ll2 = int(granteeAddressLen) granteeAddressBytes := skey[2+ll+1 : 2+ll+1+ll2] msgTypeBytes := skey[2+ll+1+ll2:] @@ -113,5 +113,5 @@ func grantKeyToString(skey []byte) string { granterAddr := sdk.AccAddress(granterAddressBytes) granteeAddr := sdk.AccAddress(granteeAddressBytes) - return fmt.Sprintf("%d %d %s %d %s %s", prefix, ll, granterAddr.String(), ll2, granteeAddr.String(), msgTypeBytes) + return fmt.Sprintf("%d|%d|%s|%d|%s|%s", prefix, ll, granterAddr.String(), ll2, granteeAddr.String(), msgTypeBytes) } From c61670e1aa20f0b1f1ee3d9e0051f25f1d54210b Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Tue, 2 Apr 2024 21:49:07 +0300 Subject: [PATCH 3/8] remove unnecessary debug fmt.Println --- x/authz/keeper/keeper.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 20c967369087..a4347e6132d2 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -217,17 +217,6 @@ func (k Keeper) SaveGrant(ctx context.Context, grantee, granter sdk.AccAddress, // by the granter. func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress, msgType string) error { store := k.environment.KVStoreService.OpenKVStore(ctx) - fmt.Println(grantee.Bytes()) //[165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 1] - fmt.Println(granter.Bytes()) // [165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 0] - - fmt.Println(grantee.String()) - fmt.Println(granter.String()) - - //[165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 1] - // [165 136 86 240 253 83 191 5 139 73 9 162 26 236 1 145 7 186 97 0] - // cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj - // cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq - skey := grantStoreKey(grantee, granter, msgType) grant, found := k.getGrant(ctx, skey) if !found { From 5b5e64b507edf619b1f296810dd2bd656a555c29 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Tue, 2 Apr 2024 22:06:38 +0300 Subject: [PATCH 4/8] refactor the function for parsing grsnt key to valid utf-8 string --- x/authz/keeper/keys.go | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index 8bbc0fff9a80..f222c03dc0f9 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -24,8 +24,14 @@ var ( var lenTime = len(sdk.FormatTimeBytes(time.Now())) -// StoreKey is the store key string for authz -const StoreKey = authz.ModuleName +const ( + // StoreKey is the store key string for authz + StoreKey = authz.ModuleName + + bytePositionOfGrantKeyPrefix = 0 + bytePositionOfGranterAddressLen = 1 + omitTwoBytes = 2 +) // grantStoreKey - return authorization store key // Items are stored with the following key: values @@ -101,17 +107,27 @@ func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress { } func grantKeyToString(skey []byte) string { - prefix := skey[0] - granterAddressLen := skey[1] - var ll = int(granterAddressLen) - granterAddressBytes := skey[2 : 2+ll] - granteeAddressLen := skey[2+ll] - var ll2 = int(granteeAddressLen) - granteeAddressBytes := skey[2+ll+1 : 2+ll+1+ll2] - msgTypeBytes := skey[2+ll+1+ll2:] - + // get grant key prefix + prefix := skey[bytePositionOfGrantKeyPrefix] + + // get granter address len and granter address + granterAddressLen := int(skey[bytePositionOfGranterAddressLen]) + startByteIndex := omitTwoBytes + endByteIndex := startByteIndex + granterAddressLen + granterAddressBytes := skey[startByteIndex:endByteIndex] + + // get grantee address len and grantee address + granteeAddressLen := int(skey[endByteIndex]) + startByteIndex = endByteIndex + 1 + endByteIndex = startByteIndex + granteeAddressLen + granteeAddressBytes := skey[startByteIndex:endByteIndex] + + // get message type + startByteIndex = endByteIndex + msgTypeBytes := skey[startByteIndex:] + + // build UTF-8 encoded string granterAddr := sdk.AccAddress(granterAddressBytes) granteeAddr := sdk.AccAddress(granteeAddressBytes) - - return fmt.Sprintf("%d|%d|%s|%d|%s|%s", prefix, ll, granterAddr.String(), ll2, granteeAddr.String(), msgTypeBytes) + return fmt.Sprintf("%d|%d|%s|%d|%s|%s", prefix, granterAddressLen, granterAddr.String(), granteeAddressLen, granteeAddr.String(), msgTypeBytes) } From a1d24a064cf87603c0398e8c98ed4f82cdd7ecb2 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Tue, 2 Apr 2024 22:20:35 +0300 Subject: [PATCH 5/8] add comments --- x/authz/keeper/keys.go | 9 ++++++--- x/authz/keeper/keys_test.go | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index f222c03dc0f9..0c89af07c96d 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -106,23 +106,26 @@ func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress { return sdk.AccAddress(key[1 : 1+addrLen]) } +// grantKeyToString converts a byte slice representing a grant key into a human-readable UTF-8 encoded string. +// The expected format of the byte slice is as follows: +// 0x01 func grantKeyToString(skey []byte) string { // get grant key prefix prefix := skey[bytePositionOfGrantKeyPrefix] - // get granter address len and granter address + // get granter address len and granter address, found at a specified position in the byte slice granterAddressLen := int(skey[bytePositionOfGranterAddressLen]) startByteIndex := omitTwoBytes endByteIndex := startByteIndex + granterAddressLen granterAddressBytes := skey[startByteIndex:endByteIndex] - // get grantee address len and grantee address + // get grantee address len and grantee address, found at a specified position in the byte slice granteeAddressLen := int(skey[endByteIndex]) startByteIndex = endByteIndex + 1 endByteIndex = startByteIndex + granteeAddressLen granteeAddressBytes := skey[startByteIndex:endByteIndex] - // get message type + // get message type, start from the specific byte to the end startByteIndex = endByteIndex msgTypeBytes := skey[startByteIndex:] diff --git a/x/authz/keeper/keys_test.go b/x/authz/keeper/keys_test.go index 3b1bb3d829ee..51f8d7d06a20 100644 --- a/x/authz/keeper/keys_test.go +++ b/x/authz/keeper/keys_test.go @@ -3,6 +3,7 @@ package keeper import ( "testing" "time" + "unicode/utf8" "github.com/stretchr/testify/require" @@ -47,6 +48,7 @@ func TestParseGrantStoreKey(t *testing.T) { granterAddr, _ := sdk.AccAddressFromBech32("cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq") msg := "/cosmos.bank.v1beta1.MsgSend" skey := grantStoreKey(granteeAddr, granterAddr, msg) + require.False(t, utf8.Valid(skey)) // Action actual := grantKeyToString(skey) @@ -54,4 +56,5 @@ func TestParseGrantStoreKey(t *testing.T) { // Assert expected := "1|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj|/cosmos.bank.v1beta1.MsgSend" require.Equal(t, expected, actual) + require.True(t, utf8.ValidString(actual)) } From a9b1bc5dfc3161a1b277d7206e4372c99c12ca9f Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Wed, 3 Apr 2024 06:57:18 +0300 Subject: [PATCH 6/8] for consistency woth other methods print in the logs grenter address, grantee address and the msg type --- x/authz/keeper/keeper.go | 3 ++- x/authz/keeper/keys.go | 40 ++----------------------------------- x/authz/keeper/keys_test.go | 18 ----------------- 3 files changed, 4 insertions(+), 57 deletions(-) diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index a4347e6132d2..2826e32a1b74 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -220,7 +220,8 @@ func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress skey := grantStoreKey(grantee, granter, msgType) grant, found := k.getGrant(ctx, skey) if !found { - return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, "failed to delete grant with key %s", string(skey)) + return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, + "failed to delete grant with key %s given granter: %s, grantee: %s & msgType: %s ", granter.String(), grantee.String(), msgType) } if grant.Expiration != nil { diff --git a/x/authz/keeper/keys.go b/x/authz/keeper/keys.go index 0c89af07c96d..c268a83e6079 100644 --- a/x/authz/keeper/keys.go +++ b/x/authz/keeper/keys.go @@ -1,7 +1,6 @@ package keeper import ( - "fmt" "time" "cosmossdk.io/x/authz" @@ -24,14 +23,8 @@ var ( var lenTime = len(sdk.FormatTimeBytes(time.Now())) -const ( - // StoreKey is the store key string for authz - StoreKey = authz.ModuleName - - bytePositionOfGrantKeyPrefix = 0 - bytePositionOfGranterAddressLen = 1 - omitTwoBytes = 2 -) +// StoreKey is the store key string for authz +const StoreKey = authz.ModuleName // grantStoreKey - return authorization store key // Items are stored with the following key: values @@ -105,32 +98,3 @@ func firstAddressFromGrantStoreKey(key []byte) sdk.AccAddress { addrLen := key[0] return sdk.AccAddress(key[1 : 1+addrLen]) } - -// grantKeyToString converts a byte slice representing a grant key into a human-readable UTF-8 encoded string. -// The expected format of the byte slice is as follows: -// 0x01 -func grantKeyToString(skey []byte) string { - // get grant key prefix - prefix := skey[bytePositionOfGrantKeyPrefix] - - // get granter address len and granter address, found at a specified position in the byte slice - granterAddressLen := int(skey[bytePositionOfGranterAddressLen]) - startByteIndex := omitTwoBytes - endByteIndex := startByteIndex + granterAddressLen - granterAddressBytes := skey[startByteIndex:endByteIndex] - - // get grantee address len and grantee address, found at a specified position in the byte slice - granteeAddressLen := int(skey[endByteIndex]) - startByteIndex = endByteIndex + 1 - endByteIndex = startByteIndex + granteeAddressLen - granteeAddressBytes := skey[startByteIndex:endByteIndex] - - // get message type, start from the specific byte to the end - startByteIndex = endByteIndex - msgTypeBytes := skey[startByteIndex:] - - // build UTF-8 encoded string - granterAddr := sdk.AccAddress(granterAddressBytes) - granteeAddr := sdk.AccAddress(granteeAddressBytes) - return fmt.Sprintf("%d|%d|%s|%d|%s|%s", prefix, granterAddressLen, granterAddr.String(), granteeAddressLen, granteeAddr.String(), msgTypeBytes) -} diff --git a/x/authz/keeper/keys_test.go b/x/authz/keeper/keys_test.go index 51f8d7d06a20..9438f7c98b12 100644 --- a/x/authz/keeper/keys_test.go +++ b/x/authz/keeper/keys_test.go @@ -3,7 +3,6 @@ package keeper import ( "testing" "time" - "unicode/utf8" "github.com/stretchr/testify/require" @@ -41,20 +40,3 @@ func TestGrantQueueKey(t *testing.T) { require.Equal(t, granter, granter1) require.Equal(t, grantee, grantee1) } - -func TestParseGrantStoreKey(t *testing.T) { - // SetUp - granteeAddr, _ := sdk.AccAddressFromBech32("cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj") - granterAddr, _ := sdk.AccAddressFromBech32("cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq") - msg := "/cosmos.bank.v1beta1.MsgSend" - skey := grantStoreKey(granteeAddr, granterAddr, msg) - require.False(t, utf8.Valid(skey)) - - // Action - actual := grantKeyToString(skey) - - // Assert - expected := "1|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq|20|cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgp0ctjdj|/cosmos.bank.v1beta1.MsgSend" - require.Equal(t, expected, actual) - require.True(t, utf8.ValidString(actual)) -} From ebd7df10421f86f26a19e8cac7967f818e2f7ee8 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Wed, 3 Apr 2024 08:10:50 +0300 Subject: [PATCH 7/8] remove unnecessary '%s' in the format string --- x/authz/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 2826e32a1b74..3feafa900325 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -221,7 +221,7 @@ func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress grant, found := k.getGrant(ctx, skey) if !found { return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, - "failed to delete grant with key %s given granter: %s, grantee: %s & msgType: %s ", granter.String(), grantee.String(), msgType) + "failed to delete grant with given granter: %s, grantee: %s & msgType: %s ", granter.String(), grantee.String(), msgType) } if grant.Expiration != nil { From 8b393f0c605dffa0f03c8d11313c88cdb6431479 Mon Sep 17 00:00:00 2001 From: Emil Georgiev Date: Fri, 5 Apr 2024 05:25:15 +0300 Subject: [PATCH 8/8] update the changelog.md file --- x/authz/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x/authz/CHANGELOG.md b/x/authz/CHANGELOG.md index 0963382c9e03..2caad8f47eb1 100644 --- a/x/authz/CHANGELOG.md +++ b/x/authz/CHANGELOG.md @@ -43,3 +43,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Consensus Breaking Changes * [#19188](https://github.com/cosmos/cosmos-sdk/pull/19188) Remove creation of `BaseAccount` when sending a message to an account that does not exist. + +### Bug Fixes + +* [#19874](https://github.com/cosmos/cosmos-sdk/pull/19923) Now when querying transaction events (cosmos.tx.v1beta1.Service/GetTxsEvent) the response will contains only UTF-8 characters \ No newline at end of file