From f8c6a4a7b9f4565a3f7694d29bd1e15a87fe4db9 Mon Sep 17 00:00:00 2001 From: j75689 Date: Tue, 19 Apr 2022 12:46:01 +0800 Subject: [PATCH] change type of diffLayerChanCache to sync.Map --- core/blockchain.go | 8 ++++---- core/remote_state_verifier.go | 14 +++----------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 20a57bdb3e..c1bfbff616 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -227,7 +227,7 @@ type BlockChain struct { // trusted diff layers diffLayerCache *lru.Cache // Cache for the diffLayers diffLayerRLPCache *lru.Cache // Cache for the rlp encoded diffLayers - diffLayerChanCache *lru.Cache // Cache for + diffLayerChanCache *sync.Map // Cache for diffQueue *prque.Prque // A Priority queue to store recent diff layer diffQueueBuffer chan *types.DiffLayer diffLayerFreezerBlockLimit uint64 @@ -279,7 +279,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par futureBlocks, _ := lru.New(maxFutureBlocks) diffLayerCache, _ := lru.New(diffLayerCacheLimit) diffLayerRLPCache, _ := lru.New(diffLayerRLPCacheLimit) - diffLayerChanCache, _ := lru.New(diffLayerCacheLimit) + diffLayerChanCache := new(sync.Map) bc := &BlockChain{ chainConfig: chainConfig, @@ -529,7 +529,7 @@ func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, sorted bool) { } bc.diffLayerCache.Add(diffLayer.BlockHash, diffLayer) - if cached, ok := bc.diffLayerChanCache.Get(diffLayer.BlockHash); ok { + if cached, ok := bc.diffLayerChanCache.Load(diffLayer.BlockHash); ok { diffLayerCh := cached.(chan struct{}) close(diffLayerCh) } @@ -1834,7 +1834,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. diffLayer.Number = block.NumberU64() diffLayerCh := make(chan struct{}) - bc.diffLayerChanCache.Add(diffLayer.BlockHash, diffLayerCh) + bc.diffLayerChanCache.Store(diffLayer.BlockHash, diffLayerCh) go bc.cacheDiffLayer(diffLayer, false) } diff --git a/core/remote_state_verifier.go b/core/remote_state_verifier.go index c190671850..ed768e0a94 100644 --- a/core/remote_state_verifier.go +++ b/core/remote_state_verifier.go @@ -76,7 +76,7 @@ func NewVerifyManager(blockchain *BlockChain, peers verifyPeers, allowInsecure b } diffLayerCh := make(chan struct{}) close(diffLayerCh) - blockchain.diffLayerChanCache.Add(blockHash, diffLayerCh) + blockchain.diffLayerChanCache.Store(blockHash, diffLayerCh) } vm := &remoteVerifyManager{ @@ -167,24 +167,16 @@ func (vm *remoteVerifyManager) NewBlockVerifyTask(header *types.Header) { } var diffLayer *types.DiffLayer - if cached, ok := vm.bc.diffLayerChanCache.Get(hash); ok { + if cached, ok := vm.bc.diffLayerChanCache.Load(hash); ok { diffLayerCh := cached.(chan struct{}) <-diffLayerCh - vm.bc.diffLayerChanCache.Remove(hash) + vm.bc.diffLayerChanCache.Delete(hash) diffLayer = vm.bc.GetTrustedDiffLayer(hash) } // if this block has no diff, there is no need to verify it. var err error if diffLayer == nil { log.Info("block's trusted diffLayer is nil", "hash", hash, "number", header.Number) - //if diffLayer, err = vm.bc.GenerateDiffLayer(hash); err != nil { - // log.Error("failed to get diff layer", "block", hash, "number", header.Number, "error", err) - // return - //} else if diffLayer == nil { - // log.Info("this is an empty block:", "block", hash, "number", header.Number) - // vm.cacheBlockVerified(hash) - // return - //} } diffHash, err := CalculateDiffHash(diffLayer) if err != nil {