Skip to content

Commit

Permalink
expire height according to self-stake
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jan 18, 2024
1 parent 1bba61f commit 494acb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 15 additions & 15 deletions action/protocol/staking/handler_candidate_endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,30 @@ import (

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

const (
handleCandidateEndorsement = "candidateEndorsement"
)

func (p *Protocol) handleCandidateEndorsement(ctx context.Context, act *action.CandidateEndorsement, csm CandidateStateManager) (*receiptLog, []*action.TransactionLog, error) {
var (
bucket *VoteBucket
err error
rErr ReceiptError
txLogs []*action.TransactionLog
cand *Candidate
actCtx := protocol.MustGetActionCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
log := newReceiptLog(p.addr.String(), handleCandidateEndorsement, featureCtx.NewStakingReceiptFormat)

Check warning on line 20 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L17-L20

Added lines #L17 - L20 were not covered by tests

actCtx = protocol.MustGetActionCtx(ctx)
featureCtx = protocol.MustGetFeatureCtx(ctx)
log = newReceiptLog(p.addr.String(), handleCandidateEndorsement, featureCtx.NewStakingReceiptFormat)
)
esm := NewEndorsementStateManager(csm.SM())
bucket, rErr = p.fetchBucket(csm, act.BucketIndex())
bucket, rErr := p.fetchBucket(csm, act.BucketIndex())
if rErr != nil {
return log, nil, rErr

Check warning on line 25 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L22-L25

Added lines #L22 - L25 were not covered by tests
}
cand = csm.GetByOwner(bucket.Candidate)
cand := csm.GetByOwner(bucket.Candidate)
if cand == nil {
return log, nil, errCandNotExist

Check warning on line 29 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L27-L29

Added lines #L27 - L29 were not covered by tests
}
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes(), []byte{byteutil.BoolToByte(act.Endorse())})

Check warning on line 31 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L31

Added line #L31 was not covered by tests

var err error
if act.Endorse() {
err = p.endorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
} else {
Expand All @@ -43,8 +39,7 @@ func (p *Protocol) handleCandidateEndorsement(ctx context.Context, act *action.C
if err != nil {

Check warning on line 39 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L39

Added line #L39 was not covered by tests
return log, nil, err
}

return log, txLogs, nil
return log, nil, nil

Check warning on line 42 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L42

Added line #L42 was not covered by tests
}

func (p *Protocol) endorseCandidate(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket, cand *Candidate) error {
Expand All @@ -66,8 +61,13 @@ func (p *Protocol) unEndorseCandidate(ctx context.Context, csm CandidateStateMan
if err := p.validateUnEndorse(ctx, esm, caller, bucket); err != nil {
return err

Check warning on line 62 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

expireHeight := blkCtx.BlockHeight
if csm.ContainsSelfStakingBucket(bucket.Index) {
expireHeight += p.config.UnEndorseWaitingBlocks

Check warning on line 67 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}
if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: blkCtx.BlockHeight + p.config.UnEndorseWaitingBlocks,
ExpireHeight: expireHeight,
}); err != nil {

Check warning on line 71 in action/protocol/staking/handler_candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/handler_candidate_endorsement.go#L69-L71

Added lines #L69 - L71 were not covered by tests
return csmErrorToHandleError(caller.String(), err)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/byteutil/byteutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ func Uint64ToBytesBigEndian(value uint64) []byte {
func BytesToUint64BigEndian(value []byte) uint64 {
return binary.BigEndian.Uint64(value)
}

// BoolToByte converts bool to byte
func BoolToByte(value bool) byte {
if value {
return 1
}
return 0
}

0 comments on commit 494acb0

Please sign in to comment.