Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve peer hints for pin remote add #8143

Merged
merged 4 commits into from
Jun 25, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions core/commands/pin/remotepin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net"
"sort"
"strings"
"text/tabwriter"
Expand All @@ -24,6 +25,7 @@ import (
path "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)

var log = logging.Logger("core/commands/cmdenv")
Expand Down Expand Up @@ -167,18 +169,31 @@ NOTE: a comma-separated notation is supported in CLI for convenience:
}

// Prepare Pin.origins
// Add own multiaddrs to the 'origins' array, so Pinning Service can
// use that as a hint and connect back to us (if possible)
// If CID in blockstore, add own multiaddrs to the 'origins' array
// so pinning service can use that as a hint and connect back to us.
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
if node.PeerHost != nil {

isInBlockstore, err := node.Blockstore.Has(rp.Cid())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lidel this seems fine. I don't think we're introducing any new problems on the CoreAPI/Node boundary, but wanted to double check. WDYT?

The boundary issues we generally run into are that IIRC accessing the node directly doesn't respect the --offline flag, however I think we may already have that issue given that we use node.PeerHost below.

if err != nil {
return err
}

if isInBlockstore && node.PeerHost != nil {
addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(node.PeerHost))
if err != nil {
return err
}
opts = append(opts, pinclient.PinOpts.WithOrigins(addrs...))

var filteredAddrs []ma.Multiaddr
for _, addr := range addrs {
if !isLoopbackAddr(addr) {
filteredAddrs = append(filteredAddrs, addr)
}
}
opts = append(opts, pinclient.PinOpts.WithOrigins(filteredAddrs...))
}

// Execute remote pin request
Expand Down Expand Up @@ -240,6 +255,12 @@ NOTE: a comma-separated notation is supported in CLI for convenience:
},
}

func isLoopbackAddr(addr ma.Multiaddr) bool {
// IP is located at the second index: /ip4/127.0.0.1/tcp/8080
ip := net.ParseIP(strings.Split(addr.String(), "/")[2])
return ip.IsLoopback()
}

var listRemotePinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List objects pinned to remote pinning service.",
Expand Down