diff --git a/x/multi-staking/keeper/store.go b/x/multi-staking/keeper/store.go index 47fd6ce4..df56c5ae 100644 --- a/x/multi-staking/keeper/store.go +++ b/x/multi-staking/keeper/store.go @@ -155,7 +155,7 @@ func (k Keeper) BondWeightIterator(ctx sdk.Context, cb func(denom string, bondWe func (k Keeper) GetMultiStakingUnlock(ctx sdk.Context, multiStakingUnlockID types.UnlockID) (unlock types.MultiStakingUnlock, found bool) { store := ctx.KVStore(k.storeKey) - value := store.Get(multiStakingUnlockID.ToBytes()) + value := store.Get(multiStakingUnlockID.ToByte()) if value == nil { return unlock, false @@ -173,11 +173,11 @@ func (k Keeper) SetMultiStakingUnlock(ctx sdk.Context, unlock types.MultiStaking bz := k.cdc.MustMarshal(&unlock) - store.Set(unlock.UnlockID.ToBytes(), bz) + store.Set(unlock.UnlockID.ToByte(), bz) } func (k Keeper) DeleteMultiStakingUnlock(ctx sdk.Context, unlockID types.UnlockID) { store := ctx.KVStore(k.storeKey) - store.Delete(unlockID.ToBytes()) + store.Delete(unlockID.ToByte()) } diff --git a/x/multi-staking/types/key.go b/x/multi-staking/types/key.go index 3d21ec53..338a50fb 100644 --- a/x/multi-staking/types/key.go +++ b/x/multi-staking/types/key.go @@ -1,6 +1,8 @@ package types import ( + "encoding/json" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -54,24 +56,26 @@ func MultiStakingUnlockID(multiStakerAddr string, valAddr string) UnlockID { return UnlockID{MultiStakerAddr: multiStakerAddr, ValAddr: valAddr} } -func DelAddrAndValAddrFromLockID(lockID []byte) (multiStakerAddr sdk.AccAddress, valAddr sdk.ValAddress) { - lenMultiStakerAddr := lockID[0] - - multiStakerAddr = lockID[1 : lenMultiStakerAddr+1] +func DelAddrAndValAddrFromLockID(lockIDByte []byte) (multiStakerAddr sdk.AccAddress, valAddr sdk.ValAddress, err error) { + var newLockID LockID - valAddr = lockID[1+lenMultiStakerAddr:] + err = json.Unmarshal(lockIDByte, &newLockID) + if err != nil { + return nil, nil, err + } - return multiStakerAddr, valAddr + return AccAddrAndValAddrFromStrings(newLockID.MultiStakerAddr, newLockID.ValAddr) } -func DelAddrAndValAddrFromUnlockID(unlockID []byte) (multiStakerAddr sdk.AccAddress, valAddr sdk.ValAddress) { - lenMultiStakerAddr := unlockID[0] - - multiStakerAddr = unlockID[1 : lenMultiStakerAddr+1] +func DelAddrAndValAddrFromUnlockID(unlockIDByte []byte) (multiStakerAddr sdk.AccAddress, valAddr sdk.ValAddress, err error) { + var newUnlockID UnlockID - valAddr = unlockID[1+lenMultiStakerAddr:] + err = json.Unmarshal(unlockIDByte, &newUnlockID) + if err != nil { + return nil, nil, err + } - return multiStakerAddr, valAddr + return AccAddrAndValAddrFromStrings(newUnlockID.MultiStakerAddr, newUnlockID.ValAddr) } // // GetUBDKey creates the key for an unbonding delegation by delegator and validator addr @@ -81,39 +85,17 @@ func DelAddrAndValAddrFromUnlockID(unlockID []byte) (multiStakerAddr sdk.AccAddr // } func (l LockID) ToByte() []byte { - multiStakerAddr, valAcc, err := AccAddrAndValAddrFromStrings(l.MultiStakerAddr, l.ValAddr) + bz, err := json.Marshal(l) if err != nil { - panic(err) + panic("can not Marshal") } - - lenMultiStakerAddr := len(multiStakerAddr) - - DVPair := make([]byte, 1+lenMultiStakerAddr+len(valAcc)) - - DVPair[0] = uint8(lenMultiStakerAddr) - - copy(multiStakerAddr[:], DVPair[1:]) - - copy(multiStakerAddr[:], DVPair[1+lenMultiStakerAddr:]) - - return append(MultiStakingLockPrefix, DVPair...) + return bz } -func (l UnlockID) ToBytes() []byte { - multiStakerAddr, valAcc, err := AccAddrAndValAddrFromStrings(l.MultiStakerAddr, l.ValAddr) +func (l UnlockID) ToByte() []byte { + bz, err := json.Marshal(l) if err != nil { - panic(err) + panic("can not Marshal") } - - lenMultiStakerAddr := len(multiStakerAddr) - - DVPair := make([]byte, 1+lenMultiStakerAddr+len(valAcc)) - - DVPair[0] = uint8(lenMultiStakerAddr) - - copy(multiStakerAddr[:], DVPair[1:]) - - copy(multiStakerAddr[:], DVPair[1+lenMultiStakerAddr:]) - - return append(MultiStakingLockPrefix, DVPair...) + return bz } diff --git a/x/multi-staking/types/key_test.go b/x/multi-staking/types/key_test.go new file mode 100644 index 00000000..0970eb07 --- /dev/null +++ b/x/multi-staking/types/key_test.go @@ -0,0 +1,42 @@ +package types_test + +import ( + "testing" + + "github.com/realio-tech/multi-staking-module/testutil" + mulStakingtypes "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 := mulStakingtypes.MultiStakingLockID(del.String(), val.String()) + + toByte := lockID.ToByte() + + rsDel, rsVal, err := mulStakingtypes.DelAddrAndValAddrFromLockID(toByte) + require.NoError(t, err) + require.Equal(t, del, rsDel) + require.Equal(t, val, rsVal) +} + +func TestMultiStakingLockIterator(t *testing.T) { + val := testutil.GenValAddress() + del1 := testutil.GenAddress() + del2 := testutil.GenAddress() + + lockID1 := mulStakingtypes.LockID{ + MultiStakerAddr: del1.String(), + ValAddr: val.String(), + } + + lockID2 := mulStakingtypes.LockID{ + MultiStakerAddr: del2.String(), + ValAddr: val.String(), + } + + require.NotEqual(t, lockID1, lockID2) + require.NotEqual(t, lockID1.ToByte(), lockID2.ToByte()) +}