2019-07-08 14:07:09 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
2019-08-01 17:12:41 +00:00
|
|
|
"context"
|
|
|
|
|
2019-07-08 14:07:09 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
inet "github.com/libp2p/go-libp2p-core/network"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"go.uber.org/fx"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain"
|
2019-11-09 23:00:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/blocksync"
|
2019-12-01 23:11:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/sub"
|
|
|
|
"github.com/filecoin-project/lotus/node/hello"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2019-10-17 08:57:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/peermgr"
|
2019-12-10 04:19:59 +00:00
|
|
|
retrievalmarket "github.com/filecoin-project/lotus/retrieval"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/retrieval/discovery"
|
2019-11-04 19:57:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/storagemarket"
|
2019-07-08 14:07:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RunHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello.Service) {
|
|
|
|
h.SetStreamHandler(hello.ProtocolID, svc.HandleStream)
|
|
|
|
|
|
|
|
bundle := inet.NotifyBundle{
|
|
|
|
ConnectedF: func(_ inet.Network, c inet.Conn) {
|
|
|
|
go func() {
|
|
|
|
if err := svc.SayHello(helpers.LifecycleCtx(mctx, lc), c.RemotePeer()); err != nil {
|
|
|
|
log.Warnw("failed to say hello", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
h.Network().Notify(&bundle)
|
|
|
|
}
|
|
|
|
|
2019-10-17 08:57:56 +00:00
|
|
|
func RunPeerMgr(mctx helpers.MetricsCtx, lc fx.Lifecycle, pmgr *peermgr.PeerMgr) {
|
|
|
|
go pmgr.Run(helpers.LifecycleCtx(mctx, lc))
|
|
|
|
}
|
|
|
|
|
2019-11-09 23:00:22 +00:00
|
|
|
func RunBlockSync(h host.Host, svc *blocksync.BlockSyncService) {
|
|
|
|
h.SetStreamHandler(blocksync.BlockSyncProtocolID, svc.HandleStream)
|
2019-07-08 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:06:48 +00:00
|
|
|
func HandleIncomingBlocks(mctx helpers.MetricsCtx, lc fx.Lifecycle, pubsub *pubsub.PubSub, s *chain.Syncer, h host.Host) {
|
2019-07-08 14:07:09 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
|
|
|
blocksub, err := pubsub.Subscribe("/fil/blocks")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:06:48 +00:00
|
|
|
go sub.HandleIncomingBlocks(ctx, blocksub, s, h.ConnManager())
|
2019-07-08 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 23:11:43 +00:00
|
|
|
func HandleIncomingMessages(mctx helpers.MetricsCtx, lc fx.Lifecycle, pubsub *pubsub.PubSub, mpool *messagepool.MessagePool) {
|
2019-07-08 14:07:09 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
|
|
|
msgsub, err := pubsub.Subscribe("/fil/messages")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go sub.HandleIncomingMessages(ctx, mpool, msgsub)
|
|
|
|
}
|
2019-08-01 17:12:41 +00:00
|
|
|
|
2019-11-04 19:57:54 +00:00
|
|
|
func RunDealClient(mctx helpers.MetricsCtx, lc fx.Lifecycle, c storagemarket.StorageClient) {
|
2019-09-10 12:35:43 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2019-08-01 17:12:41 +00:00
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(context.Context) error {
|
2019-09-10 12:35:43 +00:00
|
|
|
c.Run(ctx)
|
2019-08-01 17:12:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
OnStop: func(context.Context) error {
|
|
|
|
c.Stop()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-08-14 20:27:10 +00:00
|
|
|
|
2019-12-10 04:19:59 +00:00
|
|
|
func RetrievalResolver(l *discovery.Local) retrievalmarket.PeerResolver {
|
2019-08-26 13:45:36 +00:00
|
|
|
return discovery.Multi(l)
|
|
|
|
}
|