Fix no bls key issue; fix sanity check potential issue; Preempty uesless collectReward tx

pull/1906/head
Rongjian Lan 5 years ago
parent 5ae4c992b1
commit 1423953ffa
  1. 4
      core/state_transition.go
  2. 3
      shard/committee/assignment.go
  3. 18
      staking/types/validator.go

@ -40,6 +40,7 @@ var (
errNoDelegationToUndelegate = errors.New("no delegation to undelegate") errNoDelegationToUndelegate = errors.New("no delegation to undelegate")
errCommissionRateChangeTooFast = errors.New("commission rate can not be changed more than MaxChangeRate within the same epoch") errCommissionRateChangeTooFast = errors.New("commission rate can not be changed more than MaxChangeRate within the same epoch")
errCommissionRateChangeTooHigh = errors.New("commission rate can not be higher than MaxCommissionRate") errCommissionRateChangeTooHigh = errors.New("commission rate can not be higher than MaxCommissionRate")
errNoRewardsToCollect = errors.New("no rewards to collect")
) )
/* /*
@ -542,6 +543,9 @@ func (st *StateTransition) applyCollectRewards(collectRewards *staking.CollectRe
return err return err
} }
} }
if totalRewards.Int64() == 0 {
return errNoRewardsToCollect
}
st.state.AddBalance(collectRewards.DelegatorAddress, totalRewards) st.state.AddBalance(collectRewards.DelegatorAddress, totalRewards)
return nil return nil
} }

@ -117,6 +117,9 @@ func eposStakedCommittee(
// TODO benchmark difference if went with data structure that sorts on insert // TODO benchmark difference if went with data structure that sorts on insert
for i := range candidates { for i := range candidates {
validator, err := stakerReader.ReadValidatorInformation(candidates[i]) validator, err := stakerReader.ReadValidatorInformation(candidates[i])
if err := validator.SanityCheck(); err != nil {
continue
}
validatorStake := big.NewInt(0) validatorStake := big.NewInt(0)
for _, delegation := range validator.Delegations { for _, delegation := range validator.Delegations {
validatorStake.Add(validatorStake, delegation.Amount) validatorStake.Add(validatorStake, delegation.Amount)

@ -11,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
common2 "github.com/harmony-one/harmony/internal/common" common2 "github.com/harmony-one/harmony/internal/common"
"github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/ctxerror"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
) )
@ -33,6 +32,7 @@ var (
errInvalidMaxTotalDelegation = errors.New("max_total_delegation can not be less than min_self_delegation") errInvalidMaxTotalDelegation = errors.New("max_total_delegation can not be less than min_self_delegation")
errCommissionRateTooLarge = errors.New("commission rate and change rate can not be larger than max commission rate") errCommissionRateTooLarge = errors.New("commission rate and change rate can not be larger than max commission rate")
errInvalidComissionRate = errors.New("commission rate, change rate and max rate should be within 0-100 percent") errInvalidComissionRate = errors.New("commission rate, change rate and max rate should be within 0-100 percent")
errNeedAtLeastOneSlotKey = errors.New("need at least one slot key")
) )
// ValidatorWrapper contains validator and its delegation information // ValidatorWrapper contains validator and its delegation information
@ -97,14 +97,17 @@ func (w *ValidatorWrapper) TotalDelegation() *big.Int {
// SanityCheck checks the basic requirements // SanityCheck checks the basic requirements
func (w *ValidatorWrapper) SanityCheck() error { func (w *ValidatorWrapper) SanityCheck() error {
if len(w.SlotPubKeys) == 0 {
return errNeedAtLeastOneSlotKey
}
// MinSelfDelegation must be >= 1 ONE // MinSelfDelegation must be >= 1 ONE
if w.Validator.MinSelfDelegation.Cmp(big.NewInt(denominations.One)) < 0 { if w.Validator.MinSelfDelegation == nil || w.Validator.MinSelfDelegation.Cmp(big.NewInt(denominations.One)) < 0 {
return errMinSelfDelegationTooSmall return errMinSelfDelegationTooSmall
} }
selfDelegation := w.Delegations[0].Amount
// Self delegation must be >= MinSelfDelegation // Self delegation must be >= MinSelfDelegation
if selfDelegation.Cmp(w.Validator.MinSelfDelegation) < 0 { if len(w.Delegations) == 0 || w.Delegations[0].Amount.Cmp(w.Validator.MinSelfDelegation) < 0 {
return errInvalidSelfDelegation return errInvalidSelfDelegation
} }
@ -125,12 +128,6 @@ func (w *ValidatorWrapper) SanityCheck() error {
hundredPercent := numeric.NewDec(1) hundredPercent := numeric.NewDec(1)
zeroPercent := numeric.NewDec(0) zeroPercent := numeric.NewDec(0)
utils.Logger().Info().
Str("rate", w.Validator.Rate.String()).
Str("max-rate", w.Validator.MaxRate.String()).
Str("max-change-rate", w.Validator.MaxChangeRate.String()).
Msg("Sanity check on validator commission rates, should all be in [0, 1]")
if w.Validator.Rate.LT(zeroPercent) || w.Validator.Rate.GT(hundredPercent) { if w.Validator.Rate.LT(zeroPercent) || w.Validator.Rate.GT(hundredPercent) {
return errInvalidComissionRate return errInvalidComissionRate
} }
@ -144,6 +141,7 @@ func (w *ValidatorWrapper) SanityCheck() error {
if w.Validator.Rate.GT(w.Validator.MaxRate) { if w.Validator.Rate.GT(w.Validator.MaxRate) {
return errCommissionRateTooLarge return errCommissionRateTooLarge
} }
if w.Validator.MaxChangeRate.GT(w.Validator.MaxRate) { if w.Validator.MaxChangeRate.GT(w.Validator.MaxRate) {
return errCommissionRateTooLarge return errCommissionRateTooLarge
} }

Loading…
Cancel
Save