github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/types/cache.go (about)

     1  package types
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	lru "github.com/hashicorp/golang-lru"
     8  
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  )
    11  
    12  const cacheSize = 1024
    13  
    14  var paramsCache = NewCache()
    15  
    16  type Cache struct {
    17  	params           Params
    18  	needParamsUpdate bool
    19  	paramsMutex      sync.RWMutex
    20  
    21  	feeSplits *lru.Cache
    22  	shares    *lru.Cache
    23  }
    24  
    25  func NewCache() *Cache {
    26  	c := &Cache{
    27  		params:           DefaultParams(),
    28  		needParamsUpdate: true,
    29  	}
    30  
    31  	c.feeSplits, _ = lru.New(cacheSize)
    32  	c.shares, _ = lru.New(cacheSize)
    33  	return c
    34  }
    35  
    36  // UpdateParams  the update in params is relates to the proposal and initGenesis
    37  func (c *Cache) UpdateParams(params Params, isCheckTx bool) {
    38  	if isCheckTx {
    39  		return
    40  	}
    41  	c.paramsMutex.Lock()
    42  	defer c.paramsMutex.Unlock()
    43  	c.params = params
    44  	c.needParamsUpdate = false
    45  }
    46  
    47  func (c *Cache) SetNeedParamsUpdate() {
    48  	c.paramsMutex.Lock()
    49  	defer c.paramsMutex.Unlock()
    50  	c.needParamsUpdate = true
    51  }
    52  
    53  func (c *Cache) IsNeedParamsUpdate() bool {
    54  	c.paramsMutex.RLock()
    55  	defer c.paramsMutex.RUnlock()
    56  	return c.needParamsUpdate
    57  }
    58  
    59  func (c *Cache) GetParams() Params {
    60  	c.paramsMutex.RLock()
    61  	defer c.paramsMutex.RUnlock()
    62  	return NewParams(c.params.EnableFeeSplit,
    63  		c.params.DeveloperShares,
    64  		c.params.AddrDerivationCostCreate,
    65  	)
    66  }
    67  
    68  // UpdateFeeSplit The change in feeSplit is only related to the user tx(register,update,cancel)
    69  func (c *Cache) UpdateFeeSplit(contract common.Address, feeSplit FeeSplit, isCheckTx bool) {
    70  	if isCheckTx {
    71  		return
    72  	}
    73  	c.feeSplits.Add(contract, feeSplit)
    74  }
    75  
    76  // DeleteFeeSplit The change in feeSplit is only related to the user tx(register,update,cancel)
    77  func (c *Cache) DeleteFeeSplit(contract common.Address, isCheckTx bool) {
    78  	if isCheckTx {
    79  		return
    80  	}
    81  	c.feeSplits.Remove(contract)
    82  }
    83  
    84  func (c *Cache) GetFeeSplit(contract common.Address) (FeeSplit, bool) {
    85  	feeSplit, found := c.feeSplits.Get(contract)
    86  	if found {
    87  		return feeSplit.(FeeSplit), true
    88  	}
    89  	return FeeSplit{}, false
    90  }
    91  
    92  // UpdateShare The change in share is only related to the proposal
    93  func (c *Cache) UpdateShare(contract common.Address, share sdk.Dec, isCheckTx bool) {
    94  	if isCheckTx {
    95  		return
    96  	}
    97  	c.shares.Add(contract, share)
    98  }
    99  
   100  func (c *Cache) GetShare(contract common.Address) (sdk.Dec, bool) {
   101  	share, found := c.shares.Get(contract)
   102  	if found {
   103  		return share.(sdk.Dec), true
   104  	}
   105  	return sdk.Dec{}, false
   106  }
   107  
   108  func SetParamsNeedUpdate() {
   109  	paramsCache.SetNeedParamsUpdate()
   110  }
   111  
   112  func GetParamsCache() *Cache {
   113  	return paramsCache
   114  }