coalesce both reverts and applys in the same manner

This commit is contained in:
vyzo 2020-11-04 13:51:18 +02:00
parent 4058f5642f
commit ec13c5f80d

View File

@ -80,41 +80,77 @@ func (c *HeadChangeCoalescer) background(delay time.Duration) {
} }
func (c *HeadChangeCoalescer) coalesce(revert, apply []*types.TipSet) { func (c *HeadChangeCoalescer) coalesce(revert, apply []*types.TipSet) {
// newly reverted tipsets cancel out pending applied tipsets // newly reverted tipsets cancel out with pending applys.
// we iterate through the revert set and if a tipset is pending for apply we cancel it. // similarly, newly applied tipsets cancel out with pending reverts.
// pending tipsets for apply // pending tipsets
applied := make(map[types.TipSetKey]struct{}) pendRevert := make(map[types.TipSetKey]struct{}, len(c.revert))
for _, ts := range c.apply { for _, ts := range c.revert {
applied[ts.Key()] = struct{}{} pendRevert[ts.Key()] = struct{}{}
} }
// freshly reverted tipsets from the pending applied set pendApply := make(map[types.TipSetKey]struct{}, len(c.apply))
reverted := make(map[types.TipSetKey]struct{}) for _, ts := range c.apply {
pendApply[ts.Key()] = struct{}{}
}
// incoming tipsets
reverting := make(map[types.TipSetKey]struct{}, len(revert))
for _, ts := range revert { for _, ts := range revert {
key := ts.Key() reverting[ts.Key()] = struct{}{}
}
_, ok := applied[key] applying := make(map[types.TipSetKey]struct{}, len(apply))
if ok { for _, ts := range apply {
reverted[key] = struct{}{} applying[ts.Key()] = struct{}{}
}
// 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()]
if cancel {
continue continue
} }
c.revert = append(c.revert, ts) newRevert = append(newRevert, ts)
} }
newApply := make([]*types.TipSet, 0, len(c.apply)-len(reverted)+len(apply)) for _, ts := range revert {
_, cancel := pendApply[ts.Key()]
if cancel {
continue
}
newRevert = append(newRevert, 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 { for _, ts := range c.apply {
_, ok := reverted[ts.Key()] _, cancel := reverting[ts.Key()]
if ok { if cancel {
continue continue
} }
newApply = append(newApply, ts) newApply = append(newApply, ts)
} }
newApply = append(newApply, apply...) for _, ts := range apply {
_, cancel := pendRevert[ts.Key()]
if cancel {
continue
}
newApply = append(newApply, ts)
}
// commit the coalesced sets
c.revert = newRevert
c.apply = newApply c.apply = newApply
} }