Skip to content

Commit

Permalink
Merge pull request #241 from stratosnet/qb1747_fix-validation-of-MsgU…
Browse files Browse the repository at this point in the history
…pdateEffectiveStake

fix/qb1747: fix validation of msg update effective stake
  • Loading branch information
alexstratos authored Apr 4, 2023
2 parents 83fbbf3 + aa02786 commit 0b8ea4d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
7 changes: 2 additions & 5 deletions x/pot/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,8 @@ func (k Keeper) CalcRewardForMetaNode(ctx sdk.Context, distributeGoalBalance typ
func (k Keeper) GetTotalConsumedNoz(trafficList []*types.SingleWalletVolume) sdk.Int {
totalTraffic := sdk.ZeroInt()
for _, vol := range trafficList {
toAdd, ok := sdk.NewIntFromString(vol.Volume.String())
if !ok {
continue
}
totalTraffic = totalTraffic.Add(toAdd)
toAdd := vol.Volume
totalTraffic = totalTraffic.Add(*toAdd)
}
return totalTraffic
}
Expand Down
11 changes: 10 additions & 1 deletion x/register/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (k Keeper) InsertUnbondingNodeQueue(ctx sdk.Context, ubd types.UnbondingNod
// Iteration for dequeuing all mature unbonding queue
// TODO: Unused parameter: currTime
func (k Keeper) DequeueAllMatureUBDQueue(ctx sdk.Context, currTime time.Time) (matureUnbonds []string) {
keysToDelete := make([][]byte, 0)
store := ctx.KVStore(k.storeKey)
// gets an iterator for all timeslices from time 0 until the current Blockheader time
unbondingTimesliceIterator := k.UnbondingNodeQueueIterator(ctx, ctx.BlockHeader().Time)
Expand All @@ -193,7 +194,11 @@ func (k Keeper) DequeueAllMatureUBDQueue(ctx sdk.Context, currTime time.Time) (m
k.cdc.MustUnmarshalLengthPrefixed(value, &timeSliceVal)
timeSlice := timeSliceVal.GetAddresses()
matureUnbonds = append(matureUnbonds, timeSlice...)
store.Delete(unbondingTimesliceIterator.Key())
keysToDelete = append(keysToDelete, unbondingTimesliceIterator.Key())
}
// safe removal
for _, key := range keysToDelete {
store.Delete(key)
}
ctx.Logger().Debug(fmt.Sprintf("DequeueAllMatureUBDQueue, %d matured unbonding nodes detected", len(matureUnbonds)))
return matureUnbonds
Expand Down Expand Up @@ -379,6 +384,10 @@ func (k Keeper) UnbondMetaNode(ctx sdk.Context, metaNode types.MetaNode, amt sdk
bondDenom := k.GetParams(ctx).BondDenom
coin := sdk.NewCoin(bondDenom, amt)
if metaNode.GetStatus() == stakingtypes.Bonded {
// to prevent remainingOzoneLimit from being negative value
if !k.IsUnbondable(ctx, amt) {
return sdk.ZeroInt(), time.Time{}, types.ErrInsufficientBalance
}
// transfer the node tokens to the not bonded pool
k.bondedToUnbonding(ctx, metaNode, true, coin)
// adjust ozone limit
Expand Down
2 changes: 1 addition & 1 deletion x/register/keeper/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (k Keeper) UpdateEffectiveStake(ctx sdk.Context, networkAddr stratos.SdsAdd
node.Suspend = false
k.SetResourceNode(ctx, node)

if effectiveStakeChange.IsNegative() {
if effectiveStakeChange.IsNegative() && k.IsUnbondable(ctx, effectiveStakeChange.Abs()) {
ozoneLimitChange = k.DecreaseOzoneLimitBySubtractStake(ctx, effectiveStakeChange.Abs())
}
if effectiveStakeChange.IsPositive() {
Expand Down
6 changes: 6 additions & 0 deletions x/register/keeper/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func (k Keeper) GetRemainingOzoneLimit(ctx sdk.Context) (value sdk.Int) {
return
}

func (k Keeper) IsUnbondable(ctx sdk.Context, unbondAmt sdk.Int) bool {
remaining := k.GetRemainingOzoneLimit(ctx)
stakeNozRate := k.GetStakeNozRate(ctx)
return remaining.ToDec().GTE(unbondAmt.ToDec().Quo(stakeNozRate))
}

// SetUnbondingNode sets the unbonding MetaNode
func (k Keeper) SetUnbondingNode(ctx sdk.Context, ubd types.UnbondingNode) {
store := ctx.KVStore(k.storeKey)
Expand Down
9 changes: 9 additions & 0 deletions x/register/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@ func (m MsgUpdateEffectiveStake) ValidateBasic() error {
if len(m.NetworkAddress) == 0 {
return ErrInvalidNetworkAddr
}
if len(m.Reporters) == 0 {
return ErrReporterAddress
}
if len(m.ReporterOwner) == 0 || len(m.Reporters) != len(m.ReporterOwner) {
return ErrInvalidOwnerAddr
}
for _, r := range m.Reporters {
if len(r) == 0 {
return ErrReporterAddress
Expand Down Expand Up @@ -668,5 +674,8 @@ func (m MsgUpdateEffectiveStake) GetSigners() []sdk.AccAddress {
}
addrs = append(addrs, reporterOwner)
}
if len(addrs) == 0 {
panic("no valid signer for MsgUpdateEffectiveStake")
}
return addrs
}
4 changes: 4 additions & 0 deletions x/sds/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/hex"
"errors"
"fmt"

"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -90,6 +91,9 @@ func (k Keeper) purchaseNozAndSubCoins(ctx sdk.Context, from sdk.AccAddress, amo
Add(amount)).ToDec()).
TruncateInt()

if purchased.GT(Lt) {
return sdk.ZeroInt(), errors.New("not enough remaining ozone limit to complete prepay")
}
// send coins to total unissued prepay pool
prepayAmt := sdk.NewCoin(k.BondDenom(ctx), amount)
err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, from, registertypes.TotalUnissuedPrepay, sdk.NewCoins(prepayAmt))
Expand Down

0 comments on commit 0b8ea4d

Please sign in to comment.