2019-09-05 07:40:50 +00:00
|
|
|
package events
|
2019-09-03 17:45:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-18 11:01:52 +00:00
|
|
|
"context"
|
2019-09-03 17:45:55 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2019-09-18 11:01:52 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-09-04 16:09:08 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-03 17:45:55 +00:00
|
|
|
)
|
|
|
|
|
2019-09-05 07:40:50 +00:00
|
|
|
var log = logging.Logger("events")
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
// HeightHandler `curH`-`ts.Height` = `confidence`
|
2020-12-09 11:25:20 +00:00
|
|
|
type (
|
|
|
|
HeightHandler func(ctx context.Context, ts *types.TipSet, curH abi.ChainEpoch) error
|
|
|
|
RevertHandler func(ctx context.Context, ts *types.TipSet) error
|
|
|
|
)
|
2019-09-03 17:45:55 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
// A TipSetObserver receives notifications of tipsets
|
|
|
|
type TipSetObserver interface {
|
|
|
|
Apply(ctx context.Context, from, to *types.TipSet) error
|
|
|
|
Revert(ctx context.Context, from, to *types.TipSet) error
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 11:23:46 +00:00
|
|
|
type EventAPI interface {
|
2020-04-23 22:15:00 +00:00
|
|
|
ChainNotify(context.Context) (<-chan []*api.HeadChange, error)
|
2019-09-18 11:01:52 +00:00
|
|
|
ChainGetBlockMessages(context.Context, cid.Cid) (*api.BlockMessages, error)
|
2020-02-24 17:32:02 +00:00
|
|
|
ChainGetTipSetByHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error)
|
2021-08-04 00:10:30 +00:00
|
|
|
ChainGetTipSetAfterHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error)
|
2020-09-07 12:43:06 +00:00
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
2021-04-05 17:56:53 +00:00
|
|
|
StateSearchMsg(ctx context.Context, from types.TipSetKey, msg cid.Cid, limit abi.ChainEpoch, allowReplaced bool) (*api.MsgLookup, error)
|
2020-05-13 22:48:36 +00:00
|
|
|
ChainGetTipSet(context.Context, types.TipSetKey) (*types.TipSet, error)
|
2021-08-04 00:10:30 +00:00
|
|
|
ChainGetPath(ctx context.Context, from, to types.TipSetKey) ([]*api.HeadChange, error)
|
2019-11-19 21:27:25 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
StateGetActor(ctx context.Context, actor address.Address, tsk types.TipSetKey) (*types.Actor, error) // optional / for CalledMsg
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Events struct {
|
2021-08-04 00:10:30 +00:00
|
|
|
*observer
|
|
|
|
*heightEvents
|
2020-06-26 19:42:44 +00:00
|
|
|
*hcEvents
|
2021-03-03 21:12:53 +00:00
|
|
|
}
|
2019-09-03 17:45:55 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
func NewEventsWithConfidence(ctx context.Context, api EventAPI, gcConfidence abi.ChainEpoch) (*Events, error) {
|
|
|
|
cache := newCache(api, gcConfidence)
|
2019-09-04 16:09:08 +00:00
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
ob := newObserver(cache, gcConfidence)
|
|
|
|
if err := ob.start(ctx); err != nil {
|
|
|
|
return nil, err
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:53:57 +00:00
|
|
|
he := newHeightEvents(cache, ob, gcConfidence)
|
|
|
|
headChange := newHCEvents(cache, ob)
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
return &Events{ob, he, headChange}, nil
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:10:30 +00:00
|
|
|
func NewEvents(ctx context.Context, api EventAPI) (*Events, error) {
|
2021-03-03 21:12:53 +00:00
|
|
|
gcConfidence := 2 * build.ForkLengthThreshold
|
|
|
|
return NewEventsWithConfidence(ctx, api, gcConfidence)
|
|
|
|
}
|