Skip to content

Commit

Permalink
refactor coalescing logic into its own function, take both cancellati…
Browse files Browse the repository at this point in the history
…on sets into account
  • Loading branch information
vyzo committed Feb 2, 2021
1 parent 2b0022f commit 5c6988a
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions chain/store/coalescer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,50 +157,49 @@ func (c *HeadChangeCoalescer) coalesce(revert, apply []*types.TipSet) {
// coalesced revert set
// - pending reverts are cancelled by incoming applys
// - incoming reverts are cancelled by pending applys
newRevert := make([]*types.TipSet, 0, len(c.revert)+len(revert))
for _, ts := range c.revert {
_, cancel := applying[ts.Key()]
newRevert := c.merge(c.revert, revert, pendApply, applying)

// coalesced apply set
// - pending applys are cancelled by incoming reverts
// - incoming applys are cancelled by pending reverts
newApply := c.merge(c.apply, apply, pendRevert, reverting)

// commit the coalesced sets
c.revert = newRevert
c.apply = newApply
}

func (c *HeadChangeCoalescer) merge(pend, incoming []*types.TipSet, cancel1, cancel2 map[types.TipSetKey]struct{}) []*types.TipSet {
result := make([]*types.TipSet, 0, len(pend)+len(incoming))
for _, ts := range pend {
_, cancel := cancel1[ts.Key()]
if cancel {
continue
}

newRevert = append(newRevert, ts)
}

for _, ts := range revert {
_, cancel := pendApply[ts.Key()]
_, cancel = cancel2[ts.Key()]
if cancel {
continue
}

newRevert = append(newRevert, ts)
result = append(result, ts)
}

// coalesced apply set
// - pending applys are cancelled by incoming reverts
// - incoming applys are cancelled by pending reverts
newApply := make([]*types.TipSet, 0, len(c.apply)+len(apply))
for _, ts := range c.apply {
_, cancel := reverting[ts.Key()]
for _, ts := range incoming {
_, cancel := cancel1[ts.Key()]
if cancel {
continue
}

newApply = append(newApply, ts)
}

for _, ts := range apply {
_, cancel := pendRevert[ts.Key()]
_, cancel = cancel2[ts.Key()]
if cancel {
continue
}

newApply = append(newApply, ts)
result = append(result, ts)
}

// commit the coalesced sets
c.revert = newRevert
c.apply = newApply
return result
}

func (c *HeadChangeCoalescer) dispatch() {
Expand Down

0 comments on commit 5c6988a

Please sign in to comment.