Skip to content
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] Handling CandidateEndorsement Action #4020

Merged
merged 7 commits into from
Jan 30, 2024

Conversation

envestcc
Copy link
Member

Description

Implement action handling for CandidateEndorsement

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • [] make test
  • [] fullsync
  • [] Other test (please specify)

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • [] My code follows the style guidelines of this project
  • [] I have performed a self-review of my code
  • [] I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [] My changes generate no new warnings
  • [] I have added tests that prove my fix is effective or that my feature works
  • [] New and existing unit tests pass locally with my changes
  • [] Any dependent changes have been merged and published in downstream modules

@envestcc envestcc changed the title [staking] Handling CandidateEndorsement Action (based on 4017,4018,4019) [staking] Handling CandidateEndorsement Action (based on 4018,4019) Jan 5, 2024
Copy link

codecov bot commented Jan 5, 2024

Codecov Report

Attention: 547 lines in your changes are missing coverage. Please review.

Comparison is base (e1f0636) 75.38% compared to head (df180c4) 76.20%.
Report is 165 commits behind head on master.

Files Patch % Lines
action/protocol/staking/staking_statereader.go 69.76% 35 Missing and 17 partials ⚠️
action/protocol/execution/evm/evm.go 48.38% 47 Missing and 1 partial ⚠️
api/coreservice.go 63.24% 37 Missing and 6 partials ⚠️
api/web3server.go 79.24% 30 Missing and 3 partials ⚠️
action/candidate_endorsement.go 0.00% 31 Missing ⚠️
action/protocol/staking/protocol.go 45.09% 28 Missing ⚠️
action/candidate_activate.go 0.00% 25 Missing ⚠️
...tion/protocol/staking/contractstake_bucket_type.go 0.00% 24 Missing ⚠️
action/protocol/rewarding/fund.go 23.07% 19 Missing and 1 partial ⚠️
api/websocket.go 0.00% 20 Missing ⚠️
... and 43 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4020      +/-   ##
==========================================
+ Coverage   75.38%   76.20%   +0.81%     
==========================================
  Files         303      339      +36     
  Lines       25923    28862    +2939     
==========================================
+ Hits        19541    21993    +2452     
- Misses       5360     5746     +386     
- Partials     1022     1123     +101     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@envestcc envestcc marked this pull request as ready for review January 5, 2024 12:06
@envestcc envestcc changed the title [staking] Handling CandidateEndorsement Action (based on 4018,4019) [staking] Handling CandidateEndorsement Action (based on 4019) Jan 10, 2024
@envestcc envestcc force-pushed the pr_endorsehandle branch 2 times, most recently from 57e706b to 0241d03 Compare January 12, 2024 15:39
@envestcc envestcc changed the title [staking] Handling CandidateEndorsement Action (based on 4019) [staking] Handling CandidateEndorsement Action Jan 12, 2024
@envestcc envestcc force-pushed the pr_endorsehandle branch 2 times, most recently from ab2e5ad to 494acb0 Compare January 18, 2024 01:57
if err != nil {
return log, nil, err
}
return log, nil, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need if, just return log, nil, err

}
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes(), []byte{byteutil.BoolToByte(act.Endorse())})

var err error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move esm := NewEndorsementStateManager(csm.SM()) to here
var (
err error
esm = ...
)

if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: endorsementNotExpireHeight,
}); err != nil {
return csmErrorToHandleError(caller.String(), err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for csm to use? i. e., when the value/type of err is related to candidate error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean, it will return a regular err, not the handleError you may want

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should return regular err here, b/c the error returned must be some unknown errors from db

if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: expireHeight,
}); err != nil {
return csmErrorToHandleError(caller.String(), err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


var err error
if act.Endorse() {
err = p.endorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expireHeight = endorsementNotExpireHeight
if err := p.validateEndorsement(ctx, csm, esm, caller, bucket, cand); err != nil {
    return err // wrap it
}

if act.Endorse() {
err = p.endorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
} else {
err = p.unEndorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        if err := validateWithdrawal(ctx, esm, caller, bucket); err != nil {
		return err // wrap it
	}

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

} else {
err = p.unEndorseCandidate(ctx, csm, esm, actCtx.Caller, bucket, cand)
}
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        if err := esm.Put(bucket.Index, &Endorsement{
		ExpireHeight: expireHeight,
	}); err != nil {
		return log, nil, csmErrorToHandleError(caller.String(), err)
	}
	return log, nil, nil


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the case for a regular (not self-stake) endorsed bucket?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It expire immediately if the bucket is not self-staked, otherwise, expire after unendorse waiting period. Also added code comments for this


func (p *Protocol) validateUnEndorse(ctx context.Context, esm *EndorsementStateManager, caller address.Address, bucket *VoteBucket) ReceiptError {
blkCtx := protocol.MustGetBlockCtx(ctx)
if err := validateBucketOwner(bucket, caller); err != nil {
Copy link
Member

@dustinxie dustinxie Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check other conditions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two checks should be enough:

  • only bucket owner is authorized
  • bucket should be endorsed

// new endorsement with not expire height
if err := esm.Put(bucket.Index, &Endorsement{
ExpireHeight: endorsementNotExpireHeight,
}); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part is the same, can move to common end

uint64 expireHeight
if act.Endorse() {
   xxx
   expireHeight = endorsementNotExpireHeight,
} else {
   xxx
   expireHeight := protocol.MustGetBlockCtx(ctx).BlockHeight
   if csm.ContainsSelfStakingBucket(bucket.Index) {
	expireHeight += p.config.EndorsementWithdrawWaitingBlocks
   }
}
esm.Put(bucket.Index, &Endorsement{
		ExpireHeight: expireHeight,
	}

@@ -31,30 +31,26 @@ func (p *Protocol) handleCandidateEndorsement(ctx context.Context, act *action.C
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes(), []byte{byteutil.BoolToByte(act.Endorse())})

esm := NewEndorsementStateManager(csm.SM())
// handle endorsement
expireHeight := uint64(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to use

var (
    expireHeight uint64
    esm = NewEndorsementStateManager(csm.SM())
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not my preference

@@ -31,30 +31,26 @@ func (p *Protocol) handleCandidateEndorsement(ctx context.Context, act *action.C
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes(), []byte{byteutil.BoolToByte(act.Endorse())})

esm := NewEndorsementStateManager(csm.SM())
// handle endorsement
expireHeight := uint64(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not my preference

WithdrawWaitingPeriod: 3 * 24 * time.Hour,
MinStakeAmount: unit.ConvertIotxToRau(100).String(),
BootstrapCandidates: []BootstrapCandidate{},
EndorsementWithdrawWaitingBlocks: 24 * 60 * 60 / 5,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the meaning of the value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After submitting the endorsement withdraw action, it takes the waiting blocks for it to take effect. During this period, the delegate can continue to participate in consensus, and the bucket is locked, meaning it cannot be unstaked or voted to others.


esm := NewEndorsementStateManager(csm.SM())
expireHeight := uint64(0)
if act.Endorse() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if act.IsEndorsed() {}

if err := validateBucketSelfStake(csm, bucket, false); err != nil {
return err
}
return validateBucketEndorsement(esm, bucket, false, protocol.MustGetBlockCtx(ctx).BlockHeight)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be as the method of endorsement object

Copy link

sonarcloud bot commented Jan 30, 2024

Quality Gate Failed Quality Gate failed

Failed conditions

4.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@envestcc envestcc merged commit ed9c857 into iotexproject:master Jan 30, 2024
3 of 5 checks passed
@envestcc envestcc deleted the pr_endorsehandle branch January 30, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants