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

[contractstaking] improve robustness #3892

Merged
merged 4 commits into from
Jun 26, 2023

Conversation

envestcc
Copy link
Member

Description

improve code robustness according to new comments in #3888

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

@codecov
Copy link

codecov bot commented Jun 22, 2023

Codecov Report

Merging #3892 (26e0cb1) into master (e1f0636) will increase coverage by 0.19%.
The diff coverage is 67.20%.

❗ Current head 26e0cb1 differs from pull request most recent head bbb26ed. Consider uploading reports for the commit bbb26ed to get more accurate results

@@            Coverage Diff             @@
##           master    #3892      +/-   ##
==========================================
+ Coverage   75.38%   75.57%   +0.19%     
==========================================
  Files         303      328      +25     
  Lines       25923    27808    +1885     
==========================================
+ Hits        19541    21016    +1475     
- Misses       5360     5716     +356     
- Partials     1022     1076      +54     
Impacted Files Coverage Δ
action/protocol/execution/evm/evm.go 43.52% <0.00%> (-2.95%) ⬇️
action/protocol/execution/evm/evmstatedbadapter.go 66.77% <ø> (ø)
action/protocol/poll/consortium.go 0.00% <0.00%> (ø)
action/protocol/poll/staking_committee.go 43.85% <0.00%> (ø)
...tion/protocol/staking/contractstake_bucket_type.go 0.00% <0.00%> (ø)
api/grpcserver.go 86.40% <0.00%> (ø)
api/loglistener.go 25.00% <0.00%> (ø)
api/web3server_marshal.go 93.21% <ø> (ø)
api/websocket.go 5.17% <0.00%> (ø)
blockchain/config.go 74.54% <ø> (ø)
... and 51 more

... and 4 files with indirect coverage changes

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

if !errors.Is(err, db.ErrNotExist) {
return err
}
height = 0
Copy link
Member

Choose a reason for hiding this comment

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

add totalBucketCount = 0 to L247 below, or remove this line, to be consistent

s.bucketTypeMap[id] = bt
// add new bucket type map
Copy link
Member

Choose a reason for hiding this comment

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

combine 2 comments into 1 line

oldDelegate := oldBi.Delegate.String()
if oldDelegate != newDelegate {
delete(s.candidateBucketMap[oldDelegate], id)
}
Copy link
Member

@dustinxie dustinxie Jun 22, 2023

Choose a reason for hiding this comment

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

how likely is the case old bucket needs to be deleted? if it's not too much (like < 5-10%), then I think just add 2 lines (to delete empty map) here would suffice:

if len(s.candidateBucketMap[oldDelegate]) == 0 {
				delete(s.candidateBucketMap, oldDelegate)
			}

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, if most case is to update the same bucket, then the new code is not efficient, it is doing

delete(map, 5)
map[5] = bucket

while the first delete is wasted

Copy link
Member

Choose a reason for hiding this comment

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

the efficiency doesn't matter much here, otherwise more if condition will harm readability

Copy link
Member

Choose a reason for hiding this comment

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

given the same correct logic, i would say we should aim for better code
the if here indicates the need to remove old/existing bucket, it's not too excessive if i think

@@ -23,6 +23,7 @@ type (
bucketTypeMap map[uint64]*BucketType // map[bucketTypeId]BucketType
propertyBucketTypeMap map[int64]map[uint64]uint64 // map[amount][duration]index
totalBucketCount uint64 // total number of buckets including burned buckets
height uint64 // current block height
Copy link
Member

Choose a reason for hiding this comment

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

add comments for the reason to put height in the cache

@sonarcloud
Copy link

sonarcloud bot commented Jun 26, 2023

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 13f4826 into iotexproject:master Jun 26, 2023
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.

3 participants