Skip to content

Commit

Permalink
[staking] Handling CandidateEndorsement Action (#4020)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Jan 30, 2024
1 parent 56eeca0 commit ed9c857
Show file tree
Hide file tree
Showing 6 changed files with 549 additions and 20 deletions.
4 changes: 2 additions & 2 deletions action/candidate_endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func (act *CandidateEndorsement) BucketIndex() uint64 {
return act.bucketIndex
}

// Endorse returns true if the action is to endorse a candidate
func (act *CandidateEndorsement) Endorse() bool {
// IsEndorse returns true if the action is to endorse a candidate
func (act *CandidateEndorsement) IsEndorse() bool {
return act.endorse
}

Expand Down
86 changes: 86 additions & 0 deletions action/protocol/staking/handler_candidate_endorsement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package staking

import (
"context"

"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

const (
handleCandidateEndorsement = "candidateEndorsement"
)

func (p *Protocol) handleCandidateEndorsement(ctx context.Context, act *action.CandidateEndorsement, csm CandidateStateManager) (*receiptLog, []*action.TransactionLog, error) {
actCtx := protocol.MustGetActionCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
log := newReceiptLog(p.addr.String(), handleCandidateEndorsement, featureCtx.NewStakingReceiptFormat)

bucket, rErr := p.fetchBucket(csm, act.BucketIndex())
if rErr != nil {
return log, nil, rErr
}
cand := csm.GetByOwner(bucket.Candidate)
if cand == nil {
return log, nil, errCandNotExist
}
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes(), []byte{byteutil.BoolToByte(act.IsEndorse())})

esm := NewEndorsementStateManager(csm.SM())
expireHeight := uint64(0)
if act.IsEndorse() {
// handle endorsement
if err := p.validateEndorsement(ctx, csm, esm, actCtx.Caller, bucket, cand); err != nil {
return log, nil, err
}
expireHeight = uint64(endorsementNotExpireHeight)
} else {
// handle withdrawal
if err := p.validateEndorsementWithdrawal(ctx, esm, actCtx.Caller, bucket); err != nil {
return log, nil, err
}
// expire immediately if the bucket is not self-staked
// otherwise, expire after withdraw waiting period
expireHeight = protocol.MustGetBlockCtx(ctx).BlockHeight
if csm.ContainsSelfStakingBucket(bucket.Index) {
expireHeight += p.config.EndorsementWithdrawWaitingBlocks
}
}
// update endorsement state
if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: expireHeight,
}); err != nil {
return log, nil, errors.Wrapf(err, "failed to put endorsement with bucket index %d", bucket.Index)
}
return log, nil, nil
}

func (p *Protocol) validateEndorsement(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) ReceiptError {
if err := validateBucketOwner(bucket, caller); err != nil {
return err
}
if err := validateBucketMinAmount(bucket, p.config.RegistrationConsts.MinSelfStake); err != nil {
return err
}
if err := validateBucketStake(bucket, true); err != nil {
return err
}
if err := validateBucketCandidate(bucket, cand.Owner); err != nil {
return err
}
if err := validateBucketSelfStake(csm, bucket, false); err != nil {
return err
}
return validateBucketEndorsement(esm, bucket, false, protocol.MustGetBlockCtx(ctx).BlockHeight)
}

func (p *Protocol) validateEndorsementWithdrawal(ctx context.Context, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket) ReceiptError {
if err := validateBucketOwner(bucket, caller); err != nil {
return err
}
return validateBucketEndorsement(esm, bucket, true, protocol.MustGetBlockCtx(ctx).BlockHeight)
}
Loading

0 comments on commit ed9c857

Please sign in to comment.