Skip to content

Commit

Permalink
handle candidate endorsement
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jan 12, 2024
1 parent 590c4bc commit 0241d03
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
103 changes: 103 additions & 0 deletions action/protocol/staking/handler_candidate_endorsement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package staking

import (
"context"

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

"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) {
var (
bucket *VoteBucket
err error
rErr ReceiptError
txLogs []*action.TransactionLog
cand *Candidate

Check warning on line 22 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L16-L22

Added lines #L16 - L22 were not covered by tests

actCtx = protocol.MustGetActionCtx(ctx)
featureCtx = protocol.MustGetFeatureCtx(ctx)
log = newReceiptLog(p.addr.String(), handleCandidateEndorsement, featureCtx.NewStakingReceiptFormat)
)
esm := NewEndorsementStateManager(csm.SM())
bucket, rErr = p.fetchBucket(csm, act.BucketIndex())
if rErr != nil {
return log, nil, rErr

Check warning on line 31 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L24-L31

Added lines #L24 - L31 were not covered by tests
}
cand = csm.GetByOwner(bucket.Candidate)
if cand == nil {
return log, nil, errCandNotExist

Check warning on line 35 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L33-L35

Added lines #L33 - L35 were not covered by tests
}

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

Check warning on line 41 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L38-L41

Added lines #L38 - L41 were not covered by tests
}
if err != nil {

Check warning on line 43 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L43

Added line #L43 was not covered by tests
return log, nil, err
}

return log, txLogs, nil

Check warning on line 47 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L47

Added line #L47 was not covered by tests
}

func (p *Protocol) endorseCandidate(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) error {
if err := p.validateEndorse(ctx, csm, esm, caller, bucket, cand); err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L50-L52

Added lines #L50 - L52 were not covered by tests
}

if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: endorsementNotExpireHeight,
}); err != nil {

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L55-L57

Added lines #L55 - L57 were not covered by tests
return csmErrorToHandleError(caller.String(), err)
}
return nil

Check warning on line 60 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L60

Added line #L60 was not covered by tests
}

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

Check warning on line 64 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L63-L64

Added lines #L63 - L64 were not covered by tests

if err := p.validateUnEndorse(ctx, esm, caller, bucket); err != nil {
return err

Check warning on line 67 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L66-L67

Added lines #L66 - L67 were not covered by tests
}
if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: blkCtx.BlockHeight + p.config.UnEndorseWaitingBlocks,
}); err != nil {

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L69-L71

Added lines #L69 - L71 were not covered by tests
return csmErrorToHandleError(caller.String(), err)
}
return nil

Check warning on line 74 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L74

Added line #L74 was not covered by tests
}

func (p *Protocol) validateEndorse(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) ReceiptError {
blkCtx := protocol.MustGetBlockCtx(ctx)
if err := validateBucketOwner(bucket, caller); err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}
if err := validateBucketMinAmount(bucket, p.config.RegistrationConsts.MinSelfStake); err != nil {
return err

Check warning on line 83 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L82-L83

Added lines #L82 - L83 were not covered by tests
}
if err := validateBucketStake(bucket, true); err != nil {
return err

Check warning on line 86 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}
if err := validateBucketCandidate(bucket, cand.Owner); err != nil {
return err

Check warning on line 89 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}
if err := validateBucketSelfStake(csm, bucket, false); err != nil {
return err

Check warning on line 92 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}
return validateBucketEndorsement(esm, bucket, false, blkCtx.BlockHeight)

Check warning on line 94 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L94

Added line #L94 was not covered by tests
}

func (p *Protocol) validateUnEndorse(ctx context.Context, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket) ReceiptError {
blkCtx := protocol.MustGetBlockCtx(ctx)
if err := validateBucketOwner(bucket, caller); err != nil {
return err

Check warning on line 100 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L97-L100

Added lines #L97 - L100 were not covered by tests
}
return validateBucketEndorsement(esm, bucket, true, blkCtx.BlockHeight)

Check warning on line 102 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L102

Added line #L102 was not covered by tests
}
4 changes: 4 additions & 0 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type (
MinStakeAmount *big.Int
BootstrapCandidates []genesis.BootstrapCandidate
PersistStakingPatchBlock uint64
UnEndorseWaitingBlocks uint64
}

// DepositGas deposits gas to some pool
Expand Down Expand Up @@ -159,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 @@ -418,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)

Check warning on line 424 in action/protocol/staking/protocol.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L423-L424

Added lines #L423 - L424 were not covered by tests
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 0241d03

Please sign in to comment.