Skip to content

Commit 19b0a95

Browse files
authored
Refactor match bucket type (#4706)
* store contract staking data in trie * fix unit test and address comment * snapshot/revert views * fix two unit tests and refactor matchbuckettype
1 parent 4e82010 commit 19b0a95

File tree

7 files changed

+38
-43
lines changed

7 files changed

+38
-43
lines changed

blockindex/contractstaking/cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type (
2525
BucketInfo(id uint64) (*bucketInfo, bool)
2626
MustGetBucketInfo(id uint64) *bucketInfo
2727
MustGetBucketType(id uint64) *BucketType
28-
MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType, bool)
28+
MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType)
2929
BucketType(id uint64) (*BucketType, bool)
3030
BucketTypeCount() int
3131
Buckets() ([]uint64, []*BucketType, []*bucketInfo)
@@ -210,15 +210,15 @@ func (s *contractStakingCache) DeleteBucketInfo(id uint64) {
210210
s.deltaBuckets[id] = nil
211211
}
212212

213-
func (s *contractStakingCache) MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType, bool) {
213+
func (s *contractStakingCache) MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType) {
214214
s.mutex.RLock()
215215
defer s.mutex.RUnlock()
216216

217217
id, ok := s.getBucketTypeIndex(amount, duration)
218218
if !ok {
219-
return 0, nil, false
219+
return 0, nil
220220
}
221-
return id, s.mustGetBucketType(id), true
221+
return id, s.mustGetBucketType(id)
222222
}
223223

224224
func (s *contractStakingCache) LoadFromDB(kvstore db.KVStore) error {

blockindex/contractstaking/cache_test.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -376,33 +376,29 @@ func TestContractStakingCache_MatchBucketType(t *testing.T) {
376376
cache := newContractStakingCache()
377377

378378
// no bucket types
379-
_, bucketType, ok := cache.MatchBucketType(big.NewInt(100), 100)
380-
require.False(ok)
379+
_, bucketType := cache.MatchBucketType(big.NewInt(100), 100)
381380
require.Nil(bucketType)
382381

383382
// one bucket type
384383
cache.PutBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})
385384
// match exact bucket type
386-
id, bucketType, ok := cache.MatchBucketType(big.NewInt(100), 100)
387-
require.True(ok)
385+
id, bucketType := cache.MatchBucketType(big.NewInt(100), 100)
386+
require.NotNil(bucketType)
388387
require.EqualValues(1, id)
389388
require.EqualValues(100, bucketType.Amount.Int64())
390389
require.EqualValues(100, bucketType.Duration)
391390
require.EqualValues(1, bucketType.ActivatedAt)
392391

393392
// match bucket type with different amount
394-
_, bucketType, ok = cache.MatchBucketType(big.NewInt(200), 100)
395-
require.False(ok)
393+
_, bucketType = cache.MatchBucketType(big.NewInt(200), 100)
396394
require.Nil(bucketType)
397395

398396
// match bucket type with different duration
399-
_, bucketType, ok = cache.MatchBucketType(big.NewInt(100), 200)
400-
require.False(ok)
397+
_, bucketType = cache.MatchBucketType(big.NewInt(100), 200)
401398
require.Nil(bucketType)
402399

403400
// no match
404-
_, bucketType, ok = cache.MatchBucketType(big.NewInt(200), 200)
405-
require.False(ok)
401+
_, bucketType = cache.MatchBucketType(big.NewInt(200), 200)
406402
require.Nil(bucketType)
407403
}
408404

@@ -533,8 +529,8 @@ func TestContractStakingCache_LoadFromDB(t *testing.T) {
533529
require.Equal(bucketInfo, bi)
534530
btc = cache.BucketTypeCount()
535531
require.EqualValues(1, btc)
536-
id, bt, ok := cache.MatchBucketType(big.NewInt(100), 100)
537-
require.True(ok)
532+
id, bt := cache.MatchBucketType(big.NewInt(100), 100)
533+
require.NotNil(bt)
538534
require.EqualValues(1, id)
539535
require.EqualValues(100, bt.Amount.Int64())
540536
require.EqualValues(100, bt.Duration)

blockindex/contractstaking/dirty_cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func (dirty *contractStakingDirty) deleteBucketInfo(id uint64) {
7373
}
7474

7575
func (dirty *contractStakingDirty) putBucketType(bt *BucketType) {
76-
id, _, ok := dirty.matchBucketType(bt.Amount, bt.Duration)
77-
if !ok {
76+
id, old := dirty.matchBucketType(bt.Amount, bt.Duration)
77+
if old == nil {
7878
id = dirty.getBucketTypeCount()
7979
dirty.addBucketType(id, bt)
8080
}
@@ -112,7 +112,7 @@ func (dirty *contractStakingDirty) addBucketType(id uint64, bt *BucketType) {
112112
dirty.cache.PutBucketType(id, bt)
113113
}
114114

115-
func (dirty *contractStakingDirty) matchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType, bool) {
115+
func (dirty *contractStakingDirty) matchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType) {
116116
return dirty.cache.MatchBucketType(amount, duration)
117117
}
118118

blockindex/contractstaking/dirty_cache_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,23 @@ func TestContractStakingDirty_matchBucketType(t *testing.T) {
9696
dirty := newContractStakingDirty(clean)
9797

9898
// no bucket type
99-
id, bt, ok := dirty.matchBucketType(big.NewInt(100), 100)
100-
require.False(ok)
99+
id, bt := dirty.matchBucketType(big.NewInt(100), 100)
101100
require.Nil(bt)
102101
require.EqualValues(0, id)
103102

104103
// bucket type in clean cache
105104
clean.PutBucketType(1, &BucketType{Amount: big.NewInt(100), Duration: 100, ActivatedAt: 1})
106-
id, bt, ok = dirty.matchBucketType(big.NewInt(100), 100)
107-
require.True(ok)
105+
id, bt = dirty.matchBucketType(big.NewInt(100), 100)
106+
require.NotNil(bt)
108107
require.EqualValues(100, bt.Amount.Int64())
109108
require.EqualValues(100, bt.Duration)
110109
require.EqualValues(1, bt.ActivatedAt)
111110
require.EqualValues(1, id)
112111

113112
// added bucket type
114113
dirty.addBucketType(2, &BucketType{Amount: big.NewInt(200), Duration: 200, ActivatedAt: 2})
115-
id, bt, ok = dirty.matchBucketType(big.NewInt(200), 200)
116-
require.True(ok)
114+
id, bt = dirty.matchBucketType(big.NewInt(200), 200)
115+
require.NotNil(bt)
117116
require.EqualValues(200, bt.Amount.Int64())
118117
require.EqualValues(200, bt.Duration)
119118
require.EqualValues(2, bt.ActivatedAt)

blockindex/contractstaking/event_handler.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ func (eh *contractStakingEventHandler) handleBucketTypeDeactivatedEvent(event ev
491491
return err
492492
}
493493

494-
id, bt, ok := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
495-
if !ok {
494+
id, bt := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
495+
if bt == nil {
496496
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", amountParam.Int64(), durationParam.Uint64())
497497
}
498498
bt.ActivatedAt = maxBlockNumber
@@ -519,8 +519,8 @@ func (eh *contractStakingEventHandler) handleStakedEvent(event eventParam, heigh
519519
return err
520520
}
521521

522-
btIdx, _, ok := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
523-
if !ok {
522+
btIdx, bt := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
523+
if bt == nil {
524524
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", amountParam.Int64(), durationParam.Uint64())
525525
}
526526
owner, ok := eh.tokenOwner[tokenIDParam.Uint64()]
@@ -557,8 +557,8 @@ func (eh *contractStakingEventHandler) handleLockedEvent(event eventParam) error
557557
if !ok {
558558
return errors.Wrapf(errBucketTypeNotExist, "id %d", b.TypeIndex)
559559
}
560-
newBtIdx, _, ok := eh.dirty.matchBucketType(bt.Amount, durationParam.Uint64())
561-
if !ok {
560+
newBtIdx, newBt := eh.dirty.matchBucketType(bt.Amount, durationParam.Uint64())
561+
if newBt == nil {
562562
return errors.Wrapf(errBucketTypeNotExist, "amount %v, duration %d", bt.Amount, durationParam.Uint64())
563563
}
564564
b.TypeIndex = newBtIdx
@@ -615,8 +615,8 @@ func (eh *contractStakingEventHandler) handleMergedEvent(event eventParam) error
615615
}
616616

617617
// merge to the first bucket
618-
btIdx, _, ok := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
619-
if !ok {
618+
btIdx, bt := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
619+
if bt == nil {
620620
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", amountParam.Int64(), durationParam.Uint64())
621621
}
622622
b, ok := eh.dirty.getBucketInfo(tokenIDsParam[0].Uint64())
@@ -651,8 +651,8 @@ func (eh *contractStakingEventHandler) handleBucketExpandedEvent(event eventPara
651651
if !ok {
652652
return errors.Wrapf(ErrBucketNotExist, "token id %d", tokenIDParam.Uint64())
653653
}
654-
newBtIdx, _, ok := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
655-
if !ok {
654+
newBtIdx, newBucketType := eh.dirty.matchBucketType(amountParam, durationParam.Uint64())
655+
if newBucketType == nil {
656656
return errors.Wrapf(errBucketTypeNotExist, "amount %d, duration %d", amountParam.Int64(), durationParam.Uint64())
657657
}
658658
b.TypeIndex = newBtIdx

blockindex/contractstaking/indexer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ func (s *Indexer) LoadStakeView(ctx context.Context, sr protocol.StateReader) (s
128128
if buckets[i] == nil {
129129
return nil, errors.New("bucket is nil")
130130
}
131-
tid, _, ok := cache.MatchBucketType(buckets[i].StakedAmount, buckets[i].StakedDuration)
132-
if !ok {
131+
tid, bt := cache.MatchBucketType(buckets[i].StakedAmount, buckets[i].StakedDuration)
132+
if bt == nil {
133133
return nil, errors.Errorf(
134134
"no bucket type found for bucket %d with staked amount %s and duration %d",
135135
id,

blockindex/contractstaking/wrappedcache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ func (wc *wrappedCache) PutBucketType(id uint64, bt *BucketType) {
198198
panic("bucket type amount or duration cannot be changed")
199199
}
200200
}
201-
oldId, _, ok := wc.matchBucketType(bt.Amount, bt.Duration)
202-
if ok && oldId != id {
201+
oldId, oldBucketType := wc.matchBucketType(bt.Amount, bt.Duration)
202+
if oldBucketType != nil && oldId != id {
203203
panic("bucket type with same amount and duration already exists")
204204
}
205205
if _, ok := wc.propertyBucketTypeMap[bt.Amount.Uint64()]; !ok {
@@ -313,21 +313,21 @@ func (wc *wrappedCache) DeleteBucketInfo(id uint64) {
313313
wc.updatedBucketInfos[id] = nil
314314
}
315315

316-
func (wc *wrappedCache) MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType, bool) {
316+
func (wc *wrappedCache) MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType) {
317317
wc.mu.RLock()
318318
defer wc.mu.RUnlock()
319319
return wc.matchBucketType(amount, duration)
320320
}
321321

322-
func (wc *wrappedCache) matchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType, bool) {
322+
func (wc *wrappedCache) matchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType) {
323323
amountUint64 := amount.Uint64()
324324
if amountMap, ok := wc.propertyBucketTypeMap[amountUint64]; ok {
325325
if id, ok := amountMap[duration]; ok {
326326
if bt, ok := wc.updatedBucketTypes[id]; ok {
327327
if bt != nil {
328-
return id, bt, true
328+
return id, bt
329329
}
330-
return 0, nil, false
330+
return 0, nil
331331
}
332332
}
333333
}

0 commit comments

Comments
 (0)