2019-07-08 14:07:09 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
2019-08-01 17:12:41 +00:00
|
|
|
"context"
|
|
|
|
|
2020-05-20 22:46:44 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2020-03-19 04:13:04 +00:00
|
|
|
eventbus "github.com/libp2p/go-eventbus"
|
|
|
|
event "github.com/libp2p/go-libp2p-core/event"
|
2019-07-08 14:07:09 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2020-05-05 14:12:06 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-07-08 14:07:09 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"go.uber.org/fx"
|
2020-03-19 04:13:04 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2020-01-10 17:13:12 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket/discovery"
|
2020-01-10 18:01:48 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
2020-03-03 23:44:08 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain"
|
2020-03-25 23:16:17 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/beacon"
|
2020-04-14 03:05:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/beacon/drand"
|
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"
|
2020-05-12 18:05:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
2020-04-14 03:05:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/sub"
|
2020-02-22 11:36:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/peermgr"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/hello"
|
2019-12-17 03:17:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2019-07-08 14:07:09 +00:00
|
|
|
)
|
|
|
|
|
2020-03-19 04:13:04 +00:00
|
|
|
func RunHello(mctx helpers.MetricsCtx, lc fx.Lifecycle, h host.Host, svc *hello.Service) error {
|
2019-07-08 14:07:09 +00:00
|
|
|
h.SetStreamHandler(hello.ProtocolID, svc.HandleStream)
|
|
|
|
|
2020-03-19 04:13:04 +00:00
|
|
|
sub, err := h.EventBus().Subscribe(new(event.EvtPeerIdentificationCompleted), eventbus.BufSize(1024))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to subscribe to event bus: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for evt := range sub.Out() {
|
|
|
|
pic := evt.(event.EvtPeerIdentificationCompleted)
|
2019-07-08 14:07:09 +00:00
|
|
|
go func() {
|
2020-03-19 04:13:04 +00:00
|
|
|
if err := svc.SayHello(helpers.LifecycleCtx(mctx, lc), pic.Peer); err != nil {
|
2019-07-08 14:07:09 +00:00
|
|
|
log.Warnw("failed to say hello", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2020-03-19 04:13:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
2019-07-08 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:29 +00:00
|
|
|
func HandleIncomingBlocks(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, s *chain.Syncer, chain *store.ChainStore, stmgr *stmgr.StateManager, h host.Host, nn dtypes.NetworkName) {
|
2019-07-08 14:07:09 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
blocksub, err := ps.Subscribe(build.BlocksTopic(nn))
|
2019-07-08 14:07:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:05:29 +00:00
|
|
|
v := sub.NewBlockValidator(
|
|
|
|
chain, stmgr,
|
|
|
|
func(p peer.ID) {
|
|
|
|
ps.BlacklistPeer(p)
|
|
|
|
h.ConnManager().TagPeer(p, "badblock", -1000)
|
|
|
|
})
|
2020-02-17 05:51:18 +00:00
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
if err := ps.RegisterTopicValidator(build.BlocksTopic(nn), v.Validate); err != nil {
|
2020-02-17 05:51:18 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:29:06 +00:00
|
|
|
go sub.HandleIncomingBlocks(ctx, blocksub, s, h.ConnManager())
|
2019-07-08 14:07:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
func HandleIncomingMessages(mctx helpers.MetricsCtx, lc fx.Lifecycle, ps *pubsub.PubSub, mpool *messagepool.MessagePool, nn dtypes.NetworkName) {
|
2019-07-08 14:07:09 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
msgsub, err := ps.Subscribe(build.MessagesTopic(nn))
|
2019-07-08 14:07:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:39:07 +00:00
|
|
|
v := sub.NewMessageValidator(mpool)
|
2020-02-17 05:51:18 +00:00
|
|
|
|
2020-03-31 23:13:37 +00:00
|
|
|
if err := ps.RegisterTopicValidator(build.MessagesTopic(nn), v.Validate); err != nil {
|
2020-02-17 05:51:18 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-08 14:07:09 +00:00
|
|
|
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-17 03:17:46 +00:00
|
|
|
func NewLocalDiscovery(ds dtypes.MetadataDS) *discovery.Local {
|
2020-05-20 22:46:44 +00:00
|
|
|
return discovery.NewLocal(namespace.Wrap(ds, datastore.NewKey("/deals/local")))
|
2019-12-17 03:17:46 +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)
|
|
|
|
}
|
2020-03-25 23:16:17 +00:00
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
func RandomBeacon(cs *store.ChainStore, _ dtypes.AfterGenesisSet) (beacon.RandomBeacon, error) {
|
|
|
|
gen, err := cs.GetGenesis()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//return beacon.NewMockBeacon(build.BlockDelay * time.Second)
|
|
|
|
return drand.NewDrandBeacon(gen.Timestamp, build.BlockDelay)
|
2020-03-25 23:16:17 +00:00
|
|
|
}
|