Skip to content

Commit

Permalink
change type of diffLayerChanCache to sync.Map
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Apr 19, 2022
1 parent 5675916 commit f8c6a4a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 3 additions & 11 deletions core/remote_state_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f8c6a4a

Please sign in to comment.