Skip to content

Commit b0ac60b

Browse files
authored
Fix data race for block (#4462)
1 parent 1938d2a commit b0ac60b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

blockchain/blockdao/blockdao.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,15 @@ func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
334334
}
335335
}
336336
atomic.StoreUint64(&dao.tipHeight, blk.Height())
337-
header := blk.Header
338-
hash := blk.HashBlock()
339-
lruCachePut(dao.headerCache, blk.Height(), &header)
340-
lruCachePut(dao.headerCache, header.HashHeader(), &header)
341-
lruCachePut(dao.blockCache, hash, blk)
342-
lruCachePut(dao.blockCache, blk.Height(), blk)
343337
timer.End()
338+
defer func() {
339+
header := blk.Header
340+
hash := blk.HashBlock()
341+
lruCachePut(dao.headerCache, blk.Height(), &header)
342+
lruCachePut(dao.headerCache, header.HashHeader(), &header)
343+
lruCachePut(dao.blockCache, hash, blk)
344+
lruCachePut(dao.blockCache, blk.Height(), blk)
345+
}()
344346

345347
// index the block if there's indexer
346348
timer = dao.timerFactory.NewTimer("index_block")

0 commit comments

Comments
 (0)