-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[staking] ActiveCandidate Exclude Candidate with Expired Endorsement #4062
Changes from all commits
84e3158
fdd2b62
bd408d5
ae5e699
55a48e1
77bbf07
392a8c4
1a03f4c
96663e0
46e75e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,8 +56,9 @@ | |
|
||
// Errors | ||
var ( | ||
ErrWithdrawnBucket = errors.New("the bucket is already withdrawn") | ||
TotalBucketKey = append([]byte{_const}, []byte("totalBucket")...) | ||
ErrWithdrawnBucket = errors.New("the bucket is already withdrawn") | ||
ErrEndorsementNotExist = errors.New("the endorsement does not exist") | ||
TotalBucketKey = append([]byte{_const}, []byte("totalBucket")...) | ||
) | ||
|
||
var ( | ||
|
@@ -472,6 +473,31 @@ | |
return nil | ||
} | ||
|
||
func (p *Protocol) isActiveCandidate(ctx context.Context, csr CandidateStateReader, cand *Candidate) (bool, error) { | ||
if cand.SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 { | ||
return false, nil | ||
} | ||
featureCtx := protocol.MustGetFeatureCtx(ctx) | ||
if featureCtx.DisableDelegateEndorsement { | ||
// before endorsement feature, candidates with enough amount must be active | ||
return true, nil | ||
} | ||
bucket, err := csr.getBucket(cand.SelfStakeBucketIdx) | ||
switch { | ||
case errors.Cause(err) == state.ErrStateNotExist: | ||
// endorse bucket has been withdrawn | ||
return false, nil | ||
case err != nil: | ||
return false, errors.Wrapf(err, "failed to get bucket %d", cand.SelfStakeBucketIdx) | ||
default: | ||
} | ||
selfStake, err := isSelfStakeBucket(featureCtx, csr, bucket) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "failed to check self-stake bucket %d", cand.SelfStakeBucketIdx) | ||
} | ||
return selfStake, nil | ||
} | ||
|
||
// 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() | ||
|
@@ -496,7 +522,11 @@ | |
} | ||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are unactive cands included in L527? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two scenarios:
Both situations belong to a registered delegate, but they cannot participate in consensus due to a lack of staking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. It seems the state of candidates aren't tracked in the db(active/unactive). Pls add ur comments to explain why the list is filtered above L541 |
||
if err != nil { | ||
return nil, err | ||
} | ||
if active { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from the usage here, it is checking current height, not |
||
cand = append(cand, list[i]) | ||
} | ||
} | ||
|
@@ -686,9 +716,9 @@ | |
} | ||
|
||
// isSelfStakeBucket returns true if the bucket is self-stake bucket and not expired | ||
func isSelfStakeBucket(featureCtx protocol.FeatureCtx, csm CandidateStateManager, bucket *VoteBucket) (bool, error) { | ||
func isSelfStakeBucket(featureCtx protocol.FeatureCtx, csc CandidiateStateCommon, bucket *VoteBucket) (bool, error) { | ||
// bucket index should be settled in one of candidates | ||
selfStake := csm.ContainsSelfStakingBucket(bucket.Index) | ||
selfStake := csc.ContainsSelfStakingBucket(bucket.Index) | ||
if featureCtx.DisableDelegateEndorsement || !selfStake { | ||
return selfStake, nil | ||
} | ||
|
@@ -698,7 +728,7 @@ | |
return !bucket.isUnstaked(), nil | ||
} | ||
// otherwise bucket should be an endorse bucket which is not expired | ||
esm := NewEndorsementStateManager(csm.SM()) | ||
esm := NewEndorsementStateReader(csc.SR()) | ||
height, err := esm.Height() | ||
if err != nil { | ||
return false, err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just
return cand.SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) >= 0, nil