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

     1  package types
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  var evmParamsCache = NewCache()
     8  
     9  type Cache struct {
    10  	paramsCache      Params
    11  	needParamsUpdate bool
    12  	paramsMutex      sync.RWMutex
    13  
    14  	blockedContractMethodsCache map[string]BlockedContract
    15  	needBlockedUpdate           bool
    16  	blockedMutex                sync.RWMutex
    17  }
    18  
    19  func NewCache() *Cache {
    20  	return &Cache{
    21  		paramsCache:                 DefaultParams(),
    22  		blockedContractMethodsCache: make(map[string]BlockedContract, 0),
    23  		needParamsUpdate:            true,
    24  		needBlockedUpdate:           true,
    25  	}
    26  }
    27  
    28  func (c *Cache) UpdateParams(params Params, isCheckTx bool) {
    29  	if isCheckTx {
    30  		return
    31  	}
    32  	c.paramsMutex.Lock()
    33  	defer c.paramsMutex.Unlock()
    34  	c.paramsCache = params
    35  	c.needParamsUpdate = false
    36  }
    37  
    38  func (c *Cache) SetNeedParamsUpdate() {
    39  	c.paramsMutex.Lock()
    40  	defer c.paramsMutex.Unlock()
    41  	c.needParamsUpdate = true
    42  }
    43  
    44  func (c *Cache) IsNeedParamsUpdate() bool {
    45  	c.paramsMutex.RLock()
    46  	defer c.paramsMutex.RUnlock()
    47  	return c.needParamsUpdate
    48  }
    49  
    50  func (c *Cache) GetParams() Params {
    51  	c.paramsMutex.RLock()
    52  	defer c.paramsMutex.RUnlock()
    53  	return NewParams(c.paramsCache.EnableCreate,
    54  		c.paramsCache.EnableCall,
    55  		c.paramsCache.EnableContractDeploymentWhitelist,
    56  		c.paramsCache.EnableContractBlockedList,
    57  		c.paramsCache.MaxGasLimitPerTx,
    58  		c.paramsCache.ExtraEIPs...)
    59  }
    60  
    61  func (c *Cache) SetNeedBlockedUpdate() {
    62  	c.blockedMutex.Lock()
    63  	defer c.blockedMutex.Unlock()
    64  	c.needBlockedUpdate = true
    65  }
    66  
    67  func (c *Cache) IsNeedBlockedUpdate() bool {
    68  	c.blockedMutex.RLock()
    69  	defer c.blockedMutex.RUnlock()
    70  	return c.needBlockedUpdate
    71  }
    72  
    73  func (c *Cache) GetBlockedContractMethod(addr string) (contract *BlockedContract) {
    74  	c.blockedMutex.RLock()
    75  	bc, ok := c.blockedContractMethodsCache[addr]
    76  	c.blockedMutex.RUnlock()
    77  	if ok {
    78  		return NewBlockContract(bc.Address, bc.BlockMethods)
    79  	}
    80  	return nil
    81  }
    82  
    83  func (c *Cache) UpdateBlockedContractMethod(bcl BlockedContractList, isCheckTx bool) {
    84  	if isCheckTx {
    85  		return
    86  	}
    87  	c.blockedMutex.Lock()
    88  	c.blockedContractMethodsCache = make(map[string]BlockedContract, len(bcl))
    89  	for i, _ := range bcl {
    90  		c.blockedContractMethodsCache[string(bcl[i].Address)] = bcl[i]
    91  	}
    92  	c.needBlockedUpdate = false
    93  	c.blockedMutex.Unlock()
    94  }
    95  
    96  func SetEvmParamsNeedUpdate() {
    97  	GetEvmParamsCache().SetNeedParamsUpdate()
    98  }
    99  
   100  func GetEvmParamsCache() *Cache {
   101  	return evmParamsCache
   102  }