lotus/node/modules/storageminer_idxprov.go

72 lines
2.2 KiB
Go
Raw Normal View History

2021-11-10 16:28:23 +00:00
package modules
import (
"context"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"go.uber.org/fx"
"github.com/filecoin-project/go-address"
2021-11-10 16:28:23 +00:00
provider "github.com/filecoin-project/index-provider"
"github.com/filecoin-project/index-provider/engine"
"github.com/ipfs/go-datastore/namespace"
"github.com/libp2p/go-libp2p-core/host"
"golang.org/x/xerrors"
2021-11-10 16:28:23 +00:00
"github.com/filecoin-project/lotus/node/config"
"github.com/filecoin-project/lotus/node/modules/dtypes"
)
type IdxProv struct {
fx.In
fx.Lifecycle
Datastore dtypes.MetadataDS
PeerID peer.ID
peerstore.Peerstore
}
func IndexProvider(cfg config.IndexProviderConfig) func(params IdxProv, marketHost host.Host, dt dtypes.ProviderDataTransfer, maddr dtypes.MinerAddress) (provider.Interface, error) {
return func(args IdxProv, marketHost host.Host, dt dtypes.ProviderDataTransfer, maddr dtypes.MinerAddress) (provider.Interface, error) {
2022-02-03 14:53:30 +00:00
ipds := namespace.Wrap(args.Datastore, datastore.NewKey("/index-provider"))
2022-02-02 10:36:08 +00:00
pkey := args.Peerstore.PrivKey(args.PeerID)
if pkey == nil {
return nil, xerrors.Errorf("missing private key for node ID: %s", args.PeerID)
2021-11-10 16:28:23 +00:00
}
var retAdds []string
2022-01-13 11:31:15 +00:00
for _, a := range marketHost.Addrs() {
retAdds = append(retAdds, a.String())
2022-01-13 11:31:15 +00:00
}
// Get the miner ID and set as extra gossip data.
// The extra data is required by the lotus-specific index-provider gossip message validators.
ma := address.Address(maddr)
log.Infof("Using extra gossip data in index provider engine: %s", ma)
e, err := engine.New(cfg.Ingest, pkey, dt, marketHost, ipds, retAdds, engine.WithExtraGossipData(ma.Bytes()))
2021-11-10 16:28:23 +00:00
if err != nil {
return nil, xerrors.Errorf("creating indexer provider engine: %w", err)
}
args.Lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
if err := e.Start(ctx); err != nil {
return xerrors.Errorf("starting indexer provider engine: %w", err)
2021-11-10 16:28:23 +00:00
}
return nil
},
OnStop: func(_ context.Context) error {
if err := e.Shutdown(); err != nil {
return xerrors.Errorf("shutting down indexer provider engine: %w", err)
}
return nil
2021-11-10 16:28:23 +00:00
},
})
return e, nil
}
}