Merge branch 'feat/net-ping' of github.com:ychiaoli18/lotus into feat/net-ping

This commit is contained in:
Łukasz Magiera 2022-03-21 13:06:11 +01:00
commit 701d0a111e
15 changed files with 238 additions and 0 deletions

View File

@ -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

View File

@ -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)
}
// NetProtectAdd mocks base method. // NetProtectAdd mocks base method.
func (m *MockFullNode) NetProtectAdd(arg0 context.Context, arg1 []peer.ID) error { func (m *MockFullNode) NetProtectAdd(arg0 context.Context, arg1 []peer.ID) error {
m.ctrl.T.Helper() m.ctrl.T.Helper()

View File

@ -597,6 +597,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"`
NetProtectAdd func(p0 context.Context, p1 []peer.ID) error `perm:"admin"` NetProtectAdd func(p0 context.Context, p1 []peer.ID) error `perm:"admin"`
NetProtectList func(p0 context.Context) ([]peer.ID, error) `perm:"read"` NetProtectList func(p0 context.Context) ([]peer.ID, error) `perm:"read"`
@ -3708,6 +3710,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) NetProtectAdd(p0 context.Context, p1 []peer.ID) error { func (s *NetStruct) NetProtectAdd(p0 context.Context, p1 []peer.ID) error {
if s.Internal.NetProtectAdd == nil { if s.Internal.NetProtectAdd == nil {
return ErrNotSupported return ErrNotSupported

View File

@ -0,0 +1,64 @@
package main
import (
"encoding/gob"
"flag"
"fmt"
"os"
"path"
"reflect"
"github.com/golang/mock/mockgen/model"
pkg_ "github.com/filecoin-project/lotus/api/v0api"
)
var output = flag.String("output", "", "The output file name, or empty to use stdout.")
func main() {
flag.Parse()
its := []struct {
sym string
typ reflect.Type
}{
{"FullNode", reflect.TypeOf((*pkg_.FullNode)(nil)).Elem()},
}
pkg := &model.Package{
// NOTE: This behaves contrary to documented behaviour if the
// package name is not the final component of the import path.
// The reflect package doesn't expose the package name, though.
Name: path.Base("github.com/filecoin-project/lotus/api/v0api"),
}
for _, it := range its {
intf, err := model.InterfaceFromInterfaceType(it.typ)
if err != nil {
fmt.Fprintf(os.Stderr, "Reflection: %v\n", err)
os.Exit(1)
}
intf.Name = it.sym
pkg.Interfaces = append(pkg.Interfaces, intf)
}
outfile := os.Stdout
if len(*output) != 0 {
var err error
outfile, err = os.Create(*output)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open output file %q", *output)
}
defer func() {
if err := outfile.Close(); err != nil {
fmt.Fprintf(os.Stderr, "failed to close output file %q", *output)
os.Exit(1)
}
}()
}
if err := gob.NewEncoder(outfile).Encode(pkg); err != nil {
fmt.Fprintf(os.Stderr, "gob encode: %v\n", err)
os.Exit(1)
}
}

View File

@ -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)
}
// NetProtectAdd mocks base method. // NetProtectAdd mocks base method.
func (m *MockFullNode) NetProtectAdd(arg0 context.Context, arg1 []peer.ID) error { func (m *MockFullNode) NetProtectAdd(arg0 context.Context, arg1 []peer.ID) error {
m.ctrl.T.Helper() m.ctrl.T.Helper()

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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,
@ -117,6 +118,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",

View File

@ -88,6 +88,7 @@
* [NetLimit](#NetLimit) * [NetLimit](#NetLimit)
* [NetPeerInfo](#NetPeerInfo) * [NetPeerInfo](#NetPeerInfo)
* [NetPeers](#NetPeers) * [NetPeers](#NetPeers)
* [NetPing](#NetPing)
* [NetProtectAdd](#NetProtectAdd) * [NetProtectAdd](#NetProtectAdd)
* [NetProtectList](#NetProtectList) * [NetProtectList](#NetProtectList)
* [NetProtectRemove](#NetProtectRemove) * [NetProtectRemove](#NetProtectRemove)
@ -1851,6 +1852,20 @@ Response:
] ]
``` ```
### NetPing
Perms: read
Inputs:
```json
[
"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"
]
```
Response: `60000000000`
### NetProtectAdd ### NetProtectAdd

View File

@ -131,6 +131,7 @@
* [NetLimit](#NetLimit) * [NetLimit](#NetLimit)
* [NetPeerInfo](#NetPeerInfo) * [NetPeerInfo](#NetPeerInfo)
* [NetPeers](#NetPeers) * [NetPeers](#NetPeers)
* [NetPing](#NetPing)
* [NetProtectAdd](#NetProtectAdd) * [NetProtectAdd](#NetProtectAdd)
* [NetProtectList](#NetProtectList) * [NetProtectList](#NetProtectList)
* [NetProtectRemove](#NetProtectRemove) * [NetProtectRemove](#NetProtectRemove)
@ -3908,6 +3909,20 @@ Response:
] ]
``` ```
### NetPing
Perms: read
Inputs:
```json
[
"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"
]
```
Response: `60000000000`
### NetProtectAdd ### NetProtectAdd

View File

@ -137,6 +137,7 @@
* [NetLimit](#NetLimit) * [NetLimit](#NetLimit)
* [NetPeerInfo](#NetPeerInfo) * [NetPeerInfo](#NetPeerInfo)
* [NetPeers](#NetPeers) * [NetPeers](#NetPeers)
* [NetPing](#NetPing)
* [NetProtectAdd](#NetProtectAdd) * [NetProtectAdd](#NetProtectAdd)
* [NetProtectList](#NetProtectList) * [NetProtectList](#NetProtectList)
* [NetProtectRemove](#NetProtectRemove) * [NetProtectRemove](#NetProtectRemove)
@ -4270,6 +4271,20 @@ Response:
] ]
``` ```
### NetPing
Perms: read
Inputs:
```json
[
"12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf"
]
```
Response: `60000000000`
### NetProtectAdd ### NetProtectAdd

View File

@ -1200,6 +1200,7 @@ USAGE:
COMMANDS: COMMANDS:
peers Print peers peers Print peers
ping Ping peers
connect Connect to a peer connect Connect to a peer
listen List listen addresses listen List listen addresses
id Get node identity id Get node identity
@ -1235,6 +1236,20 @@ OPTIONS:
``` ```
### lotus-miner net ping
```
NAME:
lotus-miner net ping - Ping peers
USAGE:
lotus-miner net ping [command options] [arguments...]
OPTIONS:
--count value, -c value specify the number of times it should ping (default: 10)
--help, -h show help (default: false)
```
### lotus-miner net connect ### lotus-miner net connect
``` ```
NAME: NAME:

View File

@ -2602,6 +2602,7 @@ USAGE:
COMMANDS: COMMANDS:
peers Print peers peers Print peers
ping Ping peers
connect Connect to a peer connect Connect to a peer
listen List listen addresses listen List listen addresses
id Get node identity id Get node identity
@ -2637,6 +2638,20 @@ OPTIONS:
``` ```
### lotus net ping
```
NAME:
lotus net ping - Ping peers
USAGE:
lotus net ping [command options] [arguments...]
OPTIONS:
--count value, -c value specify the number of times it should ping (default: 10)
--help, -h show help (default: false)
```
### lotus net connect ### lotus net connect
``` ```
NAME: NAME:

View File

@ -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
} }