github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/validations.go (about)

     1  // Copyright (c) 2020 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package staking
     7  
     8  import (
     9  	"context"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/iotexproject/iotex-core/action"
    14  	"github.com/iotexproject/iotex-core/action/protocol"
    15  )
    16  
    17  // Errors
    18  var (
    19  	ErrInvalidOwner        = errors.New("invalid owner address")
    20  	ErrInvalidOperator     = errors.New("invalid operator address")
    21  	ErrInvalidReward       = errors.New("invalid reward address")
    22  	ErrInvalidSelfStkIndex = errors.New("invalid self-staking bucket index")
    23  	ErrMissingField        = errors.New("missing data field")
    24  	ErrTypeAssertion       = errors.New("failed type assertion")
    25  )
    26  
    27  func (p *Protocol) validateCreateStake(ctx context.Context, act *action.CreateStake) error {
    28  	if !action.IsValidCandidateName(act.Candidate()) {
    29  		return action.ErrInvalidCanName
    30  	}
    31  	if act.Amount().Cmp(p.config.MinStakeAmount) == -1 {
    32  		return errors.Wrap(action.ErrInvalidAmount, "stake amount is less than the minimum requirement")
    33  	}
    34  	return nil
    35  }
    36  
    37  func (p *Protocol) validateUnstake(ctx context.Context, act *action.Unstake) error {
    38  	return nil
    39  }
    40  
    41  func (p *Protocol) validateWithdrawStake(ctx context.Context, act *action.WithdrawStake) error {
    42  	return nil
    43  }
    44  
    45  func (p *Protocol) validateChangeCandidate(ctx context.Context, act *action.ChangeCandidate) error {
    46  	if !action.IsValidCandidateName(act.Candidate()) {
    47  		return action.ErrInvalidCanName
    48  	}
    49  	return nil
    50  }
    51  
    52  func (p *Protocol) validateTransferStake(ctx context.Context, act *action.TransferStake) error {
    53  	return nil
    54  }
    55  
    56  func (p *Protocol) validateDepositToStake(ctx context.Context, act *action.DepositToStake) error {
    57  	return nil
    58  }
    59  
    60  func (p *Protocol) validateRestake(ctx context.Context, act *action.Restake) error {
    61  	return nil
    62  }
    63  
    64  func (p *Protocol) validateCandidateRegister(ctx context.Context, act *action.CandidateRegister) error {
    65  	if !action.IsValidCandidateName(act.Name()) {
    66  		return action.ErrInvalidCanName
    67  	}
    68  
    69  	if act.Amount().Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 {
    70  		if !protocol.MustGetFeatureCtx(ctx).CandidateRegisterMustWithStake &&
    71  			act.Amount().Sign() == 0 {
    72  			return nil
    73  		}
    74  		return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid")
    75  	}
    76  	return nil
    77  }
    78  
    79  func (p *Protocol) validateCandidateUpdate(ctx context.Context, act *action.CandidateUpdate) error {
    80  	if len(act.Name()) != 0 {
    81  		if !action.IsValidCandidateName(act.Name()) {
    82  			return action.ErrInvalidCanName
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func (p *Protocol) validateCandidateEndorsement(ctx context.Context, act *action.CandidateEndorsement) error {
    89  	if protocol.MustGetFeatureCtx(ctx).DisableDelegateEndorsement {
    90  		return errors.Wrap(action.ErrInvalidAct, "candidate endorsement is disabled")
    91  	}
    92  	return nil
    93  }
    94  
    95  func (p *Protocol) validateCandidateActivate(ctx context.Context, act *action.CandidateActivate) error {
    96  	if protocol.MustGetFeatureCtx(ctx).DisableDelegateEndorsement {
    97  		return errors.Wrap(action.ErrInvalidAct, "candidate activate is disabled")
    98  	}
    99  	return nil
   100  }