lotus/markets/idxprov/mesh.go
Masih H. Derkani 2ebc111b70 Use the same host and datatransfer as markets for index provider
Remove the bespoke instantiation of libp2p host and datatransfer manager
for index provider and reuse the existing instances used by markets.

The rationale for reuse is the following:
 1. Separation of host introduces a discovery problem, where without
    gossipsub the index provider endpoint will not be discoverable.
    Using the same host as markets would mean the chain can be used to
    discover addresses, putting less empassis on criticality of
    gossipsub considering its set-up cost and lack of message delivery
    guarantees.

 2. Only a single instance of graphsync/datatransfer can be instantiated
    per libp2p host; therefore, if the host is shared, so should
    datatransfer manager.

 3. it is not clear if the assumptions under which separation was
    decided still hold.
2022-02-16 13:46:44 +00:00

58 lines
1.6 KiB
Go

package idxprov
import (
"context"
"fmt"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/filecoin-project/lotus/api/v1api"
logging "github.com/ipfs/go-log/v2"
)
var log = logging.Logger("idxprov")
const protectTag = "index-provider-gossipsub"
type MeshCreator interface {
Connect(ctx context.Context) error
}
type Libp2pMeshCreator struct {
fullnodeApi v1api.FullNode
marketsHost host.Host
}
func (mc Libp2pMeshCreator) Connect(ctx context.Context) error {
// Add the markets host ID to list of daemon's protected peers first, before any attempt to
// connect to full node over libp2p.
marketsPeerID := mc.marketsHost.ID()
if err := mc.fullnodeApi.NetProtectAdd(ctx, []peer.ID{marketsPeerID}); err != nil {
return fmt.Errorf("failed to call NetProtectAdd on the full node, err: %w", err)
}
faddrs, err := mc.fullnodeApi.NetAddrsListen(ctx)
if err != nil {
return fmt.Errorf("failed to fetch full node listen addrs, err: %w", err)
}
// Connect to the full node, ask it to protect the connection and protect the connection on
// markets end too.
if err := mc.marketsHost.Connect(ctx, faddrs); err != nil {
return fmt.Errorf("failed to connect index provider host with the full node: %w", err)
}
mc.marketsHost.ConnManager().Protect(faddrs.ID, protectTag)
log.Debugw("successfully connected to full node and asked it protect indexer provider peer conn", "fullNodeInfo", faddrs.String(),
"peerId", marketsPeerID)
return nil
}
func NewMeshCreator(fullnodeApi v1api.FullNode, marketsHost host.Host) MeshCreator {
return Libp2pMeshCreator{fullnodeApi, marketsHost}
}