Skip to content

Commit

Permalink
feat: add stats cmd to stats power
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao committed Aug 29, 2022
1 parent f875083 commit b4b680b
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
175 changes: 175 additions & 0 deletions cli/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package cli

import (
"bytes"
"fmt"
"log"
"os"
"strings"
"sync"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/power"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/urfave/cli/v2"
)

var StatsCmd = &cli.Command{
Name: "stats",
Description: "Statistics on how many SPs are running Venus",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Usage: "verbose output",
Aliases: []string{"v", "debug"},
},
},
Action: func(cctx *cli.Context) error {
ctx := ReqContext(cctx)
if cctx.Bool("verbose") {
log.SetOutput(os.Stdout)
} else {
log.SetOutput(bytes.NewBuffer(nil))
}

api, acloser, err := NewFullNode(cctx)
if err != nil {
return fmt.Errorf("setting up venus node connection: %w", err)
}
defer acloser()

miners, err := api.StateListMiners(ctx, types.EmptyTSK)
if err != nil {
return err
}

log.Println("Total SPs on chain: ", len(miners))

var wg sync.WaitGroup
wg.Add(len(miners))
var lk sync.Mutex
var withMinPower []address.Address
minerToMinerPower := make(map[address.Address]power.Claim)
minerToTotalPower := make(map[address.Address]power.Claim)

throttle := make(chan struct{}, 50)
for _, miner := range miners {
throttle <- struct{}{}
go func(miner address.Address) {
defer wg.Done()
defer func() {
<-throttle
}()

power, err := api.StateMinerPower(ctx, miner, types.EmptyTSK)
if err != nil {
panic(err)
}

if power.HasMinPower {
lk.Lock()
withMinPower = append(withMinPower, miner)
minerToMinerPower[miner] = power.MinerPower
minerToTotalPower[miner] = power.TotalPower
lk.Unlock()
}
}(miner)
}

wg.Wait()

log.Println("Total SPs with minimum power: ", len(withMinPower))

var venusNodes int

RawBytePower := big.NewInt(0)
QualityAdjPower := big.NewInt(0)

host, err := libp2p.New(libp2p.NoListenAddrs)
if err != nil {
return err
}

for _, maddr := range withMinPower {
select {
case <-ctx.Done():
return nil
default:
}
err := func() error {

log.Println("Checking SP: ", maddr)

minfo, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK)
if err != nil {
return err
}
if minfo.PeerId == nil {
return fmt.Errorf("storage provider %s has no peer ID set on-chain", maddr)
}

var maddrs []multiaddr.Multiaddr
for _, mma := range minfo.Multiaddrs {
ma, err := multiaddr.NewMultiaddrBytes(mma)
if err != nil {
return fmt.Errorf("storage provider %s had invalid multiaddrs in their info: %w", maddr, err)
}
maddrs = append(maddrs, ma)
}
if len(maddrs) == 0 {
return fmt.Errorf("storage provider %s has no multiaddrs set on-chain", maddr)
}

addrInfo := peer.AddrInfo{
ID: *minfo.PeerId,
Addrs: maddrs,
}

if err := host.Connect(ctx, addrInfo); err != nil {
return fmt.Errorf("connecting to peer %s: %w", addrInfo.ID, err)
}

userAgentI, err := host.Peerstore().Get(addrInfo.ID, "AgentVersion")
if err != nil {
return fmt.Errorf("getting user agent for peer %s: %w", addrInfo.ID, err)
}

userAgent, ok := userAgentI.(string)
if !ok {
return fmt.Errorf("user agent for peer %s was not a string", addrInfo.ID)
}
log.Println("User agent: ", userAgent)

if strings.Contains(userAgent, "venus") {

log.Println("Provider %s is running venus" + maddr.String())
log.Println("venus provider ", maddr.String(), "raw power:", minerToMinerPower[maddr].RawBytePower)
log.Println("venus provider ", maddr.String(), "quality adj power:", minerToMinerPower[maddr].QualityAdjPower)

venusNodes++
QualityAdjPower = big.Add(QualityAdjPower, minerToMinerPower[maddr].QualityAdjPower)
RawBytePower = big.Add(RawBytePower, minerToMinerPower[maddr].RawBytePower)
}
log.Println()

return nil
}()
if err != nil {
log.Println("Error: ", err)
continue
}
}

fmt.Println()
fmt.Println("Total venus nodes:", venusNodes)
fmt.Println("Total venus raw power:", types.DeciStr(RawBytePower))
fmt.Println("Total venus quality adj power:", types.DeciStr(QualityAdjPower))
fmt.Println("Total SPs with minimum power: ", len(withMinPower))

return nil
},
}
1 change: 1 addition & 0 deletions cmd/venus-market/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func main() {
cli2.MigrateCmd,
cli2.PieceStorageCmd,
cli2.MarketCmds,
cli2.StatsCmd,
},
}

Expand Down
2 changes: 1 addition & 1 deletion network/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Host(mctx metrics.MetricsCtx, lc fx.Lifecycle, params P2PHostIn) (host.Host
libp2p.Peerstore(params.Peerstore),
libp2p.NoListenAddrs,
libp2p.Ping(true),
libp2p.UserAgent("venus-" + version.UserVersion()),
libp2p.UserAgent("venus-market" + version.UserVersion()),
}
for _, o := range params.Opts {
opts = append(opts, o...)
Expand Down

0 comments on commit b4b680b

Please sign in to comment.