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
9 changes: 9 additions & 0 deletions action/protocol/staking/contractstake_bucket_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (bt *ContractStakingBucketType) Deserialize(b []byte) error {
return bt.loadProto(&m)
}

// Clone clones the bucket type
func (bt *ContractStakingBucketType) Clone() *ContractStakingBucketType {
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
return &ContractStakingBucketType{
Amount: big.NewInt(0).Set(bt.Amount),
Duration: bt.Duration,
ActivatedAt: bt.ActivatedAt,
}
}

func (bt *ContractStakingBucketType) toProto() *stakingpb.BucketType {
return &stakingpb.BucketType{
Amount: bt.Amount.String(),
Expand Down
12 changes: 12 additions & 0 deletions blockindex/contractstaking/bucket_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ func (bi *bucketInfo) Deserialize(b []byte) error {
return bi.loadProto(&m)
}

// Clone clones the bucket info
func (bi *bucketInfo) Clone() *bucketInfo {
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
return &bucketInfo{
TypeIndex: bi.TypeIndex,
CreatedAt: bi.CreatedAt,
UnlockedAt: bi.UnlockedAt,
UnstakedAt: bi.UnstakedAt,
Delegate: bi.Delegate,
Owner: bi.Owner,
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
}
}

func (bi *bucketInfo) toProto() *contractstakingpb.BucketInfo {
pb := &contractstakingpb.BucketInfo{
TypeIndex: bi.TypeIndex,
Expand Down
37 changes: 22 additions & 15 deletions blockindex/contractstaking/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (s *contractStakingCache) ActiveBucketTypes() map[uint64]*BucketType {
m := make(map[uint64]*BucketType)
for k, v := range s.bucketTypeMap {
if v.ActivatedAt != maxBlockNumber {
m[k] = v
m[k] = v.Clone()
}
}
return m
Expand Down Expand Up @@ -233,7 +233,6 @@ func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {
s.mutex.Lock()
defer s.mutex.Unlock()

delta := newContractStakingDelta()
// load height
var height uint64
h, err := kvstore.Get(_StakingNS, _stakingHeightKey)
Expand All @@ -246,7 +245,7 @@ func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {
height = byteutil.BytesToUint64BigEndian(h)

}
delta.PutHeight(height)
s.putHeight(height)

// load total bucket count
var totalBucketCount uint64
Expand All @@ -258,7 +257,7 @@ func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {
} else {
totalBucketCount = byteutil.BytesToUint64BigEndian(tbc)
}
delta.PutTotalBucketCount(totalBucketCount)
s.putTotalBucketCount(totalBucketCount)

// load bucket info
ks, vs, err := kvstore.Filter(_StakingBucketInfoNS, func(k, v []byte) bool { return true }, nil, nil)
Expand All @@ -270,7 +269,7 @@ func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {
if err := b.Deserialize(vs[i]); err != nil {
return err
}
delta.AddBucketInfo(byteutil.BytesToUint64BigEndian(ks[i]), &b)
s.putBucketInfo(byteutil.BytesToUint64BigEndian(ks[i]), &b)
}

// load bucket type
Expand All @@ -283,9 +282,9 @@ func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {
if err := b.Deserialize(vs[i]); err != nil {
return err
}
delta.AddBucketType(byteutil.BytesToUint64BigEndian(ks[i]), &b)
s.putBucketType(byteutil.BytesToUint64BigEndian(ks[i]), &b)
}
return s.merge(delta)
return nil
}

func (s *contractStakingCache) getBucketTypeIndex(amount *big.Int, duration uint64) (uint64, bool) {
Expand All @@ -299,28 +298,34 @@ func (s *contractStakingCache) getBucketTypeIndex(amount *big.Int, duration uint

func (s *contractStakingCache) getBucketType(id uint64) (*BucketType, bool) {
bt, ok := s.bucketTypeMap[id]
return bt, ok
if !ok {
return nil, false
}
return bt.Clone(), ok
}

func (s *contractStakingCache) mustGetBucketType(id uint64) *BucketType {
bt, ok := s.getBucketType(id)
if !ok {
panic("bucket type not found")
}
return bt
return bt.Clone()
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *contractStakingCache) getBucketInfo(id uint64) (*bucketInfo, bool) {
bi, ok := s.bucketInfoMap[id]
return bi, ok
if !ok {
return nil, false
}
return bi.Clone(), ok
}

func (s *contractStakingCache) mustGetBucketInfo(id uint64) *bucketInfo {
bt, ok := s.getBucketInfo(id)
if !ok {
panic("bucket info not found")
}
return bt
return bt.Clone()
dustinxie marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *contractStakingCache) mustGetBucket(id uint64) *Bucket {
Expand All @@ -341,7 +346,7 @@ func (s *contractStakingCache) getBucket(id uint64) (*Bucket, bool) {
func (s *contractStakingCache) getAllBucketInfo() map[uint64]*bucketInfo {
m := make(map[uint64]*bucketInfo)
for k, v := range s.bucketInfoMap {
m[k] = v
m[k] = v.Clone()
}
return m
}
Expand All @@ -350,7 +355,7 @@ func (s *contractStakingCache) getBucketInfoByCandidate(candidate address.Addres
m := make(map[uint64]*bucketInfo)
for k, v := range s.candidateBucketMap[candidate.String()] {
if v {
m[k] = s.bucketInfoMap[k]
m[k] = s.bucketInfoMap[k].Clone()
}
}
return m
Expand All @@ -372,11 +377,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 Expand Up @@ -418,7 +427,5 @@ func (s *contractStakingCache) merge(delta *contractStakingDelta) error {
}
}
}
s.putHeight(delta.GetHeight())
s.putTotalBucketCount(s.getTotalBucketCount() + delta.AddedBucketCnt())
return nil
}
Loading