github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/contractstaking/delta_cache.go (about)

     1  // Copyright (c) 2023 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package contractstaking
     7  
     8  import (
     9  	"math/big"
    10  )
    11  
    12  type (
    13  	contractStakingDelta struct {
    14  		cache *contractStakingCache // easy to query buckets
    15  
    16  		bucketTypeDeltaState map[uint64]deltaState
    17  		bucketInfoDeltaState map[uint64]deltaState
    18  	}
    19  )
    20  
    21  func newContractStakingDelta() *contractStakingDelta {
    22  	return &contractStakingDelta{
    23  		cache:                newContractStakingCache(Config{}),
    24  		bucketTypeDeltaState: make(map[uint64]deltaState),
    25  		bucketInfoDeltaState: make(map[uint64]deltaState),
    26  	}
    27  }
    28  
    29  func (s *contractStakingDelta) BucketInfoDelta() map[deltaState]map[uint64]*bucketInfo {
    30  	delta := map[deltaState]map[uint64]*bucketInfo{
    31  		deltaStateAdded:    make(map[uint64]*bucketInfo),
    32  		deltaStateRemoved:  make(map[uint64]*bucketInfo),
    33  		deltaStateModified: make(map[uint64]*bucketInfo),
    34  	}
    35  	for id, state := range s.bucketInfoDeltaState {
    36  		switch state {
    37  		case deltaStateAdded:
    38  			delta[state][id] = s.cache.MustGetBucketInfo(id)
    39  		case deltaStateRemoved:
    40  			delta[state][id] = nil
    41  		case deltaStateModified:
    42  			delta[state][id] = s.cache.MustGetBucketInfo(id)
    43  		}
    44  	}
    45  	return delta
    46  }
    47  
    48  func (s *contractStakingDelta) BucketTypeDelta() map[deltaState]map[uint64]*BucketType {
    49  	delta := map[deltaState]map[uint64]*BucketType{
    50  		deltaStateAdded:    make(map[uint64]*BucketType),
    51  		deltaStateModified: make(map[uint64]*BucketType),
    52  	}
    53  	for id, state := range s.bucketTypeDeltaState {
    54  		switch state {
    55  		case deltaStateAdded:
    56  			delta[state][id] = s.cache.MustGetBucketType(id)
    57  		case deltaStateModified:
    58  			delta[state][id] = s.cache.MustGetBucketType(id)
    59  		}
    60  	}
    61  	return delta
    62  }
    63  
    64  func (s *contractStakingDelta) MustGetBucketType(id uint64) *BucketType {
    65  	return s.cache.MustGetBucketType(id)
    66  }
    67  
    68  func (s *contractStakingDelta) MatchBucketType(amount *big.Int, duration uint64) (uint64, *BucketType, bool) {
    69  	return s.cache.MatchBucketType(amount, duration)
    70  }
    71  
    72  func (s *contractStakingDelta) GetBucketInfo(id uint64) (*bucketInfo, deltaState) {
    73  	state := s.bucketInfoDeltaState[id]
    74  	switch state {
    75  	case deltaStateAdded, deltaStateModified:
    76  		return s.cache.MustGetBucketInfo(id), state
    77  	default: // deltaStateRemoved, deltaStateUnchanged
    78  		return nil, state
    79  	}
    80  }
    81  
    82  func (s *contractStakingDelta) GetBucketType(id uint64) (*BucketType, deltaState) {
    83  	state := s.bucketTypeDeltaState[id]
    84  	switch state {
    85  	case deltaStateAdded, deltaStateModified:
    86  		return s.cache.MustGetBucketType(id), state
    87  	default: // deltaStateUnchanged
    88  		return nil, state
    89  	}
    90  }
    91  
    92  func (s *contractStakingDelta) AddBucketInfo(id uint64, bi *bucketInfo) error {
    93  	return s.addBucketInfo(id, bi)
    94  }
    95  
    96  func (s *contractStakingDelta) AddBucketType(id uint64, bt *BucketType) error {
    97  	var err error
    98  	s.bucketTypeDeltaState[id], err = s.bucketTypeDeltaState[id].Transfer(deltaActionAdd)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	s.cache.PutBucketType(id, bt)
   104  	return nil
   105  }
   106  
   107  func (s *contractStakingDelta) UpdateBucketType(id uint64, bt *BucketType) error {
   108  	var err error
   109  	s.bucketTypeDeltaState[id], err = s.bucketTypeDeltaState[id].Transfer(deltaActionModify)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	s.cache.PutBucketType(id, bt)
   114  	return nil
   115  }
   116  
   117  func (s *contractStakingDelta) UpdateBucketInfo(id uint64, bi *bucketInfo) error {
   118  	var err error
   119  	s.bucketInfoDeltaState[id], err = s.bucketInfoDeltaState[id].Transfer(deltaActionModify)
   120  	if err != nil {
   121  		return err
   122  	}
   123  	s.cache.PutBucketInfo(id, bi)
   124  	return nil
   125  }
   126  
   127  func (s *contractStakingDelta) DeleteBucketInfo(id uint64) error {
   128  	var err error
   129  	s.bucketInfoDeltaState[id], err = s.bucketInfoDeltaState[id].Transfer(deltaActionRemove)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	s.cache.DeleteBucketInfo(id)
   134  	return nil
   135  }
   136  
   137  func (s *contractStakingDelta) AddedBucketCnt() uint64 {
   138  	addedBucketCnt := uint64(0)
   139  	for _, state := range s.bucketInfoDeltaState {
   140  		if state == deltaStateAdded {
   141  			addedBucketCnt++
   142  		}
   143  	}
   144  	return addedBucketCnt
   145  }
   146  
   147  func (s *contractStakingDelta) AddedBucketTypeCnt() uint64 {
   148  	cnt := uint64(0)
   149  	for _, state := range s.bucketTypeDeltaState {
   150  		if state == deltaStateAdded {
   151  			cnt++
   152  		}
   153  	}
   154  	return cnt
   155  }
   156  
   157  func (s *contractStakingDelta) isBucketDeleted(id uint64) bool {
   158  	return s.bucketInfoDeltaState[id] == deltaStateRemoved
   159  }
   160  
   161  func (s *contractStakingDelta) addBucketInfo(id uint64, bi *bucketInfo) error {
   162  	var err error
   163  	s.bucketInfoDeltaState[id], err = s.bucketInfoDeltaState[id].Transfer(deltaActionAdd)
   164  	if err != nil {
   165  		return err
   166  	}
   167  	s.cache.PutBucketInfo(id, bi)
   168  	return nil
   169  }