|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "text/tabwriter" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/dustin/go-humanize" |
| 12 | + "github.com/filecoin-project/go-state-types/big" |
| 13 | + "github.com/filecoin-project/venus/app/node" |
| 14 | + v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1" |
| 15 | + "github.com/filecoin-project/venus/venus-shared/types" |
| 16 | + cmds "github.com/ipfs/go-ipfs-cmds" |
| 17 | +) |
| 18 | + |
| 19 | +var infoCmd = &cmds.Command{ |
| 20 | + Helptext: cmds.HelpText{ |
| 21 | + Tagline: "Print node info", |
| 22 | + }, |
| 23 | + Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error { |
| 24 | + ctx := req.Context |
| 25 | + chainapi := env.(*node.Env).ChainAPI |
| 26 | + buf := new(bytes.Buffer) |
| 27 | + writer := NewSilentWriter(buf) |
| 28 | + |
| 29 | + netParams, err := chainapi.StateGetNetworkParams(ctx) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + writer.Printf("Network: %s\n", netParams.NetworkName) |
| 34 | + |
| 35 | + if err := SyncBasefeeCheck(ctx, chainapi, int64(netParams.BlockDelaySecs), writer); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + status, err := env.(*node.Env).CommonAPI.NodeStatus(ctx, true) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + writer.Printf("Peers to: [publish messages %d] [publish blocks %d]\n", status.PeerStatus.PeersToPublishMsgs, |
| 43 | + status.PeerStatus.PeersToPublishBlocks) |
| 44 | + |
| 45 | + //Chain health calculated as percentage: amount of blocks in last finality / very healthy amount of blocks in a finality (900 epochs * 5 blocks per tipset) |
| 46 | + health := (100 * (900 * status.ChainStatus.BlocksPerTipsetLastFinality) / (900 * 5)) |
| 47 | + switch { |
| 48 | + case health > 85: |
| 49 | + writer.Printf("Chain health: %.f%% [healthy]\n", health) |
| 50 | + case health < 85: |
| 51 | + writer.Printf("Chain health: %.f%% [unhealthy]\n", health) |
| 52 | + } |
| 53 | + writer.Println() |
| 54 | + |
| 55 | + addr, err := env.(*node.Env).WalletAPI.WalletDefaultAddress(ctx) |
| 56 | + if err == nil && !addr.Empty() { |
| 57 | + fmt.Printf("Default address: \n") |
| 58 | + balance, err := env.(*node.Env).WalletAPI.WalletBalance(ctx, addr) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + writer.Printf(" %s [%s]\n", addr.String(), types.FIL(balance).Short()) |
| 63 | + } else { |
| 64 | + writer.Printf("Default address: address not set\n") |
| 65 | + } |
| 66 | + writer.Println() |
| 67 | + |
| 68 | + addrs := env.(*node.Env).WalletAPI.WalletAddresses(ctx) |
| 69 | + totalBalance := big.Zero() |
| 70 | + for _, addr := range addrs { |
| 71 | + totbal, err := env.(*node.Env).WalletAPI.WalletBalance(ctx, addr) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + totalBalance = big.Add(totalBalance, totbal) |
| 76 | + } |
| 77 | + writer.Printf("Wallet: %v address\n", len(addrs)) |
| 78 | + writer.Printf(" Total balance: %s\n", types.FIL(totalBalance).Short()) |
| 79 | + |
| 80 | + mbLockedSum := big.Zero() |
| 81 | + mbAvailableSum := big.Zero() |
| 82 | + for _, addr := range addrs { |
| 83 | + mbal, err := env.(*node.Env).ChainAPI.StateMarketBalance(ctx, addr, types.EmptyTSK) |
| 84 | + if err != nil { |
| 85 | + if strings.Contains(err.Error(), "actor not found") { |
| 86 | + continue |
| 87 | + } |
| 88 | + return err |
| 89 | + } |
| 90 | + mbLockedSum = big.Add(mbLockedSum, mbal.Locked) |
| 91 | + mbAvailableSum = big.Add(mbAvailableSum, mbal.Escrow) |
| 92 | + } |
| 93 | + writer.Printf(" Market locked: %s\n", types.FIL(mbLockedSum).Short()) |
| 94 | + writer.Printf(" Market available: %s\n", types.FIL(mbAvailableSum).Short()) |
| 95 | + writer.Println() |
| 96 | + |
| 97 | + chs, err := env.(*node.Env).PaychAPI.PaychList(ctx) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + writer.Printf("Payment Channels: %v channels\n", len(chs)) |
| 102 | + writer.Println() |
| 103 | + |
| 104 | + s, err := env.(*node.Env).NetworkAPI.NetBandwidthStats(ctx) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + tw := tabwriter.NewWriter(writer.w, 6, 6, 2, ' ', 0) |
| 109 | + writer.Printf("Bandwidth:\n") |
| 110 | + fmt.Fprintf(tw, "\tTotalIn\tTotalOut\tRateIn\tRateOut\n") |
| 111 | + fmt.Fprintf(tw, "\t%s\t%s\t%s/s\t%s/s\n", humanize.Bytes(uint64(s.TotalIn)), humanize.Bytes(uint64(s.TotalOut)), humanize.Bytes(uint64(s.RateIn)), humanize.Bytes(uint64(s.RateOut))) |
| 112 | + if err := tw.Flush(); err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + return re.Emit(buf) |
| 117 | + }, |
| 118 | +} |
| 119 | + |
| 120 | +func SyncBasefeeCheck(ctx context.Context, chainapi v1.IChain, blockDelaySecs int64, writer *SilentWriter) error { |
| 121 | + head, err := chainapi.ChainHead(ctx) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + var syncStatus string |
| 127 | + switch { |
| 128 | + case time.Now().Unix()-int64(head.MinTimestamp()) < blockDelaySecs*3/2: // within 1.5 epochs |
| 129 | + syncStatus = "[sync ok]" |
| 130 | + case time.Now().Unix()-int64(head.MinTimestamp()) < blockDelaySecs*5: // within 5 epochs |
| 131 | + syncStatus = fmt.Sprintf("[sync slow (%s behind)]", time.Since(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second)) |
| 132 | + default: |
| 133 | + syncStatus = fmt.Sprintf("[sync behind! (%s behind)]", time.Since(time.Unix(int64(head.MinTimestamp()), 0)).Truncate(time.Second)) |
| 134 | + } |
| 135 | + basefee := head.MinTicketBlock().ParentBaseFee |
| 136 | + |
| 137 | + writer.Printf("Chain: %s [basefee %s] [epoch %v]\n", syncStatus, types.FIL(basefee).Short(), head.Height()) |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments