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] Candidate Register without Staking #4059

Merged
merged 12 commits into from
Mar 11, 2024

Conversation

envestcc
Copy link
Member

@envestcc envestcc commented Jan 18, 2024

Description

We expanded the action CandidateRegister to support registration without staking. However, in the absence of self-staking, they will not be selected as consensus nodes.

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause a new or changed behavior of existing 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

Copy link

codecov bot commented Jan 18, 2024

Codecov Report

Attention: Patch coverage is 56.92308% with 28 lines in your changes are missing coverage. Please review.

Project coverage is 76.36%. Comparing base (e1f0636) to head (ef0e99a).
Report is 193 commits behind head on master.

Files Patch % Lines
action/signedaction.go 0.00% 20 Missing ⚠️
action/protocol/staking/handlers.go 80.00% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4059      +/-   ##
==========================================
+ Coverage   75.38%   76.36%   +0.98%     
==========================================
  Files         303      340      +37     
  Lines       25923    29022    +3099     
==========================================
+ Hits        19541    22163    +2622     
- Misses       5360     5754     +394     
- Partials     1022     1105      +83     

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

@@ -66,7 +70,11 @@ func (p *Protocol) validateCandidateRegister(ctx context.Context, act *action.Ca
}

if act.Amount().Cmp(p.config.RegistrationConsts.MinSelfStake) < 0 {
return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid")
featureCtx := protocol.MustGetFeatureCtx(ctx)
if featureCtx.CandidateRegisterMustWithStake || act.Amount().Cmp(big.NewInt(0)) != 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if featureCtx.CandidateRegisterMustWithStake always return error?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, only featureCtx.CandidateRegisterMustWithStake==false && act.Amount()==0 is valid if act.Amount() < MinSelfStake

Copy link
Member

Choose a reason for hiding this comment

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

!featureCtx.CandidateRegisterMustWithStake && act.Amount()==0 is easier to read than featureCtx.CandidateRegisterMustWithStake || act.Amount().Cmp(big.NewInt(0)) != 0

// register with self-stake
bucketIdx := uint64(candidateNoSelfStakeBucketIndex)
var bucketLogs []*action.TransactionLog
votes := big.NewInt(0)
Copy link
Member

Choose a reason for hiding this comment

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

use var () to put 3 together

@@ -529,7 +532,7 @@ func (cb *candBase) collision(d *Candidate) (address.Address, address.Address, a
oper = c.Owner
}

if c, hit := cb.selfStkBucketMap[d.SelfStakeBucketIdx]; hit && !address.Equal(c.Owner, d.Owner) {
if c, hit := cb.selfStkBucketMap[d.SelfStakeBucketIdx]; hit && d.SelfStakeBucketIdx != candidateNoSelfStakeBucketIndex && !address.Equal(c.Owner, d.Owner) {
Copy link
Member

Choose a reason for hiding this comment

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

change is this file should use what's in PR4061, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, it's part of PR4061

if err != nil {
return log, nil, err
}
bucketIdx = bktIdx
Copy link
Member

Choose a reason for hiding this comment

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

L672: bucketIdx, err = csm.xxx, no need to have a new var bktIdx here

Amount: registrationFee,
},
}, nil
txLogs := append(bucketLogs, &action.TransactionLog{
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 to define a new txLogs? can use existing bucketLogs
i think rename bucketLogs to txLogs to make the name more clear

Amount: act.Amount(),
})
votes = p.calculateVoteWeight(bucket, true)
log.AddTopics(byteutil.Uint64ToBytesBigEndian(bucketIdx), owner.Bytes())
Copy link
Member

Choose a reason for hiding this comment

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

this needs to be outside of if{} -- registration without self-stake should also create a topic in the log

Copy link

sonarcloud bot commented Jan 30, 2024

Quality Gate Failed Quality Gate failed

Failed conditions

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

See analysis details on SonarCloud

return errors.Wrap(action.ErrInvalidAmount, "self staking amount is not valid")
featureCtx := protocol.MustGetFeatureCtx(ctx)
if featureCtx.CandidateRegisterMustWithStake || act.Amount().Cmp(big.NewInt(0)) != 0 {
log.L().Warn("Candidate register amount is less than the minimum requirement", zap.Bool("feature", featureCtx.CandidateRegisterMustWithStake), zap.String("amount", act.Amount().String()))
Copy link
Member

Choose a reason for hiding this comment

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

i think the error has been logged upstream

votes = big.NewInt(0)
err error
)
if act.Amount().Sign() > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

no hardfork here?

Copy link
Member Author

@envestcc envestcc Mar 7, 2024

Choose a reason for hiding this comment

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

Before the hard fork, the act.Amount must be greater than 0 (garunteed by action validation), behaves the same as in the old version.

Copy link
Contributor

Choose a reason for hiding this comment

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

if act.Amount().Sign() <= 0 { should be return error?

Copy link
Member Author

Choose a reason for hiding this comment

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

bucketIdx, err := csm.putBucketAndIndex(bucket)
if err != nil {
return log, nil, err
// register with self-stake
Copy link
Member

Choose a reason for hiding this comment

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

move to L713

Recipient: address.StakingBucketPoolAddr,
Amount: act.Amount(),
})
votes = p.calculateVoteWeight(bucket, true)
}
Copy link
Member

Choose a reason for hiding this comment

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

think adding an else make the code more explicit

else {
    // register w/o self-stake, waiting to be endorsed
   bucketIdx = uint64(candidateNoSelfStakeBucketIndex)
   votes  = big.NewInt(0)
}

bucketIdx = uint64(candidateNoSelfStakeBucketIndex)
txLogs []*action.TransactionLog
votes = big.NewInt(0)
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.

  1. withSelfStake = act.Amount().Sign() > 0
  2. err error not ncessary?

votes = big.NewInt(0)
err error
)
if act.Amount().Sign() > 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 withSelfStake {

return log, nil, &handleError{
err: errors.Wrapf(err, "failed to update staking bucket pool %s", err.Error()),
failureStatus: iotextypes.ReceiptStatus_ErrWriteAccount,
if act.Amount().Sign() > 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 withSelfStake {

@envestcc envestcc requested a review from a team as a code owner March 11, 2024 03:02
Copy link

sonarcloud bot commented Mar 11, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
5.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

@envestcc envestcc merged commit 9505d91 into iotexproject:master Mar 11, 2024
2 of 5 checks passed
@envestcc envestcc deleted the pr-registernonstake branch March 11, 2024 03:35
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.

5 participants