2019-08-20 16:48:33 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-02 18:00:08 +00:00
|
|
|
|
2019-12-03 19:33:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2019-08-20 16:48:33 +00:00
|
|
|
"go.uber.org/fx"
|
2019-09-16 14:17:08 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-11-17 07:44:06 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-12-01 23:11:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
2019-12-03 19:33:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-11 17:32:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2019-08-20 16:48:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MpoolAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2019-09-16 14:17:08 +00:00
|
|
|
WalletAPI
|
2020-07-20 17:48:30 +00:00
|
|
|
GasAPI
|
2019-09-16 14:17:08 +00:00
|
|
|
|
2019-12-03 19:33:29 +00:00
|
|
|
Chain *store.ChainStore
|
|
|
|
|
2019-12-01 23:11:43 +00:00
|
|
|
Mpool *messagepool.MessagePool
|
2020-08-08 16:56:33 +00:00
|
|
|
|
2020-08-11 17:32:31 +00:00
|
|
|
PushLocks *dtypes.MpoolLocker
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 14:45:31 +00:00
|
|
|
func (a *MpoolAPI) MpoolGetConfig(context.Context) (*types.MpoolConfig, error) {
|
|
|
|
return a.Mpool.GetConfig(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *MpoolAPI) MpoolSetConfig(ctx context.Context, cfg *types.MpoolConfig) error {
|
|
|
|
a.Mpool.SetConfig(cfg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:05:04 +00:00
|
|
|
func (a *MpoolAPI) MpoolSelect(ctx context.Context, tsk types.TipSetKey, ticketQuality float64) ([]*types.SignedMessage, error) {
|
2020-08-05 20:17:14 +00:00
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:05:04 +00:00
|
|
|
return a.Mpool.SelectMessages(ts, ticketQuality)
|
2020-08-05 20:17:14 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *MpoolAPI) MpoolPending(ctx context.Context, tsk types.TipSetKey) ([]*types.SignedMessage, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-12-03 19:33:29 +00:00
|
|
|
pending, mpts := a.Mpool.Pending()
|
|
|
|
|
|
|
|
haveCids := map[cid.Cid]struct{}{}
|
|
|
|
for _, m := range pending {
|
|
|
|
haveCids[m.Cid()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:34:31 +00:00
|
|
|
if ts == nil || mpts.Height() > ts.Height() {
|
2019-12-03 19:33:29 +00:00
|
|
|
return pending, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if mpts.Height() == ts.Height() {
|
|
|
|
if mpts.Equals(ts) {
|
|
|
|
return pending, nil
|
|
|
|
}
|
|
|
|
// different blocks in tipsets
|
|
|
|
|
|
|
|
have, err := a.Mpool.MessagesForBlocks(ts.Blocks())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting messages for base ts: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range have {
|
|
|
|
haveCids[m.Cid()] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := a.Mpool.MessagesForBlocks(ts.Blocks())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf(": %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range msgs {
|
|
|
|
if _, ok := haveCids[m.Cid()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
haveCids[m.Cid()] = struct{}{}
|
|
|
|
pending = append(pending, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mpts.Height() >= ts.Height() {
|
|
|
|
return pending, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err = a.Chain.LoadTipSet(ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading parent tipset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:44:55 +00:00
|
|
|
func (a *MpoolAPI) MpoolPush(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error) {
|
2019-09-16 14:17:08 +00:00
|
|
|
return a.Mpool.Push(smsg)
|
|
|
|
}
|
|
|
|
|
2019-09-17 08:15:26 +00:00
|
|
|
func (a *MpoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Message) (*types.SignedMessage, error) {
|
2020-08-08 16:56:33 +00:00
|
|
|
{
|
2020-08-08 20:51:24 +00:00
|
|
|
fromA, err := a.Stmgr.ResolveToKeyAddress(ctx, msg.From, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting key address: %w", err)
|
|
|
|
}
|
2020-08-11 17:32:31 +00:00
|
|
|
done, err := a.PushLocks.TakeLock(ctx, fromA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("taking lock: %w", err)
|
2020-08-08 20:51:24 +00:00
|
|
|
}
|
2020-08-11 17:32:31 +00:00
|
|
|
defer done()
|
2020-08-08 16:56:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 14:17:08 +00:00
|
|
|
if msg.Nonce != 0 {
|
2019-09-17 08:15:26 +00:00
|
|
|
return nil, xerrors.Errorf("MpoolPushMessage expects message nonce to be 0, was %d", msg.Nonce)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
2020-07-15 19:21:53 +00:00
|
|
|
if msg.GasLimit == 0 {
|
2020-07-20 17:48:30 +00:00
|
|
|
gasLimit, err := a.GasEstimateGasLimit(ctx, msg, types.TipSetKey{})
|
|
|
|
if err != nil {
|
2020-08-01 07:40:11 +00:00
|
|
|
return nil, xerrors.Errorf("estimating gas used: %w", err)
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
2020-08-08 15:09:47 +00:00
|
|
|
msg.GasLimit = int64(float64(gasLimit) * a.Mpool.GetConfig().GasLimitOverestimation)
|
2020-07-15 19:21:53 +00:00
|
|
|
}
|
2020-07-20 19:50:03 +00:00
|
|
|
|
2020-08-06 21:08:42 +00:00
|
|
|
if msg.GasPremium == types.EmptyInt || types.BigCmp(msg.GasPremium, types.NewInt(0)) == 0 {
|
2020-08-11 05:10:12 +00:00
|
|
|
gasPremium, err := a.GasEstimateGasPremium(ctx, 2, msg.From, msg.GasLimit, types.TipSetKey{})
|
2020-07-20 17:48:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("estimating gas price: %w", err)
|
|
|
|
}
|
2020-08-06 22:10:55 +00:00
|
|
|
msg.GasPremium = gasPremium
|
2020-08-06 19:14:08 +00:00
|
|
|
}
|
2020-08-06 22:10:55 +00:00
|
|
|
|
2020-08-06 19:14:08 +00:00
|
|
|
if msg.GasFeeCap == types.EmptyInt || types.BigCmp(msg.GasFeeCap, types.NewInt(0)) == 0 {
|
2020-08-08 20:51:24 +00:00
|
|
|
feeCap, err := a.GasEstimateFeeCap(ctx, msg, 10, types.EmptyTSK)
|
2020-08-06 22:10:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("estimating fee cap: %w", err)
|
|
|
|
}
|
|
|
|
msg.GasFeeCap = feeCap
|
2020-08-06 19:14:08 +00:00
|
|
|
}
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2020-04-16 21:06:31 +00:00
|
|
|
return a.Mpool.PushWithNonce(ctx, msg.From, func(from address.Address, nonce uint64) (*types.SignedMessage, error) {
|
2019-09-16 14:17:08 +00:00
|
|
|
msg.Nonce = nonce
|
2020-04-17 16:47:14 +00:00
|
|
|
if msg.From.Protocol() == address.ID {
|
|
|
|
log.Warnf("Push from ID address (%s), adjusting to %s", msg.From, from)
|
|
|
|
msg.From = from
|
|
|
|
}
|
2019-10-06 01:34:45 +00:00
|
|
|
|
|
|
|
b, err := a.WalletBalance(ctx, msg.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("mpool push: getting origin balance: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.LessThan(msg.Value) {
|
|
|
|
return nil, xerrors.Errorf("mpool push: not enough funds: %s < %s", b, msg.Value)
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:06:31 +00:00
|
|
|
return a.WalletSignMessage(ctx, from, msg)
|
2019-09-16 14:17:08 +00:00
|
|
|
})
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *MpoolAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
|
|
|
|
return a.Mpool.GetNonce(addr)
|
|
|
|
}
|
2019-11-17 07:44:06 +00:00
|
|
|
|
|
|
|
func (a *MpoolAPI) MpoolSub(ctx context.Context) (<-chan api.MpoolUpdate, error) {
|
|
|
|
return a.Mpool.Updates(ctx)
|
|
|
|
}
|