Skip to content

Commit 58ce93a

Browse files
committed
feat: add cmd to show overview state for semsher
1 parent a1e889a commit 58ce93a

3 files changed

Lines changed: 175 additions & 3 deletions

File tree

cmd/overview.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
units "github.com/docker/go-units"
7+
"github.com/golang/protobuf/ptypes/empty"
8+
"github.com/olekukonko/tablewriter"
9+
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
10+
"github.com/spacemeshos/go-spacemesh/common/types"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
13+
"google.golang.org/grpc"
14+
"google.golang.org/grpc/credentials/insecure"
15+
"google.golang.org/protobuf/types/known/emptypb"
16+
_ "net/http/pprof"
17+
"os"
18+
"strings"
19+
)
20+
21+
var overviewCmd = &cobra.Command{
22+
Use: "overview",
23+
Short: "print overview of smesher",
24+
Run: func(c *cobra.Command, args []string) {
25+
ctx := context.Background()
26+
if err := showOverviewStatus(ctx); err != nil {
27+
fmt.Println(err.Error())
28+
}
29+
},
30+
}
31+
32+
func showOverviewStatus(ctx context.Context) error {
33+
privateURL := resolveToLocalhost(viper.GetString("api.grpc-private-listener"))
34+
publicURL := resolveToLocalhost(viper.GetString("api.grpc-public-listener"))
35+
36+
overViewState, err := getOverViewState(ctx, publicURL, privateURL)
37+
if err != nil {
38+
return err
39+
}
40+
41+
percent := fmt.Sprintf("%.2f %%", 100*(float64(overViewState.CompletedSize)/float64(overViewState.CommitmentSize)))
42+
commitStatus := fmt.Sprintf("%s / %s %s", overViewState.CompletedSize.String(), overViewState.CommitmentSize.String(), percent)
43+
var syncStatus string
44+
if overViewState.IsSynced {
45+
syncStatus = fmt.Sprintf("Success Current(%d)GenesisEndLayer(%d)", overViewState.CurrentLayerId, overViewState.GenesisEndLayer)
46+
} else {
47+
syncStatus = fmt.Sprintf("Fail Current(%d) GenesisEndLayer(%d)", overViewState.CurrentLayerId, overViewState.GenesisEndLayer)
48+
}
49+
50+
tbl := tablewriter.NewWriter(os.Stdout)
51+
tbl.Append([]string{"SmeshId", fmt.Sprintf("ID(0x%s) Addr(%s)", types.BytesToNodeID(overViewState.SmesherId).String(), types.GenerateAddress(overViewState.SmesherId).String())})
52+
tbl.Append([]string{"SmeshState", overViewState.State.String()})
53+
tbl.Append([]string{"CoinBase", overViewState.CoinBase})
54+
tbl.Append([]string{"GenesisId", overViewState.GenesisId.String()})
55+
tbl.Append([]string{"SyncStatus", syncStatus})
56+
tbl.Append([]string{"SmeshProgress", commitStatus})
57+
tbl.Render()
58+
return nil
59+
}
60+
61+
func getOverViewState(ctx context.Context, publicURL, privateURL string) (*OverViewStatus, error) {
62+
publicConn, err := grpc.Dial(publicURL, grpc.WithTransportCredentials(insecure.NewCredentials()))
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
privateConn, err := grpc.Dial(privateURL, grpc.WithTransportCredentials(insecure.NewCredentials()))
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
smesherClient := pb.NewSmesherServiceClient(privateConn)
73+
status, err := smesherClient.PostSetupStatus(ctx, &empty.Empty{})
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
postCfg, err := smesherClient.PostConfig(ctx, &empty.Empty{})
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
smeshId, err := smesherClient.SmesherID(ctx, &emptypb.Empty{})
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
coinBase, err := smesherClient.Coinbase(ctx, &empty.Empty{})
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
nodeClient := pb.NewNodeServiceClient(publicConn)
94+
nodeStatus, err := nodeClient.Status(ctx, &pb.StatusRequest{})
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
nodeInfo, err := nodeClient.NodeInfo(ctx, &empty.Empty{})
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
meshClient := pb.NewMeshServiceClient(publicConn)
105+
genesisIDResp, err := meshClient.GenesisID(ctx, &pb.GenesisIDRequest{})
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
commitmentSize := postCfg.LabelsPerUnit * uint64(postCfg.BitsPerLabel) * (uint64(status.GetStatus().GetOpts().NumUnits)) / 8
111+
completed := status.GetStatus().NumLabelsWritten * uint64(postCfg.BitsPerLabel) / 8
112+
113+
v := types.Hash20{}
114+
copy(v[:], genesisIDResp.GenesisId)
115+
return &OverViewStatus{
116+
CompletedSize: StorageSize(completed),
117+
CommitmentSize: StorageSize(commitmentSize),
118+
State: status.GetStatus().GetState(),
119+
CoinBase: coinBase.AccountId.Address,
120+
SmesherId: smeshId.PublicKey,
121+
GenesisId: v,
122+
IsSynced: nodeStatus.GetStatus().IsSynced,
123+
CurrentLayerId: int(nodeStatus.GetStatus().TopLayer.Number),
124+
GenesisEndLayer: int(nodeInfo.GetEffectiveGenesis()),
125+
}, nil
126+
}
127+
128+
type StorageSize int64
129+
130+
func (s StorageSize) String() string {
131+
return units.BytesSize(float64(s))
132+
}
133+
134+
type OverViewStatus struct {
135+
State pb.PostSetupStatus_State
136+
CompletedSize StorageSize
137+
CommitmentSize StorageSize
138+
139+
CoinBase string
140+
SmesherId []byte
141+
GenesisId types.Hash20
142+
143+
IsSynced bool
144+
CurrentLayerId int
145+
GenesisEndLayer int
146+
}
147+
148+
func resolveToLocalhost(URL string) string {
149+
return strings.ReplaceAll(URL, "0.0.0.0", "127.0.0.1")
150+
}
151+
152+
func init() {
153+
rootCmd.AddCommand(overviewCmd)
154+
}

go.mod

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ go 1.18
44

55
require (
66
github.com/btcsuite/btcutil v1.0.2
7+
github.com/docker/go-units v0.5.0
8+
github.com/golang/protobuf v1.5.3
79
github.com/jedib0t/go-pretty/v6 v6.4.6
10+
github.com/olekukonko/tablewriter v0.0.5
11+
github.com/spacemeshos/api/release/go v1.16.0
812
github.com/spacemeshos/economics v0.1.0
913
github.com/spacemeshos/go-spacemesh v1.0.2
1014
github.com/spacemeshos/smkeys v1.0.4
1115
github.com/stretchr/testify v1.8.4
16+
google.golang.org/grpc v1.56.2
17+
google.golang.org/protobuf v1.31.0
1218
)
1319

1420
require (
@@ -18,9 +24,9 @@ require (
1824
github.com/cosmos/btcutil v1.0.5 // indirect
1925
github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a // indirect
2026
github.com/golang/mock v1.6.0 // indirect
21-
github.com/golang/protobuf v1.5.3 // indirect
2227
github.com/google/go-cmp v0.5.9 // indirect
2328
github.com/google/uuid v1.3.0 // indirect
29+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
2430
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
2531
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
2632
github.com/minio/sha256-simd v1.0.1 // indirect
@@ -40,9 +46,8 @@ require (
4046
go.uber.org/zap v1.24.0 // indirect
4147
golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb // indirect
4248
golang.org/x/net v0.12.0 // indirect
49+
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 // indirect
4350
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
44-
google.golang.org/grpc v1.56.2 // indirect
45-
google.golang.org/protobuf v1.31.0 // indirect
4651
)
4752

4853
require (

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2
7373
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7474
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7575
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
76+
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
77+
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
7678
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
7779
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
7880
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -89,6 +91,7 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2
8991
github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a h1:2GgRlm6BrV7CIOjjE/o7WJs6foe33AqCQC8bnl1RJQc=
9092
github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a/go.mod h1:suaTfGNQ00ObHGOoHxPb8pkAki7jm0/ZkR2rcY9yF1s=
9193
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
94+
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
9295
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
9396
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
9497
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -153,6 +156,8 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
153156
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
154157
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
155158
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
159+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms=
160+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
156161
github.com/hashicorp/go-secure-stdlib/password v0.1.2 h1:fcaMDeWE3a3PiCijEhRZaka7QxAN/AJwCAcQxg7MqBQ=
157162
github.com/hashicorp/go-secure-stdlib/password v0.1.2/go.mod h1:zO6IH1UOstJM0DZ/qzxCz2Jym+nkdvNtej4/3RpH+DQ=
158163
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -184,6 +189,7 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
184189
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
185190
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
186191
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
192+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
187193
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
188194
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
189195
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
@@ -195,6 +201,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
195201
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
196202
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce h1:/pEpMk55wH0X+E5zedGEMOdLuWmV8P4+4W3+LZaM6kg=
197203
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
204+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
205+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
198206
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
199207
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
200208
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
@@ -221,6 +229,8 @@ github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
221229
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
222230
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
223231
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
232+
github.com/spacemeshos/api/release/go v1.16.0 h1:DcRjnD+UBU4nx5TljxcPairHlI/wrbFX2VPUmWTarTs=
233+
github.com/spacemeshos/api/release/go v1.16.0/go.mod h1:aSK7c2PUsle+EpC9VPAsPnsjKWDbo+NzT7kZgmzIZeM=
224234
github.com/spacemeshos/economics v0.1.0 h1:PJAKbhBKqbbdCYTB29pkmc8sYqK3pKUAiuAvQxuSJEg=
225235
github.com/spacemeshos/economics v0.1.0/go.mod h1:Bz0wRDwCOUP1A6w3cPW6iuUBGME8Tz48sIriYiohsBg=
226236
github.com/spacemeshos/go-scale v1.1.10 h1:wOfUR6l2KzAu+m/KU0JE7iopTrczvFgI21ZNpIET3Dw=
@@ -588,6 +598,9 @@ google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6D
588598
google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
589599
google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
590600
google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
601+
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 h1:Au6te5hbKUV8pIYWHqOUZ1pva5qK/rwbIhoXEUB9Lu8=
602+
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 h1:s5YSX+ZH5b5vS9rnpGymvIyMpLRJizowqDlOuyjXnTk=
603+
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig=
591604
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U=
592605
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM=
593606
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=

0 commit comments

Comments
 (0)