diff --git a/cli/net.go b/cli/net.go index f3b5ae2e9..9c40c70c7 100644 --- a/cli/net.go +++ b/cli/net.go @@ -8,12 +8,17 @@ import ( "strings" "text/tabwriter" - "github.com/libp2p/go-libp2p-core/peer" - protocol "github.com/libp2p/go-libp2p-core/protocol" - "github.com/dustin/go-humanize" "github.com/urfave/cli/v2" + "golang.org/x/xerrors" + "github.com/libp2p/go-libp2p-core/peer" + protocol "github.com/libp2p/go-libp2p-core/protocol" + "github.com/multiformats/go-multiaddr" + + "github.com/filecoin-project/go-address" + + "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/lib/addrutil" ) @@ -141,7 +146,7 @@ var NetListen = &cli.Command{ var netConnect = &cli.Command{ Name: "connect", Usage: "Connect to a peer", - ArgsUsage: "[peerMultiaddr]", + ArgsUsage: "[peerMultiaddr|minerActorAddress]", Action: func(cctx *cli.Context) error { api, closer, err := GetAPI(cctx) if err != nil { @@ -152,7 +157,43 @@ var netConnect = &cli.Command{ pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice()) if err != nil { - return err + a, perr := address.NewFromString(cctx.Args().First()) + if perr != nil { + return err + } + + na, fc, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer fc() + + mi, err := na.StateMinerInfo(ctx, a, types.EmptyTSK) + if err != nil { + return xerrors.Errorf("getting miner info: %w", err) + } + + if mi.PeerId == nil { + return xerrors.Errorf("no PeerID for miner") + } + multiaddrs := make([]multiaddr.Multiaddr, 0, len(mi.Multiaddrs)) + for i, a := range mi.Multiaddrs { + maddr, err := multiaddr.NewMultiaddrBytes(a) + if err != nil { + log.Warnf("parsing multiaddr %d (%x): %s", i, a, err) + continue + } + multiaddrs = append(multiaddrs, maddr) + } + + pi := peer.AddrInfo{ + ID: *mi.PeerId, + Addrs: multiaddrs, + } + + fmt.Printf("%s -> %s\n", a, pi) + + pis = append(pis, pi) } for _, pi := range pis {