support extended pubsub peer scores

This commit is contained in:
vyzo 2020-07-31 11:27:22 +03:00
parent a8e9a2fe92
commit d8ca29dd52
3 changed files with 26 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/filecoin-project/specs-actors/actors/builtin/miner" "github.com/filecoin-project/specs-actors/actors/builtin/miner"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
@ -40,7 +41,7 @@ type ObjStat struct {
type PubsubScore struct { type PubsubScore struct {
ID peer.ID ID peer.ID
Score float64 Score *pubsub.PeerScoreSnapshot
} }
type MinerInfo struct { type MinerInfo struct {

View File

@ -1,7 +1,9 @@
package cli package cli
import ( import (
"encoding/json"
"fmt" "fmt"
"os"
"sort" "sort"
"strings" "strings"
@ -55,6 +57,12 @@ var netPeers = &cli.Command{
var netScores = &cli.Command{ var netScores = &cli.Command{
Name: "scores", Name: "scores",
Usage: "Print peers' pubsub scores", Usage: "Print peers' pubsub scores",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "extended",
Usage: "print extended peer scores in json",
},
},
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
api, closer, err := GetAPI(cctx) api, closer, err := GetAPI(cctx)
if err != nil { if err != nil {
@ -67,8 +75,18 @@ var netScores = &cli.Command{
return err return err
} }
for _, peer := range scores { if cctx.Bool("extended") {
fmt.Printf("%s, %f\n", peer.ID, peer.Score) enc := json.NewEncoder(os.Stdout)
for _, peer := range scores {
err := enc.Encode(peer)
if err != nil {
return err
}
}
} else {
for _, peer := range scores {
fmt.Printf("%s, %f\n", peer.ID, peer.Score.Score)
}
} }
return nil return nil

View File

@ -4,20 +4,21 @@ import (
"sync" "sync"
peer "github.com/libp2p/go-libp2p-core/peer" peer "github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
) )
type ScoreKeeper struct { type ScoreKeeper struct {
lk sync.Mutex lk sync.Mutex
scores map[peer.ID]float64 scores map[peer.ID]*pubsub.PeerScoreSnapshot
} }
func (sk *ScoreKeeper) Update(scores map[peer.ID]float64) { func (sk *ScoreKeeper) Update(scores map[peer.ID]*pubsub.PeerScoreSnapshot) {
sk.lk.Lock() sk.lk.Lock()
sk.scores = scores sk.scores = scores
sk.lk.Unlock() sk.lk.Unlock()
} }
func (sk *ScoreKeeper) Get() map[peer.ID]float64 { func (sk *ScoreKeeper) Get() map[peer.ID]*pubsub.PeerScoreSnapshot {
sk.lk.Lock() sk.lk.Lock()
defer sk.lk.Unlock() defer sk.lk.Unlock()
return sk.scores return sk.scores