Skip to content

Commit

Permalink
!fix: fix LockID.ToBytes and UnlockID.ToBytes (#108)
Browse files Browse the repository at this point in the history
* Fix key prefix and add key tests

* fix lint
  • Loading branch information
neitdung authored Jan 16, 2024
1 parent d96beeb commit 75dd430
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
6 changes: 3 additions & 3 deletions x/multi-staking/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (k Keeper) ValidatorMultiStakingCoinIterator(ctx sdk.Context, cb func(valAd
func (k Keeper) GetMultiStakingLock(ctx sdk.Context, multiStakingLockID types.LockID) (types.MultiStakingLock, bool) {
store := ctx.KVStore(k.storeKey)

bz := store.Get(multiStakingLockID.ToByte())
bz := store.Get(multiStakingLockID.ToBytes())
if bz == nil {
return types.MultiStakingLock{}, false
}
Expand All @@ -95,13 +95,13 @@ func (k Keeper) SetMultiStakingLock(ctx sdk.Context, multiStakingLock types.Mult

bz := k.cdc.MustMarshal(&multiStakingLock)

store.Set(multiStakingLock.LockID.ToByte(), bz)
store.Set(multiStakingLock.LockID.ToBytes(), bz)
}

func (k Keeper) RemoveMultiStakingLock(ctx sdk.Context, multiStakingLockID types.LockID) {
store := ctx.KVStore(k.storeKey)

store.Delete(multiStakingLockID.ToByte())
store.Delete(multiStakingLockID.ToBytes())
}

func (k Keeper) MultiStakingLockIterator(ctx sdk.Context, cb func(stakingLock types.MultiStakingLock) (stop bool)) {
Expand Down
24 changes: 12 additions & 12 deletions x/multi-staking/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ func MultiStakingUnlockID(multiStakerAddr string, valAddr string) UnlockID {
}

func DelAddrAndValAddrFromLockID(lockID []byte) (multiStakerAddr sdk.AccAddress, valAddr sdk.ValAddress) {
lenMultiStakerAddr := lockID[0]
lenMultiStakerAddr := lockID[1]

multiStakerAddr = lockID[1 : lenMultiStakerAddr+1]
multiStakerAddr = lockID[2 : lenMultiStakerAddr+2]

valAddr = lockID[1+lenMultiStakerAddr:]
valAddr = lockID[2+lenMultiStakerAddr:]

return multiStakerAddr, valAddr
}

func DelAddrAndValAddrFromUnlockID(unlockID []byte) (multiStakerAddr sdk.AccAddress, valAddr sdk.ValAddress) {
lenMultiStakerAddr := unlockID[0]
lenMultiStakerAddr := unlockID[1]

multiStakerAddr = unlockID[1 : lenMultiStakerAddr+1]
multiStakerAddr = unlockID[2 : lenMultiStakerAddr+2]

valAddr = unlockID[1+lenMultiStakerAddr:]
valAddr = unlockID[2+lenMultiStakerAddr:]

return multiStakerAddr, valAddr
}
Expand All @@ -80,7 +80,7 @@ func DelAddrAndValAddrFromUnlockID(unlockID []byte) (multiStakerAddr sdk.AccAddr
// return append(GetUBDsKey(delAddr.Bytes()), address.MustLengthPrefix(valAddr)...)
// }

func (l LockID) ToByte() []byte {
func (l LockID) ToBytes() []byte {
multiStakerAddr, valAcc, err := AccAddrAndValAddrFromStrings(l.MultiStakerAddr, l.ValAddr)
if err != nil {
panic(err)
Expand All @@ -92,9 +92,9 @@ func (l LockID) ToByte() []byte {

DVPair[0] = uint8(lenMultiStakerAddr)

copy(multiStakerAddr[:], DVPair[1:])
copy(DVPair[1:], multiStakerAddr[:])

copy(multiStakerAddr[:], DVPair[1+lenMultiStakerAddr:])
copy(DVPair[1+lenMultiStakerAddr:], valAcc[:])

return append(MultiStakingLockPrefix, DVPair...)
}
Expand All @@ -111,9 +111,9 @@ func (l UnlockID) ToBytes() []byte {

DVPair[0] = uint8(lenMultiStakerAddr)

copy(multiStakerAddr[:], DVPair[1:])
copy(DVPair[1:], multiStakerAddr[:])

copy(multiStakerAddr[:], DVPair[1+lenMultiStakerAddr:])
copy(DVPair[1+lenMultiStakerAddr:], valAcc[:])

return append(MultiStakingLockPrefix, DVPair...)
return append(MultiStakingUnlockPrefix, DVPair...)
}
40 changes: 40 additions & 0 deletions x/multi-staking/types/key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types_test

import (
"testing"

"github.com/realio-tech/multi-staking-module/testutil"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
"github.com/stretchr/testify/require"
)

func TestDelAddrAndValAddrFromLockID(t *testing.T) {
val := testutil.GenValAddress()
del := testutil.GenAddress()

lockID := multistakingtypes.MultiStakingLockID(del.String(), val.String())
lockBytes := lockID.ToBytes()
rsDel, rsVal := multistakingtypes.DelAddrAndValAddrFromLockID(lockBytes)

require.Equal(t, del, rsDel)
require.Equal(t, val, rsVal)
}

func TestMultiStakingLockIterator(t *testing.T) {
val := testutil.GenValAddress()
delA := testutil.GenAddress()
delB := testutil.GenAddress()

lockIDA := multistakingtypes.LockID{
MultiStakerAddr: delA.String(),
ValAddr: val.String(),
}

lockIDB := multistakingtypes.LockID{
MultiStakerAddr: delB.String(),
ValAddr: val.String(),
}

require.NotEqual(t, lockIDA, lockIDB)
require.NotEqual(t, lockIDA.ToBytes(), lockIDB.ToBytes())
}

0 comments on commit 75dd430

Please sign in to comment.