2020-06-24 18:10:52 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
2020-06-24 18:10:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const NoTimeout = math.MaxInt64
|
2020-06-25 16:12:03 +00:00
|
|
|
const NoHeight = abi.ChainEpoch(-1)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
type triggerID = uint64
|
|
|
|
|
|
|
|
// msgH is the block height at which a message was present / event has happened
|
|
|
|
type msgH = abi.ChainEpoch
|
|
|
|
|
|
|
|
// triggerH is the block height at which the listener will be notified about the
|
2022-08-29 14:25:30 +00:00
|
|
|
//
|
|
|
|
// message (msgH+confidence)
|
2020-06-24 18:10:52 +00:00
|
|
|
type triggerH = abi.ChainEpoch
|
|
|
|
|
|
|
|
type eventData interface{}
|
|
|
|
|
|
|
|
// EventHandler arguments:
|
2020-06-25 16:12:03 +00:00
|
|
|
// `prevTs` is the previous tipset, eg the "from" tipset for a state change.
|
|
|
|
// `ts` is the event tipset, eg the tipset in which the `msg` is included.
|
2020-06-24 18:10:52 +00:00
|
|
|
// `curH`-`ts.Height` = `confidence`
|
2021-08-04 00:10:30 +00:00
|
|
|
type EventHandler func(ctx context.Context, data eventData, prevTs, ts *types.TipSet, curH abi.ChainEpoch) (more bool, err error)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
// CheckFunc is used for atomicity guarantees. If the condition the callbacks
|
|
|
|
// wait for has already happened in tipset `ts`
|
|
|
|
//
|
|
|
|
// If `done` is true, timeout won't be triggered
|
|
|
|
// If `more` is false, no messages will be sent to EventHandler (RevertHandler
|
2022-08-29 14:25:30 +00:00
|
|
|
//
|
|
|
|
// may still be called)
|
2021-08-04 00:10:30 +00:00
|
|
|
type CheckFunc func(ctx context.Context, ts *types.TipSet) (done bool, more bool, err error)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
// Keep track of information for an event handler
|
|
|
|
type handlerInfo struct {
|
|
|
|
confidence int
|
|
|
|
timeout abi.ChainEpoch
|
|
|
|
|
|
|
|
disabled bool // TODO: GC after gcConfidence reached
|
|
|
|
|
|
|
|
handle EventHandler
|
|
|
|
revert RevertHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a change occurs, a queuedEvent is created and put into a queue
|
|
|
|
// until the required confidence is reached
|
|
|
|
type queuedEvent struct {
|
|
|
|
trigger triggerID
|
2021-08-04 00:10:30 +00:00
|
|
|
data eventData
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
prevTipset, tipset *types.TipSet
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
called bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manages chain head change events, which may be forward (new tipset added to
|
|
|
|
// chain) or backward (chain branch discarded in favour of heavier branch)
|
|
|
|
type hcEvents struct {
|
2021-08-04 00:10:30 +00:00
|
|
|
cs EventAPI
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-11 06:53:57 +00:00
|
|
|
lk sync.Mutex
|
2020-06-24 18:10:52 +00:00
|
|
|
lastTs *types.TipSet
|
|
|
|
|
|
|
|
ctr triggerID
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// TODO: get rid of trigger IDs and just use pointers as keys.
|
2020-06-24 18:10:52 +00:00
|
|
|
triggers map[triggerID]*handlerInfo
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// TODO: instead of scheduling events in the future, look at the chain in the past. We can sip the "confidence" queue entirely.
|
2020-06-24 18:10:52 +00:00
|
|
|
// maps block heights to events
|
|
|
|
// [triggerH][msgH][event]
|
|
|
|
confQueue map[triggerH]map[msgH][]*queuedEvent
|
|
|
|
|
|
|
|
// [msgH][triggerH]
|
|
|
|
revertQueue map[msgH][]triggerH
|
|
|
|
|
|
|
|
// [timeoutH+confidence][triggerID]{calls}
|
|
|
|
timeouts map[abi.ChainEpoch]map[triggerID]int
|
|
|
|
|
|
|
|
messageEvents
|
|
|
|
watcherEvents
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:53:57 +00:00
|
|
|
func newHCEvents(api EventAPI, obs *observer) *hcEvents {
|
2021-08-04 00:10:30 +00:00
|
|
|
e := &hcEvents{
|
|
|
|
cs: api,
|
2020-06-24 18:10:52 +00:00
|
|
|
confQueue: map[triggerH]map[msgH][]*queuedEvent{},
|
|
|
|
revertQueue: map[msgH][]triggerH{},
|
|
|
|
triggers: map[triggerID]*handlerInfo{},
|
|
|
|
timeouts: map[abi.ChainEpoch]map[triggerID]int{},
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
e.messageEvents = newMessageEvents(e, api)
|
|
|
|
e.watcherEvents = newWatcherEvents(e, api)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-11 06:53:57 +00:00
|
|
|
// We need to take the lock as the observer could immediately try calling us.
|
|
|
|
e.lk.Lock()
|
|
|
|
e.lastTs = obs.Observe((*hcEventsObserver)(e))
|
|
|
|
e.lk.Unlock()
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
return e
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
type hcEventsObserver hcEvents
|
|
|
|
|
|
|
|
func (e *hcEventsObserver) Apply(ctx context.Context, from, to *types.TipSet) error {
|
2020-06-25 16:12:03 +00:00
|
|
|
e.lk.Lock()
|
|
|
|
defer e.lk.Unlock()
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
defer func() { e.lastTs = to }()
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// Check if the head change caused any state changes that we were
|
|
|
|
// waiting for
|
|
|
|
stateChanges := e.checkStateChanges(from, to)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// Queue up calls until there have been enough blocks to reach
|
|
|
|
// confidence on the state changes
|
|
|
|
for tid, data := range stateChanges {
|
|
|
|
e.queueForConfidence(tid, data, from, to)
|
|
|
|
}
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// Check if the head change included any new message calls
|
|
|
|
newCalls := e.checkNewCalls(ctx, from, to)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// Queue up calls until there have been enough blocks to reach
|
|
|
|
// confidence on the message calls
|
|
|
|
for tid, calls := range newCalls {
|
|
|
|
for _, data := range calls {
|
|
|
|
e.queueForConfidence(tid, data, nil, to)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
for at := from.Height() + 1; at <= to.Height(); at++ {
|
|
|
|
// Apply any queued events and timeouts that were targeted at the
|
|
|
|
// current chain height
|
|
|
|
e.applyWithConfidence(ctx, at)
|
2021-08-26 15:36:06 +00:00
|
|
|
e.applyTimeouts(ctx, at, to)
|
2021-08-04 00:10:30 +00:00
|
|
|
}
|
2020-06-24 18:10:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
func (e *hcEventsObserver) Revert(ctx context.Context, from, to *types.TipSet) error {
|
|
|
|
e.lk.Lock()
|
|
|
|
defer e.lk.Unlock()
|
|
|
|
|
|
|
|
defer func() { e.lastTs = to }()
|
|
|
|
|
|
|
|
reverts, ok := e.revertQueue[from.Height()]
|
2020-06-24 18:10:52 +00:00
|
|
|
if !ok {
|
2021-08-04 00:10:30 +00:00
|
|
|
return nil // nothing to do
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, triggerH := range reverts {
|
2021-08-04 00:10:30 +00:00
|
|
|
toRevert := e.confQueue[triggerH][from.Height()]
|
2020-06-24 18:10:52 +00:00
|
|
|
for _, event := range toRevert {
|
|
|
|
if !event.called {
|
|
|
|
continue // event wasn't apply()-ied yet
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger := e.triggers[event.trigger]
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
if err := trigger.revert(ctx, from); err != nil {
|
|
|
|
log.Errorf("reverting chain trigger (@H %d, triggered @ %d) failed: %s", from.Height(), triggerH, err)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-04 00:10:30 +00:00
|
|
|
delete(e.confQueue[triggerH], from.Height())
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
2021-08-04 00:10:30 +00:00
|
|
|
delete(e.revertQueue, from.Height())
|
|
|
|
return nil
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Queue up events until the chain has reached a height that reflects the
|
|
|
|
// desired confidence
|
2021-08-04 00:10:30 +00:00
|
|
|
func (e *hcEventsObserver) queueForConfidence(trigID uint64, data eventData, prevTs, ts *types.TipSet) {
|
2020-06-24 18:10:52 +00:00
|
|
|
trigger := e.triggers[trigID]
|
|
|
|
|
|
|
|
appliedH := ts.Height()
|
|
|
|
|
|
|
|
triggerH := appliedH + abi.ChainEpoch(trigger.confidence)
|
|
|
|
|
|
|
|
byOrigH, ok := e.confQueue[triggerH]
|
|
|
|
if !ok {
|
|
|
|
byOrigH = map[abi.ChainEpoch][]*queuedEvent{}
|
|
|
|
e.confQueue[triggerH] = byOrigH
|
|
|
|
}
|
|
|
|
|
|
|
|
byOrigH[appliedH] = append(byOrigH[appliedH], &queuedEvent{
|
2021-08-04 00:10:30 +00:00
|
|
|
trigger: trigID,
|
|
|
|
data: data,
|
|
|
|
tipset: ts,
|
|
|
|
prevTipset: prevTs,
|
2020-06-24 18:10:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
e.revertQueue[appliedH] = append(e.revertQueue[appliedH], triggerH)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply any events that were waiting for this chain height for confidence
|
2021-08-04 00:10:30 +00:00
|
|
|
func (e *hcEventsObserver) applyWithConfidence(ctx context.Context, height abi.ChainEpoch) {
|
2020-06-24 18:10:52 +00:00
|
|
|
byOrigH, ok := e.confQueue[height]
|
|
|
|
if !ok {
|
2020-06-25 16:12:03 +00:00
|
|
|
return // no triggers at this height
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for origH, events := range byOrigH {
|
|
|
|
for _, event := range events {
|
|
|
|
if event.called {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger := e.triggers[event.trigger]
|
|
|
|
if trigger.disabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
more, err := trigger.handle(ctx, event.data, event.prevTipset, event.tipset, height)
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
2020-06-25 16:12:03 +00:00
|
|
|
log.Errorf("chain trigger (@H %d, triggered @ %d) failed: %s", origH, height, err)
|
2020-06-24 18:10:52 +00:00
|
|
|
continue // don't revert failed calls
|
|
|
|
}
|
|
|
|
|
|
|
|
event.called = true
|
|
|
|
|
|
|
|
touts, ok := e.timeouts[trigger.timeout]
|
|
|
|
if ok {
|
|
|
|
touts[event.trigger]++
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger.disabled = !more
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply any timeouts that expire at this height
|
2021-08-26 15:36:06 +00:00
|
|
|
func (e *hcEventsObserver) applyTimeouts(ctx context.Context, at abi.ChainEpoch, ts *types.TipSet) {
|
|
|
|
triggers, ok := e.timeouts[at]
|
2020-06-24 18:10:52 +00:00
|
|
|
if !ok {
|
|
|
|
return // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
for triggerID, calls := range triggers {
|
|
|
|
if calls > 0 {
|
|
|
|
continue // don't timeout if the method was called
|
|
|
|
}
|
|
|
|
trigger := e.triggers[triggerID]
|
|
|
|
if trigger.disabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// This should be cached.
|
2021-08-26 15:36:06 +00:00
|
|
|
timeoutTs, err := e.cs.ChainGetTipSetAfterHeight(ctx, at-abi.ChainEpoch(trigger.confidence), ts.Key())
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
2021-08-26 15:36:06 +00:00
|
|
|
log.Errorf("events: applyTimeouts didn't find tipset for event; wanted %d; current %d", at-abi.ChainEpoch(trigger.confidence), at)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 15:36:06 +00:00
|
|
|
more, err := trigger.handle(ctx, nil, nil, timeoutTs, at)
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
2021-08-26 15:36:06 +00:00
|
|
|
log.Errorf("chain trigger (call @H %d, called @ %d) failed: %s", timeoutTs.Height(), at, err)
|
2020-06-24 18:10:52 +00:00
|
|
|
continue // don't revert failed calls
|
|
|
|
}
|
|
|
|
|
|
|
|
trigger.disabled = !more // allows messages after timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen for an event
|
2020-06-26 19:36:48 +00:00
|
|
|
// - CheckFunc: immediately checks if the event already occurred
|
2020-06-24 18:10:52 +00:00
|
|
|
// - EventHandler: called when the event has occurred, after confidence tipsets
|
|
|
|
// - RevertHandler: called if the chain head changes causing the event to revert
|
|
|
|
// - confidence: wait this many tipsets before calling EventHandler
|
|
|
|
// - timeout: at this chain height, timeout on waiting for this event
|
2021-08-04 00:10:30 +00:00
|
|
|
func (e *hcEvents) onHeadChanged(ctx context.Context, check CheckFunc, hnd EventHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch) (triggerID, error) {
|
2020-06-24 18:10:52 +00:00
|
|
|
e.lk.Lock()
|
|
|
|
defer e.lk.Unlock()
|
|
|
|
|
2020-06-26 18:59:23 +00:00
|
|
|
// Check if the event has already occurred
|
2021-08-11 06:53:57 +00:00
|
|
|
done, more, err := check(ctx, e.lastTs)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("called check error (h: %d): %w", e.lastTs.Height(), err)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
if done {
|
|
|
|
timeout = NoTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
id := e.ctr
|
|
|
|
e.ctr++
|
|
|
|
|
|
|
|
e.triggers[id] = &handlerInfo{
|
|
|
|
confidence: confidence,
|
|
|
|
timeout: timeout + abi.ChainEpoch(confidence),
|
|
|
|
|
|
|
|
disabled: !more,
|
|
|
|
|
|
|
|
handle: hnd,
|
|
|
|
revert: rev,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a timeout, set up a timeout check at that height
|
|
|
|
if timeout != NoTimeout {
|
|
|
|
if e.timeouts[timeout+abi.ChainEpoch(confidence)] == nil {
|
|
|
|
e.timeouts[timeout+abi.ChainEpoch(confidence)] = map[uint64]int{}
|
|
|
|
}
|
|
|
|
e.timeouts[timeout+abi.ChainEpoch(confidence)][id] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headChangeAPI is used to allow the composed event APIs to call back to hcEvents
|
|
|
|
// to listen for changes
|
|
|
|
type headChangeAPI interface {
|
2021-08-04 00:10:30 +00:00
|
|
|
onHeadChanged(ctx context.Context, check CheckFunc, hnd EventHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch) (triggerID, error)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// watcherEvents watches for a state change
|
|
|
|
type watcherEvents struct {
|
2021-04-05 11:23:46 +00:00
|
|
|
cs EventAPI
|
2020-06-26 19:36:48 +00:00
|
|
|
hcAPI headChangeAPI
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
lk sync.RWMutex
|
|
|
|
matchers map[triggerID]StateMatchFunc
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
func newWatcherEvents(hcAPI headChangeAPI, cs EventAPI) watcherEvents {
|
2020-06-24 18:10:52 +00:00
|
|
|
return watcherEvents{
|
|
|
|
cs: cs,
|
2020-06-26 19:36:48 +00:00
|
|
|
hcAPI: hcAPI,
|
2020-06-24 18:10:52 +00:00
|
|
|
matchers: make(map[triggerID]StateMatchFunc),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run each of the matchers against the previous and current state to see if
|
|
|
|
// there's a change
|
|
|
|
func (we *watcherEvents) checkStateChanges(oldState, newState *types.TipSet) map[triggerID]eventData {
|
|
|
|
we.lk.RLock()
|
|
|
|
defer we.lk.RUnlock()
|
|
|
|
|
|
|
|
res := make(map[triggerID]eventData)
|
|
|
|
for tid, matchFn := range we.matchers {
|
2020-06-25 16:12:03 +00:00
|
|
|
ok, data, err := matchFn(oldState, newState)
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("event diff fn failed: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
res[tid] = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-06-26 19:36:48 +00:00
|
|
|
// StateChange represents a change in state
|
2020-06-25 21:43:37 +00:00
|
|
|
type StateChange interface{}
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
// StateChangeHandler arguments:
|
2020-06-25 16:12:03 +00:00
|
|
|
// `oldTs` is the state "from" tipset
|
|
|
|
// `newTs` is the state "to" tipset
|
2020-06-25 21:43:37 +00:00
|
|
|
// `states` is the change in state
|
2020-06-24 18:10:52 +00:00
|
|
|
// `curH`-`ts.Height` = `confidence`
|
2020-06-25 21:43:37 +00:00
|
|
|
type StateChangeHandler func(oldTs, newTs *types.TipSet, states StateChange, curH abi.ChainEpoch) (more bool, err error)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
2020-06-25 21:43:37 +00:00
|
|
|
type StateMatchFunc func(oldTs, newTs *types.TipSet) (bool, StateChange, error)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
// StateChanged registers a callback which is triggered when a specified state
|
|
|
|
// change occurs or a timeout is reached.
|
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `CheckFunc` callback is invoked immediately with a recent tipset, it
|
|
|
|
// returns two booleans - `done`, and `more`.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `done` should be true when some on-chain state change we are waiting
|
2020-06-24 18:10:52 +00:00
|
|
|
// for has happened. When `done` is set to true, timeout trigger is disabled.
|
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `more` should be false when we don't want to receive new notifications
|
2020-06-24 18:10:52 +00:00
|
|
|
// through StateChangeHandler. Note that notifications may still be delivered to
|
|
|
|
// RevertHandler
|
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `StateChangeHandler` is called when the specified state change was observed
|
|
|
|
// on-chain, and a confidence threshold was reached, or the specified `timeout`
|
|
|
|
// height was reached with no state change observed. When this callback is
|
|
|
|
// invoked on a timeout, `oldTs` and `states are set to nil.
|
|
|
|
// This callback returns a boolean specifying whether further notifications
|
|
|
|
// should be sent, like `more` return param from `CheckFunc` above.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `RevertHandler` is called after apply handler, when we drop the tipset
|
|
|
|
// containing the message. The tipset passed as the argument is the tipset
|
|
|
|
// that is being dropped. Note that the event dropped may be re-applied
|
|
|
|
// in a different tipset in small amount of time.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `StateMatchFunc` is called against each tipset state. If there is a match,
|
|
|
|
// the state change is queued up until the confidence interval has elapsed (and
|
|
|
|
// `StateChangeHandler` is called)
|
2020-06-24 18:10:52 +00:00
|
|
|
func (we *watcherEvents) StateChanged(check CheckFunc, scHnd StateChangeHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch, mf StateMatchFunc) error {
|
2021-08-04 00:10:30 +00:00
|
|
|
hnd := func(ctx context.Context, data eventData, prevTs, ts *types.TipSet, height abi.ChainEpoch) (bool, error) {
|
2020-06-25 21:43:37 +00:00
|
|
|
states, ok := data.(StateChange)
|
2020-06-25 16:35:14 +00:00
|
|
|
if data != nil && !ok {
|
2020-06-25 21:43:37 +00:00
|
|
|
panic("expected StateChange")
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 16:12:03 +00:00
|
|
|
return scHnd(prevTs, ts, states, height)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
id, err := we.hcAPI.onHeadChanged(context.TODO(), check, hnd, rev, confidence, timeout)
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
we.lk.Lock()
|
|
|
|
defer we.lk.Unlock()
|
2020-06-30 18:24:08 +00:00
|
|
|
we.matchers[id] = mf
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// messageEvents watches for message calls to actors
|
|
|
|
type messageEvents struct {
|
2021-04-05 11:23:46 +00:00
|
|
|
cs EventAPI
|
2020-06-26 19:36:48 +00:00
|
|
|
hcAPI headChangeAPI
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
lk sync.RWMutex
|
2020-10-30 13:00:32 +00:00
|
|
|
matchers map[triggerID]MsgMatchFunc
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
func newMessageEvents(hcAPI headChangeAPI, cs EventAPI) messageEvents {
|
2020-06-24 18:10:52 +00:00
|
|
|
return messageEvents{
|
2021-08-04 00:10:30 +00:00
|
|
|
cs: cs,
|
|
|
|
hcAPI: hcAPI,
|
|
|
|
matchers: make(map[triggerID]MsgMatchFunc),
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there are any new actor calls
|
2021-08-04 00:10:30 +00:00
|
|
|
func (me *messageEvents) checkNewCalls(ctx context.Context, from, to *types.TipSet) map[triggerID][]eventData {
|
2020-06-30 18:24:08 +00:00
|
|
|
me.lk.RLock()
|
|
|
|
defer me.lk.RUnlock()
|
|
|
|
|
2020-10-30 13:00:32 +00:00
|
|
|
// For each message in the tipset
|
2021-05-30 15:20:14 +00:00
|
|
|
res := make(map[triggerID][]eventData)
|
2021-08-04 00:10:30 +00:00
|
|
|
me.messagesForTs(from, func(msg *types.Message) {
|
2020-06-24 18:10:52 +00:00
|
|
|
// TODO: provide receipts
|
|
|
|
|
2020-10-30 13:00:32 +00:00
|
|
|
// Run each trigger's matcher against the message
|
|
|
|
for tid, matchFn := range me.matchers {
|
|
|
|
matched, err := matchFn(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("event matcher failed: %s", err)
|
|
|
|
continue
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 13:00:32 +00:00
|
|
|
// If there was a match, include the message in the results for the
|
|
|
|
// trigger
|
2020-06-24 18:10:52 +00:00
|
|
|
if matched {
|
2021-05-30 15:20:14 +00:00
|
|
|
res[tid] = append(res[tid], msg)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
return res
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the messages in a tipset
|
|
|
|
func (me *messageEvents) messagesForTs(ts *types.TipSet, consume func(*types.Message)) {
|
|
|
|
seen := map[cid.Cid]struct{}{}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
for i, tsb := range ts.Cids() {
|
|
|
|
msgs, err := me.cs.ChainGetBlockMessages(context.TODO(), tsb)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("messagesForTs MessagesForBlock failed (ts.H=%d, Bcid:%s, B.Mcid:%s): %s",
|
|
|
|
ts.Height(), tsb, ts.Blocks()[i].Messages, err)
|
|
|
|
continue
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
2021-08-04 00:10:30 +00:00
|
|
|
for i, c := range msgs.Cids {
|
|
|
|
// We iterate over the CIDs to avoid having to recompute them.
|
|
|
|
_, ok := seen[c]
|
2020-06-24 18:10:52 +00:00
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-04 00:10:30 +00:00
|
|
|
seen[c] = struct{}{}
|
|
|
|
if i < len(msgs.BlsMessages) {
|
|
|
|
consume(msgs.BlsMessages[i])
|
|
|
|
} else {
|
|
|
|
consume(&msgs.SecpkMessages[i-len(msgs.BlsMessages)].Message)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MsgHandler arguments:
|
|
|
|
// `ts` is the tipset, in which the `msg` is included.
|
|
|
|
// `curH`-`ts.Height` = `confidence`
|
|
|
|
type MsgHandler func(msg *types.Message, rec *types.MessageReceipt, ts *types.TipSet, curH abi.ChainEpoch) (more bool, err error)
|
|
|
|
|
2020-10-30 13:00:32 +00:00
|
|
|
type MsgMatchFunc func(msg *types.Message) (matched bool, err error)
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
// Called registers a callback which is triggered when a specified method is
|
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// called on an actor, or a timeout is reached.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `CheckFunc` callback is invoked immediately with a recent tipset, it
|
|
|
|
// returns two booleans - `done`, and `more`.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `done` should be true when some on-chain action we are waiting for has
|
|
|
|
// happened. When `done` is set to true, timeout trigger is disabled.
|
|
|
|
//
|
|
|
|
// - `more` should be false when we don't want to receive new notifications
|
|
|
|
// through MsgHandler. Note that notifications may still be delivered to
|
|
|
|
// RevertHandler
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `MsgHandler` is called when the specified event was observed on-chain,
|
|
|
|
// and a confidence threshold was reached, or the specified `timeout` height
|
|
|
|
// was reached with no events observed. When this callback is invoked on a
|
|
|
|
// timeout, `msg` is set to nil. This callback returns a boolean specifying
|
|
|
|
// whether further notifications should be sent, like `more` return param
|
|
|
|
// from `CheckFunc` above.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `RevertHandler` is called after apply handler, when we drop the tipset
|
|
|
|
// containing the message. The tipset passed as the argument is the tipset
|
|
|
|
// that is being dropped. Note that the message dropped may be re-applied
|
|
|
|
// in a different tipset in small amount of time.
|
2020-06-24 18:10:52 +00:00
|
|
|
//
|
2022-08-29 14:25:30 +00:00
|
|
|
// - `MsgMatchFunc` is called against each message. If there is a match, the
|
|
|
|
// message is queued up until the confidence interval has elapsed (and
|
|
|
|
// `MsgHandler` is called)
|
2021-08-04 00:10:30 +00:00
|
|
|
func (me *messageEvents) Called(ctx context.Context, check CheckFunc, msgHnd MsgHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch, mf MsgMatchFunc) error {
|
|
|
|
hnd := func(ctx context.Context, data eventData, prevTs, ts *types.TipSet, height abi.ChainEpoch) (bool, error) {
|
2020-06-24 18:10:52 +00:00
|
|
|
msg, ok := data.(*types.Message)
|
|
|
|
if data != nil && !ok {
|
|
|
|
panic("expected msg")
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
ml, err := me.cs.StateSearchMsg(ctx, ts.Key(), msg.Cid(), stmgr.LookbackNoLimit, true)
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-04-05 17:56:53 +00:00
|
|
|
if ml == nil {
|
|
|
|
return msgHnd(msg, nil, ts, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgHnd(msg, &ml.Receipt, ts, height)
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
id, err := me.hcAPI.onHeadChanged(ctx, check, hnd, rev, confidence, timeout)
|
2020-06-24 18:10:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
me.lk.Lock()
|
|
|
|
defer me.lk.Unlock()
|
2020-10-30 13:00:32 +00:00
|
|
|
me.matchers[id] = mf
|
2020-06-24 18:10:52 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience function for checking and matching messages
|
|
|
|
func (me *messageEvents) CalledMsg(ctx context.Context, hnd MsgHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch, msg types.ChainMsg) error {
|
2021-08-04 00:10:30 +00:00
|
|
|
return me.Called(ctx, me.CheckMsg(msg, hnd), hnd, rev, confidence, timeout, me.MatchMsg(msg.VMMessage()))
|
2020-06-24 18:10:52 +00:00
|
|
|
}
|