2023-12-19 11:07:08 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-02-07 08:17:46 +00:00
|
|
|
"strings"
|
2023-12-19 11:07:08 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events/filter"
|
2024-02-07 08:17:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2023-12-19 11:07:08 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ActorEventAPI interface {
|
|
|
|
GetActorEvents(ctx context.Context, filter *types.ActorEventFilter) ([]*types.ActorEvent, error)
|
|
|
|
SubscribeActorEvents(ctx context.Context, filter *types.SubActorEventFilter) (<-chan *types.ActorEvent, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ ActorEventAPI = *new(api.FullNode)
|
|
|
|
_ ActorEventAPI = *new(api.Gateway)
|
|
|
|
)
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
type ActorEventHandler struct {
|
2023-12-19 11:07:08 +00:00
|
|
|
EventFilterManager *filter.EventFilterManager
|
|
|
|
MaxFilterHeightRange abi.ChainEpoch
|
2024-02-07 08:17:46 +00:00
|
|
|
Chain *store.ChainStore
|
2023-12-19 11:07:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
var _ ActorEventAPI = (*ActorEventHandler)(nil)
|
2023-12-19 11:07:08 +00:00
|
|
|
|
|
|
|
type ActorEventsAPI struct {
|
|
|
|
fx.In
|
|
|
|
ActorEventAPI
|
|
|
|
}
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
func (a *ActorEventHandler) GetActorEvents(ctx context.Context, filter *types.ActorEventFilter) ([]*types.ActorEvent, error) {
|
2023-12-19 11:07:08 +00:00
|
|
|
if a.EventFilterManager == nil {
|
|
|
|
return nil, api.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:17:46 +00:00
|
|
|
params, err := a.parseFilter(filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-19 11:07:08 +00:00
|
|
|
// Create a temporary filter
|
2024-02-07 08:17:46 +00:00
|
|
|
f, err := a.EventFilterManager.Install(ctx, params.MinHeight, params.MaxHeight, params.TipSetCid, filter.Addresses, filter.Fields, false)
|
2023-12-19 11:07:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
evs, err := getCollected(ctx, f)
|
|
|
|
_ = a.EventFilterManager.Remove(ctx, f.ID())
|
|
|
|
return evs, err
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:17:46 +00:00
|
|
|
type filterParams struct {
|
|
|
|
MinHeight abi.ChainEpoch
|
|
|
|
MaxHeight abi.ChainEpoch
|
|
|
|
TipSetCid cid.Cid
|
|
|
|
}
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
func (a *ActorEventHandler) parseFilter(f *types.ActorEventFilter) (*filterParams, error) {
|
2024-02-07 08:17:46 +00:00
|
|
|
if f.TipSetCid != nil {
|
|
|
|
if len(f.FromEpoch) != 0 || len(f.ToEpoch) != 0 {
|
|
|
|
return nil, fmt.Errorf("cannot specify both TipSetCid and FromEpoch/ToEpoch")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filterParams{
|
|
|
|
MinHeight: 0,
|
|
|
|
MaxHeight: 0,
|
|
|
|
TipSetCid: *f.TipSetCid,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
from := f.FromEpoch
|
|
|
|
if len(from) != 0 && from != "latest" && from != "earliest" && !strings.HasPrefix(from, "0x") {
|
|
|
|
from = "0x" + from
|
|
|
|
}
|
|
|
|
|
|
|
|
to := f.ToEpoch
|
|
|
|
if len(to) != 0 && to != "latest" && to != "earliest" && !strings.HasPrefix(to, "0x") {
|
|
|
|
to = "0x" + to
|
|
|
|
}
|
|
|
|
|
|
|
|
min, max, err := parseBlockRange(a.Chain.GetHeaviestTipSet().Height(), &from, &to, a.MaxFilterHeightRange)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filterParams{
|
|
|
|
MinHeight: min,
|
|
|
|
MaxHeight: max,
|
|
|
|
TipSetCid: cid.Undef,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-19 04:54:38 +00:00
|
|
|
func (a *ActorEventHandler) SubscribeActorEvents(ctx context.Context, f *types.SubActorEventFilter) (<-chan *types.ActorEvent, error) {
|
2023-12-19 11:07:08 +00:00
|
|
|
if a.EventFilterManager == nil {
|
|
|
|
return nil, api.ErrNotSupported
|
|
|
|
}
|
2024-02-07 08:17:46 +00:00
|
|
|
|
|
|
|
params, err := a.parseFilter(&f.Filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fm, err := a.EventFilterManager.Install(ctx, params.MinHeight, params.MaxHeight, params.TipSetCid, f.Filter.Addresses, f.Filter.Fields, false)
|
2023-12-19 11:07:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(chan *types.ActorEvent, 25)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
// Tell the caller we're done
|
|
|
|
close(out)
|
|
|
|
|
|
|
|
// Unsubscribe.
|
|
|
|
fm.ClearSubChannel()
|
|
|
|
_ = a.EventFilterManager.Remove(ctx, fm.ID())
|
|
|
|
}()
|
|
|
|
|
|
|
|
if f.Prefill {
|
|
|
|
evs, err := getCollected(ctx, fm)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get collected events: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range evs {
|
|
|
|
ev := ev
|
|
|
|
select {
|
|
|
|
case out <- ev:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
log.Errorf("closing event subscription due to slow reader")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in := make(chan interface{}, 256)
|
|
|
|
fm.SetSubChannel(in)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case val, ok := <-in:
|
|
|
|
if !ok {
|
|
|
|
// Shutting down.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ce, ok := val.(*filter.CollectedEvent)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("got unexpected value from event filter: %T", val)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c, err := ce.TipSetKey.Cid()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get tipset cid: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := &types.ActorEvent{
|
|
|
|
Entries: ce.Entries,
|
|
|
|
EmitterAddr: ce.EmitterAddr,
|
|
|
|
Reverted: ce.Reverted,
|
|
|
|
Height: ce.Height,
|
2024-02-08 06:05:55 +00:00
|
|
|
TipSetCid: c,
|
2023-12-19 11:07:08 +00:00
|
|
|
MsgCid: ce.MsgCid,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case out <- ev:
|
|
|
|
default:
|
|
|
|
log.Errorf("closing event subscription due to slow reader")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(out) > 5 {
|
|
|
|
log.Warnf("event subscription is slow, has %d buffered entries", len(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCollected(ctx context.Context, f *filter.EventFilter) ([]*types.ActorEvent, error) {
|
|
|
|
ces := f.TakeCollectedEvents(ctx)
|
|
|
|
|
|
|
|
var out []*types.ActorEvent
|
|
|
|
|
|
|
|
for _, e := range ces {
|
|
|
|
e := e
|
|
|
|
c, err := e.TipSetKey.Cid()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get tipset cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ev := &types.ActorEvent{
|
|
|
|
Entries: e.Entries,
|
|
|
|
EmitterAddr: e.EmitterAddr,
|
|
|
|
Reverted: e.Reverted,
|
|
|
|
Height: e.Height,
|
2024-02-08 06:05:55 +00:00
|
|
|
TipSetCid: c,
|
2023-12-19 11:07:08 +00:00
|
|
|
MsgCid: e.MsgCid,
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, ev)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|