lotus/chain/events/events.go

68 lines
2.2 KiB
Go
Raw Normal View History

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
2019-09-18 11:01:52 +00:00
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
2019-09-04 16:09:08 +00:00
"github.com/filecoin-project/go-address"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/go-state-types/abi"
"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")
// HeightHandler `curH`-`ts.Height` = `confidence`
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 {
ChainNotify(context.Context) (<-chan []*api.HeadChange, error)
2019-09-18 11:01:52 +00:00
ChainGetBlockMessages(context.Context, cid.Cid) (*api.BlockMessages, error)
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)
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)
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
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
*hcEvents
}
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
}
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) {
gcConfidence := 2 * build.ForkLengthThreshold
return NewEventsWithConfidence(ctx, api, gcConfidence)
}