From 337f5026eb6b3efb72a55c766b085a18ea34e4dd Mon Sep 17 00:00:00 2001 From: Xiong Date: Wed, 24 May 2023 16:50:02 -0400 Subject: [PATCH] bug fix --- x/register/keeper/keeper.go | 57 +++++++++++++++++----------------- x/register/keeper/meta_node.go | 34 ++++++++++---------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/x/register/keeper/keeper.go b/x/register/keeper/keeper.go index d07d1822..c79ecd66 100644 --- a/x/register/keeper/keeper.go +++ b/x/register/keeper/keeper.go @@ -19,25 +19,22 @@ import ( regtypes "github.com/stratosnet/stratos-chain/x/register/types" ) -var ( - metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY - cacheMutex sync.RWMutex -) - // Keeper of the register store type Keeper struct { - storeKey sdk.StoreKey - cdc codec.Codec - paramSpace paramtypes.Subspace - accountKeeper types.AccountKeeper - bankKeeper types.BankKeeper - distrKeeper types.DistrKeeper - hooks types.RegisterHooks - resourceNodeCache map[string]cachedResourceNode - resourceNodeCacheList *list.List - metaNodeCache map[string]cachedMetaNode - metaNodeCacheList *list.List - metaNodeBitMapIndexCache map[string]int + storeKey sdk.StoreKey + cdc codec.Codec + paramSpace paramtypes.Subspace + accountKeeper types.AccountKeeper + bankKeeper types.BankKeeper + distrKeeper types.DistrKeeper + hooks types.RegisterHooks + resourceNodeCache map[string]cachedResourceNode + resourceNodeCacheList *list.List + metaNodeCache map[string]cachedMetaNode + metaNodeCacheList *list.List + metaNodeBitMapIndexCache map[string]int + metaNodeBitMapIndexCacheStatus types.CacheStatus + cacheMutex sync.RWMutex } // NewKeeper creates a register keeper @@ -45,18 +42,20 @@ func NewKeeper(cdc codec.Codec, key sdk.StoreKey, paramSpace paramtypes.Subspace accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, distrKeeper types.DistrKeeper) Keeper { keeper := Keeper{ - storeKey: key, - cdc: cdc, - paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()), - accountKeeper: accountKeeper, - bankKeeper: bankKeeper, - distrKeeper: distrKeeper, - hooks: nil, - resourceNodeCache: make(map[string]cachedResourceNode, resourceNodeCacheSize), - resourceNodeCacheList: list.New(), - metaNodeCache: make(map[string]cachedMetaNode, metaNodeCacheSize), - metaNodeCacheList: list.New(), - metaNodeBitMapIndexCache: make(map[string]int), + storeKey: key, + cdc: cdc, + paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()), + accountKeeper: accountKeeper, + bankKeeper: bankKeeper, + distrKeeper: distrKeeper, + hooks: nil, + resourceNodeCache: make(map[string]cachedResourceNode, resourceNodeCacheSize), + resourceNodeCacheList: list.New(), + metaNodeCache: make(map[string]cachedMetaNode, metaNodeCacheSize), + metaNodeCacheList: list.New(), + metaNodeBitMapIndexCache: make(map[string]int), + metaNodeBitMapIndexCacheStatus: types.CACHE_DIRTY, + cacheMutex: sync.RWMutex{}, } return keeper } diff --git a/x/register/keeper/meta_node.go b/x/register/keeper/meta_node.go index e3bb8c33..014852ba 100644 --- a/x/register/keeper/meta_node.go +++ b/x/register/keeper/meta_node.go @@ -556,8 +556,8 @@ func (k Keeper) OwnMetaNode(ctx sdk.Context, ownerAddr sdk.AccAddress, p2pAddr s func (k Keeper) GetMetaNodeBitMapIndex(ctx sdk.Context, networkAddr stratos.SdsAddress) (index int, err error) { k.UpdateMetaNodeBitMapIdxCache(ctx) - cacheMutex.RLock() - defer cacheMutex.RUnlock() + k.cacheMutex.RLock() + defer k.cacheMutex.RUnlock() index, ok := k.metaNodeBitMapIndexCache[networkAddr.String()] if !ok { @@ -571,29 +571,30 @@ func (k Keeper) GetMetaNodeBitMapIndex(ctx sdk.Context, networkAddr stratos.SdsA } func (k Keeper) AddMetaNodeToBitMapIdxCache(networkAddr stratos.SdsAddress) { - cacheMutex.Lock() - defer cacheMutex.Unlock() + k.cacheMutex.Lock() + defer k.cacheMutex.Unlock() k.metaNodeBitMapIndexCache[networkAddr.String()] = -1 - metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY + k.metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY } func (k Keeper) RemoveMetaNodeFromBitMapIdxCache(networkAddr stratos.SdsAddress) { - cacheMutex.Lock() - defer cacheMutex.Unlock() + k.cacheMutex.Lock() + defer k.cacheMutex.Unlock() delete(k.metaNodeBitMapIndexCache, networkAddr.String()) - metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY + k.metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY } func (k Keeper) UpdateMetaNodeBitMapIdxCache(ctx sdk.Context) { - cacheMutex.Lock() - defer cacheMutex.Unlock() + k.cacheMutex.Lock() - if metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY { + if k.metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY { + k.cacheMutex.Unlock() return } if len(k.metaNodeBitMapIndexCache) == 0 { + k.cacheMutex.Unlock() k.ReloadMetaNodeBitMapIdxCache(ctx) return } @@ -608,14 +609,15 @@ func (k Keeper) UpdateMetaNodeBitMapIdxCache(ctx sdk.Context) { for index, key := range keys { k.metaNodeBitMapIndexCache[key] = index } - metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY + k.metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY + k.cacheMutex.Unlock() } func (k Keeper) ReloadMetaNodeBitMapIdxCache(ctx sdk.Context) { - cacheMutex.Lock() - defer cacheMutex.Unlock() + k.cacheMutex.Lock() + defer k.cacheMutex.Unlock() - if metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY { + if k.metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY { return } keys := make([]string, 0) @@ -637,5 +639,5 @@ func (k Keeper) ReloadMetaNodeBitMapIdxCache(ctx sdk.Context) { for index, key := range keys { k.metaNodeBitMapIndexCache[key] = index } - metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY + k.metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY }