Initialise event index in di

This commit is contained in:
Ian Davis 2022-11-15 14:23:23 +00:00
parent 0d9c474a59
commit 32839f6919
4 changed files with 86 additions and 57 deletions

View File

@ -306,6 +306,12 @@ func (m *EventFilterManager) Apply(ctx context.Context, from, to *types.TipSet)
load: m.loadExecutedMessages, load: m.loadExecutedMessages,
} }
if m.EventIndex != nil {
if err := m.EventIndex.CollectEvents(ctx, tse, false, m.AddressResolver); err != nil {
return err
}
}
// TODO: could run this loop in parallel with errgroup if there are many filters // TODO: could run this loop in parallel with errgroup if there are many filters
for _, f := range m.filters { for _, f := range m.filters {
if err := f.CollectEvents(ctx, tse, false, m.AddressResolver); err != nil { if err := f.CollectEvents(ctx, tse, false, m.AddressResolver); err != nil {
@ -331,6 +337,12 @@ func (m *EventFilterManager) Revert(ctx context.Context, from, to *types.TipSet)
load: m.loadExecutedMessages, load: m.loadExecutedMessages,
} }
if m.EventIndex != nil {
if err := m.EventIndex.CollectEvents(ctx, tse, true, m.AddressResolver); err != nil {
return err
}
}
// TODO: could run this loop in parallel with errgroup if there are many filters // TODO: could run this loop in parallel with errgroup if there are many filters
for _, f := range m.filters { for _, f := range m.filters {
if err := f.CollectEvents(ctx, tse, true, m.AddressResolver); err != nil { if err := f.CollectEvents(ctx, tse, true, m.AddressResolver); err != nil {
@ -346,7 +358,7 @@ func (m *EventFilterManager) Install(ctx context.Context, minHeight, maxHeight a
currentHeight := m.currentHeight currentHeight := m.currentHeight
m.mu.Unlock() m.mu.Unlock()
if m.EventIndex == nil && (minHeight < currentHeight || maxHeight < currentHeight) { if m.EventIndex == nil && minHeight < currentHeight {
return nil, xerrors.Errorf("historic event index disabled") return nil, xerrors.Errorf("historic event index disabled")
} }
@ -365,7 +377,8 @@ func (m *EventFilterManager) Install(ctx context.Context, minHeight, maxHeight a
maxResults: m.MaxFilterResults, maxResults: m.MaxFilterResults,
} }
if m.EventIndex != nil && (minHeight < currentHeight || maxHeight < currentHeight) { if m.EventIndex != nil && minHeight < currentHeight {
// Filter needs historic events
if err := m.EventIndex.PrefillFilter(ctx, f); err != nil { if err := m.EventIndex.PrefillFilter(ctx, f); err != nil {
return nil, err return nil, err
} }

View File

@ -50,7 +50,7 @@ var ddls = []string{
value BLOB NOT NULL value BLOB NOT NULL
)`, )`,
// placeholder version to enable migrations. // metadata containing version of schema
`CREATE TABLE IF NOT EXISTS _meta ( `CREATE TABLE IF NOT EXISTS _meta (
version UINT64 NOT NULL UNIQUE version UINT64 NOT NULL UNIQUE
)`, )`,

View File

@ -624,6 +624,11 @@ type ActorEventConfig struct {
// the entire chain) // the entire chain)
MaxFilterHeightRange uint64 MaxFilterHeightRange uint64
// EventHistoryDatabasePath is the full path to a sqlite database that will be used to index actor events to
// support the historic filter APIs. If the database does not exist it will be created. The directory containing
// the database must already exist and be writeable.
ActorEventDatabasePath string
// Others, not implemented yet: // Others, not implemented yet:
// Set a limit on the number of active websocket subscriptions (may be zero) // Set a limit on the number of active websocket subscriptions (may be zero)
// Set a timeout for subscription clients // Set a timeout for subscription clients

View File

@ -38,8 +38,9 @@ func EthEventAPI(cfg config.ActorEventConfig) func(helpers.MetricsCtx, fx.Lifecy
MaxFilterHeightRange: abi.ChainEpoch(cfg.MaxFilterHeightRange), MaxFilterHeightRange: abi.ChainEpoch(cfg.MaxFilterHeightRange),
} }
if !cfg.EnableRealTimeFilterAPI && !cfg.EnableHistoricFilterAPI { if !cfg.EnableRealTimeFilterAPI {
// all event functionality is disabled // all event functionality is disabled
// the historic filter API relies on the real time one
return ee, nil return ee, nil
} }
@ -53,9 +54,25 @@ func EthEventAPI(cfg config.ActorEventConfig) func(helpers.MetricsCtx, fx.Lifecy
}, },
}) })
if cfg.EnableRealTimeFilterAPI { // Enable indexing of actor events
var eventIndex *filter.EventIndex
if cfg.EnableHistoricFilterAPI {
var err error
eventIndex, err = filter.NewEventIndex(cfg.ActorEventDatabasePath)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return eventIndex.Close()
},
})
}
ee.EventFilterManager = &filter.EventFilterManager{ ee.EventFilterManager = &filter.EventFilterManager{
ChainStore: cs, ChainStore: cs,
EventIndex: eventIndex, // will be nil unless EnableHistoricFilterAPI is true
AddressResolver: func(ctx context.Context, emitter abi.ActorID, ts *types.TipSet) (address.Address, bool) { AddressResolver: func(ctx context.Context, emitter abi.ActorID, ts *types.TipSet) (address.Address, bool) {
// we only want to match using f4 addresses // we only want to match using f4 addresses
idAddr, err := address.NewIDAddress(uint64(emitter)) idAddr, err := address.NewIDAddress(uint64(emitter))
@ -109,12 +126,6 @@ func EthEventAPI(cfg config.ActorEventConfig) func(helpers.MetricsCtx, fx.Lifecy
}, },
}) })
}
if cfg.EnableHistoricFilterAPI {
// TODO: enable indexer
}
return ee, nil return ee, nil
} }
} }