2023-01-13 19:11:13 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-19 22:44:58 +00:00
|
|
|
"path/filepath"
|
2023-01-13 19:11:13 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/fx"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
2024-03-01 06:23:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2023-01-13 19:11:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/events"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events/filter"
|
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2023-01-19 22:44:58 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2023-01-13 19:11:13 +00:00
|
|
|
)
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
type EventHelperAPI struct {
|
2023-01-13 19:11:13 +00:00
|
|
|
fx.In
|
|
|
|
|
|
|
|
full.ChainAPI
|
|
|
|
full.StateAPI
|
|
|
|
}
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
var _ events.EventHelperAPI = &EventHelperAPI{}
|
2023-01-13 19:11:13 +00:00
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
func EthEventHandler(cfg config.FevmConfig) func(helpers.MetricsCtx, repo.LockedRepo, fx.Lifecycle, *filter.EventFilterManager, *store.ChainStore, *stmgr.StateManager, EventHelperAPI, *messagepool.MessagePool, full.StateAPI, full.ChainAPI) (*full.EthEventHandler, error) {
|
|
|
|
return func(mctx helpers.MetricsCtx, r repo.LockedRepo, lc fx.Lifecycle, fm *filter.EventFilterManager, cs *store.ChainStore, sm *stmgr.StateManager, evapi EventHelperAPI, mp *messagepool.MessagePool, stateapi full.StateAPI, chainapi full.ChainAPI) (*full.EthEventHandler, error) {
|
2023-01-13 19:11:13 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
ee := &full.EthEventHandler{
|
2023-01-13 19:11:13 +00:00
|
|
|
Chain: cs,
|
2023-01-19 22:44:58 +00:00
|
|
|
MaxFilterHeightRange: abi.ChainEpoch(cfg.Events.MaxFilterHeightRange),
|
2023-01-26 14:20:49 +00:00
|
|
|
SubscribtionCtx: ctx,
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 22:44:58 +00:00
|
|
|
if !cfg.EnableEthRPC || cfg.Events.DisableRealTimeFilterAPI {
|
2023-01-13 19:11:13 +00:00
|
|
|
// all event functionality is disabled
|
|
|
|
// the historic filter API relies on the real time one
|
|
|
|
return ee, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ee.SubManager = &full.EthSubscriptionManager{
|
|
|
|
Chain: cs,
|
|
|
|
StateAPI: stateapi,
|
|
|
|
ChainAPI: chainapi,
|
|
|
|
}
|
2023-01-19 22:44:58 +00:00
|
|
|
ee.FilterStore = filter.NewMemFilterStore(cfg.Events.MaxFilters)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
|
|
|
// Start garbage collection for filters
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(context.Context) error {
|
2023-01-19 22:44:58 +00:00
|
|
|
go ee.GC(ctx, time.Duration(cfg.Events.FilterTTL))
|
2023-01-13 19:11:13 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-12-19 11:07:08 +00:00
|
|
|
ee.TipSetFilterManager = &filter.TipSetFilterManager{
|
|
|
|
MaxFilterResults: cfg.Events.MaxFilterResults,
|
|
|
|
}
|
|
|
|
ee.MemPoolFilterManager = &filter.MemPoolFilterManager{
|
|
|
|
MaxFilterResults: cfg.Events.MaxFilterResults,
|
|
|
|
}
|
|
|
|
ee.EventFilterManager = fm
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(context.Context) error {
|
|
|
|
ev, err := events.NewEvents(ctx, &evapi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ignore returned tipsets
|
|
|
|
_ = ev.Observe(ee.TipSetFilterManager)
|
|
|
|
|
|
|
|
ch, err := mp.Updates(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go ee.MemPoolFilterManager.WaitForMpoolUpdates(ctx, ch)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return ee, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
func EventFilterManager(cfg config.FevmConfig) func(helpers.MetricsCtx, repo.LockedRepo, fx.Lifecycle, *store.ChainStore, *stmgr.StateManager, EventHelperAPI, full.ChainAPI) (*filter.EventFilterManager, error) {
|
|
|
|
return func(mctx helpers.MetricsCtx, r repo.LockedRepo, lc fx.Lifecycle, cs *store.ChainStore, sm *stmgr.StateManager, evapi EventHelperAPI, chainapi full.ChainAPI) (*filter.EventFilterManager, error) {
|
2023-12-19 11:07:08 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
// Enable indexing of actor events
|
|
|
|
var eventIndex *filter.EventIndex
|
2023-01-19 22:44:58 +00:00
|
|
|
if !cfg.Events.DisableHistoricFilterAPI {
|
|
|
|
var dbPath string
|
|
|
|
if cfg.Events.DatabasePath == "" {
|
|
|
|
sqlitePath, err := r.SqlitePath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dbPath = filepath.Join(sqlitePath, "events.db")
|
|
|
|
} else {
|
|
|
|
dbPath = cfg.Events.DatabasePath
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
var err error
|
2023-06-28 16:07:03 +00:00
|
|
|
eventIndex, err = filter.NewEventIndex(ctx, dbPath, chainapi.Chain)
|
2023-01-13 19:11:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(context.Context) error {
|
|
|
|
return eventIndex.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-19 11:07:08 +00:00
|
|
|
fm := &filter.EventFilterManager{
|
2023-01-13 19:11:13 +00:00
|
|
|
ChainStore: cs,
|
|
|
|
EventIndex: eventIndex, // will be nil unless EnableHistoricFilterAPI is true
|
2024-02-07 08:17:46 +00:00
|
|
|
// TODO:
|
|
|
|
// We don't need this address resolution anymore once https://github.com/filecoin-project/lotus/issues/11594 lands
|
2023-01-13 19:11:13 +00:00
|
|
|
AddressResolver: func(ctx context.Context, emitter abi.ActorID, ts *types.TipSet) (address.Address, bool) {
|
|
|
|
idAddr, err := address.NewIDAddress(uint64(emitter))
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, false
|
|
|
|
}
|
|
|
|
|
|
|
|
actor, err := sm.LoadActor(ctx, idAddr, ts)
|
|
|
|
if err != nil || actor.Address == nil {
|
2024-02-07 08:17:46 +00:00
|
|
|
return idAddr, true
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *actor.Address, true
|
|
|
|
},
|
|
|
|
|
2023-01-19 22:44:58 +00:00
|
|
|
MaxFilterResults: cfg.Events.MaxFilterResults,
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(context.Context) error {
|
2023-04-20 21:44:16 +00:00
|
|
|
ev, err := events.NewEvents(ctx, &evapi)
|
2023-01-13 19:11:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-19 11:07:08 +00:00
|
|
|
_ = ev.Observe(fm)
|
2023-01-13 19:11:13 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-12-19 11:07:08 +00:00
|
|
|
return fm, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-22 08:16:06 +00:00
|
|
|
func ActorEventHandler(enable bool, fevmCfg config.FevmConfig) func(helpers.MetricsCtx, repo.LockedRepo, fx.Lifecycle, *filter.EventFilterManager, *store.ChainStore, *stmgr.StateManager, EventHelperAPI, *messagepool.MessagePool, full.StateAPI, full.ChainAPI) (*full.ActorEventHandler, error) {
|
2024-02-19 04:54:38 +00:00
|
|
|
return func(mctx helpers.MetricsCtx, r repo.LockedRepo, lc fx.Lifecycle, fm *filter.EventFilterManager, cs *store.ChainStore, sm *stmgr.StateManager, evapi EventHelperAPI, mp *messagepool.MessagePool, stateapi full.StateAPI, chainapi full.ChainAPI) (*full.ActorEventHandler, error) {
|
2023-12-19 11:07:08 +00:00
|
|
|
|
2024-02-22 08:16:06 +00:00
|
|
|
if !enable || fevmCfg.Events.DisableRealTimeFilterAPI {
|
2024-03-01 06:23:04 +00:00
|
|
|
fm = nil
|
2023-12-19 11:07:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 06:23:04 +00:00
|
|
|
return full.NewActorEventHandler(
|
|
|
|
cs,
|
|
|
|
fm,
|
|
|
|
time.Duration(build.BlockDelaySecs)*time.Second,
|
|
|
|
abi.ChainEpoch(fevmCfg.Events.MaxFilterHeightRange),
|
|
|
|
), nil
|
2023-01-13 19:11:13 +00:00
|
|
|
}
|
|
|
|
}
|