Skip to content

Commit

Permalink
correct candidate selfstake in api
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jul 17, 2024
1 parent 351a54e commit b72da16
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
6 changes: 3 additions & 3 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (c *candSR) readStateCandidates(ctx context.Context, req *iotexapi.ReadStak
limit := int(req.GetPagination().GetLimit())
candidates := getPageOfCandidates(c.AllCandidates(), offset, limit)

list, err := toIoTeXTypesCandidateListV2(c, candidates, !protocol.MustGetFeatureCtx(ctx).EnforceLegacyEndorsement)
list, err := toIoTeXTypesCandidateListV2(c, candidates, protocol.MustGetFeatureCtx(ctx))
return list, c.Height(), err
}

Expand All @@ -475,7 +475,7 @@ func (c *candSR) readStateCandidateByName(ctx context.Context, req *iotexapi.Rea
if cand == nil {
return &iotextypes.CandidateV2{}, c.Height(), nil
}
list, err := toIoTeXTypesCandidateV2(c, cand, !protocol.MustGetFeatureCtx(ctx).EnforceLegacyEndorsement)
list, err := toIoTeXTypesCandidateV2(c, cand, protocol.MustGetFeatureCtx(ctx))
return list, c.Height(), err
}

Expand Down Expand Up @@ -505,7 +505,7 @@ func (c *candSR) readStateCandidateByAddress(ctx context.Context, req *iotexapi.
if cand == nil {
return &iotextypes.CandidateV2{}, c.Height(), nil
}
candV2, err := toIoTeXTypesCandidateV2(c, cand, !protocol.MustGetFeatureCtx(ctx).EnforceLegacyEndorsement)
candV2, err := toIoTeXTypesCandidateV2(c, cand, protocol.MustGetFeatureCtx(ctx))
return candV2, c.Height(), err
}

Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (p *Protocol) handleStakingIndexer(ctx context.Context, epochStartHeight ui
if err != nil && errors.Cause(err) != state.ErrStateNotExist {
return err
}
candidateList, err := toIoTeXTypesCandidateListV2(csr, all, !protocol.MustGetFeatureCtx(ctx).EnforceLegacyEndorsement)
candidateList, err := toIoTeXTypesCandidateListV2(csr, all, protocol.MustGetFeatureCtx(ctx))
if err != nil {
return err
}
Expand Down
18 changes: 6 additions & 12 deletions action/protocol/staking/read_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ func getPageOfBuckets(buckets []*VoteBucket, offset, limit int) []*VoteBucket {
return getPageOfArray(buckets, offset, limit)
}

func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, isClean bool) (*iotextypes.CandidateV2, error) {
func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateV2, error) {
esr := NewEndorsementStateReader(csr.SR())
height, _ := csr.SR().Height()
needClear := func(c *Candidate) (bool, error) {
if isClean {
return false, nil
}
if !c.isSelfStakeBucketSettled() {
return false, nil
}
Expand All @@ -80,16 +77,13 @@ func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, isClean
return false, err
}
if isSelfOwnedBucket(csr, vb) {
return false, nil
return vb.isUnstaked(), nil
}
endorse, err := esr.Get(c.SelfStakeBucketIdx)
status, err := esr.Status(featureCtx, c.SelfStakeBucketIdx, height)
if err != nil {
if errors.Is(err, state.ErrStateNotExist) {
return true, nil
}
return false, err
}
return endorse.LegacyStatus(height) == EndorseExpired, nil
return status == EndorseExpired, nil
}
c := cand.toIoTeXTypes()
// clear self-stake bucket if endorsement is expired but not updated yet
Expand All @@ -104,12 +98,12 @@ func toIoTeXTypesCandidateV2(csr CandidateStateReader, cand *Candidate, isClean
return c, nil
}

func toIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, isClean bool) (*iotextypes.CandidateListV2, error) {
func toIoTeXTypesCandidateListV2(csr CandidateStateReader, candidates CandidateList, featureCtx protocol.FeatureCtx) (*iotextypes.CandidateListV2, error) {
res := iotextypes.CandidateListV2{
Candidates: make([]*iotextypes.CandidateV2, 0, len(candidates)),
}
for _, c := range candidates {
cand, err := toIoTeXTypesCandidateV2(csr, c, isClean)
cand, err := toIoTeXTypesCandidateV2(csr, c, featureCtx)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b72da16

Please sign in to comment.