cwgoes comments addressed

This commit is contained in:
rigelrozanski 2018-03-28 02:54:54 +02:00
parent cbbba2cf4f
commit b2c5814fd0
1 changed files with 20 additions and 19 deletions

View File

@ -212,19 +212,29 @@ func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result {
return ErrInsufficientFunds().Result() return ErrInsufficientFunds().Result()
} }
// if shares set to special case Max then we're good // test getting rational number from decimal provided
if msg.Shares != "MAX" { shares, err := sdk.NewRatFromDecimal(msg.Shares)
// test getting rational number from decimal provided if err != nil {
shares, err := sdk.NewRatFromDecimal(msg.Shares) return err.Result()
if err != nil { }
return err.Result()
}
// test that there are enough shares to unbond // test that there are enough shares to unbond
if msg.Shares == "MAX" {
if !bond.Shares.GT(sdk.ZeroRat) {
return ErrNotEnoughBondShares(msg.Shares).Result()
}
} else {
if !bond.Shares.GT(shares) { if !bond.Shares.GT(shares) {
return ErrNotEnoughBondShares(msg.Shares).Result() return ErrNotEnoughBondShares(msg.Shares).Result()
} }
} }
// get candidate
candidate, found := k.GetCandidate(ctx, msg.CandidateAddr)
if !found {
return ErrNoCandidateForAddress().Result()
}
if ctx.IsCheckTx() { if ctx.IsCheckTx() {
return sdk.Result{ return sdk.Result{
GasUsed: GasUnbond, GasUsed: GasUnbond,
@ -244,17 +254,9 @@ func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result {
} }
// subtract bond tokens from delegator bond // subtract bond tokens from delegator bond
if bond.Shares.LT(shares) { // bond shares < msg shares
return ErrInsufficientFunds().Result()
}
bond.Shares = bond.Shares.Sub(shares) bond.Shares = bond.Shares.Sub(shares)
// get pubKey candidate // remove the bond
candidate, found := k.GetCandidate(ctx, msg.CandidateAddr)
if !found {
return ErrNoCandidateForAddress().Result()
}
revokeCandidacy := false revokeCandidacy := false
if bond.Shares.IsZero() { if bond.Shares.IsZero() {
@ -265,7 +267,6 @@ func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result {
revokeCandidacy = true revokeCandidacy = true
} }
// remove the bond
k.removeDelegatorBond(ctx, bond) k.removeDelegatorBond(ctx, bond)
} else { } else {
k.setDelegatorBond(ctx, bond) k.setDelegatorBond(ctx, bond)
@ -276,7 +277,7 @@ func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result {
returnCoins := sdk.Coins{{k.GetParams(ctx).BondDenom, returnAmount}} returnCoins := sdk.Coins{{k.GetParams(ctx).BondDenom, returnAmount}}
k.coinKeeper.AddCoins(ctx, bond.DelegatorAddr, returnCoins) k.coinKeeper.AddCoins(ctx, bond.DelegatorAddr, returnCoins)
// lastly if an revoke candidate if necessary // revoke candidate if necessary
if revokeCandidacy { if revokeCandidacy {
// change the share types to unbonded if they were not already // change the share types to unbonded if they were not already