diff --git a/app/protocol/flowcontext/flow_context.go b/app/protocol/flowcontext/flow_context.go index a8cd7d24c..c88906a02 100644 --- a/app/protocol/flowcontext/flow_context.go +++ b/app/protocol/flowcontext/flow_context.go @@ -53,7 +53,7 @@ type FlowContext struct { peersMutex sync.RWMutex orphans map[externalapi.DomainHash]*externalapi.DomainBlock - orphansMutex sync.Mutex + orphansMutex sync.RWMutex } // New returns a new instance of FlowContext. diff --git a/app/protocol/flowcontext/ibd.go b/app/protocol/flowcontext/ibd.go index fd2a038ba..e7d8a7b92 100644 --- a/app/protocol/flowcontext/ibd.go +++ b/app/protocol/flowcontext/ibd.go @@ -58,11 +58,15 @@ func (f *FlowContext) selectPeerForIBD(syncInfo *externalapi.SyncInfo) (*peerpkg for _, peer := range f.peers { peerSelectedTipHash := peer.SelectedTipHash() + + if f.IsOrphan(peerSelectedTipHash) { + continue + } + blockInfo, err := f.domain.Consensus().GetBlockInfo(peerSelectedTipHash) if err != nil { return nil, err } - if syncInfo.State == externalapi.SyncStateHeadersFirst { if !blockInfo.Exists { return peer, nil diff --git a/app/protocol/flowcontext/orphans.go b/app/protocol/flowcontext/orphans.go index f0e2e4e96..5411dfb39 100644 --- a/app/protocol/flowcontext/orphans.go +++ b/app/protocol/flowcontext/orphans.go @@ -18,6 +18,15 @@ func (f *FlowContext) AddOrphan(orphanBlock *externalapi.DomainBlock) { log.Infof("Received a block with missing parents, adding to orphan pool: %s", orphanHash) } +// IsOrphan returns whether the given blockHash belongs to an orphan block +func (f *FlowContext) IsOrphan(blockHash *externalapi.DomainHash) bool { + f.orphansMutex.RLock() + defer f.orphansMutex.RUnlock() + + _, ok := f.orphans[*blockHash] + return ok +} + // UnorphanBlocks removes the block from the orphan set, and remove all of the blocks that are not orphans anymore. func (f *FlowContext) UnorphanBlocks(rootBlock *externalapi.DomainBlock) ([]*externalapi.DomainBlock, error) { f.orphansMutex.Lock() diff --git a/app/protocol/flows/blockrelay/handle_relay_invs.go b/app/protocol/flows/blockrelay/handle_relay_invs.go index 3f10eff38..ed354f149 100644 --- a/app/protocol/flows/blockrelay/handle_relay_invs.go +++ b/app/protocol/flows/blockrelay/handle_relay_invs.go @@ -26,6 +26,7 @@ type RelayInvsContext interface { IsInIBD() bool Broadcast(message appmessage.Message) error AddOrphan(orphanBlock *externalapi.DomainBlock) + IsOrphan(blockHash *externalapi.DomainHash) bool } type handleRelayInvsFlow struct { @@ -71,6 +72,10 @@ func (flow *handleRelayInvsFlow) start() error { continue } + if flow.IsOrphan(inv.Hash) { + continue + } + err = flow.StartIBDIfRequired() if err != nil { return err