lotus/node/impl/full/mpool.go

300 lines
8.3 KiB
Go
Raw Normal View History

2019-08-20 16:48:33 +00:00
package full
import (
"context"
"encoding/json"
2022-08-16 19:56:32 +00:00
"github.com/ipfs/go-cid"
consensus "github.com/libp2p/go-libp2p-consensus"
"github.com/libp2p/go-libp2p/core/peer"
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
2022-08-16 19:56:32 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
2022-06-14 15:00:51 +00:00
2019-11-17 07:44:06 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/messagepool"
"github.com/filecoin-project/lotus/chain/messagesigner"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/modules/dtypes"
2019-08-20 16:48:33 +00:00
)
type MpoolModuleAPI interface {
2020-09-30 15:59:18 +00:00
MpoolPush(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error)
}
2021-04-03 11:20:50 +00:00
var _ MpoolModuleAPI = *new(api.FullNode)
// MpoolModule provides a default implementation of MpoolModuleAPI.
// It can be swapped out with another implementation through Dependency
// Injection (for example with a thin RPC client).
type MpoolModule struct {
fx.In
Mpool *messagepool.MessagePool
}
var _ MpoolModuleAPI = (*MpoolModule)(nil)
2019-08-20 16:48:33 +00:00
type MpoolAPI struct {
fx.In
MpoolModuleAPI
2019-09-16 14:17:08 +00:00
WalletAPI
GasAPI
2019-09-16 14:17:08 +00:00
MessageSigner messagesigner.MsgSigner
// MessageSigner *messagesigner.MessageSigner
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 {
2021-12-11 21:03:00 +00:00
return a.Mpool.SetConfig(ctx, cfg)
2020-08-07 14:45:31 +00:00
}
func (a *MpoolAPI) MpoolSelect(ctx context.Context, tsk types.TipSetKey, ticketQuality float64) ([]*types.SignedMessage, error) {
2021-12-11 21:03:00 +00:00
ts, err := a.Chain.GetTipSetFromKey(ctx, tsk)
2020-08-05 20:17:14 +00:00
if err != nil {
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
}
return a.Mpool.SelectMessages(ctx, ts, ticketQuality)
2020-08-05 20:17:14 +00:00
}
func (a *MpoolAPI) MpoolPending(ctx context.Context, tsk types.TipSetKey) ([]*types.SignedMessage, error) {
2021-12-11 21:03:00 +00:00
ts, err := a.Chain.GetTipSetFromKey(ctx, tsk)
if err != nil {
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
}
2021-05-18 18:56:42 +00:00
pending, mpts := a.Mpool.Pending(ctx)
2019-12-03 19:33:29 +00:00
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
}
2021-07-29 18:58:59 +00:00
// different blocks in tipsets of the same height
// we exclude messages that have been included in blocks in the mpool tipset
2021-12-17 09:42:09 +00:00
have, err := a.Mpool.MessagesForBlocks(ctx, mpts.Blocks())
2019-12-03 19:33:29 +00:00
if err != nil {
return nil, xerrors.Errorf("getting messages for base ts: %w", err)
}
for _, m := range have {
haveCids[m.Cid()] = struct{}{}
}
}
2021-12-17 09:42:09 +00:00
msgs, err := a.Mpool.MessagesForBlocks(ctx, ts.Blocks())
2019-12-03 19:33:29 +00:00
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
}
2021-12-11 21:03:00 +00:00
ts, err = a.Chain.LoadTipSet(ctx, ts.Parents())
2019-12-03 19:33:29 +00:00
if err != nil {
return nil, xerrors.Errorf("loading parent tipset: %w", err)
}
}
2019-08-20 16:48:33 +00:00
}
func (a *MpoolAPI) MpoolClear(ctx context.Context, local bool) error {
a.Mpool.Clear(ctx, local)
2020-08-21 17:28:45 +00:00
return nil
}
func (m *MpoolModule) MpoolPush(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error) {
2021-05-18 18:56:42 +00:00
return m.Mpool.Push(ctx, smsg)
2019-09-16 14:17:08 +00:00
}
2020-09-18 06:40:43 +00:00
func (a *MpoolAPI) MpoolPushUntrusted(ctx context.Context, smsg *types.SignedMessage) (cid.Cid, error) {
2021-05-18 18:56:42 +00:00
return a.Mpool.PushUntrusted(ctx, smsg)
2020-09-18 06:40:43 +00:00
}
func (a *MpoolAPI) MpoolPushMessage(ctx context.Context, msg *types.Message, spec *api.MessageSendSpec) (*types.SignedMessage, error) {
cp := *msg
msg = &cp
inMsg := *msg
// Redirect to leader if current node is not leader. A single non raft based node is always the leader
if !a.MessageSigner.IsLeader(ctx) {
var signedMsg types.SignedMessage
redirected, err := a.MessageSigner.RedirectToLeader(ctx, "MpoolPushMessage", api.MpoolMessageWhole{msg, spec}, &signedMsg)
if err != nil {
return nil, err
}
// It's possible that the current node became the leader between the check and the redirect
// In that case, continue with rest of execution and only return signedMsg if something was redirected
if redirected {
return &signedMsg, nil
}
}
// Check if this uuid has already been processed
if spec != nil {
signedMessage, err := a.MessageSigner.GetSignedMessage(ctx, spec.MsgUuid)
if err == nil {
log.Warnf("Message already processed. cid=%s", signedMessage.Cid())
return signedMessage, nil
}
}
fromA, err := a.Stmgr.ResolveToKeyAddress(ctx, msg.From, nil)
if err != nil {
return nil, xerrors.Errorf("getting key address: %w", err)
}
{
done, err := a.PushLocks.TakeLock(ctx, fromA)
if err != nil {
return nil, xerrors.Errorf("taking lock: %w", err)
}
defer done()
}
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
}
msg, err = a.GasAPI.GasEstimateMessageGas(ctx, msg, spec, types.EmptyTSK)
2020-08-19 21:25:58 +00:00
if err != nil {
return nil, xerrors.Errorf("GasEstimateMessageGas error: %w", err)
}
2019-08-20 16:48:33 +00:00
if msg.GasPremium.GreaterThan(msg.GasFeeCap) {
inJson, _ := json.Marshal(inMsg)
outJson, _ := json.Marshal(msg)
return nil, xerrors.Errorf("After estimation, GasPremium is greater than GasFeeCap, inmsg: %s, outmsg: %s",
inJson, outJson)
}
if msg.From.Protocol() == address.ID {
log.Warnf("Push from ID address (%s), adjusting to %s", msg.From, fromA)
msg.From = fromA
}
b, err := a.WalletBalance(ctx, msg.From)
if err != nil {
return nil, xerrors.Errorf("mpool push: getting origin balance: %w", err)
}
requiredFunds := big.Add(msg.Value, msg.RequiredFunds())
if b.LessThan(requiredFunds) {
return nil, xerrors.Errorf("mpool push: not enough funds: %s < %s", b, requiredFunds)
}
// Sign and push the message
signedMsg, err := a.MessageSigner.SignMessage(ctx, msg, spec, func(smsg *types.SignedMessage) error {
if _, err := a.MpoolModuleAPI.MpoolPush(ctx, smsg); err != nil {
return xerrors.Errorf("mpool push: failed to push message: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
// Store uuid->signed message in datastore
if spec != nil {
err = a.MessageSigner.StoreSignedMessage(ctx, spec.MsgUuid, signedMsg)
if err != nil {
return nil, err
}
}
return signedMsg, nil
2019-08-20 16:48:33 +00:00
}
2020-10-08 02:04:43 +00:00
func (a *MpoolAPI) MpoolBatchPush(ctx context.Context, smsgs []*types.SignedMessage) ([]cid.Cid, error) {
var messageCids []cid.Cid
for _, smsg := range smsgs {
2021-05-18 18:56:42 +00:00
smsgCid, err := a.Mpool.Push(ctx, smsg)
2020-10-08 02:04:43 +00:00
if err != nil {
return messageCids, err
}
messageCids = append(messageCids, smsgCid)
}
return messageCids, nil
}
func (a *MpoolAPI) MpoolBatchPushUntrusted(ctx context.Context, smsgs []*types.SignedMessage) ([]cid.Cid, error) {
var messageCids []cid.Cid
for _, smsg := range smsgs {
2021-05-18 18:56:42 +00:00
smsgCid, err := a.Mpool.PushUntrusted(ctx, smsg)
2020-10-08 02:04:43 +00:00
if err != nil {
return messageCids, err
}
messageCids = append(messageCids, smsgCid)
}
return messageCids, nil
}
func (a *MpoolAPI) MpoolBatchPushMessage(ctx context.Context, msgs []*types.Message, spec *api.MessageSendSpec) ([]*types.SignedMessage, error) {
var smsgs []*types.SignedMessage
for _, msg := range msgs {
smsg, err := a.MpoolPushMessage(ctx, msg, spec)
if err != nil {
return smsgs, err
}
smsgs = append(smsgs, smsg)
}
return smsgs, nil
}
func (a *MpoolAPI) MpoolCheckMessages(ctx context.Context, protos []*api.MessagePrototype) ([][]api.MessageCheckStatus, error) {
return a.Mpool.CheckMessages(ctx, protos)
}
func (a *MpoolAPI) MpoolCheckPendingMessages(ctx context.Context, from address.Address) ([][]api.MessageCheckStatus, error) {
return a.Mpool.CheckPendingMessages(ctx, from)
}
func (a *MpoolAPI) MpoolCheckReplaceMessages(ctx context.Context, msgs []*types.Message) ([][]api.MessageCheckStatus, error) {
return a.Mpool.CheckReplaceMessages(ctx, msgs)
}
2019-08-20 16:48:33 +00:00
func (a *MpoolAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
return a.Mpool.GetNonce(ctx, addr, types.EmptyTSK)
2019-08-20 16:48:33 +00:00
}
2019-11-17 07:44:06 +00:00
func (a *MpoolAPI) MpoolSub(ctx context.Context) (<-chan api.MpoolUpdate, error) {
return a.Mpool.Updates(ctx)
}
func (a *MpoolAPI) GetRaftState(ctx context.Context) (consensus.State, error) {
return a.MessageSigner.GetRaftState(ctx)
}
func (a *MpoolAPI) RaftLeader(ctx context.Context) (peer.ID, error) {
return a.MessageSigner.RaftLeader(ctx)
}