From b7bd9948d584785bf102c84a07f272f1a67ecfb7 Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Fri, 8 Dec 2023 14:45:34 +0530 Subject: [PATCH] address comments --- x/distribution/keeper/delegation.go | 4 ++-- x/slashing/keeper/signing_info.go | 5 +++-- x/staking/keeper/alias_functions.go | 2 +- x/staking/keeper/delegation.go | 13 +++++++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index e97a694a67f7..36482b407b68 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -183,10 +183,10 @@ func (k Keeper) CalculateDelegationRewards(ctx context.Context, val sdk.Validato if stake.LTE(currentStake.Add(marginOfErr)) { stake = currentStake } else { - panic(fmt.Sprintf("calculated final stake for delegator %s greater than current stake"+ + return sdk.DecCoins{}, fmt.Errorf("calculated final stake for delegator %s greater than current stake"+ "\n\tfinal stake:\t%s"+ "\n\tcurrent stake:\t%s", - del.GetDelegatorAddr(), stake, currentStake)) + del.GetDelegatorAddr(), stake, currentStake) } } diff --git a/x/slashing/keeper/signing_info.go b/x/slashing/keeper/signing_info.go index c68a9a9a982b..51e258fea960 100644 --- a/x/slashing/keeper/signing_info.go +++ b/x/slashing/keeper/signing_info.go @@ -3,6 +3,7 @@ package keeper import ( "context" "errors" + "fmt" "time" "github.com/bits-and-blooms/bitset" @@ -26,7 +27,7 @@ func (k Keeper) HasValidatorSigningInfo(ctx context.Context, consAddr sdk.ConsAd func (k Keeper) JailUntil(ctx context.Context, consAddr sdk.ConsAddress, jailTime time.Time) error { signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr) if err != nil { - return errorsmod.Wrap(err, "cannot jail validator that does not have any signing information") + return errorsmod.Wrap(err, fmt.Sprintf("cannot jail validator with consensus address %s that does not have any signing information", consAddr.String())) } signInfo.JailedUntil = jailTime @@ -37,7 +38,7 @@ func (k Keeper) JailUntil(ctx context.Context, consAddr sdk.ConsAddress, jailTim func (k Keeper) Tombstone(ctx context.Context, consAddr sdk.ConsAddress) error { signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr) if err != nil { - return types.ErrNoSigningInfoFound.Wrap("cannot tombstone validator that does not have any signing information") + return types.ErrNoSigningInfoFound.Wrap(fmt.Sprintf("cannot tombstone validator with consensus address %s that does not have any signing information", consAddr.String())) } if signInfo.Tombstoned { diff --git a/x/staking/keeper/alias_functions.go b/x/staking/keeper/alias_functions.go index c5dffdddbc60..fde1c5254e1e 100644 --- a/x/staking/keeper/alias_functions.go +++ b/x/staking/keeper/alias_functions.go @@ -59,7 +59,7 @@ func (k Keeper) IterateBondedValidatorsByPower(ctx context.Context, fn func(inde address := iterator.Value() validator, err := k.GetValidator(ctx, address) if err != nil { - return fmt.Errorf("validator record not found for address: %X", address) + return fmt.Errorf("validator record not found for address: %s", sdk.ValAddress(address).String()) } if validator.IsBonded() { stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to? diff --git a/x/staking/keeper/delegation.go b/x/staking/keeper/delegation.go index 2fb46c9fa94c..5f8aab0fd9f0 100644 --- a/x/staking/keeper/delegation.go +++ b/x/staking/keeper/delegation.go @@ -216,9 +216,11 @@ func (k Keeper) GetDelegatorUnbonding(ctx context.Context, delegator sdk.AccAddr func (k Keeper) GetDelegatorBonded(ctx context.Context, delegator sdk.AccAddress) (math.Int, error) { bonded := math.LegacyZeroDec() + var iterErr error err := k.IterateDelegatorDelegations(ctx, delegator, func(delegation types.Delegation) bool { validatorAddr, err := k.validatorAddressCodec.StringToBytes(delegation.ValidatorAddress) if err != nil { + iterErr = err return true } validator, err := k.GetValidator(ctx, validatorAddr) @@ -229,6 +231,9 @@ func (k Keeper) GetDelegatorBonded(ctx context.Context, delegator sdk.AccAddress } return false }) + if iterErr != nil { + return bonded.RoundInt(), iterErr + } return bonded.RoundInt(), err } @@ -728,7 +733,7 @@ func (k Keeper) Delegate( case validator.IsUnbonding(), validator.IsUnbonded(): sendName = types.NotBondedPoolName default: - return math.LegacyZeroDec(), fmt.Errorf("invalid validator status") + return math.LegacyZeroDec(), fmt.Errorf("invalid validator status: %v", validator.Status) } bondDenom, err := k.BondDenom(ctx) @@ -760,7 +765,7 @@ func (k Keeper) Delegate( return math.LegacyDec{}, err } default: - return math.LegacyZeroDec(), fmt.Errorf("unknown token source bond status") + return math.LegacyZeroDec(), fmt.Errorf("unknown token source bond status: %v", tokenSrc) } } @@ -832,7 +837,7 @@ func (k Keeper) Unbond( validator.TokensFromShares(delegation.Shares).TruncateInt().LT(validator.MinSelfDelegation) { err = k.jailValidator(ctx, validator) if err != nil { - return amount, err + return amount, fmt.Errorf("failed to get updated validator after jailing: %v", err) } validator, err = k.GetValidator(ctx, valbz) if err != nil { @@ -909,7 +914,7 @@ func (k Keeper) getBeginInfo( return validator.UnbondingTime, validator.UnbondingHeight, false, nil default: - return completionTime, height, false, fmt.Errorf("unknown validator status: %s", validator.Status) + return completionTime, height, false, fmt.Errorf("unknown validator status: %v", validator.Status) } }