2020-08-10 07:07:44 +00:00
|
|
|
package messagepool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Provider interface {
|
|
|
|
SubscribeHeadChanges(func(rev, app []*types.TipSet) error) *types.TipSet
|
|
|
|
PutMessage(m types.ChainMsg) (cid.Cid, error)
|
|
|
|
PubSubPublish(string, []byte) error
|
2020-08-25 02:02:06 +00:00
|
|
|
GetActorAfter(address.Address, *types.TipSet) (*types.Actor, error)
|
2020-08-10 07:07:44 +00:00
|
|
|
StateAccountKey(context.Context, address.Address, *types.TipSet) (address.Address, error)
|
|
|
|
MessagesForBlock(*types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error)
|
|
|
|
MessagesForTipset(*types.TipSet) ([]types.ChainMsg, error)
|
|
|
|
LoadTipSet(tsk types.TipSetKey) (*types.TipSet, error)
|
|
|
|
ChainComputeBaseFee(ctx context.Context, ts *types.TipSet) (types.BigInt, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mpoolProvider struct {
|
|
|
|
sm *stmgr.StateManager
|
|
|
|
ps *pubsub.PubSub
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProvider(sm *stmgr.StateManager, ps *pubsub.PubSub) Provider {
|
|
|
|
return &mpoolProvider{sm: sm, ps: ps}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) SubscribeHeadChanges(cb func(rev, app []*types.TipSet) error) *types.TipSet {
|
|
|
|
mpp.sm.ChainStore().SubscribeHeadChanges(cb)
|
|
|
|
return mpp.sm.ChainStore().GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) PutMessage(m types.ChainMsg) (cid.Cid, error) {
|
|
|
|
return mpp.sm.ChainStore().PutMessage(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) PubSubPublish(k string, v []byte) error {
|
2020-08-20 04:49:10 +00:00
|
|
|
return mpp.ps.Publish(k, v) //nolint
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 02:02:06 +00:00
|
|
|
func (mpp *mpoolProvider) GetActorAfter(addr address.Address, ts *types.TipSet) (*types.Actor, error) {
|
2020-08-10 07:07:44 +00:00
|
|
|
var act types.Actor
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &act, mpp.sm.WithStateTree(stcid, mpp.sm.WithActor(addr, stmgr.GetActor(&act)))
|
2020-08-10 07:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) StateAccountKey(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) {
|
|
|
|
return mpp.sm.ResolveToKeyAddress(ctx, addr, ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) MessagesForBlock(h *types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error) {
|
|
|
|
return mpp.sm.ChainStore().MessagesForBlock(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) MessagesForTipset(ts *types.TipSet) ([]types.ChainMsg, error) {
|
|
|
|
return mpp.sm.ChainStore().MessagesForTipset(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpp *mpoolProvider) LoadTipSet(tsk types.TipSetKey) (*types.TipSet, error) {
|
|
|
|
return mpp.sm.ChainStore().LoadTipSet(tsk)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|