Skip to content

Commit

Permalink
reuse isSelfStakeBucket
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Mar 7, 2024
1 parent ae5e699 commit 55a48e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
4 changes: 3 additions & 1 deletion action/protocol/poll/nativestakingV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-election/util"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/staking"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-election/util"
)

type nativeStakingV2 struct {
Expand Down Expand Up @@ -83,6 +84,7 @@ func (ns *nativeStakingV2) CalculateCandidatesByHeight(ctx context.Context, sr p
return cands, err
}
bcCtx := protocol.MustGetBlockchainCtx(ctx)
// TODO: put candidates which will expired during current or next epoch behind the active candidates
return ns.filterAndSortCandidatesByVoteScore(cands, bcCtx.Tip.Timestamp), nil
}

Expand Down
32 changes: 0 additions & 32 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type (
TotalStakedAmount() *big.Int
ActiveBucketsCount() uint64
ContainsSelfStakingBucket(index uint64) bool
IsActiveCandidateAt(cand *Candidate, height uint64) (bool, error)
}

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

func (c *candSR) IsActiveCandidateAt(cand *Candidate, height uint64) (bool, error) {
// self-stake bucket should be either self-owned or endorsed
vb, err := c.getBucket(cand.SelfStakeBucketIdx)
if err != nil {
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
}
// endorsed bucket should not be expired
if !address.Equal(vb.Candidate, cand.Owner) {
return false, nil
}
esr := NewEndorsementStateReader(c.SR())
endorse, err := esr.Get(cand.SelfStakeBucketIdx)
switch {
case err == nil:
// endorsement should not be expired
if endorse.Status(height) == EndorseExpired {
return false, nil
}
return true, nil
case errors.Is(err, state.ErrStateNotExist):
// impossible to happen
return false, errors.Wrapf(ErrEndorsementNotExist, "bucket index %d", cand.SelfStakeBucketIdx)
default:
return false, err
}
}

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
}
Expand Down
38 changes: 26 additions & 12 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,22 +474,28 @@ 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) {
if cand.SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 {
return false, nil
}
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
}
// before endorsement feature, candidates with enough amount must be active
return true, nil
}

srHeight, err := csr.SR().Height()
bucket, err := csr.getBucket(cand.SelfStakeBucketIdx)
switch {
case errors.Cause(err) == state.ErrStateNotExist:

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#L485-L487

Added lines #L485 - L487 were not covered by tests
// endorse bucket has been withdrawn
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#L489

Added line #L489 was not covered by tests
case err != nil:
return false, errors.Wrapf(err, "failed to get bucket %d", cand.SelfStakeBucketIdx)
default:

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#L492

Added line #L492 was not covered by tests
}
selfStake, err := isSelfStakeBucketCommon(featureCtx, csr.ContainsSelfStakingBucket, csr.SR(), bucket)
if err != 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
return false, errors.Wrap(err, "failed to get StateReader height")
return false, errors.Wrapf(err, "failed to check self-stake bucket %d", cand.SelfStakeBucketIdx)
}
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
currentEpochNum := rp.GetEpochNum(srHeight)
return csr.IsActiveCandidateAt(cand, rp.GetEpochLastBlockHeight(currentEpochNum+1))
return selfStake, nil

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

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L498

Added line #L498 was not covered by tests
}

// ActiveCandidates returns all active candidates in candidate center
Expand Down Expand Up @@ -711,8 +717,16 @@ func writeCandCenterStateToStateDB(sm protocol.StateManager, name, op, owners Ca

// isSelfStakeBucket returns true if the bucket is self-stake bucket and not expired
func isSelfStakeBucket(featureCtx protocol.FeatureCtx, csm CandidateStateManager, bucket *VoteBucket) (bool, error) {
return isSelfStakeBucketCommon(featureCtx, csm.ContainsSelfStakingBucket, csm.SM(), bucket)
}

func isSelfStakeBucketCommon(
featureCtx protocol.FeatureCtx,
containsSelfStakingBucket func(uint64) bool,
sr protocol.StateReader,
bucket *VoteBucket) (bool, error) {
// bucket index should be settled in one of candidates
selfStake := csm.ContainsSelfStakingBucket(bucket.Index)
selfStake := containsSelfStakingBucket(bucket.Index)
if featureCtx.DisableDelegateEndorsement || !selfStake {
return selfStake, nil
}
Expand All @@ -722,7 +736,7 @@ func isSelfStakeBucket(featureCtx protocol.FeatureCtx, csm CandidateStateManager
return !bucket.isUnstaked(), nil
}
// otherwise bucket should be an endorse bucket which is not expired
esm := NewEndorsementStateManager(csm.SM())
esm := NewEndorsementStateReader(sr)
height, err := esm.Height()
if err != nil {
return false, err
Expand Down

0 comments on commit 55a48e1

Please sign in to comment.