diff --git a/internal/db/merge.go b/internal/db/merge.go index 1720960ccd..69d001784f 100644 --- a/internal/db/merge.go +++ b/internal/db/merge.go @@ -158,7 +158,7 @@ func (db *db) newMergeProcessor( type mergeTarget struct { heads map[cid.Cid]*coreblock.Block - headHeigth uint64 + headHeight uint64 } func newMergeTarget() mergeTarget { @@ -189,7 +189,7 @@ func (mp *mergeProcessor) loadComposites( return nil } - if block.Delta.GetPriority() >= mt.headHeigth { + if block.Delta.GetPriority() >= mt.headHeight { mp.composites.PushFront(block) for _, link := range block.Links { if link.Name == core.HEAD { @@ -214,7 +214,7 @@ func (mp *mergeProcessor) loadComposites( } newMT.heads[link.Cid] = childBlock - newMT.headHeigth = childBlock.Delta.GetPriority() + newMT.headHeight = childBlock.Delta.GetPriority() } } return mp.loadComposites(ctx, blockCid, newMT) @@ -385,7 +385,7 @@ func getHeadsAsMergeTarget(ctx context.Context, txn datastore.Txn, dsKey core.Da mt.heads[cid] = block // All heads have the same height so overwriting is ok. - mt.headHeigth = block.Delta.GetPriority() + mt.headHeight = block.Delta.GetPriority() } return mt, nil } diff --git a/net/process.go b/net/process.go index bc99216022..b4f85134fb 100644 --- a/net/process.go +++ b/net/process.go @@ -34,7 +34,7 @@ type blockProcessor struct { *Peer wg *sync.WaitGroup bsSession *blockservice.Session - queuedChildren *cidSafeSet + queuedChildren *sync.Map } func newBlockProcessor( @@ -45,7 +45,7 @@ func newBlockProcessor( Peer: p, wg: &sync.WaitGroup{}, bsSession: blockservice.NewSession(ctx, p.bserv), - queuedChildren: newCidSafeSet(), + queuedChildren: &sync.Map{}, } } @@ -93,7 +93,7 @@ func (bp *blockProcessor) handleChildBlocks( if exists { continue } - if bp.queuedChildren.Visit(link.Cid) { + if _, loaded := bp.queuedChildren.LoadOrStore(link.Cid, struct{}{}); !loaded { links = append(links, link.Cid) } } @@ -119,40 +119,6 @@ func (bp *blockProcessor) handleChildBlocks( } for _, link := range links { - bp.queuedChildren.Remove(link) + bp.queuedChildren.Delete(link) } } - -type cidSafeSet struct { - set map[cid.Cid]struct{} - mux sync.Mutex -} - -func newCidSafeSet() *cidSafeSet { - return &cidSafeSet{ - set: make(map[cid.Cid]struct{}), - } -} - -// Visit checks if we can visit this node, or -// if its already being visited -func (s *cidSafeSet) Visit(c cid.Cid) bool { - var b bool - s.mux.Lock() - { - if _, ok := s.set[c]; !ok { - s.set[c] = struct{}{} - b = true - } - } - s.mux.Unlock() - return b -} - -func (s *cidSafeSet) Remove(c cid.Cid) { - s.mux.Lock() - { - delete(s.set, c) - } - s.mux.Unlock() -}