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 CandidateSelfStake Action #4011

Merged
merged 6 commits into from
Jan 18, 2024

Conversation

envestcc
Copy link
Member

@envestcc envestcc commented Dec 7, 2023

Description

CandidateSelfStake action accept a bucket as the self-stake bucket of the sender's candidate. The bucket can be:

  • sender's bucket with enough amount
  • a bucket endorsed to the candidate

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 CandidateSelfStake Action (based on #4007, #4008, #4010) [staking] Handling CandidateSelfStake Action (based on #4007) Dec 28, 2023
@envestcc envestcc changed the title [staking] Handling CandidateSelfStake Action (based on #4007) [staking] Handling CandidateSelfStake Action Jan 2, 2024
@envestcc envestcc force-pushed the pr_handleselfstake branch 2 times, most recently from 0ed2fea to 4a4c1c1 Compare January 10, 2024 12:34
@envestcc envestcc marked this pull request as ready for review January 12, 2024 16:03
Copy link

codecov bot commented Jan 15, 2024

Codecov Report

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

Comparison is base (e1f0636) 75.38% compared to head (494013e) 76.19%.
Report is 157 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 62.74% 33 Missing and 5 partials ⚠️
api/web3server.go 79.24% 30 Missing and 3 partials ⚠️
action/candidate_endorsement.go 0.00% 31 Missing ⚠️
action/protocol/staking/protocol.go 36.36% 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 35 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4011      +/-   ##
==========================================
+ Coverage   75.38%   76.19%   +0.81%     
==========================================
  Files         303      338      +35     
  Lines       25923    28774    +2851     
==========================================
+ Hits        19541    21925    +2384     
- Misses       5360     5733     +373     
- Partials     1022     1116      +94     

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

}
if bucketCand = csm.GetByOwner(bucket.Candidate); bucketCand == nil {
return log, nil, &handleError{
err: errors.New("bucket candidate does not exist"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

global variable

Copy link
Member Author

Choose a reason for hiding this comment

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

deleted, b/c candidate has been validated in p.validateBucketSelfStake()

Comment on lines 42 to 47
if cand.SelfStakeBucketIdx != candidateNoSelfStakeBucketIndex {
prevBucket, err = p.fetchBucket(csm, cand.SelfStakeBucketIdx)
if err != nil {
return log, nil, err
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

move to line 65

if err := validateBucketCandidate(bucket, cand.Owner); err != nil {
return err
}
if validateBucketOwner(bucket, cand.Owner) != nil &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

&& or ||?

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 be &&, it means only owned or endorsed bucket can be used to activate candidate self

Comment on lines 145 to 147
if err := csm.Upsert(cand); err != nil {
return csmErrorToHandleError(cand.Owner.String(), err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

skip?

Copy link
Member Author

Choose a reason for hiding this comment

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

function deleted, b/c bucket must be voted to the candidate

cand.SelfStake = big.NewInt(0).SetBytes(bucket.StakedAmount.Bytes())
prevVotes := p.calculateVoteWeight(bucket, false)
postVotes := p.calculateVoteWeight(bucket, true)
if err := cand.AddVote(postVotes.Sub(postVotes, prevVotes)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

if AddVote err, does the SubVote above need sto rollback? any changes to db here should be put into a transaction.

Copy link
Member Author

Choose a reason for hiding this comment

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

ACID is guaranteed by workingSet

Copy link
Member

Choose a reason for hiding this comment

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

just cand.AddVote(post), cand.SubVote(prev)


func (p *Protocol) validateBucketSelfStake(ctx context.Context, csm CandidateStateManager, esm *EndorsementStateManager, bucket *VoteBucket, cand *Candidate) ReceiptError {
blkCtx := protocol.MustGetBlockCtx(ctx)
if err := validateBucketMinAmount(bucket, p.config.RegistrationConsts.MinSelfStake); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe better

validateFn = []func()error
for _,fn := range validateFn {
  if err := fn(); err != nil {
    return err
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Their parameters are different, i think no need to enforce using same interface.

Comment on lines 48 to 50
prevVotes := p.calculateVoteWeight(prevBucket, true)
postVotes := p.calculateVoteWeight(prevBucket, false)
if err := cand.SubVote(prevVotes.Sub(prevVotes, postVotes)); 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.

more readable

Copy link
Member

Choose a reason for hiding this comment

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

+1, i think just cand.AddVote(post), cand.SubVote(prev)

Copy link
Member Author

Choose a reason for hiding this comment

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

udpated

if err := cand.SubVote(prevVotes.Sub(prevVotes, postVotes)); err != nil {
return log, nil, err
}
cand.SelfStake = big.NewInt(0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

candidateNoSelfStakeBucketIndex = math.MaxUint64
)

func (p *Protocol) handleCandidateSelfStake(ctx context.Context, act *action.CandidateActivate, csm CandidateStateManager,
Copy link
Member

Choose a reason for hiding this comment

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

handleCandidateActivate

)

const (
handleCandidateSelfStake = "candidateSelfStake"
Copy link
Member

Choose a reason for hiding this comment

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

_handleCandidateActivate

log := newReceiptLog(p.addr.String(), handleCandidateSelfStake, featureCtx.NewStakingReceiptFormat)

// caller must be the owner of a candidate
cand := csm.GetByOwner(actCtx.Caller)
Copy link
Member

Choose a reason for hiding this comment

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

move this to after get and validate bucket, to be consistent with other handlers

const (
handleCandidateActivate = "candidateActivate"

candidateNoSelfStakeBucketIndex = math.MaxUint64
Copy link
Member

Choose a reason for hiding this comment

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

remove since it's only used in test, and why is it needed in test?

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 will be used when register candidate without stake

) (*receiptLog, []*action.TransactionLog, error) {
actCtx := protocol.MustGetActionCtx(ctx)
featureCtx := protocol.MustGetFeatureCtx(ctx)
log := newReceiptLog(p.addr.String(), handleCandidateActivate, featureCtx.NewStakingReceiptFormat)
Copy link
Member

Choose a reason for hiding this comment

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

add a new line?


log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucket.Index), bucket.Candidate.Bytes())
// convert previous self-stake bucket to vote bucket
if cand.SelfStake.Cmp(big.NewInt(0)) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

if cand.SelfStake.Sign() > 0 is more efficient and clear

Copy link
Member Author

Choose a reason for hiding this comment

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

good suggest


// convert vote bucket to self-stake bucket
cand.SelfStakeBucketIdx = bucket.Index
cand.SelfStake = big.NewInt(0).SetBytes(bucket.StakedAmount.Bytes())
Copy link
Member

Choose a reason for hiding this comment

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

cand.SelfStake is an existing big.Int, no need to new again, so:
cand.SelfStake.SetBytes(bucket.StakedAmount.Bytes())

Copy link
Member Author

Choose a reason for hiding this comment

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

okay

if err := csm.Upsert(cand); err != nil {
return log, nil, csmErrorToHandleError(cand.Owner.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.

remove new line

Copy link

sonarcloud bot commented Jan 17, 2024

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

2 New issues
0 Security Hotspots
No data about Coverage
2.8% Duplication on New Code

See analysis details on SonarCloud

@dustinxie dustinxie merged commit d4ba76e into iotexproject:master Jan 18, 2024
4 of 5 checks passed
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