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

[blockindex] restrict height for contract_staking_indexer during read operation #3927

Merged
merged 6 commits into from
Aug 25, 2023

Conversation

envestcc
Copy link
Member

Description

Motivation:
Strictly limiting the blocks during PutBlock can avoid block skipping issues
Specifying the height during queries can help detect errors in the indexer at an earlier stage
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 Aug 22, 2023

Codecov Report

Merging #3927 (e8343f2) into master (e1f0636) will increase coverage by 0.66%.
Report is 81 commits behind head on master.
The diff coverage is 73.82%.

❗ Current head e8343f2 differs from pull request most recent head 4ab8f36. Consider uploading reports for the commit 4ab8f36 to get more accurate results

@@            Coverage Diff             @@
##           master    #3927      +/-   ##
==========================================
+ Coverage   75.38%   76.04%   +0.66%     
==========================================
  Files         303      328      +25     
  Lines       25923    27897    +1974     
==========================================
+ Hits        19541    21215    +1674     
- Misses       5360     5590     +230     
- Partials     1022     1092      +70     
Files Changed 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%> (ø)
action/receipt.go 82.17% <ø> (ø)
api/grpcserver.go 86.40% <0.00%> (ø)
api/loglistener.go 25.00% <0.00%> (ø)
api/websocket.go 4.10% <0.00%> (-1.07%) ⬇️
blockchain/config.go 74.54% <ø> (ø)
... and 65 more

... and 6 files with indirect coverage changes

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

return nil
}
// indexer only store latest height
if height != s.height {
Copy link
Member

@dustinxie dustinxie Aug 22, 2023

Choose a reason for hiding this comment

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

should be height > s.height?

in action/protocol/poll/util.go, it is called with epoch start height

func createPostSystemActions(ctx context.Context, sr protocol.StateReader, p Protocol) {
    xxx
    l, err := p.CalculateCandidatesByHeight(ctx, sr, epochHeight)
    xxx
}

@@ -105,7 +105,7 @@ func (dirty *contractStakingDirty) finalize() (batch.KVStoreBatch, *contractStak

func (dirty *contractStakingDirty) finalizeBatch() batch.KVStoreBatch {
dirty.once.Do(func() {
total := dirty.clean.TotalBucketCount() + dirty.delta.AddedBucketCnt()
total := dirty.clean.totalBucketCount + dirty.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.

not needed?
TotalBucketCount() has a lock 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.

ok, use 0 height to query latest state

@@ -125,7 +125,7 @@ func (dirty *contractStakingDirty) matchBucketType(amount *big.Int, duration uin
}

func (dirty *contractStakingDirty) getBucketTypeCount() uint64 {
return dirty.clean.BucketTypeCount() + dirty.delta.AddedBucketTypeCnt()
return uint64(len(dirty.clean.bucketTypeMap)) + dirty.delta.AddedBucketTypeCnt()
Copy link
Member

Choose a reason for hiding this comment

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

same as above

@@ -476,7 +476,11 @@ func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader
featureCtx := protocol.MustGetFeatureCtx(ctx)
for i := range list {
if p.contractStakingIndexer != nil && featureCtx.AddContractStakingVotes {
list[i].Votes.Add(list[i].Votes, p.contractStakingIndexer.CandidateVotes(list[i].Owner))
contractVotes, err := p.contractStakingIndexer.CandidateVotes(list[i].Owner, height)
Copy link
Member

Choose a reason for hiding this comment

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

height here equals to the height of block to be committed, that is, current factory/indexer's height + 1
so should use height-1? pls confirm

Copy link
Collaborator

Choose a reason for hiding this comment

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

should be covered by unit test

return errors.Wrapf(ErrInvalidHeight, "expected %d, actual %d", s.height, height)
}
return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

add test cases to test ErrInvalidHeight

@@ -1342,7 +1342,7 @@ func TestContractStaking(t *testing.T) {
receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
r.Len(receipts, 1)
r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
buckets, err := indexer.Buckets()
buckets, err := indexer.Buckets(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.

if blk.Height() == indexer height + 1, it should fire an error 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.

block has been committed after writeContract

@@ -467,6 +467,10 @@ func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.

// ActiveCandidates returns all active candidates in candidate center
func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader, height uint64) (state.CandidateList, error) {
height, err := sr.Height()
Copy link
Member

Choose a reason for hiding this comment

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

height will override the 3rd input param, use a diff name?

BucketTypes() ([]*ContractStakingBucketType, error)
BucketTypes(height uint64) ([]*ContractStakingBucketType, error)
// Height returns the indexer height
Height() (uint64, error)
Copy link
Member

Choose a reason for hiding this comment

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

not needed?

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, forget to revert

noErr(indexer.Buckets(h))
noErr(indexer.BucketTypes(h))
noErr(indexer.BucketsByCandidate(delegate1, h))
noErr(indexer.BucketsByIndices([]uint64{1, 2, 3, 4, 5, 8}, h))
Copy link
Member

Choose a reason for hiding this comment

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

just use r.NoErr()

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 does not work for function with multiple return values

noErr(indexer.Bucket(1, h))
noErr(indexer.TotalBucketCount(h))
} else {
hasErr(indexer.Buckets(h))
Copy link
Member

Choose a reason for hiding this comment

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

just use r.ErrorIs(err, ErrInvalidHeight)

Copy link
Member Author

Choose a reason for hiding this comment

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

this can simplify the error checking only in one line

Copy link
Member

@dustinxie dustinxie Aug 24, 2023

Choose a reason for hiding this comment

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

not significant/necessary, other places also have similar test code

_, err := func(x, y, z)
r.Error(err)

Copy link
Member

@dustinxie dustinxie Aug 24, 2023

Choose a reason for hiding this comment

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

also, no need to test all these funcs, they just call the same validateHeight()
only need to call cache.validateHeight() on these test cases

Copy link
Member

@dustinxie dustinxie Aug 24, 2023

Choose a reason for hiding this comment

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

and move to cache_test.go

@sonarcloud
Copy link

sonarcloud bot commented Aug 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 1 Code Smell

No Coverage information No Coverage information
7.0% 7.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

return nil, false, err
}
bt, ok := s.getBucket(id)
return bt, ok, nil
Copy link
Member

@Liuhaai Liuhaai Aug 24, 2023

Choose a reason for hiding this comment

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

BucketInfo and BucketType without height?

Copy link
Member Author

@envestcc envestcc Aug 25, 2023

Choose a reason for hiding this comment

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

Currently, it is only necessary to add height restrictions to the indexer's API

@dustinxie dustinxie merged commit 1a63b13 into iotexproject:master Aug 25, 2023
3 of 4 checks passed
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