From 716efbc718b2b1c318fbec3216d918bdb3170e89 Mon Sep 17 00:00:00 2001 From: msutton Date: Sat, 9 Jul 2022 22:08:31 +0300 Subject: [PATCH] Avoid locking consensus twice in the common case of adding block with updateVirtual=true --- domain/consensus/consensus.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/domain/consensus/consensus.go b/domain/consensus/consensus.go index ef45183ed..f1591a551 100644 --- a/domain/consensus/consensus.go +++ b/domain/consensus/consensus.go @@ -196,11 +196,26 @@ func (s *consensus) BuildBlockTemplate(coinbaseData *externalapi.DomainCoinbaseD func (s *consensus) ValidateAndInsertBlock(block *externalapi.DomainBlock, updateVirtual bool) error { if updateVirtual { - // Make sure virtual is resolved before adding the new block - err := s.ResolveVirtual(nil) + s.lock.Lock() + // Make sure virtual is resolved correctly before adding the new block + isCompletelyResolved, err := s.resolveVirtualNoLock(100) if err != nil { + s.lock.Unlock() return err } + if isCompletelyResolved { + defer s.lock.Unlock() + _, err := s.validateAndInsertBlockNoLock(block, updateVirtual) + if err != nil { + return err + } + return nil + } + s.lock.Unlock() + + for !isCompletelyResolved { + isCompletelyResolved, err = s.resolveVirtualChunkWithLock(100) + } } return s.validateAndInsertBlockWithLock(block, updateVirtual)