2022-02-03 14:44:18 +00:00
|
|
|
package idxprov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-02-04 09:04:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
|
2022-02-03 14:44:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("idxprov")
|
|
|
|
|
|
|
|
type MeshCreator interface {
|
|
|
|
Connect(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Libp2pMeshCreator struct {
|
|
|
|
fullnodeApi v1api.FullNode
|
|
|
|
idxProvHost Host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc Libp2pMeshCreator) Connect(ctx context.Context) error {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, connect to the full node, ask it to protect the connection and protect the connection on our end too
|
|
|
|
if err := mc.idxProvHost.Connect(ctx, faddrs); err != nil {
|
2022-02-03 14:44:18 +00:00
|
|
|
return fmt.Errorf("failed to connect index provider host with the full node: %w", err)
|
|
|
|
}
|
2022-02-04 09:58:47 +00:00
|
|
|
mc.idxProvHost.ConnManager().Protect(faddrs.ID, "index-provider-gossipsub")
|
2022-02-04 09:55:26 +00:00
|
|
|
if err := mc.fullnodeApi.NetProtectAdd(ctx, []peer.ID{mc.idxProvHost.ID()}); err != nil {
|
|
|
|
return fmt.Errorf("failed to call NetProtectAdd on the full node, err: %w", err)
|
|
|
|
}
|
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(),
|
|
|
|
"idxProviderPeerId", mc.idxProvHost.ID())
|
2022-02-03 14:44:18 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMeshCreator(fullnodeApi v1api.FullNode, idxProvHost Host) MeshCreator {
|
|
|
|
return Libp2pMeshCreator{fullnodeApi, idxProvHost}
|
|
|
|
}
|