diff --git a/process/block/baseProcess.go b/process/block/baseProcess.go index a22fa002d9..657d4a5471 100644 --- a/process/block/baseProcess.go +++ b/process/block/baseProcess.go @@ -2370,7 +2370,7 @@ func (bp *baseProcessor) requestNextHeader(currentHeaderHash []byte, nonce uint6 } } -func (bp *baseProcessor) waitAllMissingProofs() error { +func (bp *baseProcessor) waitAllMissingProofs(waitTime time.Duration) error { bp.mutRequestedAttestingNoncesMap.RLock() isWaitingForProofs := len(bp.requestedAttestingNoncesMap) > 0 bp.mutRequestedAttestingNoncesMap.RUnlock() @@ -2381,7 +2381,7 @@ func (bp *baseProcessor) waitAllMissingProofs() error { select { case <-bp.allProofsReceived: return nil - case <-time.After(bp.extraDelayRequestBlockInfo): + case <-time.After(waitTime): bp.mutRequestedAttestingNoncesMap.RLock() defer bp.mutRequestedAttestingNoncesMap.RUnlock() @@ -2396,7 +2396,7 @@ func (bp *baseProcessor) waitAllMissingProofs() error { } } -func (bp *baseProcessor) checkReceivedHeaderAndUpdateMissingAttesting(headerHandler data.HeaderHandler) { +func (bp *baseProcessor) checkReceivedHeaderIfAttestingIsNeeded(headerHandler data.HeaderHandler) { if !common.ShouldBlockHavePrevProof(headerHandler, bp.enableEpochsHandler, common.EquivalentMessagesFlag) { return } @@ -2408,29 +2408,30 @@ func (bp *baseProcessor) checkReceivedHeaderAndUpdateMissingAttesting(headerHand return } + allProofsReceived := bp.checkReceivedHeaderAndUpdateMissingAttesting(headerHandler) + if allProofsReceived { + bp.allProofsReceived <- true + } +} + +func (bp *baseProcessor) checkReceivedHeaderAndUpdateMissingAttesting(headerHandler data.HeaderHandler) bool { bp.mutRequestedAttestingNoncesMap.Lock() + defer bp.mutRequestedAttestingNoncesMap.Unlock() receivedShard := headerHandler.GetShardID() prevHash := headerHandler.GetPrevHash() _, isHeaderWithoutProof := bp.requestedAttestingNoncesMap[string(prevHash)] if !isHeaderWithoutProof { log.Debug("received header does not have previous hash any of the requested ones") - bp.mutRequestedAttestingNoncesMap.Unlock() - return + return len(bp.requestedAttestingNoncesMap) == 0 } if !bp.proofsPool.HasProof(receivedShard, prevHash) { log.Debug("received next header but proof is still missing", "hash", hex.EncodeToString(prevHash)) - bp.mutRequestedAttestingNoncesMap.Unlock() - return + return len(bp.requestedAttestingNoncesMap) == 0 } delete(bp.requestedAttestingNoncesMap, string(prevHash)) - allProofsReceived := len(bp.requestedAttestingNoncesMap) == 0 - bp.mutRequestedAttestingNoncesMap.Unlock() - - if allProofsReceived { - bp.allProofsReceived <- true - } + return len(bp.requestedAttestingNoncesMap) == 0 } diff --git a/process/block/metablock.go b/process/block/metablock.go index 2f8ce9571d..9f7c193fcf 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -343,7 +343,7 @@ func (mp *metaProcessor) ProcessBlock( } } - err = mp.checkProofsForShardData(header) + err = mp.checkProofsForShardData(header, haveTime()) if err != nil { return err } @@ -418,15 +418,15 @@ func (mp *metaProcessor) ProcessBlock( return nil } -func (mp *metaProcessor) checkProofsForShardData(header *block.MetaBlock) error { +func (mp *metaProcessor) checkProofsForShardData(header *block.MetaBlock, waitTime time.Duration) error { if !(mp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, header.Epoch) && header.GetNonce() > 1) { return nil } mp.mutRequestedAttestingNoncesMap.Lock() mp.requestedAttestingNoncesMap = make(map[string]uint64) - _ = core.EmptyChannel(mp.allProofsReceived) mp.mutRequestedAttestingNoncesMap.Unlock() + _ = core.EmptyChannel(mp.allProofsReceived) for _, shardData := range header.ShardInfo { // TODO: consider the validation of the proof: @@ -466,7 +466,7 @@ func (mp *metaProcessor) checkProofsForShardData(header *block.MetaBlock) error } } - return mp.waitAllMissingProofs() + return mp.waitAllMissingProofs(waitTime) } func (mp *metaProcessor) processEpochStartMetaBlock( @@ -2075,7 +2075,7 @@ func (mp *metaProcessor) receivedShardHeader(headerHandler data.HeaderHandler, s "hash", shardHeaderHash, ) - mp.checkReceivedHeaderAndUpdateMissingAttesting(headerHandler) + mp.checkReceivedHeaderIfAttestingIsNeeded(headerHandler) mp.hdrsForCurrBlock.mutHdrsForBlock.Lock() diff --git a/process/block/shardblock.go b/process/block/shardblock.go index dc00a4fc25..e88dc71828 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -295,8 +295,8 @@ func (sp *shardProcessor) ProcessBlock( sp.mutRequestedAttestingNoncesMap.Lock() sp.requestedAttestingNoncesMap = make(map[string]uint64) - _ = core.EmptyChannel(sp.allProofsReceived) sp.mutRequestedAttestingNoncesMap.Unlock() + _ = core.EmptyChannel(sp.allProofsReceived) // check proofs for cross notarized metablocks for _, metaBlockHash := range header.GetMetaBlockHashes() { @@ -312,7 +312,7 @@ func (sp *shardProcessor) ProcessBlock( sp.checkProofRequestingNextHeaderIfMissing(core.MetachainShardId, metaBlockHash, hInfo.hdr.GetNonce()) } - err = sp.waitAllMissingProofs() + err = sp.waitAllMissingProofs(haveTime()) if err != nil { return err } @@ -1763,7 +1763,7 @@ func (sp *shardProcessor) receivedMetaBlock(headerHandler data.HeaderHandler, me "hash", metaBlockHash, ) - sp.checkReceivedHeaderAndUpdateMissingAttesting(headerHandler) + sp.checkReceivedHeaderIfAttestingIsNeeded(headerHandler) sp.hdrsForCurrBlock.mutHdrsForBlock.Lock()