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
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
}

// AppendLengthPrefixedBytes combines the slices of bytes to one slice of bytes.
func AppendLengthPrefixedBytes(args ...[]byte) []byte {
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
}

// ParseLengthPrefixedBytes panics when store key length is not equal to the given length.
func ParseLengthPrefixedBytes(key []byte, startIndex int, sliceLength int) ([]byte, int) {
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.AppendLengthPrefixedBytes(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.ParseLengthPrefixedBytes(combinedBytes, 0, 1)
parseTest1, parseTest1EndIndex := sdk.ParseLengthPrefixedBytes(combinedBytes, test1LenEndIndex+1, int(test1Len[0]))
s.Require().Equal(testByte1, parseTest1)

test2Len, test2LenEndIndex := sdk.ParseLengthPrefixedBytes(combinedBytes, parseTest1EndIndex+1, 1)
parseTest2, _ := sdk.ParseLengthPrefixedBytes(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.AppendLengthPrefixedBytes(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.ParseLengthPrefixedBytes(key, 1, 1) // ignore key[0] since it is a prefix key
granterAddr, granterAddrEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrLenEndIndex+1, int(granterAddrLen[0]))

granteeAddrLen, granteeAddrLenEndIndex := sdk.ParseLengthPrefixedBytes(key, granterAddrEndIndex+1, 1)
granteeAddr, granteeAddrEndIndex := sdk.ParseLengthPrefixedBytes(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.ParseLengthPrefixedBytes(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.ParseLengthPrefixedBytes(key, expEndIndex+1, 1)
granter, granterEndIndex := sdk.ParseLengthPrefixedBytes(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.ParseLengthPrefixedBytes(key, granterEndIndex+1, 1)
grantee, _ := sdk.ParseLengthPrefixedBytes(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.AppendLengthPrefixedBytes(GrantQueuePrefix, exp, granter, grantee)
}

// GrantQueueTimePrefix - return grant queue time prefix
Expand Down