Skip to content

Commit

Permalink
Merge branch 'main' into dong/testt
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Jan 16, 2024
2 parents b6bc583 + 4cd2564 commit 56308cf
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 153 deletions.
3 changes: 2 additions & 1 deletion proto/multistaking/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ message GenesisState {
[ (gogoproto.nullable) = false ];
repeated ValidatorMultiStakingCoin validator_multi_staking_coins = 4
[ (gogoproto.nullable) = false ];
cosmos.staking.v1beta1.GenesisState staking_genesis_state = 5;
cosmos.staking.v1beta1.GenesisState staking_genesis_state = 5
[ (gogoproto.nullable) = false ];
}

message MultiStakingCoinInfo {
Expand Down
4 changes: 2 additions & 2 deletions proto/multistaking/v1/multi_staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ message MultiStakingLock {
option (gogoproto.goproto_getters) = false;
// option (gogoproto.goproto_stringer) = false;

LockID lockID = 1;
LockID lockID = 1 [ (gogoproto.nullable) = false ];

MultiStakingCoin locked_coin = 2 [ (gogoproto.nullable) = false ];
};
Expand All @@ -50,7 +50,7 @@ message MultiStakingUnlock {
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;

UnlockID unlockID = 1;
UnlockID unlockID = 1 [ (gogoproto.nullable) = false ];

repeated UnlockEntry entries = 2 [ (gogoproto.nullable) = false ];
}
Expand Down
4 changes: 2 additions & 2 deletions testing/simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func genesisStateWithValSet(app *SimApp, genesisState GenesisState, valSet *tmty
})

lockId := multistakingtypes.MultiStakingLockID(genAcc.GetAddress().String(), sdk.ValAddress(val.Address).String())
lockRecord := multistakingtypes.NewMultiStakingLock(&lockId, valMsCoin)
lockRecord := multistakingtypes.NewMultiStakingLock(lockId, valMsCoin)

locks = append(locks, lockRecord)
lockCoins = lockCoins.Add(valMsCoin.ToCoin())
Expand Down Expand Up @@ -182,7 +182,7 @@ func genesisStateWithValSet(app *SimApp, genesisState GenesisState, valSet *tmty
MultiStakingUnlocks: []multistakingtypes.MultiStakingUnlock{},
MultiStakingCoinInfo: msCoinInfos,
ValidatorMultiStakingCoins: validatorMsCoins,
StakingGenesisState: stakingGenesis,
StakingGenesisState: *stakingGenesis,
}
genesisState[multistakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&multistakingGenesis)

Expand Down
4 changes: 2 additions & 2 deletions x/multi-staking/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) (res []abc
k.SetValidatorMultiStakingCoin(ctx, valAddr, valMultiStakingCoin.CoinDenom)
}

return k.stakingKeeper.InitGenesis(ctx, data.StakingGenesisState)
return k.stakingKeeper.InitGenesis(ctx, &data.StakingGenesisState)
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
Expand Down Expand Up @@ -69,6 +69,6 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
MultiStakingUnlocks: multiStakingUnlocks,
MultiStakingCoinInfo: multiStakingCoinInfos,
ValidatorMultiStakingCoins: ValidatorMultiStakingCoinLists,
StakingGenesisState: k.stakingKeeper.ExportGenesis(ctx),
StakingGenesisState: *k.stakingKeeper.ExportGenesis(ctx),
}
}
2 changes: 1 addition & 1 deletion x/multi-staking/keeper/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (k Keeper) GetOrCreateMultiStakingLock(ctx sdk.Context, lockID types.LockID) types.MultiStakingLock {
multiStakingLock, found := k.GetMultiStakingLock(ctx, lockID)
if !found {
multiStakingLock = types.NewMultiStakingLock(&lockID, types.MultiStakingCoin{Amount: sdk.ZeroInt()})
multiStakingLock = types.NewMultiStakingLock(lockID, types.MultiStakingCoin{Amount: sdk.ZeroInt()})
}
return multiStakingLock
}
Expand Down
8 changes: 4 additions & 4 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 @@ -87,21 +87,21 @@ func (k Keeper) GetMultiStakingLock(ctx sdk.Context, multiStakingLockID types.Lo

func (k Keeper) SetMultiStakingLock(ctx sdk.Context, multiStakingLock types.MultiStakingLock) {
if multiStakingLock.IsEmpty() {
k.RemoveMultiStakingLock(ctx, *multiStakingLock.LockID)
k.RemoveMultiStakingLock(ctx, multiStakingLock.LockID)
return
}

store := ctx.KVStore(k.storeKey)

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
2 changes: 1 addition & 1 deletion x/multi-staking/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func DefaultGenesis() *GenesisState {
stakingGenesis := stakingtypes.DefaultGenesisState()

return &GenesisState{
StakingGenesisState: stakingGenesis,
StakingGenesisState: *stakingGenesis,
}
}

Expand Down
99 changes: 46 additions & 53 deletions x/multi-staking/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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())
}
2 changes: 1 addition & 1 deletion x/multi-staking/types/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func NewMultiStakingLock(lockID *LockID, lockedCoin MultiStakingCoin) MultiStakingLock {
func NewMultiStakingLock(lockID LockID, lockedCoin MultiStakingCoin) MultiStakingLock {
return MultiStakingLock{
LockID: lockID,
LockedCoin: lockedCoin,
Expand Down
Loading

0 comments on commit 56308cf

Please sign in to comment.