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
Merged
4 changes: 4 additions & 0 deletions blockindex/contractstaking/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,15 @@ 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
}

s.bucketInfoMap[id] = bi
if _, ok := s.candidateBucketMap[bi.Delegate.String()]; !ok {
s.candidateBucketMap[bi.Delegate.String()] = make(map[uint64]bool)
}
s.candidateBucketMap[bi.Delegate.String()][id] = true
if oldBi != nil && oldBi.Delegate.String() != bi.Delegate.String() {
delete(s.candidateBucketMap[oldBi.Delegate.String()], id)
}
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *contractStakingCache) deleteBucketInfo(id uint64) {
Expand Down