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

[IIP-13] handle contract staking events to store buckets into index (3/3 split by 3853) #3863

Merged
merged 17 commits into from
May 25, 2023

Conversation

envestcc
Copy link
Member

Description

For #3853 is too large to review, it has been splited into three small pr. This is the last one.
It's based on #3862

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

@envestcc envestcc marked this pull request as ready for review May 19, 2023 17:35
@envestcc envestcc requested review from CoderZhi, dustinxie, Liuhaai and a team as code owners May 19, 2023 17:35
@codecov
Copy link

codecov bot commented May 19, 2023

Codecov Report

Merging #3863 (6f4939a) into master (e1f0636) will decrease coverage by 0.27%.
The diff coverage is 64.17%.

❗ Current head 6f4939a differs from pull request most recent head 122b66b. Consider uploading reports for the commit 122b66b to get more accurate results

@@            Coverage Diff             @@
##           master    #3863      +/-   ##
==========================================
- Coverage   75.38%   75.12%   -0.27%     
==========================================
  Files         303      316      +13     
  Lines       25923    26926    +1003     
==========================================
+ Hits        19541    20227     +686     
- Misses       5360     5662     +302     
- Partials     1022     1037      +15     
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%> (ø)
action/protocol/staking/staking_statereader.go 0.00% <0.00%> (ø)
api/web3server_marshal.go 93.21% <ø> (ø)
blockindex/sgd_indexer.go 0.00% <0.00%> (ø)
action/protocol/staking/protocol.go 60.50% <16.66%> (+0.62%) ⬆️
api/web3server_utils.go 69.20% <16.66%> (-3.67%) ⬇️
action/protocol/rewarding/fund.go 64.70% <23.07%> (-18.91%) ⬇️
... and 21 more

... and 3 files with indirect coverage changes

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

}

func (s *contractStakingCache) getTotalBucketTypeCount() uint64 {
func (s *contractStakingCache) GetTotalBucketTypeCount() uint64 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rename to BucketTypeCount, and move to public function group

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

// new dirty cache for this block
// it's not necessary to use thread safe cache here, because only one thread will call this function
// and no update to cache will happen before dirty merge to clean
dirty := newContractStakingDirty(s.cache.Unsafe())
Copy link
Collaborator

Choose a reason for hiding this comment

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

it doesn't hurt much to use CacheSafe, thus, Cache and CacheSafe could be merged into one.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, removed CacheSafe and move the mutex into Cache

@@ -82,3 +105,110 @@ func (s *Indexer) BucketTypes() ([]*BucketType, error) {
}
return bts, nil
}

// PutBlock puts a block into indexer
func (s *Indexer) PutBlock(ctx context.Context, blk *block.Block) error {
Copy link
Member

@Liuhaai Liuhaai May 23, 2023

Choose a reason for hiding this comment

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

make sure the indexer-related code/files are in /blockindex

Copy link
Member Author

Choose a reason for hiding this comment

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

moved

action/protocol/staking/contractstaking/indexer.go Outdated Show resolved Hide resolved
@@ -21,6 +22,7 @@ type (
height uint64
totalBucketCount uint64 // total number of buckets including burned buckets
contractAddress string // contract address for the bucket
mutex sync.RWMutex
Copy link
Member

Choose a reason for hiding this comment

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

On the L21, is a map necessary for amount-duration indexing? (No comment on the mtx)

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, the params of stake are the amount and duration, this map can help get the bucket type

func (dirty *contractStakingDirty) PutBucketType(bt *BucketType) error {
id, _, ok := dirty.matchBucketType(bt.Amount, bt.Duration)
if !ok {
id = dirty.getBucketTypeCount()
Copy link
Member

Choose a reason for hiding this comment

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

what's the reason to use getBucketTypeCount as id?

Copy link
Member Author

Choose a reason for hiding this comment

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

id of the BucketType starts from 0, which is consistent with the staking contract

action/protocol/staking/contractstaking/delta_state.go Outdated Show resolved Hide resolved
deltaStateAdded deltaState = iota
deltaStateRemoved
deltaStateModified
deltaStateReverted
Copy link
Member

Choose a reason for hiding this comment

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

what does Reverted mean?

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 means a state where is added and then removed, it's reverted with the original state. If state is reverted, there is no need to be concerned about it.

// bucket related namespace in db
_StakingBucketInfoNS = "sbi"
_StakingBucketTypeNS = "sbt"
_StakingNS = "s"
Copy link
Member

Choose a reason for hiding this comment

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

use a 2-letter namespace to be consistent

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

return err
}
if err := s.kvstore.WriteBatch(batch); err != nil {
s.reloadCache()
Copy link
Member

@dustinxie dustinxie May 24, 2023

Choose a reason for hiding this comment

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

if write fails, just discard dirty cache? can we avoid reading all items from the kvStore? that should only be needed in Start() once

Copy link
Member Author

Choose a reason for hiding this comment

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

to handle the retry of PutBlock

}
}

func (dirty *contractStakingDirty) PutHeight(h uint64) {
Copy link
Member

Choose a reason for hiding this comment

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

these funcs need not be public?

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

}
}

func (s *contractStakingDelta) PutHeight(height uint64) {
Copy link
Member

Choose a reason for hiding this comment

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

these funcs need not be public?

// dirty is the cache to update during event processing, which will be merged to clean cache after all events are processed.
// if errors occur during event processing, dirty cache will be discarded.
dirty := newContractStakingDirty(s.cache)
dirty.PutHeight(blk.Height())
Copy link
Member

Choose a reason for hiding this comment

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

convert these to cache's func

Copy link
Member

Choose a reason for hiding this comment

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

cache.NewWorkingset(blk.Height())

Copy link
Member

Choose a reason for hiding this comment

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

instead of indexer --> cache, dirty
indexer --> cache
let cache handle --> dirty inside

Copy link
Member Author

Choose a reason for hiding this comment

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

make the indexer unaware of the dirty

@sonarcloud
Copy link

sonarcloud bot commented May 24, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

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

No Coverage information No Coverage information
3.7% 3.7% Duplication

@@ -247,6 +230,65 @@ func (s *contractStakingCache) BucketTypeCount() uint64 {
return uint64(len(s.bucketTypeMap))
}

func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {
Copy link
Member

Choose a reason for hiding this comment

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

loadFromDB(), this is internal func
also, do we have a test to test this func?

}
}
s.putHeight(delta.GetHeight())
s.putTotalBucketCount(s.getTotalBucketCount() + delta.AddedBucketCnt())
Copy link
Member

Choose a reason for hiding this comment

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

move these 2 lines out of merge

@envestcc envestcc merged commit 0ba92d0 into iotexproject:master May 25, 2023
@envestcc envestcc deleted the iip13_v2_3 branch May 25, 2023 01:39
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