2022-02-03 14:44:18 +00:00
|
|
|
package idxprov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-08-25 18:20:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-02-04 09:04:07 +00:00
|
|
|
|
2022-02-03 14:44:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("idxprov")
|
|
|
|
|
2022-02-16 12:46:03 +00:00
|
|
|
const protectTag = "index-provider-gossipsub"
|
|
|
|
|
2022-02-03 14:44:18 +00:00
|
|
|
type MeshCreator interface {
|
|
|
|
Connect(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Libp2pMeshCreator struct {
|
|
|
|
fullnodeApi v1api.FullNode
|
2022-02-16 12:46:03 +00:00
|
|
|
marketsHost host.Host
|
2022-02-03 14:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mc Libp2pMeshCreator) Connect(ctx context.Context) error {
|
2022-02-15 11:32:02 +00:00
|
|
|
|
2022-02-16 12:46:03 +00:00
|
|
|
// 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 {
|
2022-02-15 11:32:02 +00:00
|
|
|
return fmt.Errorf("failed to call NetProtectAdd on the full node, err: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-02-04 09:04:07 +00:00
|
|
|
faddrs, err := mc.fullnodeApi.NetAddrsListen(ctx)
|
2022-02-03 14:44:18 +00:00
|
|
|
if err != nil {
|
2022-02-04 09:58:47 +00:00
|
|
|
return fmt.Errorf("failed to fetch full node listen addrs, err: %w", err)
|
2022-02-04 09:04:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 16:21:51 +00:00
|
|
|
// Connect from the full node, ask it to protect the connection and protect the connection on
|
|
|
|
// markets end too. Connection is initiated form full node to avoid the need to expose libp2p port on full node
|
|
|
|
if err := mc.fullnodeApi.NetConnect(ctx, peer.AddrInfo{
|
|
|
|
ID: mc.marketsHost.ID(),
|
|
|
|
Addrs: mc.marketsHost.Addrs(),
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("failed to connect to index provider host from full node: %w", err)
|
2022-02-03 14:44:18 +00:00
|
|
|
}
|
2022-02-16 12:46:03 +00:00
|
|
|
mc.marketsHost.ConnManager().Protect(faddrs.ID, protectTag)
|
2022-02-04 09:04:07 +00:00
|
|
|
|
|
|
|
log.Debugw("successfully connected to full node and asked it protect indexer provider peer conn", "fullNodeInfo", faddrs.String(),
|
2022-02-16 12:46:03 +00:00
|
|
|
"peerId", marketsPeerID)
|
2022-02-03 14:44:18 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-16 12:46:03 +00:00
|
|
|
func NewMeshCreator(fullnodeApi v1api.FullNode, marketsHost host.Host) MeshCreator {
|
|
|
|
return Libp2pMeshCreator{fullnodeApi, marketsHost}
|
2022-02-03 14:44:18 +00:00
|
|
|
}
|