2021-08-04 00:10:30 +00:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2023-11-21 14:02:09 +00:00
|
|
|
"github.com/hashicorp/golang-lru/arc/v2"
|
2021-08-04 00:10:30 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-08-04 00:10:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type messageCache struct {
|
|
|
|
api EventAPI
|
|
|
|
|
|
|
|
blockMsgLk sync.Mutex
|
2023-11-21 14:02:09 +00:00
|
|
|
blockMsgCache *arc.ARCCache[cid.Cid, *api.BlockMessages]
|
2021-08-04 00:10:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 22:29:09 +00:00
|
|
|
func newMessageCache(a EventAPI) *messageCache {
|
2023-11-21 14:02:09 +00:00
|
|
|
blsMsgCache, _ := arc.NewARC[cid.Cid, *api.BlockMessages](500)
|
2021-08-04 00:10:30 +00:00
|
|
|
|
|
|
|
return &messageCache{
|
2023-03-13 22:29:09 +00:00
|
|
|
api: a,
|
2021-08-04 00:10:30 +00:00
|
|
|
blockMsgCache: blsMsgCache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *messageCache) ChainGetBlockMessages(ctx context.Context, blkCid cid.Cid) (*api.BlockMessages, error) {
|
|
|
|
c.blockMsgLk.Lock()
|
|
|
|
defer c.blockMsgLk.Unlock()
|
|
|
|
|
2023-03-13 22:29:09 +00:00
|
|
|
msgs, ok := c.blockMsgCache.Get(blkCid)
|
2021-08-04 00:10:30 +00:00
|
|
|
var err error
|
|
|
|
if !ok {
|
2023-03-13 22:29:09 +00:00
|
|
|
msgs, err = c.api.ChainGetBlockMessages(ctx, blkCid)
|
2021-08-04 00:10:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-13 22:29:09 +00:00
|
|
|
c.blockMsgCache.Add(blkCid, msgs)
|
2021-08-04 00:10:30 +00:00
|
|
|
}
|
2023-03-13 22:29:09 +00:00
|
|
|
return msgs, nil
|
2021-08-04 00:10:30 +00:00
|
|
|
}
|