2020-08-10 07:07:44 +00:00
|
|
|
package messagepool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-19 18:46:51 +00:00
|
|
|
"errors"
|
2020-11-04 15:53:00 +00:00
|
|
|
"time"
|
2020-08-10 07:07:44 +00:00
|
|
|
|
2024-03-12 09:44:56 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2020-08-10 07:07:44 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2022-09-21 15:55:59 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2020-08-10 07:07:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
2020-11-04 15:53:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2020-08-10 07:07:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2024-03-12 09:44:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/must"
|
|
|
|
"github.com/filecoin-project/lotus/lib/result"
|
2020-08-10 07:07:44 +00:00
|
|
|
)
|
|
|
|
|
2020-11-09 11:35:42 +00:00
|
|
|
var (
|
|
|
|
HeadChangeCoalesceMinDelay = 2 * time.Second
|
|
|
|
HeadChangeCoalesceMaxDelay = 6 * time.Second
|
|
|
|
HeadChangeCoalesceMergeInterval = time.Second
|
|
|
|
)
|
2020-11-04 15:53:00 +00:00
|
|
|
|
2020-08-10 07:07:44 +00:00
|
|
|
type Provider interface {
|
|
|
|
SubscribeHeadChanges(func(rev, app []*types.TipSet) error) *types.TipSet
|
2021-12-17 09:42:09 +00:00
|
|
|
PutMessage(ctx context.Context, m types.ChainMsg) (cid.Cid, error)
|
2020-08-10 07:07:44 +00:00
|
|
|
PubSubPublish(string, []byte) error
|
2023-04-19 18:46:51 +00:00
|
|
|
GetActorBefore(address.Address, *types.TipSet) (*types.Actor, error)
|
2020-08-25 02:02:06 +00:00
|
|
|
GetActorAfter(address.Address, *types.TipSet) (*types.Actor, error)
|
2023-01-13 19:11:13 +00:00
|
|
|
StateDeterministicAddressAtFinality(context.Context, address.Address, *types.TipSet) (address.Address, error)
|
2022-09-21 15:49:24 +00:00
|
|
|
StateNetworkVersion(context.Context, abi.ChainEpoch) network.Version
|
2021-12-17 09:42:09 +00:00
|
|
|
MessagesForBlock(context.Context, *types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error)
|
|
|
|
MessagesForTipset(context.Context, *types.TipSet) ([]types.ChainMsg, error)
|
2021-12-11 21:03:00 +00:00
|
|
|
LoadTipSet(ctx context.Context, tsk types.TipSetKey) (*types.TipSet, error)
|
2020-08-10 07:07:44 +00:00
|
|
|
ChainComputeBaseFee(ctx context.Context, ts *types.TipSet) (types.BigInt, error)
|
2021-05-07 14:38:40 +00:00
|
|
|
IsLite() bool
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 09:44:56 +00:00
|
|
|
type actorCacheKey struct {
|
|
|
|
types.TipSetKey
|
|
|
|
address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
var nonceCacheSize = 128
|
|
|
|
|
2020-08-10 07:07:44 +00:00
|
|
|
type mpoolProvider struct {
|
|
|
|
sm *stmgr.StateManager
|
|
|
|
ps *pubsub.PubSub
|
2021-05-07 13:20:37 +00:00
|
|
|
|
2024-03-12 09:44:56 +00:00
|
|
|
liteActorCache *lru.Cache[actorCacheKey, result.Result[*types.Actor]]
|
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
lite MpoolNonceAPI
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 03:51:42 +00:00
|
|
|
var _ Provider = (*mpoolProvider)(nil)
|
|
|
|
|
2020-08-10 07:07:44 +00:00
|
|
|
func NewProvider(sm *stmgr.StateManager, ps *pubsub.PubSub) Provider {
|
|
|
|
return &mpoolProvider{sm: sm, ps: ps}
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:56:57 +00:00
|
|
|
func NewProviderLite(sm *stmgr.StateManager, ps *pubsub.PubSub, noncer MpoolNonceAPI) Provider {
|
2024-03-12 09:44:56 +00:00
|
|
|
return &mpoolProvider{
|
|
|
|
sm: sm,
|
|
|
|
ps: ps,
|
|
|
|
lite: noncer,
|
|
|
|
liteActorCache: must.One(lru.New[actorCacheKey, result.Result[*types.Actor]](nonceCacheSize)),
|
|
|
|
}
|
2021-05-07 13:20:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 14:38:40 +00:00
|
|
|
func (mpp *mpoolProvider) IsLite() bool {
|
|
|
|
return mpp.lite != nil
|
|
|
|
}
|
|
|
|
|
2024-03-12 09:44:56 +00:00
|
|
|
func (mpp *mpoolProvider) getActorLite(addr address.Address, ts *types.TipSet) (act *types.Actor, err error) {
|
2023-04-19 18:46:51 +00:00
|
|
|
if !mpp.IsLite() {
|
|
|
|
return nil, errors.New("should not use getActorLite on non lite Provider")
|
|
|
|
}
|
|
|
|
|
2024-03-12 09:44:56 +00:00
|
|
|
if c, ok := mpp.liteActorCache.Get(actorCacheKey{ts.Key(), addr}); ok {
|
|
|
|
return c.Unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
mpp.liteActorCache.Add(actorCacheKey{ts.Key(), addr}, result.Wrap(act, err))
|
|
|
|
}()
|
|
|
|
|
2023-04-19 18:46:51 +00:00
|
|
|
n, err := mpp.lite.GetNonce(context.TODO(), addr, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting nonce over lite: %w", err)
|
|
|
|
}
|
|
|
|
a, err := mpp.lite.GetActor(context.TODO(), addr, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting actor over lite: %w", err)
|
|
|
|
}
|
|
|
|
a.Nonce = n
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 07:07:44 +00:00
|
|
|
func (mpp *mpoolProvider) SubscribeHeadChanges(cb func(rev, app []*types.TipSet) error) *types.TipSet {
|
2020-11-09 11:35:42 +00:00
|
|
|
mpp.sm.ChainStore().SubscribeHeadChanges(
|
|
|
|
store.WrapHeadChangeCoalescer(
|
|
|
|
cb,
|
|
|
|
HeadChangeCoalesceMinDelay,
|
|
|
|
HeadChangeCoalesceMaxDelay,
|
|
|
|
HeadChangeCoalesceMergeInterval,
|
|
|
|
))
|
2020-08-10 07:07:44 +00:00
|
|
|
return mpp.sm.ChainStore().GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
func (mpp *mpoolProvider) PutMessage(ctx context.Context, m types.ChainMsg) (cid.Cid, error) {
|
|
|
|
return mpp.sm.ChainStore().PutMessage(ctx, m)
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) PubSubPublish(k string, v []byte) error {
|
2023-01-13 19:11:13 +00:00
|
|
|
return mpp.ps.Publish(k, v) // nolint
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 18:46:51 +00:00
|
|
|
func (mpp *mpoolProvider) GetActorBefore(addr address.Address, ts *types.TipSet) (*types.Actor, error) {
|
|
|
|
if mpp.IsLite() {
|
|
|
|
return mpp.getActorLite(addr, ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mpp.sm.LoadActor(context.TODO(), addr, ts)
|
|
|
|
}
|
|
|
|
|
2020-08-25 02:02:06 +00:00
|
|
|
func (mpp *mpoolProvider) GetActorAfter(addr address.Address, ts *types.TipSet) (*types.Actor, error) {
|
2021-05-07 14:38:40 +00:00
|
|
|
if mpp.IsLite() {
|
2023-04-19 18:46:51 +00:00
|
|
|
return mpp.getActorLite(addr, ts)
|
2021-05-07 13:20:37 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 02:02:06 +00:00
|
|
|
stcid, _, err := mpp.sm.TipSetState(context.TODO(), ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("computing tipset state for GetActor: %w", err)
|
|
|
|
}
|
2020-09-15 17:57:32 +00:00
|
|
|
st, err := mpp.sm.StateTree(stcid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load state tree: %w", err)
|
|
|
|
}
|
|
|
|
return st.GetActor(addr)
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
func (mpp *mpoolProvider) StateDeterministicAddressAtFinality(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
|
|
|
return mpp.sm.ResolveToDeterministicAddressAtFinality(ctx, addr, ts)
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 15:49:24 +00:00
|
|
|
func (mpp *mpoolProvider) StateNetworkVersion(ctx context.Context, height abi.ChainEpoch) network.Version {
|
|
|
|
return mpp.sm.GetNetworkVersion(ctx, height)
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
func (mpp *mpoolProvider) MessagesForBlock(ctx context.Context, h *types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error) {
|
|
|
|
return mpp.sm.ChainStore().MessagesForBlock(ctx, h)
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
func (mpp *mpoolProvider) MessagesForTipset(ctx context.Context, ts *types.TipSet) ([]types.ChainMsg, error) {
|
|
|
|
return mpp.sm.ChainStore().MessagesForTipset(ctx, ts)
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (mpp *mpoolProvider) LoadTipSet(ctx context.Context, tsk types.TipSetKey) (*types.TipSet, error) {
|
|
|
|
return mpp.sm.ChainStore().LoadTipSet(ctx, tsk)
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) ChainComputeBaseFee(ctx context.Context, ts *types.TipSet) (types.BigInt, error) {
|
|
|
|
baseFee, err := mpp.sm.ChainStore().ComputeBaseFee(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return types.NewInt(0), xerrors.Errorf("computing base fee at %s: %w", ts, err)
|
|
|
|
}
|
|
|
|
return baseFee, nil
|
|
|
|
}
|