diff --git a/core/blockchain.go b/core/blockchain.go index 946fb78c40..3b1c3c96b4 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 *sync.Map // Cache for + diffLayerChanCache *lru.Cache // 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 := new(sync.Map) + diffLayerChanCache, _ := lru.New(diffLayerCacheLimit) 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.Load(diffLayer.BlockHash); ok { + if cached, ok := bc.diffLayerChanCache.Get(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.Store(diffLayer.BlockHash, diffLayerCh) + bc.diffLayerChanCache.Add(diffLayer.BlockHash, diffLayerCh) go bc.cacheDiffLayer(diffLayer, false) } diff --git a/core/remote_state_verifier.go b/core/remote_state_verifier.go index 7c1687fd83..02cbd40f39 100644 --- a/core/remote_state_verifier.go +++ b/core/remote_state_verifier.go @@ -182,16 +182,24 @@ func (vm *remoteVerifyManager) NewBlockVerifyTask(header *types.Header) { } var diffLayer *types.DiffLayer - if cached, ok := vm.bc.diffLayerChanCache.Load(hash); ok { + if cached, ok := vm.bc.diffLayerChanCache.Get(hash); ok { diffLayerCh := cached.(chan struct{}) <-diffLayerCh - vm.bc.diffLayerChanCache.Delete(hash) + vm.bc.diffLayerChanCache.Remove(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 {