ERROR = "error"
)
+// Response describes the response standard.
type Response struct {
Status string
Msg string
Data []string
}
+// DefaultRawResponse is used as the default response when fail to get data.
var DefaultRawResponse = []byte(`{"Status":"error","Msg":"Unable to get data","Data":null}`)
//BlockchainReactor handles long-term catchup syncing.
evsw types.EventSwitch
}
-const (
- SUCCESS = "success"
- FAIL = "fail"
- ERROR = "error"
-)
-
-// DefaultRawResponse is used as the default response when fail to get data
-var DefaultRawResponse = []byte(`{"Status":"error","Msg":"Unable to get data","Data":null}`)
-
-// Response describes the response standard.
-type Response struct {
- Status string `json:"status"`
- Msg string `json:"msg"`
- Data []string `json:"data"`
-}
-
func batchRecover(ctx context.Context, v *interface{}) {
if r := recover(); r != nil {
var err error
return nil
}
-func (bcr *BlockchainReactor) isNetListening() bool {
- return bcr.sw.IsListening()
+func (bcr *BlockchainReactor) isNetListening() []byte {
+ data := []string{strconv.FormatBool(bcr.sw.IsListening())}
+ return resWrapper(data)
}
-func (bcr *BlockchainReactor) peerCount() int {
- return len(bcr.sw.Peers().List())
+func (bcr *BlockchainReactor) peerCount() []byte {
+ // TODO: use key-value instead of bare value
+ data := []string{strconv.FormatInt(int64(len(bcr.sw.Peers().List())), 16)}
+ return resWrapper(data)
}
-func (bcr *BlockchainReactor) isNetSyncing() bool {
- return bcr.blockKeeper.IsCaughtUp()
+func (bcr *BlockchainReactor) isNetSyncing() []byte {
+ data := []string{strconv.FormatBool(bcr.blockKeeper.IsCaughtUp())}
+ return resWrapper(data)
}
func (bcr *BlockchainReactor) getBlockTransactionsCountByHeight(height uint64) []byte {
import (
"context"
+ "encoding/json"
+ "strconv"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
+
+ "github.com/bytom/blockchain"
)
var netInfoCmd = &cobra.Command{
jww.FEEDBACK.Printf("net info: %v\n", response)
},
}
-
var netListeningCmd = &cobra.Command{
Use: "net-listening",
Short: "If client is actively listening for network connections",
Run: func(cmd *cobra.Command, args []string) {
- var response interface{}
+ var rawResponse []byte
+ var response blockchain.Response
+
client := mustRPCClient()
- client.Call(context.Background(), "/net-listening", nil, &response)
- jww.FEEDBACK.Printf("net listening: %v\n", response)
+ client.Call(context.Background(), "/net-listening", nil, &rawResponse)
+
+ if err := json.Unmarshal(rawResponse, &response); err != nil {
+ jww.ERROR.Println(err)
+ return
+ }
+
+ // TODO: code reuse
+ if response.Status == blockchain.SUCCESS {
+ data := response.Data
+ res, err := strconv.ParseBool(data[0])
+ if err != nil {
+ jww.ERROR.Println("Fail to parse response data")
+ return
+ }
+ jww.FEEDBACK.Printf("net listening: %v\n", res)
+ return
+ }
+ jww.ERROR.Println(response.Msg)
+
},
}
Use: "peer-count",
Short: "Number of peers currently connected to the client",
Run: func(cmd *cobra.Command, args []string) {
- var response interface{}
+ var rawResponse []byte
+ var response blockchain.Response
+
client := mustRPCClient()
- client.Call(context.Background(), "/peer-count", nil, &response)
- jww.FEEDBACK.Printf("peer count: %v\n", response)
+ client.Call(context.Background(), "/peer-count", nil, &rawResponse)
+
+ if err := json.Unmarshal(rawResponse, &response); err != nil {
+ jww.ERROR.Println(err)
+ return
+ }
+
+ if response.Status == blockchain.SUCCESS {
+ data := response.Data
+ i, err := strconv.ParseInt(data[0], 16, 64)
+ if err != nil {
+ jww.ERROR.Println("Fail to parse response data")
+ return
+ }
+ jww.FEEDBACK.Printf("peer count: %v\n", i)
+ return
+ }
+ jww.ERROR.Println(response.Msg)
+
},
}
Use: "net-syncing",
Short: "If the network is still syncing",
Run: func(cmd *cobra.Command, args []string) {
- var response interface{}
+ var rawResponse []byte
+ var response blockchain.Response
+
client := mustRPCClient()
- client.Call(context.Background(), "/net-syncing", nil, &response)
- jww.FEEDBACK.Printf("net syncing: %v\n", response)
+ client.Call(context.Background(), "/net-syncing", nil, &rawResponse)
+
+ if err := json.Unmarshal(rawResponse, &response); err != nil {
+ jww.ERROR.Println(err)
+ return
+ }
+
+ if response.Status == blockchain.SUCCESS {
+ data := response.Data
+ res, err := strconv.ParseBool(data[0])
+ if err != nil {
+ jww.ERROR.Println("Fail to parse response data")
+ return
+ }
+ jww.FEEDBACK.Printf("net syncing: %v\n", res)
+ return
+ }
+ jww.ERROR.Println(response.Msg)
},
}