Skip to content

Commit

Permalink
for consistency woth other methods print in the logs grenter address,…
Browse files Browse the repository at this point in the history
… grantee address and the msg type
  • Loading branch information
EmilGeorgiev committed Apr 3, 2024
1 parent a1d24a0 commit 90510d7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 53 deletions.
3 changes: 2 additions & 1 deletion x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 0 additions & 30 deletions x/authz/keeper/keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"fmt"
"time"

"cosmossdk.io/x/authz"
Expand Down Expand Up @@ -105,32 +104,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<prefix(1 Byte)><granterAddressLen(1 Byte)><granterAddress_Bytes><granteeAddressLen(1 Byte)><granteeAddress_Bytes><msgType_Bytes>
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)
}
24 changes: 2 additions & 22 deletions x/authz/keeper/keys_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package keeper

import (
bank "cosmossdk.io/x/bank/types"
"github.com/stretchr/testify/require"
"testing"
"time"
"unicode/utf8"

"github.com/stretchr/testify/require"

bank "cosmossdk.io/x/bank/types"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -41,20 +38,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))
}

0 comments on commit 90510d7

Please sign in to comment.