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

Clean up candidates #3696

Merged
merged 10 commits into from
Nov 29, 2022
Merged

Clean up candidates #3696

merged 10 commits into from
Nov 29, 2022

Conversation

CoderZhi
Copy link
Collaborator

  1. After hardfork, fix logic bug of updating candidate name/operator
  2. Clean up candidate names and operator addresses

Fixes #(issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Code refactor or improvement
  • [] Breaking change (fix or feature that would cause a new or changed behavior of existing functionality)
  • [] This change requires a documentation update

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

@CoderZhi CoderZhi requested a review from a team as a code owner November 17, 2022 23:22
@codecov
Copy link

codecov bot commented Nov 17, 2022

Codecov Report

Merging #3696 (b158f8d) into master (263ff82) will decrease coverage by 0.73%.
The diff coverage is 87.24%.

@@            Coverage Diff             @@
##           master    #3696      +/-   ##
==========================================
- Coverage   74.47%   73.73%   -0.74%     
==========================================
  Files         269      276       +7     
  Lines       23925    24187     +262     
==========================================
+ Hits        17818    17835      +17     
- Misses       5174     5444     +270     
+ Partials      933      908      -25     
Impacted Files Coverage Δ
action/protocol/context.go 66.40% <0.00%> (-1.60%) ⬇️
action/protocol/staking/candidate_statereader.go 53.94% <ø> (-3.38%) ⬇️
db/db_bolt.go 68.53% <0.00%> (-3.60%) ⬇️
ioctl/newcmd/action/actiontransfer.go 100.00% <ø> (ø)
ioctl/newcmd/action/stake2withdraw.go 95.74% <ø> (ø)
ioctl/newcmd/action/xrc20transferfrom.go 100.00% <ø> (ø)
ioctl/newcmd/did/didgethash.go 100.00% <ø> (ø)
ioctl/newcmd/did/didgeturi.go 100.00% <ø> (ø)
server/itx/server.go 62.20% <ø> (+2.20%) ⬆️
action/protocol/staking/handlers.go 74.47% <33.33%> (-0.64%) ⬇️
... and 23 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -56,7 +56,7 @@ type (
Upsert(*Candidate) error
CreditBucketPool(*big.Int) error
DebitBucketPool(*big.Int, bool) error
Commit() error
Commit(bool) error
Copy link
Member

Choose a reason for hiding this comment

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

this fix should only happen one-time at the HF height, why need to add it as an extra input

func (vr *VoteReviser) storeToCache(height uint64, cands CandidateList) {
vr.cache[height] = cands
func (vr *VoteReviser) storeToCache(height uint64, dirty, cands CandidateList) {
vr.cache[height] = [2]CandidateList{dirty, cands}
Copy link
Member

@dustinxie dustinxie Nov 18, 2022

Choose a reason for hiding this comment

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

cache should only contain the correct candidates after revise, we should be able to do the fix w/o modifying these low-level implementations

return err
}
log.L().Info("committed revise action",
zap.String("name", cand.Name), zap.String("votes", cand.Votes.String()))
Copy link
Member

Choose a reason for hiding this comment

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

we should be able to do the fix w/o modifying these low-level implementations

@@ -440,14 +444,20 @@ func (cb *candBase) all() CandidateList {
return list
}

func (cb *candBase) commit(change *candChange) (int, error) {
func (cb *candBase) commit(change *candChange, keepAliasBug bool) (int, error) {
Copy link
Member

Choose a reason for hiding this comment

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

if we could have the flag in the candBase, then I think it's inefficient to pass it down every time

@@ -17,7 +17,8 @@ import (
type (
// candChange captures the change to candidates
candChange struct {
dirty map[string]*Candidate
candidates []*Candidate
dirty map[string]*Candidate
Copy link
Member

Choose a reason for hiding this comment

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

we should be able to do the fix w/o modifying these low-level implementations

dirty = append(dirty, c)
}
for _, c := range csm.DirtyView().candCenter.base.operatorMap {
dirty = append(dirty, c)
Copy link
Member

Choose a reason for hiding this comment

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

reviser should not bother/deal with candCenter.base.operatorMap

return nil
}

// LegacyCommit writes the change into base with legacy logic
Copy link
Member

Choose a reason for hiding this comment

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

It is cleaner than the bool argument.

ownerMap[addr] = owner
}
for _, c := range cands {
if owner, ok := ownerMap[c.Owner.String()]; ok {
Copy link
Member

Choose a reason for hiding this comment

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

if owner, ok := csm.DirtyView().candCenter.base.ownerMap[c.Owner.String()]; ok {}

@Liuhaai
Copy link
Member

Liuhaai commented Nov 19, 2022

Discussed offline. The idea of this pr is easier to understand

}
for _, c := range csm.DirtyView().candCenter.base.operatorMap {
retval = append(retval, c)
}
Copy link
Member

@dustinxie dustinxie Nov 20, 2022

Choose a reason for hiding this comment

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

we should not use name/operatorMap here? don't quite understand the code.

here's my understanding of the intended logic per our discussion during v1.8.4 fix:

input cands is the list of candidates (with vote correctly revised) in the statedb, as done in v1.8.4 fix, we saved the list of candidates that have been called CandidateRegister/Update actions during the affected period, these are candidates that can potentially have an alias in the candidate center

_, _, owners := readCandCenterStateFromStateDB()

so on top of cands, update the name/operator/reward of owners, that is what we need to do

Copy link
Member

@dustinxie dustinxie Nov 20, 2022

Choose a reason for hiding this comment

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

another comment is that VoteReviser logic is clean, it just reads candidates/buckets from statedb, and tally votes again.

now it needs to deal with csm.DirtyView().candCenter.xxx, this adds unnecessary code dependency/coupling

in other words, VoteReviser is designed to work w/o knowing the existence of candCenter, then it should be kept that way

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

name, operator, owners, err := readCandCenterStateFromStateDB(sr, height)
has called and loaded the data.
csm.DirtyView().candCenter.base.recordOwner(c)
candcenter has been used in various places, which is a legacy issue

Copy link
Member

Choose a reason for hiding this comment

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

yes, ideally we should not use csm.DirtyView().candCenter.xxx in protocol.go and handlers.go
so we should not expand it into vote_reviser.go if possible

Copy link
Member

@dustinxie dustinxie Nov 28, 2022

Choose a reason for hiding this comment

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

name, operator, owners, err := readCandCenterStateFromStateDB(sr, height), will NOT be called at next HF height
even if it's read, seems owners is not used in this PR

@sonarcloud
Copy link

sonarcloud bot commented Nov 28, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@CoderZhi CoderZhi merged commit c316b81 into master Nov 29, 2022
@dustinxie dustinxie deleted the revise_candidates branch December 17, 2022 04:07
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