refactor coalescing logic into its own function, take both cancellation sets into account

This commit is contained in:
vyzo 2021-02-02 21:26:50 +02:00
parent 2b0022f1a7
commit 5c6988a7c1

View File

@ -157,52 +157,51 @@ func (c *HeadChangeCoalescer) coalesce(revert, apply []*types.TipSet) {
// coalesced revert set // coalesced revert set
// - pending reverts are cancelled by incoming applys // - pending reverts are cancelled by incoming applys
// - incoming reverts are cancelled by pending applys // - incoming reverts are cancelled by pending applys
newRevert := make([]*types.TipSet, 0, len(c.revert)+len(revert)) newRevert := c.merge(c.revert, revert, pendApply, applying)
for _, ts := range c.revert {
_, cancel := applying[ts.Key()]
if cancel {
continue
}
newRevert = append(newRevert, ts)
}
for _, ts := range revert {
_, cancel := pendApply[ts.Key()]
if cancel {
continue
}
newRevert = append(newRevert, ts)
}
// coalesced apply set // coalesced apply set
// - pending applys are cancelled by incoming reverts // - pending applys are cancelled by incoming reverts
// - incoming applys are cancelled by pending reverts // - incoming applys are cancelled by pending reverts
newApply := make([]*types.TipSet, 0, len(c.apply)+len(apply)) newApply := c.merge(c.apply, apply, pendRevert, reverting)
for _, ts := range c.apply {
_, cancel := reverting[ts.Key()]
if cancel {
continue
}
newApply = append(newApply, ts)
}
for _, ts := range apply {
_, cancel := pendRevert[ts.Key()]
if cancel {
continue
}
newApply = append(newApply, ts)
}
// commit the coalesced sets // commit the coalesced sets
c.revert = newRevert c.revert = newRevert
c.apply = newApply 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
}
_, cancel = cancel2[ts.Key()]
if cancel {
continue
}
result = append(result, ts)
}
for _, ts := range incoming {
_, cancel := cancel1[ts.Key()]
if cancel {
continue
}
_, cancel = cancel2[ts.Key()]
if cancel {
continue
}
result = append(result, ts)
}
return result
}
func (c *HeadChangeCoalescer) dispatch() { func (c *HeadChangeCoalescer) dispatch() {
err := c.notify(c.revert, c.apply) err := c.notify(c.revert, c.apply)
if err != nil { if err != nil {