Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Feb 5, 2024
1 parent 22da849 commit 4a693f6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
32 changes: 32 additions & 0 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type (
TotalStakedAmount() *big.Int
ActiveBucketsCount() uint64
ContainsSelfStakingBucket(index uint64) bool
IsActiveCandidateAt(cand *Candidate, height uint64) (bool, error)
}

candSR struct {
Expand Down Expand Up @@ -111,6 +112,37 @@ func (c *candSR) AllCandidates() CandidateList {
return c.view.candCenter.All()
}

func (c *candSR) IsActiveCandidateAt(cand *Candidate, height uint64) (bool, error) {

Check warning on line 115 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L115

Added line #L115 was not covered by tests
// self-stake bucket should be either self-owned or endorsed
vb, err := c.getBucket(cand.SelfStakeBucketIdx)
if err != nil {

Check warning on line 118 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L117-L118

Added lines #L117 - L118 were not covered by tests
return false, errors.Wrapf(err, "failed to get bucket %d", cand.SelfStakeBucketIdx)
}
// self-owned bucket should not be unstaked
if address.Equal(vb.Owner, cand.Owner) {
return !vb.isUnstaked(), nil

Check warning on line 123 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L122-L123

Added lines #L122 - L123 were not covered by tests
}
// endorsed bucket should not be expired
if !address.Equal(vb.Candidate, cand.Owner) {
return false, nil

Check warning on line 127 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L126-L127

Added lines #L126 - L127 were not covered by tests
}
esr := NewEndorsementStateReader(c.SR())
endorse, err := esr.Get(cand.SelfStakeBucketIdx)
switch {
case err == nil:

Check warning on line 132 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L129-L132

Added lines #L129 - L132 were not covered by tests
// endorsement should not be expired
if endorse.Status(height) == EndorseExpired {
return false, nil

Check warning on line 135 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}
return true, nil
case errors.Is(err, state.ErrStateNotExist):

Check warning on line 138 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L137-L138

Added lines #L137 - L138 were not covered by tests
// impossible to happen
return false, errors.Wrapf(ErrEndorsementNotExist, "bucket index %d", cand.SelfStakeBucketIdx)

Check warning on line 140 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L140

Added line #L140 was not covered by tests
default:
return false, err
}
}

func (c *candSR) ContainsSelfStakingBucket(index uint64) bool {
return c.view.candCenter.ContainsSelfStakingBucket(index)

Check warning on line 147 in action/protocol/staking/candidate_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/candidate_statereader.go#L146-L147

Added lines #L146 - L147 were not covered by tests
}
Expand Down
40 changes: 8 additions & 32 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,46 +474,22 @@ func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.
}

func (p *Protocol) isActiveCandidate(ctx context.Context, csr CandidateStateReader, cand *Candidate) (bool, error) {
// at least min self stake
if cand.SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 {
return false, nil
}

// endorsement must not exipred if the self-stake bucket is an endorse bucket
featureCtx := protocol.MustGetFeatureCtx(ctx)
// before delegate endorsement enabled, candidate is always active unless it's self-stake bucket is unstaked
if featureCtx.DisableDelegateEndorsement {
if cand.SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 {
return false, nil
}
return true, nil
}

srHeight, err := csr.SR().Height()
if err != nil {

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L486-L487

Added lines #L486 - L487 were not covered by tests
return false, errors.Wrap(err, "failed to get StateReader height")
}
vb, err := csr.getBucket(cand.SelfStakeBucketIdx)
if err != nil {
return false, errors.Wrapf(err, "failed to get bucket %d", cand.SelfStakeBucketIdx)
}
// bucket is self-owned
if address.Equal(vb.Owner, cand.Owner) {
return true, nil
}
esr := NewEndorsementStateReader(csr.SR())
endorse, err := esr.Get(cand.SelfStakeBucketIdx)
switch {
case err == nil:
// endorsement exists and expired before end of next epoch
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
currentEpochNum := rp.GetEpochNum(srHeight)
if endorse.Status(rp.GetEpochLastBlockHeight(currentEpochNum+1)) == EndorseExpired {
return false, nil
}
case !errors.Is(err, state.ErrStateNotExist):
// other error
return false, err
default:
// endorsement does not exist
return false, errors.Wrapf(ErrEndorsementNotExist, "bucket index %d", cand.SelfStakeBucketIdx)
}
return true, nil
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
currentEpochNum := rp.GetEpochNum(srHeight)
return csr.IsActiveCandidateAt(cand, rp.GetEpochLastBlockHeight(currentEpochNum+1))

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L490-L492

Added lines #L490 - L492 were not covered by tests
}

// ActiveCandidates returns all active candidates in candidate center
Expand Down

0 comments on commit 4a693f6

Please sign in to comment.