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] persist cand center to statedb and patchfile #3661

Merged
merged 4 commits into from
Oct 14, 2022
Merged

Conversation

dustinxie
Copy link
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

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)

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

@dustinxie dustinxie requested a review from a team as a code owner October 12, 2022 16:26
@dustinxie dustinxie force-pushed the mapfix branch 2 times, most recently from d5a0db6 to 69fc63f Compare October 12, 2022 17:46
func (cb *candBase) ownersList() CandidateList {
cb.lock.RLock()
defer cb.lock.RUnlock()
if len(cb.owners) == 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

zhi: delete

Copy link
Member Author

Choose a reason for hiding this comment

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

if ownerList() is called before recordOwner(), cb.owners is nil, so need to populate it

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

}
for i, d := range cb.owners {
if d.Owner.String() == c.Owner.String() {
cb.owners[i] = c.Clone()
Copy link
Member Author

Choose a reason for hiding this comment

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

zhi: if nameMap and operatorMap could be type of map, why owners is a list?

Copy link
Member Author

Choose a reason for hiding this comment

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

b/c outside calls GetByName() and GetByOperator(), map better suits that need.
owners is only updated by candidate update/register tx, a list is easier and suffices for the use-case

delCandBucketIndex(addr address.Address, index uint64) error
putCandidate(*Candidate) error
delCandidate(address.Address) error
putCandidateList(CandidateList, []byte) 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.

zhi: why putCandidateList is a function of interface?

Copy link
Member Author

Choose a reason for hiding this comment

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

b/c we are calling it from the CandidateStateManager interface, for the same reason why putCandidate is a func of interface here

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 add. They are under the same pkg

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@@ -684,6 +684,7 @@ func (p *Protocol) handleCandidateRegister(ctx context.Context, act *action.Cand
if err := csm.Upsert(c); err != nil {
return log, nil, csmErrorToHandleError(owner.String(), err)
}
csm.DirtyView().candCenter.base.recordOwner(c)
Copy link
Member Author

Choose a reason for hiding this comment

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

zhi: put into csm.Upsert

Copy link
Member Author

@dustinxie dustinxie Oct 12, 2022

Choose a reason for hiding this comment

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

I also hesitate to do this, but we need to change to Upsert(*Candidate, bool)

  1. a bool flag to indicate a specific purpose (save the correct candidate) is kind of weird here
  2. too many changes

Let me know if you have a better option, or Upsert(*Candidate, bool) is fine, I can modify

var (
_nameKey = []byte("name")
_operatorKey = []byte("oper")
_ownerKey = []byte("owne")
Copy link
Member Author

Choose a reason for hiding this comment

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

zhi: using names, operators, owners won't take too many disk spaces

Copy link
Member Author

Choose a reason for hiding this comment

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

right, disk space is not a concern here
I prefer for these meta keys to have the same length, similar to namespace in filedaov2

Copy link
Member

Choose a reason for hiding this comment

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

im also confused here

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

name := base.candsInNameMap()
op := base.candsInOperatorMap()
owners := base.ownersList()
if len(name) == 0 || len(op) == 0 || len(owners) == 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

zhi: invalid checking

Copy link
Member Author

Choose a reason for hiding this comment

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

for our real use-case, it is true.
I'd just keep it for safety

@@ -76,6 +81,7 @@ var (
ChainDBPath: "/var/data/chain.db",
TrieDBPatchFile: "/var/data/trie.db.patch",
TrieDBPath: "/var/data/trie.db",
CandsMapPatchFile: "/var/data/candsmap.patch",
Copy link
Member Author

Choose a reason for hiding this comment

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

@CoderZhi is this name good? it will create file like candsmap.patch-19xxxx


package staking

func (cb *candBase) clone() *candBase {
Copy link
Member

@Liuhaai Liuhaai Oct 12, 2022

Choose a reason for hiding this comment

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

don't create two files for same class.Create another class if a new file is wanted

@codecov
Copy link

codecov bot commented Oct 12, 2022

Codecov Report

Merging #3661 (263ff82) into master (e9732a1) will decrease coverage by 0.48%.
The diff coverage is 65.92%.

❗ Current head 263ff82 differs from pull request most recent head cfc602e. Consider uploading reports for the commit cfc602e to get more accurate results

@@            Coverage Diff             @@
##           master    #3661      +/-   ##
==========================================
- Coverage   74.95%   74.47%   -0.49%     
==========================================
  Files         269      269              
  Lines       23819    23925     +106     
==========================================
- Hits        17854    17818      -36     
- Misses       5039     5174     +135     
- Partials      926      933       +7     
Impacted Files Coverage Δ
action/protocol/protocol.go 0.00% <ø> (ø)
action/protocol/staking/candidate_statemanager.go 98.59% <ø> (+1.36%) ⬆️
blockchain/config.go 74.54% <ø> (ø)
db/config.go 100.00% <ø> (ø)
db/trie/mptrie/node.go 100.00% <ø> (ø)
ioctl/newcmd/action/action.go 89.09% <ø> (+9.55%) ⬆️
ioctl/newcmd/node/nodedelegate.go 70.19% <0.00%> (-0.95%) ⬇️
ioctl/newcmd/node/nodeprobationlist.go 90.90% <0.00%> (-4.33%) ⬇️
action/protocol/staking/candidate_center_extra.go 13.55% <13.55%> (ø)
server/itx/server.go 60.00% <25.00%> (-2.21%) ⬇️
... and 35 more

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

@@ -81,6 +83,9 @@ type (
candBucketsIndexer *CandidatesBucketsIndexer
voteReviser *VoteReviser
patch *PatchStore
_nameHash string
_opHash string
_ownerHash string
Copy link
Member Author

Choose a reason for hiding this comment

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

debug code, will be removed before check-in

println("======= same owner list", p._ownerHash)
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

debug code, will be removed before check-in

BootstrapCandidates: cfg.BootstrapCandidates,
BootstrapCandidates: cfg.Staking.BootstrapCandidates,
PersistCandsMapBlock: cfg.PersistCandsMapBlock,
CandsMapPatchDir: cfg.CandsMapPatchDir,
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

},
patch:  NewPatchStore(cfg.CandsMapPatchDir),

Comment on lines 641 to 643
if p.patch == nil {
p.patch = NewPatchStore(p.config.CandsMapPatchDir)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

@@ -28,6 +28,7 @@ type (
ChainDBPath string `yaml:"chainDBPath"`
TrieDBPatchFile string `yaml:"trieDBPatchFile"`
TrieDBPath string `yaml:"trieDBPath"`
CandsMapPatchDir string `yaml:"candsMapPatchDir"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

StakingPatchDir string yaml:"stakingPatchDir"`

@@ -67,6 +68,8 @@ type (
WorkingSetCacheSize uint64 `yaml:"workingSetCacheSize"`
// StreamingBlockBufferSize
StreamingBlockBufferSize uint64 `yaml:"streamingBlockBufferSize"`
// PersistCandsMapBlock is the block to persist candidates map
PersistCandsMapBlock uint64 `yaml:"persistCandsMapBlock"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

PersistStakingPatchBlock

func (cb *candBase) ownersList() CandidateList {
cb.lock.RLock()
defer cb.lock.RUnlock()
return cb.owners
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

@dustinxie dustinxie Oct 14, 2022

Choose a reason for hiding this comment

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

name, op := candsInNameMap(), candsInOperatorMap()
name, op will be serialized and saved to DB, so actually no need to clone() for the 2 places you mentioned, clone() is for safety I believe since we don't want to touch the *Candidate in these 2 maps.
for ownerList, the only use of it is to serialize and save to DB, so I think no need to clone the list here (b/c when we add a *Candidate to ownerList, we make a clone, L77 below)

if err := d.Validate(); err != nil {
return err
}
cb.nameMap[d.Name] = d
}
cb.operatorMap[d.Operator.String()] = d
}
cb.owners = owners
Copy link
Member Author

Choose a reason for hiding this comment

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

owners at right side is created by de-serializing data read from DB, it is a new instance of list, so no need to clone here

@sonarcloud
Copy link

sonarcloud bot commented Oct 14, 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

@dustinxie dustinxie merged commit cfc602e into master Oct 14, 2022
@dustinxie dustinxie deleted the mapfix branch October 14, 2022 03:57
@dustinxie dustinxie mentioned this pull request Oct 14, 2022
@dustinxie dustinxie mentioned this pull request Oct 14, 2022
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