9
9
10
10
"github.com/ipfs/go-cid"
11
11
cbg "github.com/whyrusleeping/cbor-gen"
12
+ "golang.org/x/sync/errgroup"
12
13
"golang.org/x/xerrors"
13
14
14
15
"github.com/filecoin-project/go-address"
@@ -28,7 +29,10 @@ func isIndexedValue(b uint8) bool {
28
29
return b & (types .EventFlagIndexedKey | types .EventFlagIndexedValue ) > 0
29
30
}
30
31
31
- type AddressResolver func (context.Context , abi.ActorID , * types.TipSet ) (address.Address , bool )
32
+ // AddressResolver is a function that resolves an actor ID to an address. If the
33
+ // actor ID cannot be resolved to an address, the function should return
34
+ // address.Undef.
35
+ type AddressResolver func (context.Context , abi.ActorID , * types.TipSet ) address.Address
32
36
33
37
type EventFilter interface {
34
38
Filter
@@ -77,9 +81,6 @@ func (f *eventFilter) CollectEvents(ctx context.Context, te *TipSetEvents, rever
77
81
return nil
78
82
}
79
83
80
- // cache of lookups between actor id and f4 address
81
- addressLookups := make (map [abi.ActorID ]address.Address )
82
-
83
84
ems , err := te .messages (ctx )
84
85
if err != nil {
85
86
return xerrors .Errorf ("load executed messages: %w" , err )
@@ -89,16 +90,10 @@ func (f *eventFilter) CollectEvents(ctx context.Context, te *TipSetEvents, rever
89
90
90
91
for msgIdx , em := range ems {
91
92
for _ , ev := range em .Events () {
92
- // lookup address corresponding to the actor id
93
- addr , found := addressLookups [ev .Emitter ]
94
- if ! found {
95
- var ok bool
96
- addr , ok = resolver (ctx , ev .Emitter , te .rctTs )
97
- if ! ok {
98
- // not an address we will be able to match against
99
- continue
100
- }
101
- addressLookups [ev .Emitter ] = addr
93
+ addr := resolver (ctx , ev .Emitter , te .rctTs )
94
+ if addr == address .Undef {
95
+ // not an address we will be able to match against
96
+ continue
102
97
}
103
98
104
99
if ! f .matchAddress (addr ) {
@@ -295,7 +290,7 @@ func (e *executedMessage) Events() []*types.Event {
295
290
296
291
type EventFilterManager struct {
297
292
ChainStore * cstore.ChainStore
298
- AddressResolver func ( ctx context. Context , emitter abi. ActorID , ts * types. TipSet ) (address. Address , bool )
293
+ AddressResolver AddressResolver
299
294
MaxFilterResults int
300
295
ChainIndexer index.Indexer
301
296
@@ -319,11 +314,18 @@ func (m *EventFilterManager) Apply(ctx context.Context, from, to *types.TipSet)
319
314
load : m .loadExecutedMessages ,
320
315
}
321
316
322
- // TODO: could run this loop in parallel with errgroup if there are many filters
317
+ tsAddressResolver := m .createTipSetCachedAddressResolver ()
318
+
319
+ g , ctx := errgroup .WithContext (ctx )
323
320
for _ , f := range m .filters {
324
- if err := f .CollectEvents (ctx , tse , false , m .AddressResolver ); err != nil {
325
- return err
326
- }
321
+ f := f
322
+ g .Go (func () error {
323
+ return f .CollectEvents (ctx , tse , false , tsAddressResolver )
324
+ })
325
+ }
326
+
327
+ if err := g .Wait (); err != nil {
328
+ return err
327
329
}
328
330
329
331
return nil
@@ -344,16 +346,41 @@ func (m *EventFilterManager) Revert(ctx context.Context, from, to *types.TipSet)
344
346
load : m .loadExecutedMessages ,
345
347
}
346
348
347
- // TODO: could run this loop in parallel with errgroup if there are many filters
349
+ tsAddressResolver := m .createTipSetCachedAddressResolver ()
350
+
351
+ g , ctx := errgroup .WithContext (ctx )
348
352
for _ , f := range m .filters {
349
- if err := f .CollectEvents (ctx , tse , true , m .AddressResolver ); err != nil {
350
- return err
351
- }
353
+ f := f
354
+ g .Go (func () error {
355
+ return f .CollectEvents (ctx , tse , true , tsAddressResolver )
356
+ })
357
+ }
358
+
359
+ if err := g .Wait (); err != nil {
360
+ return err
352
361
}
353
362
354
363
return nil
355
364
}
356
365
366
+ func (m * EventFilterManager ) createTipSetCachedAddressResolver () func (ctx context.Context , emitter abi.ActorID , ts * types.TipSet ) address.Address {
367
+ addressLookups := make (map [abi.ActorID ]address.Address )
368
+ var addressLookupsLk sync.Mutex
369
+
370
+ return func (ctx context.Context , emitter abi.ActorID , ts * types.TipSet ) address.Address {
371
+ addressLookupsLk .Lock ()
372
+ defer addressLookupsLk .Unlock ()
373
+
374
+ addr , ok := addressLookups [emitter ]
375
+ if ! ok {
376
+ addr = m .AddressResolver (ctx , emitter , ts )
377
+ addressLookups [emitter ] = addr
378
+ }
379
+
380
+ return addr
381
+ }
382
+ }
383
+
357
384
func (m * EventFilterManager ) Fill (
358
385
ctx context.Context ,
359
386
minHeight ,
0 commit comments