-
Notifications
You must be signed in to change notification settings - Fork 324
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
Conversation
Codecov Report
@@ 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
... 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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved
@@ -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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
deltaStateAdded deltaState = iota | ||
deltaStateRemoved | ||
deltaStateModified | ||
deltaStateReverted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does Reverted
mean?
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache.NewWorkingset(blk.Height())
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 Quality Gate failed. 0 Bugs No Coverage information |
@@ -247,6 +230,65 @@ func (s *contractStakingCache) BucketTypeCount() uint64 { | |||
return uint64(len(s.bucketTypeMap)) | |||
} | |||
|
|||
func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error { |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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
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.
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
Test Configuration:
Checklist: