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

Dong/fix to byte #106

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions x/multi-staking/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
64 changes: 23 additions & 41 deletions x/multi-staking/types/key.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"encoding/json"

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

Expand Down Expand Up @@ -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
Expand All @@ -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
}
42 changes: 42 additions & 0 deletions x/multi-staking/types/key_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
Loading