Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: (x/authz) add helpers AppendBytes, ParseByteSlice #11713

Merged
merged 7 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/types/kv"
dbm "github.com/tendermint/tm-db"
)

Expand Down Expand Up @@ -106,3 +107,30 @@ func CopyBytes(bz []byte) (ret []byte) {
copy(ret, bz)
return ret
}

// AppendBytes combines the slices of bytes to one slice of bytes.
func AppendBytes(args ...[]byte) []byte {
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
length := 0
for _, v := range args {
length += len(v)
}
res := make([]byte, length)

length = 0
for _, v := range args {
copy(res[length:length+len(v)], v)
length += len(v)
}

return res
}

// ParseByteSlice panics when store key length is not equal to the given length.
func ParseByteSlice(key []byte, startIndex int, sliceLength int) ([]byte, int) {
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
neededLength := startIndex + sliceLength
endIndex := neededLength - 1
kv.AssertKeyAtLeastLength(key, neededLength)
byteSlice := key[startIndex:neededLength]

return byteSlice, endIndex
}
21 changes: 21 additions & 0 deletions types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)

type utilsTestSuite struct {
Expand Down Expand Up @@ -109,3 +110,23 @@ func (s *utilsTestSuite) TestParseTimeBytes() {
_, err = sdk.ParseTimeBytes([]byte{})
s.Require().Error(err)
}

func (s *utilsTestSuite) TestAppendParseBytes() {
test1 := "test1"
test2 := "testString2"
testByte1 := []byte(test1)
testByte2 := []byte(test2)

combinedBytes := sdk.AppendBytes(address.MustLengthPrefix(testByte1), address.MustLengthPrefix(testByte2))
testCombineBytes := append([]byte{}, address.MustLengthPrefix(testByte1)...)
testCombineBytes = append(testCombineBytes, address.MustLengthPrefix(testByte2)...)
s.Require().Equal(combinedBytes, testCombineBytes)

test1Len, test1LenEndIndex := sdk.ParseByteSlice(combinedBytes, 0, 1)
parseTest1, parseTest1EndIndex := sdk.ParseByteSlice(combinedBytes, test1LenEndIndex+1, int(test1Len[0]))
s.Require().Equal(testByte1, parseTest1)

test2Len, test2LenEndIndex := sdk.ParseByteSlice(combinedBytes, parseTest1EndIndex+1, 1)
parseTest2, _ := sdk.ParseByteSlice(combinedBytes, test2LenEndIndex+1, int(test2Len[0]))
s.Require().Equal(testByte2, parseTest2)
}
50 changes: 18 additions & 32 deletions x/authz/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,51 +34,43 @@ func grantStoreKey(grantee sdk.AccAddress, granter sdk.AccAddress, msgType strin
m := conv.UnsafeStrToBytes(msgType)
granter = address.MustLengthPrefix(granter)
grantee = address.MustLengthPrefix(grantee)
key := sdk.AppendBytes(GrantKey, granter, grantee, m)

l := 1 + len(grantee) + len(granter) + len(m)
var key = make([]byte, l)
copy(key, GrantKey)
copy(key[1:], granter)
copy(key[1+len(granter):], grantee)
copy(key[l-len(m):], m)
// fmt.Println(">>>> len", l, key)
return key
}

// parseGrantStoreKey - split granter, grantee address and msg type from the authorization key
func parseGrantStoreKey(key []byte) (granterAddr, granteeAddr sdk.AccAddress, msgType string) {
// key is of format:
// 0x01<granterAddressLen (1 Byte)><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes><msgType_Bytes>
kv.AssertKeyAtLeastLength(key, 2)
granterAddrLen := key[1] // remove prefix key
kv.AssertKeyAtLeastLength(key, int(3+granterAddrLen))
granterAddr = sdk.AccAddress(key[2 : 2+granterAddrLen])
granteeAddrLen := int(key[2+granterAddrLen])
kv.AssertKeyAtLeastLength(key, 4+int(granterAddrLen+byte(granteeAddrLen)))
granteeAddr = sdk.AccAddress(key[3+granterAddrLen : 3+granterAddrLen+byte(granteeAddrLen)])

return granterAddr, granteeAddr, conv.UnsafeBytesToStr(key[3+granterAddrLen+byte(granteeAddrLen):])

granterAddrLen, granterAddrLenEndIndex := sdk.ParseByteSlice(key, 1, 1) // ignore key[0] since it is a prefix key
granterAddr, granterAddrEndIndex := sdk.ParseByteSlice(key, granterAddrLenEndIndex+1, int(granterAddrLen[0]))

granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseByteSlice(key, granterAddrEndIndex+1, 1)
granteeAddr, granteeAddrEndIndex := sdk.ParseByteSlice(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0]))

kv.AssertKeyAtLeastLength(key, granteeAddrEndIndex+1)
return granterAddr, granteeAddr, conv.UnsafeBytesToStr(key[(granteeAddrEndIndex + 1):])
}

// parseGrantQueueKey split expiration time, granter and grantee from the grant queue key
func parseGrantQueueKey(key []byte) (time.Time, sdk.AccAddress, sdk.AccAddress, error) {
// key is of format:
// 0x02<grant_expiration_Bytes><granterAddress_Bytes><granteeAddressLen (1 Byte)><granteeAddress_Bytes>

kv.AssertKeyAtLeastLength(key, 1+lenTime)
exp, err := sdk.ParseTimeBytes(key[1 : 1+lenTime])
expBytes, expEndIndex := sdk.ParseByteSlice(key, 1, lenTime)

exp, err := sdk.ParseTimeBytes(expBytes)
if err != nil {
return exp, nil, nil, err
}

granterAddrLen := key[1+lenTime]
kv.AssertKeyAtLeastLength(key, 1+lenTime+int(granterAddrLen))
granter := sdk.AccAddress(key[2+lenTime : byte(2+lenTime)+granterAddrLen])
granterAddrLen, granterAddrLenEndIndex := sdk.ParseByteSlice(key, expEndIndex+1, 1)
granter, granterEndIndex := sdk.ParseByteSlice(key, granterAddrLenEndIndex+1, int(granterAddrLen[0]))

granteeAddrLen := key[byte(2+lenTime)+granterAddrLen]
granteeStart := byte(3+lenTime) + granterAddrLen
kv.AssertKeyAtLeastLength(key, int(granteeStart))
grantee := sdk.AccAddress(key[granteeStart : granteeStart+granteeAddrLen])
granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseByteSlice(key, granterEndIndex+1, 1)
grantee, _ := sdk.ParseByteSlice(key, granteeAddrLenEndIndex+1, int(granteeAddrLen[0]))

return exp, granter, grantee, nil
}
Expand All @@ -92,13 +84,7 @@ func GrantQueueKey(expiration time.Time, granter sdk.AccAddress, grantee sdk.Acc
granter = address.MustLengthPrefix(granter)
grantee = address.MustLengthPrefix(grantee)

l := 1 + len(exp) + len(granter) + len(grantee)
var key = make([]byte, l)
copy(key, GrantQueuePrefix)
copy(key[1:], exp)
copy(key[1+len(exp):], granter)
copy(key[1+len(exp)+len(granter):], grantee)
return key
return sdk.AppendBytes(GrantQueuePrefix, exp, granter, grantee)
}

// GrantQueueTimePrefix - return grant queue time prefix
Expand Down