Skip to content

Commit

Permalink
handle candidate endorsement
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Dec 15, 2023
1 parent 4f6d4e5 commit 490a9b4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
101 changes: 101 additions & 0 deletions action/protocol/staking/handler_candidate_endorsement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package staking

import (
"context"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"

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

const (
handleCandidateEndorsement = "candidateEndorsement"
)

func (p *Protocol) handleCandidateEndorsement(ctx context.Context, act *action.CandidateEndorsement, csm CandidateStateManager) (*receiptLog, []*action.TransactionLog, error) {

Check failure on line 18 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: action.CandidateEndorsement
var (
bucket *VoteBucket
err error
rErr ReceiptError
txLogs []*action.TransactionLog
cand *Candidate

actCtx = protocol.MustGetActionCtx(ctx)
featureCtx = protocol.MustGetFeatureCtx(ctx)
log = newReceiptLog(p.addr.String(), handleCandidateEndorsement, featureCtx.NewStakingReceiptFormat)
)
esm := NewEndorsementStateManager(csm.SM())

Check failure on line 30 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: NewEndorsementStateManager
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
}

if act.Endorse() {
err = p.endorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
} else {
err = p.unEndorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
}
if err != nil {
return log, nil, err
}

return log, txLogs, nil
}

func (p *Protocol) endorseCandidate(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) error {

Check failure on line 52 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: EndorsementStateManager
if err := p.validateEndorse(ctx, csm, esm, caller, bucket, cand); err != nil {
return err
}

if err := esm.Put(bucket.Index, &Endorsement{

Check failure on line 57 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: Endorsement
ExpireHeight: endorsementNotExpireHeight,

Check failure on line 58 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: endorsementNotExpireHeight
}); err != nil {
return csmErrorToHandleError(caller.String(), err)
}
return nil
}

func (p *Protocol) unEndorseCandidate(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) error {

Check failure on line 65 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: EndorsementStateManager
blkCtx := protocol.MustGetBlockCtx(ctx)

if err := p.validateUnEndorse(ctx, csm, esm, caller, bucket); err != nil {
return err
}
if err := esm.Put(bucket.Index, &Endorsement{

Check failure on line 71 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: Endorsement
ExpireHeight: blkCtx.BlockHeight + p.config.UnEndorseWaitingBlocks,
}); err != nil {
return csmErrorToHandleError(caller.String(), err)
}
return nil
}

func (p *Protocol) validateEndorse(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) ReceiptError {

Check failure on line 79 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: EndorsementStateManager
validators := []bucketValidator{

Check failure on line 80 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: bucketValidator
withBucketOwner(caller),
withBucketMinAmount(p.config.RegistrationConsts.MinSelfStake),
withBucketStake(true),
withBucketCandidate(cand.Owner),
withBucketSelfStaked(false),
withBucketEndorsed(false),
}
return validateBucket(ctx, csm, esm, bucket, validators...)
}

func (p *Protocol) validateUnEndorse(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket) ReceiptError {

Check failure on line 91 in action/protocol/staking/handler_candidate_endorsement.go

View workflow job for this annotation

GitHub Actions / ci flow

undefined: EndorsementStateManager
if validateBucket(ctx, csm, esm, bucket, withBucketOwner(caller)) != nil &&
validateBucket(ctx, csm, esm, bucket, withBucketOwner(caller)) != nil {
return &handleError{
err: errors.New("bucket owner or candidate does not match"),
failureStatus: iotextypes.ReceiptStatus_ErrUnauthorizedOperator,
}
}

return validateBucket(ctx, csm, esm, bucket, withBucketEndorsed(true))
}
5 changes: 5 additions & 0 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
_bucket
_voterIndex
_candIndex
_endorsement
)

// Errors
Expand Down Expand Up @@ -91,6 +92,7 @@ type (
MinStakeAmount *big.Int
BootstrapCandidates []genesis.BootstrapCandidate
PersistStakingPatchBlock uint64
UnEndorseWaitingBlocks uint64
}

// DepositGas deposits gas to some pool
Expand Down Expand Up @@ -158,6 +160,7 @@ func NewProtocol(
MinStakeAmount: minStakeAmount,
BootstrapCandidates: cfg.Staking.BootstrapCandidates,
PersistStakingPatchBlock: cfg.PersistStakingPatchBlock,
UnEndorseWaitingBlocks: cfg.Staking.UnEndorseWaitingBlocks,
},
depositGas: depositGas,
candBucketsIndexer: candBucketsIndexer,
Expand Down Expand Up @@ -417,6 +420,8 @@ func (p *Protocol) handle(ctx context.Context, act action.Action, csm CandidateS
rLog, tLogs, err = p.handleCandidateRegister(ctx, act, csm)
case *action.CandidateUpdate:
rLog, err = p.handleCandidateUpdate(ctx, act, csm)
case *action.CandidateEndorsement:
rLog, tLogs, err = p.handleCandidateEndorsement(ctx, act, csm)
default:
return nil, nil
}
Expand Down
18 changes: 10 additions & 8 deletions blockchain/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ func defaultConfig() Genesis {
Fee: unit.ConvertIotxToRau(100).String(),
MinSelfStake: unit.ConvertIotxToRau(1200000).String(),
},
WithdrawWaitingPeriod: 3 * 24 * time.Hour,
MinStakeAmount: unit.ConvertIotxToRau(100).String(),
BootstrapCandidates: []BootstrapCandidate{},
WithdrawWaitingPeriod: 3 * 24 * time.Hour,
MinStakeAmount: unit.ConvertIotxToRau(100).String(),
BootstrapCandidates: []BootstrapCandidate{},
UnEndorseWaitingBlocks: 24 * 60 * 60 / 5,
},
}
}
Expand Down Expand Up @@ -340,11 +341,12 @@ type (
}
// Staking contains the configs for staking protocol
Staking struct {
VoteWeightCalConsts VoteWeightCalConsts `yaml:"voteWeightCalConsts"`
RegistrationConsts RegistrationConsts `yaml:"registrationConsts"`
WithdrawWaitingPeriod time.Duration `yaml:"withdrawWaitingPeriod"`
MinStakeAmount string `yaml:"minStakeAmount"`
BootstrapCandidates []BootstrapCandidate `yaml:"bootstrapCandidates"`
VoteWeightCalConsts VoteWeightCalConsts `yaml:"voteWeightCalConsts"`
RegistrationConsts RegistrationConsts `yaml:"registrationConsts"`
WithdrawWaitingPeriod time.Duration `yaml:"withdrawWaitingPeriod"`
MinStakeAmount string `yaml:"minStakeAmount"`
BootstrapCandidates []BootstrapCandidate `yaml:"bootstrapCandidates"`
UnEndorseWaitingBlocks uint64 `yaml:"unEndorseWaitingBlocks"`
}

// VoteWeightCalConsts contains the configs for calculating vote weight
Expand Down

0 comments on commit 490a9b4

Please sign in to comment.