From 58ce93a30812b3bc1fa8f73dc5eb862ce9f27842 Mon Sep 17 00:00:00 2001 From: hunjixin <1084400399@qq.com> Date: Mon, 24 Jul 2023 11:32:07 +0800 Subject: [PATCH] feat: add cmd to show overview state for semsher --- cmd/overview.go | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 11 +++- go.sum | 13 ++++ 3 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 cmd/overview.go diff --git a/cmd/overview.go b/cmd/overview.go new file mode 100644 index 0000000..ebf7064 --- /dev/null +++ b/cmd/overview.go @@ -0,0 +1,154 @@ +package cmd + +import ( + "context" + "fmt" + units "github.com/docker/go-units" + "github.com/golang/protobuf/ptypes/empty" + "github.com/olekukonko/tablewriter" + pb "github.com/spacemeshos/api/release/go/spacemesh/v1" + "github.com/spacemeshos/go-spacemesh/common/types" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/types/known/emptypb" + _ "net/http/pprof" + "os" + "strings" +) + +var overviewCmd = &cobra.Command{ + Use: "overview", + Short: "print overview of smesher", + Run: func(c *cobra.Command, args []string) { + ctx := context.Background() + if err := showOverviewStatus(ctx); err != nil { + fmt.Println(err.Error()) + } + }, +} + +func showOverviewStatus(ctx context.Context) error { + privateURL := resolveToLocalhost(viper.GetString("api.grpc-private-listener")) + publicURL := resolveToLocalhost(viper.GetString("api.grpc-public-listener")) + + overViewState, err := getOverViewState(ctx, publicURL, privateURL) + if err != nil { + return err + } + + percent := fmt.Sprintf("%.2f %%", 100*(float64(overViewState.CompletedSize)/float64(overViewState.CommitmentSize))) + commitStatus := fmt.Sprintf("%s / %s %s", overViewState.CompletedSize.String(), overViewState.CommitmentSize.String(), percent) + var syncStatus string + if overViewState.IsSynced { + syncStatus = fmt.Sprintf("Success Current(%d)GenesisEndLayer(%d)", overViewState.CurrentLayerId, overViewState.GenesisEndLayer) + } else { + syncStatus = fmt.Sprintf("Fail Current(%d) GenesisEndLayer(%d)", overViewState.CurrentLayerId, overViewState.GenesisEndLayer) + } + + tbl := tablewriter.NewWriter(os.Stdout) + tbl.Append([]string{"SmeshId", fmt.Sprintf("ID(0x%s) Addr(%s)", types.BytesToNodeID(overViewState.SmesherId).String(), types.GenerateAddress(overViewState.SmesherId).String())}) + tbl.Append([]string{"SmeshState", overViewState.State.String()}) + tbl.Append([]string{"CoinBase", overViewState.CoinBase}) + tbl.Append([]string{"GenesisId", overViewState.GenesisId.String()}) + tbl.Append([]string{"SyncStatus", syncStatus}) + tbl.Append([]string{"SmeshProgress", commitStatus}) + tbl.Render() + return nil +} + +func getOverViewState(ctx context.Context, publicURL, privateURL string) (*OverViewStatus, error) { + publicConn, err := grpc.Dial(publicURL, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, err + } + + privateConn, err := grpc.Dial(privateURL, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, err + } + + smesherClient := pb.NewSmesherServiceClient(privateConn) + status, err := smesherClient.PostSetupStatus(ctx, &empty.Empty{}) + if err != nil { + return nil, err + } + + postCfg, err := smesherClient.PostConfig(ctx, &empty.Empty{}) + if err != nil { + return nil, err + } + + smeshId, err := smesherClient.SmesherID(ctx, &emptypb.Empty{}) + if err != nil { + return nil, err + } + + coinBase, err := smesherClient.Coinbase(ctx, &empty.Empty{}) + if err != nil { + return nil, err + } + + nodeClient := pb.NewNodeServiceClient(publicConn) + nodeStatus, err := nodeClient.Status(ctx, &pb.StatusRequest{}) + if err != nil { + return nil, err + } + + nodeInfo, err := nodeClient.NodeInfo(ctx, &empty.Empty{}) + if err != nil { + return nil, err + } + + meshClient := pb.NewMeshServiceClient(publicConn) + genesisIDResp, err := meshClient.GenesisID(ctx, &pb.GenesisIDRequest{}) + if err != nil { + return nil, err + } + + commitmentSize := postCfg.LabelsPerUnit * uint64(postCfg.BitsPerLabel) * (uint64(status.GetStatus().GetOpts().NumUnits)) / 8 + completed := status.GetStatus().NumLabelsWritten * uint64(postCfg.BitsPerLabel) / 8 + + v := types.Hash20{} + copy(v[:], genesisIDResp.GenesisId) + return &OverViewStatus{ + CompletedSize: StorageSize(completed), + CommitmentSize: StorageSize(commitmentSize), + State: status.GetStatus().GetState(), + CoinBase: coinBase.AccountId.Address, + SmesherId: smeshId.PublicKey, + GenesisId: v, + IsSynced: nodeStatus.GetStatus().IsSynced, + CurrentLayerId: int(nodeStatus.GetStatus().TopLayer.Number), + GenesisEndLayer: int(nodeInfo.GetEffectiveGenesis()), + }, nil +} + +type StorageSize int64 + +func (s StorageSize) String() string { + return units.BytesSize(float64(s)) +} + +type OverViewStatus struct { + State pb.PostSetupStatus_State + CompletedSize StorageSize + CommitmentSize StorageSize + + CoinBase string + SmesherId []byte + GenesisId types.Hash20 + + IsSynced bool + CurrentLayerId int + GenesisEndLayer int +} + +func resolveToLocalhost(URL string) string { + return strings.ReplaceAll(URL, "0.0.0.0", "127.0.0.1") +} + +func init() { + rootCmd.AddCommand(overviewCmd) +} diff --git a/go.mod b/go.mod index 461f907..b65bfc4 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,17 @@ go 1.18 require ( github.com/btcsuite/btcutil v1.0.2 + github.com/docker/go-units v0.5.0 + github.com/golang/protobuf v1.5.3 github.com/jedib0t/go-pretty/v6 v6.4.6 + github.com/olekukonko/tablewriter v0.0.5 + github.com/spacemeshos/api/release/go v1.16.0 github.com/spacemeshos/economics v0.1.0 github.com/spacemeshos/go-spacemesh v1.0.2 github.com/spacemeshos/smkeys v1.0.4 github.com/stretchr/testify v1.8.4 + google.golang.org/grpc v1.56.2 + google.golang.org/protobuf v1.31.0 ) require ( @@ -18,9 +24,9 @@ require ( github.com/cosmos/btcutil v1.0.5 // indirect github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a // indirect github.com/golang/mock v1.6.0 // indirect - github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/google/uuid v1.3.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/minio/sha256-simd v1.0.1 // indirect @@ -40,9 +46,8 @@ require ( go.uber.org/zap v1.24.0 // indirect golang.org/x/exp v0.0.0-20230711153332-06a737ee72cb // indirect golang.org/x/net v0.12.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect - google.golang.org/grpc v1.56.2 // indirect - google.golang.org/protobuf v1.31.0 // indirect ) require ( diff --git a/go.sum b/go.sum index 45fb072..f1dd142 100644 --- a/go.sum +++ b/go.sum @@ -73,6 +73,8 @@ github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 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 github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a h1:2GgRlm6BrV7CIOjjE/o7WJs6foe33AqCQC8bnl1RJQc= github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a/go.mod h1:suaTfGNQ00ObHGOoHxPb8pkAki7jm0/ZkR2rcY9yF1s= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 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+ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg= github.com/hashicorp/go-secure-stdlib/password v0.1.2 h1:fcaMDeWE3a3PiCijEhRZaka7QxAN/AJwCAcQxg7MqBQ= github.com/hashicorp/go-secure-stdlib/password v0.1.2/go.mod h1:zO6IH1UOstJM0DZ/qzxCz2Jym+nkdvNtej4/3RpH+DQ= 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= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 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 github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce h1:/pEpMk55wH0X+E5zedGEMOdLuWmV8P4+4W3+LZaM6kg= github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 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 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spacemeshos/api/release/go v1.16.0 h1:DcRjnD+UBU4nx5TljxcPairHlI/wrbFX2VPUmWTarTs= +github.com/spacemeshos/api/release/go v1.16.0/go.mod h1:aSK7c2PUsle+EpC9VPAsPnsjKWDbo+NzT7kZgmzIZeM= github.com/spacemeshos/economics v0.1.0 h1:PJAKbhBKqbbdCYTB29pkmc8sYqK3pKUAiuAvQxuSJEg= github.com/spacemeshos/economics v0.1.0/go.mod h1:Bz0wRDwCOUP1A6w3cPW6iuUBGME8Tz48sIriYiohsBg= 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 google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 h1:Au6te5hbKUV8pIYWHqOUZ1pva5qK/rwbIhoXEUB9Lu8= +google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 h1:s5YSX+ZH5b5vS9rnpGymvIyMpLRJizowqDlOuyjXnTk= +google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 h1:bVf09lpb+OJbByTj913DRJioFFAjf/ZGxEz7MajTp2U= google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98/go.mod h1:TUfxEVdsvPg18p6AslUXFoLdpED4oBnGwyqk3dV1XzM= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=