Merge pull request #1276 from filecoin-project/asr/findpeer
Re: #1232: Add a FindPeer RPC method
This commit is contained in:
commit
06cb004c33
@ -23,6 +23,7 @@ type Common interface {
|
|||||||
NetConnect(context.Context, peer.AddrInfo) error
|
NetConnect(context.Context, peer.AddrInfo) error
|
||||||
NetAddrsListen(context.Context) (peer.AddrInfo, error)
|
NetAddrsListen(context.Context) (peer.AddrInfo, error)
|
||||||
NetDisconnect(context.Context, peer.ID) error
|
NetDisconnect(context.Context, peer.ID) error
|
||||||
|
NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error)
|
||||||
|
|
||||||
// ID returns peerID of libp2p node backing this API
|
// ID returns peerID of libp2p node backing this API
|
||||||
ID(context.Context) (peer.ID, error)
|
ID(context.Context) (peer.ID, error)
|
||||||
|
@ -29,6 +29,7 @@ type CommonStruct struct {
|
|||||||
NetConnect func(context.Context, peer.AddrInfo) error `perm:"write"`
|
NetConnect func(context.Context, peer.AddrInfo) error `perm:"write"`
|
||||||
NetAddrsListen func(context.Context) (peer.AddrInfo, error) `perm:"read"`
|
NetAddrsListen func(context.Context) (peer.AddrInfo, error) `perm:"read"`
|
||||||
NetDisconnect func(context.Context, peer.ID) error `perm:"write"`
|
NetDisconnect func(context.Context, peer.ID) error `perm:"write"`
|
||||||
|
NetFindPeer func(context.Context, peer.ID) (peer.AddrInfo, error) `perm:"read"`
|
||||||
|
|
||||||
ID func(context.Context) (peer.ID, error) `perm:"read"`
|
ID func(context.Context) (peer.ID, error) `perm:"read"`
|
||||||
Version func(context.Context) (api.Version, error) `perm:"read"`
|
Version func(context.Context) (api.Version, error) `perm:"read"`
|
||||||
@ -197,6 +198,10 @@ func (c *CommonStruct) NetDisconnect(ctx context.Context, p peer.ID) error {
|
|||||||
return c.Internal.NetDisconnect(ctx, p)
|
return c.Internal.NetDisconnect(ctx, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CommonStruct) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
|
||||||
|
return c.Internal.NetFindPeer(ctx, p)
|
||||||
|
}
|
||||||
|
|
||||||
// ID implements API.ID
|
// ID implements API.ID
|
||||||
func (c *CommonStruct) ID(ctx context.Context) (peer.ID, error) {
|
func (c *CommonStruct) ID(ctx context.Context) (peer.ID, error) {
|
||||||
return c.Internal.ID(ctx)
|
return c.Internal.ID(ctx)
|
||||||
|
36
cli/net.go
36
cli/net.go
@ -2,6 +2,7 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -18,6 +19,7 @@ var netCmd = &cli.Command{
|
|||||||
netConnect,
|
netConnect,
|
||||||
netListen,
|
netListen,
|
||||||
netId,
|
netId,
|
||||||
|
netFindPeer,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,3 +125,37 @@ var netId = &cli.Command{
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var netFindPeer = &cli.Command{
|
||||||
|
Name: "findpeer",
|
||||||
|
Usage: "Find the addresses of a given peerID",
|
||||||
|
ArgsUsage: "<peer ID>",
|
||||||
|
Action: func(cctx *cli.Context) error {
|
||||||
|
if cctx.NArg() != 1 {
|
||||||
|
fmt.Println("Usage: findpeer [peer ID]")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
pid, err := peer.IDB58Decode(cctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
api, closer, err := GetAPI(cctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer closer()
|
||||||
|
|
||||||
|
ctx := ReqContext(cctx)
|
||||||
|
|
||||||
|
addrs, err := api.NetFindPeer(ctx, pid)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(addrs)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package impl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/filecoin-project/lotus/node/modules/lp2p"
|
||||||
|
|
||||||
logging "github.com/ipfs/go-log/v2"
|
logging "github.com/ipfs/go-log/v2"
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ type CommonAPI struct {
|
|||||||
|
|
||||||
APISecret *dtypes.APIAlg
|
APISecret *dtypes.APIAlg
|
||||||
Host host.Host
|
Host host.Host
|
||||||
|
Router lp2p.BaseIpfsRouting
|
||||||
}
|
}
|
||||||
|
|
||||||
type jwtPayload struct {
|
type jwtPayload struct {
|
||||||
@ -81,6 +83,10 @@ func (a *CommonAPI) NetDisconnect(ctx context.Context, p peer.ID) error {
|
|||||||
return a.Host.Network().ClosePeer(p)
|
return a.Host.Network().ClosePeer(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *CommonAPI) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
|
||||||
|
return a.Router.FindPeer(ctx, p)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *CommonAPI) ID(context.Context) (peer.ID, error) {
|
func (a *CommonAPI) ID(context.Context) (peer.ID, error) {
|
||||||
return a.Host.ID(), nil
|
return a.Host.ID(), nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user