Skip to content

Commit

Permalink
write unit test first
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilGeorgiev committed Apr 2, 2024
1 parent 319e6e4 commit ab6cc27
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions x/authz/keeper/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"time"

"cosmossdk.io/x/authz"
Expand Down Expand Up @@ -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)
}
15 changes: 15 additions & 0 deletions x/authz/keeper/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ab6cc27

Please sign in to comment.