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]fix vote bug when change delegate #3888

Merged
merged 17 commits into from
Jun 19, 2023

Conversation

envestcc
Copy link
Member

Description

The issue is that when changeDelegate is called on a bucket, the votes of the old delegate are not reduced.

The issue is caused by two factors:

  1. not remove the bucket of the old candidate from the candidateBucketMap
  2. the cache read operations return a pointer, which means that the clean cache may have already been updated before the merge.

pre-required: #3887

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 15, 2023

Codecov Report

Merging #3888 (d2cffb1) into master (e1f0636) will increase coverage by 0.18%.
The diff coverage is 66.65%.

❗ Current head d2cffb1 differs from pull request most recent head 325df6c. Consider uploading reports for the commit 325df6c to get more accurate results

@@            Coverage Diff             @@
##           master    #3888      +/-   ##
==========================================
+ Coverage   75.38%   75.56%   +0.18%     
==========================================
  Files         303      328      +25     
  Lines       25923    27792    +1869     
==========================================
+ Hits        19541    21001    +1460     
- Misses       5360     5714     +354     
- Partials     1022     1077      +55     
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 50 more

... and 4 files with indirect coverage changes

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

@sonarcloud
Copy link

sonarcloud bot commented Jun 19, 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 28 Code Smells

No Coverage information No Coverage information
2.4% 2.4% Duplication

@dustinxie dustinxie merged commit 32de9ce into iotexproject:master Jun 19, 2023
3 checks passed
@@ -140,11 +143,11 @@ func (s *Indexer) BucketTypes() ([]*BucketType, error) {

// PutBlock puts a block into indexer
func (s *Indexer) PutBlock(ctx context.Context, blk *block.Block) error {
if blk.Height() < s.contractDeployHeight {
if blk.Height() < s.contractDeployHeight || blk.Height() <= s.height.Load().(uint64) {
Copy link
Member

Choose a reason for hiding this comment

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

Tho it's guaranteed in the filedao (e.g.

if blk.Height() != tip.Height+1 {
), tip had better to be checked , otherwise the state in the cache could be flushed with discontinuous state

@@ -630,3 +955,24 @@ func transfer(r *require.Assertions, handler *contractStakingEventHandler, owner
})
r.NoError(err)
}

Copy link
Member

Choose a reason for hiding this comment

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

are two events Transfer and Staked emitted simultaneously in the contract as same as on L898-L956?

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

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

Choose a reason for hiding this comment

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

will contractStakingCache.merge() always return nil?

@@ -372,11 +331,21 @@ func (s *contractStakingCache) putBucketType(id uint64, bt *BucketType) {
}

func (s *contractStakingCache) putBucketInfo(id uint64, bi *bucketInfo) {
oldBi := s.bucketInfoMap[id]
Copy link
Member

Choose a reason for hiding this comment

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

Tests for the issue


func TestContractStakingCache_Debug(t *testing.T) {
	cache := newContractStakingCache("")

	cache.PutBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})
	cache.PutBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 200, ActivatedAt: 1})
	cache.PutBucketType(1, &BucketType{Amount: big.NewInt(200), Duration: 100, ActivatedAt: 1})
	log.L().Info("debug", zap.Any("propertyBucketTypeMap", cache.propertyBucketTypeMap))
	cache.PutBucketInfo(1, &bucketInfo{TypeIndex: 1, CreatedAt: 1, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(1), Owner: identityset.Address(2)})
	cache.PutBucketInfo(1, &bucketInfo{TypeIndex: 1, CreatedAt: 1, UnlockedAt: maxBlockNumber, UnstakedAt: maxBlockNumber, Delegate: identityset.Address(2), Owner: identityset.Address(3)})
	log.L().Info("debug", zap.Any("candidateBucketMap", cache.candidateBucketMap))

}

code for reference

func (s *contractStakingCache) putBucketType(id uint64, bt *BucketType) {
	if oldBt, existed := s.bucketTypeMap[id]; existed {
		amount := oldBt.Amount.Int64()
		if _, existed := s.propertyBucketTypeMap[amount]; !existed {
			panic("property bucket type map not found")
		}
		delete(s.propertyBucketTypeMap[amount], oldBt.Duration)
		if len(s.propertyBucketTypeMap[amount]) == 0 {
			delete(s.propertyBucketTypeMap, amount)
		}
	}
	s.bucketTypeMap[id] = bt
	amount := bt.Amount.Int64()
	if _, ok := s.propertyBucketTypeMap[amount]; !ok {
		s.propertyBucketTypeMap[amount] = make(map[uint64]uint64)
	}
	s.propertyBucketTypeMap[amount][bt.Duration] = id
}

func (s *contractStakingCache) putBucketInfo(id uint64, bi *bucketInfo) {
	// delete old candidate bucket map
	if oldBi, existed := s.bucketInfoMap[id]; existed {
		oldDelegate := oldBi.Delegate.String()
		if _, existed := s.candidateBucketMap[oldDelegate]; !existed {
			panic("candidate bucket map not found") // TODO: remove this panic
		}
		delete(s.candidateBucketMap[oldDelegate], id)
		if len(s.candidateBucketMap[oldDelegate]) == 0 {
			delete(s.candidateBucketMap, oldDelegate)
		}
	}
	s.bucketInfoMap[id] = bi
	// update candidate bucket map
	newDelegate := bi.Delegate.String()
	if _, ok := s.candidateBucketMap[newDelegate]; !ok {
		s.candidateBucketMap[newDelegate] = make(map[uint64]bool)
	}
	s.candidateBucketMap[newDelegate][id] = true
}

@@ -24,18 +24,6 @@ func newContractStakingDelta() *contractStakingDelta {
}
}

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.

the design map[deltaState]map[uint64]*bucketInfo is misleading on L27

if err := s.cache.Merge(delta); err != nil {
s.reloadCache()
return err
}
// update db
Copy link
Member

Choose a reason for hiding this comment

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

new state at old height without lock?

return err
}
s.putTotalBucketCount(s.totalBucketCount + delta.AddedBucketCnt())
return nil
}

func (s *contractStakingCache) PutTotalBucketCount(count uint64) {
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 purpose of a public PutTotalBucketCount func?

Copy link
Member Author

@envestcc envestcc Jun 21, 2023

Choose a reason for hiding this comment

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

the two principles could easily avoid dead lock:

  • Lock & Unlock is called in public method instead of private
  • public method can't call public

Copy link
Member

Choose a reason for hiding this comment

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

seems only used in test code, could be removed

@@ -140,11 +143,11 @@ func (s *Indexer) BucketTypes() ([]*BucketType, error) {

// PutBlock puts a block into indexer
func (s *Indexer) PutBlock(ctx context.Context, blk *block.Block) error {
if blk.Height() < s.contractDeployHeight {
if blk.Height() < s.contractDeployHeight || blk.Height() <= s.height.Load().(uint64) {
Copy link
Member

Choose a reason for hiding this comment

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

All buckets are returned from Indexer.Buckets(), Indexer.TotalBucketCount() and Indexer.BucketsByCandidate(), but only the active buckets are returned from Indexer.BucketTypes(). Is this expected?

@envestcc envestcc mentioned this pull request Jun 22, 2023
10 tasks
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