lotus/chain/events/events_height.go

247 lines
7.1 KiB
Go
Raw Permalink Normal View History

2019-09-05 07:40:50 +00:00
package events
2019-09-04 16:09:08 +00:00
import (
2019-11-05 14:03:59 +00:00
"context"
"sync"
2019-09-05 07:40:50 +00:00
2019-11-08 20:11:56 +00:00
"go.opencensus.io/trace"
2020-09-07 03:49:10 +00:00
"golang.org/x/xerrors"
2019-11-08 20:11:56 +00:00
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/types"
2019-09-04 16:09:08 +00:00
)
2021-08-04 00:10:30 +00:00
type heightHandler struct {
ts *types.TipSet
height abi.ChainEpoch
called bool
2021-08-04 00:10:30 +00:00
handle HeightHandler
revert RevertHandler
}
2021-08-04 00:10:30 +00:00
type heightEvents struct {
api EventAPI
gcConfidence abi.ChainEpoch
2021-08-04 00:10:30 +00:00
lk sync.Mutex
head *types.TipSet
tsHeights, triggerHeights map[abi.ChainEpoch][]*heightHandler
lastGc abi.ChainEpoch //nolint:structcheck
}
2019-11-05 14:03:59 +00:00
func newHeightEvents(api EventAPI, obs *observer, gcConfidence abi.ChainEpoch) *heightEvents {
he := &heightEvents{
2021-08-04 00:10:30 +00:00
api: api,
gcConfidence: gcConfidence,
tsHeights: map[abi.ChainEpoch][]*heightHandler{},
triggerHeights: map[abi.ChainEpoch][]*heightHandler{},
}
he.lk.Lock()
he.head = obs.Observe((*heightEventsObserver)(he))
he.lk.Unlock()
return he
}
2021-08-04 00:10:30 +00:00
// ChainAt invokes the specified `HeightHandler` when the chain reaches the
// specified height+confidence threshold. If the chain is rolled-back under the
// specified height, `RevertHandler` will be called.
//
2021-08-27 19:12:55 +00:00
// ts passed to handlers is the tipset at the specified epoch, or above if lower tipsets were null.
2021-08-04 00:10:30 +00:00
//
// The context governs cancellations of this call, it won't cancel the event handler.
func (e *heightEvents) ChainAt(ctx context.Context, hnd HeightHandler, rev RevertHandler, confidence int, h abi.ChainEpoch) error {
if abi.ChainEpoch(confidence) > e.gcConfidence {
// Need this to be able to GC effectively.
return xerrors.Errorf("confidence cannot be greater than gcConfidence: %d > %d", confidence, e.gcConfidence)
}
handler := &heightHandler{
height: h,
handle: hnd,
revert: rev,
}
triggerAt := h + abi.ChainEpoch(confidence)
2019-11-05 14:36:44 +00:00
2021-08-04 00:10:30 +00:00
// Here we try to jump onto a moving train. To avoid stopping the train, we release the lock
// while calling the API and/or the trigger functions. Unfortunately, it's entirely possible
// (although unlikely) to go back and forth across the trigger heights, so we need to keep
// going back and forth here till we're synced.
//
// TODO: Consider using a worker goroutine so we can just drop the handler in a channel? The
// downside is that we'd either need a tipset cache, or we'd need to potentially fetch
// tipsets in-line inside the event loop.
e.lk.Lock()
2021-08-04 00:10:30 +00:00
for {
head := e.head
if head.Height() >= h {
// Head is past the handler height. We at least need to stash the tipset to
// avoid doing this from the main event loop.
e.lk.Unlock()
2019-11-05 14:36:44 +00:00
2021-08-04 00:10:30 +00:00
var ts *types.TipSet
if head.Height() == h {
ts = head
} else {
var err error
ts, err = e.api.ChainGetTipSetAfterHeight(ctx, handler.height, head.Key())
2019-10-04 22:43:04 +00:00
if err != nil {
2021-08-04 00:10:30 +00:00
return xerrors.Errorf("events.ChainAt: failed to get tipset: %s", err)
2019-10-04 22:43:04 +00:00
}
2019-09-04 16:09:08 +00:00
}
2021-08-04 00:10:30 +00:00
// If we've applied the handler on the wrong tipset, revert.
if handler.called && !ts.Equals(handler.ts) {
ctx, span := trace.StartSpan(ctx, "events.HeightRevert")
span.AddAttributes(trace.BoolAttribute("immediate", true))
err := handler.revert(ctx, handler.ts)
span.End()
2019-10-04 22:43:04 +00:00
if err != nil {
return err
}
2021-08-04 00:10:30 +00:00
handler.called = false
}
// Save the tipset.
handler.ts = ts
2019-10-04 22:43:04 +00:00
2021-08-04 00:10:30 +00:00
// If we've reached confidence and haven't called, call.
if !handler.called && head.Height() >= triggerAt {
2019-11-05 14:36:44 +00:00
ctx, span := trace.StartSpan(ctx, "events.HeightApply")
2021-08-04 00:10:30 +00:00
span.AddAttributes(trace.BoolAttribute("immediate", true))
err := handler.handle(ctx, handler.ts, head.Height())
2019-11-05 14:36:44 +00:00
span.End()
if err != nil {
2021-08-04 00:10:30 +00:00
return err
2019-10-04 22:43:04 +00:00
}
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
handler.called = true
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
// If we've reached gcConfidence, return without saving anything.
if head.Height() >= h+e.gcConfidence {
return nil
}
2019-10-04 22:43:04 +00:00
}
2021-08-04 00:10:30 +00:00
e.lk.Lock()
} else if handler.called {
// We're not passed the head (anymore) but have applied the handler. Revert, try again.
e.lk.Unlock()
ctx, span := trace.StartSpan(ctx, "events.HeightRevert")
span.AddAttributes(trace.BoolAttribute("immediate", true))
err := handler.revert(ctx, handler.ts)
span.End()
if err != nil {
2019-10-04 22:43:04 +00:00
return err
2019-09-04 16:09:08 +00:00
}
2021-08-04 00:10:30 +00:00
handler.called = false
e.lk.Lock()
} // otherwise, we changed heads but the change didn't matter.
// If we managed to get through this without the head changing, we're finally done.
if head.Equals(e.head) {
e.triggerHeights[triggerAt] = append(e.triggerHeights[triggerAt], handler)
e.tsHeights[h] = append(e.tsHeights[h], handler)
e.lk.Unlock()
return nil
2019-09-04 16:09:08 +00:00
}
}
2021-08-04 00:10:30 +00:00
}
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
// Updates the head and garbage collects if we're 2x over our garbage collection confidence period.
func (e *heightEventsObserver) updateHead(h *types.TipSet) {
e.lk.Lock()
defer e.lk.Unlock()
e.head = h
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
if e.head.Height() < e.lastGc+e.gcConfidence*2 {
return
}
2021-08-04 00:10:30 +00:00
e.lastGc = h.Height()
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
targetGcHeight := e.head.Height() - e.gcConfidence
for h := range e.tsHeights {
if h >= targetGcHeight {
continue
2019-09-04 16:09:08 +00:00
}
2021-08-04 00:10:30 +00:00
delete(e.tsHeights, h)
}
for h := range e.triggerHeights {
if h >= targetGcHeight {
continue
}
delete(e.triggerHeights, h)
}
}
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
type heightEventsObserver heightEvents
2019-11-05 14:36:44 +00:00
2021-08-04 00:10:30 +00:00
func (e *heightEventsObserver) Revert(ctx context.Context, from, to *types.TipSet) error {
// Update the head first so we don't accidental skip reverting a concurrent call to ChainAt.
e.updateHead(to)
2019-11-05 14:36:44 +00:00
// Call revert on all heights between the two tipsets, handling empty tipsets.
2021-08-04 00:10:30 +00:00
for h := from.Height(); h > to.Height(); h-- {
e.lk.Lock()
2021-08-04 00:10:30 +00:00
triggers := e.tsHeights[h]
e.lk.Unlock()
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
// 1. Triggers are only invoked from the global event loop, we don't need to hold the lock while calling.
// 2. We only ever append to or replace the trigger slice, so it's safe to iterate over it without the lock.
for _, handler := range triggers {
handler.ts = nil // invalidate
if !handler.called {
// We haven't triggered this yet, or there has been a concurrent call to ChainAt.
continue
}
ctx, span := trace.StartSpan(ctx, "events.HeightRevert")
err := handler.revert(ctx, from)
span.End()
2021-08-04 00:10:30 +00:00
if err != nil {
log.Errorf("reverting chain trigger (@H %d): %s", h, err)
}
handler.called = false
}
2019-09-04 16:09:08 +00:00
}
2021-08-04 00:10:30 +00:00
return nil
}
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
func (e *heightEventsObserver) Apply(ctx context.Context, from, to *types.TipSet) error {
// Update the head first so we don't accidental skip applying a concurrent call to ChainAt.
e.updateHead(to)
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
for h := from.Height() + 1; h <= to.Height(); h++ {
e.lk.Lock()
triggers := e.triggerHeights[h]
tipsets := e.tsHeights[h]
e.lk.Unlock()
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
// Stash the tipset for future triggers.
for _, handler := range tipsets {
handler.ts = to
}
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
// Trigger the ready triggers.
for _, handler := range triggers {
if handler.called {
// We may have reverted past the trigger point, but not past the call point.
// Or there has been a concurrent call to ChainAt.
continue
}
ctx, span := trace.StartSpan(ctx, "events.HeightApply")
span.AddAttributes(trace.BoolAttribute("immediate", false))
err := handler.handle(ctx, handler.ts, h)
span.End()
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
if err != nil {
log.Errorf("chain trigger (@H %d, called @ %d) failed: %+v", h, to.Height(), err)
}
2019-09-04 16:09:08 +00:00
2021-08-04 00:10:30 +00:00
handler.called = true
}
}
2019-09-04 16:09:08 +00:00
return nil
}