feat: cli/net: implement 'net ping' command
This commit is contained in:
parent
f17bbc9eac
commit
ba72eff3e6
@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
metrics "github.com/libp2p/go-libp2p-core/metrics"
|
metrics "github.com/libp2p/go-libp2p-core/metrics"
|
||||||
"github.com/libp2p/go-libp2p-core/network"
|
"github.com/libp2p/go-libp2p-core/network"
|
||||||
@ -25,6 +26,7 @@ type Net interface {
|
|||||||
|
|
||||||
NetConnectedness(context.Context, peer.ID) (network.Connectedness, error) //perm:read
|
NetConnectedness(context.Context, peer.ID) (network.Connectedness, error) //perm:read
|
||||||
NetPeers(context.Context) ([]peer.AddrInfo, error) //perm:read
|
NetPeers(context.Context) ([]peer.AddrInfo, error) //perm:read
|
||||||
|
NetPing(context.Context, peer.ID) (time.Duration, error) //perm:read
|
||||||
NetConnect(context.Context, peer.AddrInfo) error //perm:write
|
NetConnect(context.Context, peer.AddrInfo) error //perm:write
|
||||||
NetAddrsListen(context.Context) (peer.AddrInfo, error) //perm:read
|
NetAddrsListen(context.Context) (peer.AddrInfo, error) //perm:read
|
||||||
NetDisconnect(context.Context, peer.ID) error //perm:write
|
NetDisconnect(context.Context, peer.ID) error //perm:write
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
context "context"
|
context "context"
|
||||||
json "encoding/json"
|
json "encoding/json"
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
|
time "time"
|
||||||
|
|
||||||
address "github.com/filecoin-project/go-address"
|
address "github.com/filecoin-project/go-address"
|
||||||
bitfield "github.com/filecoin-project/go-bitfield"
|
bitfield "github.com/filecoin-project/go-bitfield"
|
||||||
@ -1856,6 +1857,21 @@ func (mr *MockFullNodeMockRecorder) NetPeers(arg0 interface{}) *gomock.Call {
|
|||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetPeers", reflect.TypeOf((*MockFullNode)(nil).NetPeers), arg0)
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetPeers", reflect.TypeOf((*MockFullNode)(nil).NetPeers), arg0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetPing mocks base method.
|
||||||
|
func (m *MockFullNode) NetPing(arg0 context.Context, arg1 peer.ID) (time.Duration, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "NetPing", arg0, arg1)
|
||||||
|
ret0, _ := ret[0].(time.Duration)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetPing indicates an expected call of NetPing.
|
||||||
|
func (mr *MockFullNodeMockRecorder) NetPing(arg0, arg1 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetPing", reflect.TypeOf((*MockFullNode)(nil).NetPing), arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
// NetPubsubScores mocks base method.
|
// NetPubsubScores mocks base method.
|
||||||
func (m *MockFullNode) NetPubsubScores(arg0 context.Context) ([]api.PubsubScore, error) {
|
func (m *MockFullNode) NetPubsubScores(arg0 context.Context) ([]api.PubsubScore, error) {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
|
@ -593,6 +593,8 @@ type NetStruct struct {
|
|||||||
|
|
||||||
NetPeers func(p0 context.Context) ([]peer.AddrInfo, error) `perm:"read"`
|
NetPeers func(p0 context.Context) ([]peer.AddrInfo, error) `perm:"read"`
|
||||||
|
|
||||||
|
NetPing func(p0 context.Context, p1 peer.ID) (time.Duration, error) `perm:"read"`
|
||||||
|
|
||||||
NetPubsubScores func(p0 context.Context) ([]PubsubScore, error) `perm:"read"`
|
NetPubsubScores func(p0 context.Context) ([]PubsubScore, error) `perm:"read"`
|
||||||
|
|
||||||
NetSetLimit func(p0 context.Context, p1 string, p2 NetLimit) error `perm:"admin"`
|
NetSetLimit func(p0 context.Context, p1 string, p2 NetLimit) error `perm:"admin"`
|
||||||
@ -3670,6 +3672,17 @@ func (s *NetStub) NetPeers(p0 context.Context) ([]peer.AddrInfo, error) {
|
|||||||
return *new([]peer.AddrInfo), ErrNotSupported
|
return *new([]peer.AddrInfo), ErrNotSupported
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *NetStruct) NetPing(p0 context.Context, p1 peer.ID) (time.Duration, error) {
|
||||||
|
if s.Internal.NetPing == nil {
|
||||||
|
return *new(time.Duration), ErrNotSupported
|
||||||
|
}
|
||||||
|
return s.Internal.NetPing(p0, p1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *NetStub) NetPing(p0 context.Context, p1 peer.ID) (time.Duration, error) {
|
||||||
|
return *new(time.Duration), ErrNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
func (s *NetStruct) NetPubsubScores(p0 context.Context) ([]PubsubScore, error) {
|
func (s *NetStruct) NetPubsubScores(p0 context.Context) ([]PubsubScore, error) {
|
||||||
if s.Internal.NetPubsubScores == nil {
|
if s.Internal.NetPubsubScores == nil {
|
||||||
return *new([]PubsubScore), ErrNotSupported
|
return *new([]PubsubScore), ErrNotSupported
|
||||||
|
@ -7,6 +7,7 @@ package v0mocks
|
|||||||
import (
|
import (
|
||||||
context "context"
|
context "context"
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
|
time "time"
|
||||||
|
|
||||||
address "github.com/filecoin-project/go-address"
|
address "github.com/filecoin-project/go-address"
|
||||||
bitfield "github.com/filecoin-project/go-bitfield"
|
bitfield "github.com/filecoin-project/go-bitfield"
|
||||||
@ -1769,6 +1770,21 @@ func (mr *MockFullNodeMockRecorder) NetPeers(arg0 interface{}) *gomock.Call {
|
|||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetPeers", reflect.TypeOf((*MockFullNode)(nil).NetPeers), arg0)
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetPeers", reflect.TypeOf((*MockFullNode)(nil).NetPeers), arg0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetPing mocks base method.
|
||||||
|
func (m *MockFullNode) NetPing(arg0 context.Context, arg1 peer.ID) (time.Duration, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "NetPing", arg0, arg1)
|
||||||
|
ret0, _ := ret[0].(time.Duration)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// NetPing indicates an expected call of NetPing.
|
||||||
|
func (mr *MockFullNodeMockRecorder) NetPing(arg0, arg1 interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NetPing", reflect.TypeOf((*MockFullNode)(nil).NetPing), arg0, arg1)
|
||||||
|
}
|
||||||
|
|
||||||
// NetPubsubScores mocks base method.
|
// NetPubsubScores mocks base method.
|
||||||
func (m *MockFullNode) NetPubsubScores(arg0 context.Context) ([]api.PubsubScore, error) {
|
func (m *MockFullNode) NetPubsubScores(arg0 context.Context) ([]api.PubsubScore, error) {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
43
cli/net.go
43
cli/net.go
@ -28,6 +28,7 @@ var NetCmd = &cli.Command{
|
|||||||
Usage: "Manage P2P Network",
|
Usage: "Manage P2P Network",
|
||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
NetPeers,
|
NetPeers,
|
||||||
|
NetPing,
|
||||||
NetConnect,
|
NetConnect,
|
||||||
NetListen,
|
NetListen,
|
||||||
NetId,
|
NetId,
|
||||||
@ -114,6 +115,48 @@ var NetPeers = &cli.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var NetPing = &cli.Command{
|
||||||
|
Name: "ping",
|
||||||
|
Usage: "Ping peers",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.IntFlag{
|
||||||
|
Name: "count",
|
||||||
|
Value: 10,
|
||||||
|
Aliases: []string{"c"},
|
||||||
|
Usage: "specify the number of times it should ping",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: func(cctx *cli.Context) error {
|
||||||
|
if cctx.Args().Len() != 1 {
|
||||||
|
return xerrors.Errorf("please provide a peerID")
|
||||||
|
}
|
||||||
|
|
||||||
|
api, closer, err := GetAPI(cctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer closer()
|
||||||
|
|
||||||
|
ctx := ReqContext(cctx)
|
||||||
|
|
||||||
|
peerID, err := peer.Decode(cctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return xerrors.Errorf("failed to parse peerID: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
count := cctx.Int("count")
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
RTT, err := api.NetPing(ctx, peerID)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("ping failed: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Printf("ping %d, %v\n", i, RTT)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var NetScores = &cli.Command{
|
var NetScores = &cli.Command{
|
||||||
Name: "scores",
|
Name: "scores",
|
||||||
Usage: "Print peers' pubsub scores",
|
Usage: "Print peers' pubsub scores",
|
||||||
|
@ -84,6 +84,7 @@
|
|||||||
* [NetLimit](#NetLimit)
|
* [NetLimit](#NetLimit)
|
||||||
* [NetPeerInfo](#NetPeerInfo)
|
* [NetPeerInfo](#NetPeerInfo)
|
||||||
* [NetPeers](#NetPeers)
|
* [NetPeers](#NetPeers)
|
||||||
|
* [NetPing](#NetPing)
|
||||||
* [NetPubsubScores](#NetPubsubScores)
|
* [NetPubsubScores](#NetPubsubScores)
|
||||||
* [NetSetLimit](#NetSetLimit)
|
* [NetSetLimit](#NetSetLimit)
|
||||||
* [NetStat](#NetStat)
|
* [NetStat](#NetStat)
|
||||||
@ -1784,6 +1785,20 @@ Response:
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### NetPing
|
||||||
|
|
||||||
|
|
||||||
|
Perms: read
|
||||||
|
|
||||||
|
Inputs:
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
Response: `60000000000`
|
||||||
|
|
||||||
### NetPubsubScores
|
### NetPubsubScores
|
||||||
|
|
||||||
|
|
||||||
|
@ -131,6 +131,7 @@
|
|||||||
* [NetLimit](#NetLimit)
|
* [NetLimit](#NetLimit)
|
||||||
* [NetPeerInfo](#NetPeerInfo)
|
* [NetPeerInfo](#NetPeerInfo)
|
||||||
* [NetPeers](#NetPeers)
|
* [NetPeers](#NetPeers)
|
||||||
|
* [NetPing](#NetPing)
|
||||||
* [NetPubsubScores](#NetPubsubScores)
|
* [NetPubsubScores](#NetPubsubScores)
|
||||||
* [NetSetLimit](#NetSetLimit)
|
* [NetSetLimit](#NetSetLimit)
|
||||||
* [NetStat](#NetStat)
|
* [NetStat](#NetStat)
|
||||||
@ -3905,6 +3906,20 @@ Response:
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### NetPing
|
||||||
|
|
||||||
|
|
||||||
|
Perms: read
|
||||||
|
|
||||||
|
Inputs:
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
Response: `60000000000`
|
||||||
|
|
||||||
### NetPubsubScores
|
### NetPubsubScores
|
||||||
|
|
||||||
|
|
||||||
|
@ -137,6 +137,7 @@
|
|||||||
* [NetLimit](#NetLimit)
|
* [NetLimit](#NetLimit)
|
||||||
* [NetPeerInfo](#NetPeerInfo)
|
* [NetPeerInfo](#NetPeerInfo)
|
||||||
* [NetPeers](#NetPeers)
|
* [NetPeers](#NetPeers)
|
||||||
|
* [NetPing](#NetPing)
|
||||||
* [NetPubsubScores](#NetPubsubScores)
|
* [NetPubsubScores](#NetPubsubScores)
|
||||||
* [NetSetLimit](#NetSetLimit)
|
* [NetSetLimit](#NetSetLimit)
|
||||||
* [NetStat](#NetStat)
|
* [NetStat](#NetStat)
|
||||||
@ -4266,6 +4267,20 @@ Response:
|
|||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### NetPing
|
||||||
|
|
||||||
|
|
||||||
|
Perms: read
|
||||||
|
|
||||||
|
Inputs:
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
Response: `60000000000`
|
||||||
|
|
||||||
### NetPubsubScores
|
### NetPubsubScores
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
|
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
swarm "github.com/libp2p/go-libp2p-swarm"
|
swarm "github.com/libp2p/go-libp2p-swarm"
|
||||||
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
||||||
"github.com/libp2p/go-libp2p/p2p/net/conngater"
|
"github.com/libp2p/go-libp2p/p2p/net/conngater"
|
||||||
|
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
|
|
||||||
"github.com/filecoin-project/lotus/api"
|
"github.com/filecoin-project/lotus/api"
|
||||||
@ -177,6 +179,13 @@ func (a *NetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]metric
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *NetAPI) NetPing(ctx context.Context, p peer.ID) (time.Duration, error) {
|
||||||
|
pingService := ping.NewPingService(a.Host)
|
||||||
|
|
||||||
|
result := <-pingService.Ping(ctx, p)
|
||||||
|
return result.RTT, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
func (a *NetAPI) NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) {
|
func (a *NetAPI) NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) {
|
||||||
return a.Reporter.GetBandwidthByProtocol(), nil
|
return a.Reporter.GetBandwidthByProtocol(), nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user