Skip to content

Commit

Permalink
ActiveCandidate consider endorsement
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jan 19, 2024
1 parent eb800ae commit fe68fac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
5 changes: 5 additions & 0 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type (
AllCandidates() CandidateList
TotalStakedAmount() *big.Int
ActiveBucketsCount() uint64
ContainsSelfStakingBucket(index uint64) bool
}

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

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

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#L114-L115

Added lines #L114 - L115 were not covered by tests
}

func (c *candSR) TotalStakedAmount() *big.Int {
return c.view.bucketPool.Total()
}
Expand Down
55 changes: 54 additions & 1 deletion action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,55 @@ func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.
return nil
}

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)
if featureCtx.DisableDelegateEndorsement {
return true, nil
}
srHeight, err := csr.SR().Height()
if err != nil {

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L482-L483

Added lines #L482 - L483 were not covered by tests
return false, errors.Wrap(err, "failed to get StateReader height")
}
vb, err := csr.getBucket(cand.SelfStakeBucketIdx)
if err != nil {
if errors.Is(err, state.ErrStateNotExist) {
return false, nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L486 - L489 were not covered by tests
}
return false, err
}
// bucket is self-owned
if address.Equal(vb.Owner, cand.Owner) {
return true, nil

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L494-L495

Added lines #L494 - L495 were not covered by tests
}
// bucket is not endorsed to the candidate
if !address.Equal(vb.Candidate, cand.Owner) {
return false, nil

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L498-L499

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

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L501-L504

Added lines #L501 - L504 were not covered by tests
// 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

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L506-L509

Added lines #L506 - L509 were not covered by tests
}
case !errors.Is(err, state.ErrStateNotExist):
// other error
return false, err
default:

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L514

Added line #L514 was not covered by tests
// endorsement does not exist
}
return true, nil

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L517

Added line #L517 was not covered by tests
}

// ActiveCandidates returns all active candidates in candidate center
func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader, height uint64) (state.CandidateList, error) {
srHeight, err := sr.Height()
Expand All @@ -492,7 +541,11 @@ func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader
}
list[i].Votes.Add(list[i].Votes, contractVotes)
}
if list[i].SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) >= 0 {
active, err := p.isActiveCandidate(ctx, c, list[i])
if err != nil {
return nil, err
}
if active {
cand = append(cand, list[i])
}
}
Expand Down

0 comments on commit fe68fac

Please sign in to comment.