OSDN Git Service

Block height get block tx cnt by height (#121)
authorLiu-Cheng Xu <xuliuchengxlc@gmail.com>
Tue, 21 Nov 2017 10:47:54 +0000 (18:47 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 21 Nov 2017 10:47:54 +0000 (18:47 +0800)
* Add get-block-transactions-by-height rpc

* Add block-height

* Simplify get-best-block-hash

blockchain/reactor.go
cmd/cobra/commands/block.go
cmd/cobra/commands/bytomcli.go

index 42a2721..a223c5a 100755 (executable)
@@ -172,6 +172,8 @@ func (bcr *BlockchainReactor) BuildHander() {
        m.Handle("/net-syncing", jsonHandler(bcr.isNetSyncing))
        m.Handle("/peer-count", jsonHandler(bcr.peerCount))
        m.Handle("/get-block-by-height", jsonHandler(bcr.getBlockByHeight))
+       m.Handle("/get-block-transactions-count-by-height", jsonHandler(bcr.getBlockTransactionsCountByHeight))
+       m.Handle("/block-height", jsonHandler(bcr.getBlockHeight))
 
        latencyHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
                if l := latency(m, req); l != nil {
@@ -511,3 +513,16 @@ func (bcr *BlockchainReactor) peerCount() int {
 func (bcr *BlockchainReactor) isNetSyncing() bool {
        return bcr.blockKeeper.IsCaughtUp()
 }
+
+func (bcr *BlockchainReactor) getBlockTransactionsCountByHeight(height uint64) (int, error) {
+       legacyBlock, err := bcr.chain.GetBlockByHeight(height)
+       if err != nil {
+               log.WithField("error", err).Error("Fail to get block by hash")
+               return -1, err
+       }
+       return len(legacyBlock.Transactions), nil
+}
+
+func (bcr *BlockchainReactor) getBlockHeight() uint64 {
+       return bcr.chain.Height()
+}
index d9f86bb..c9cf706 100644 (file)
@@ -16,9 +16,9 @@ var (
        coreURL = env.String("BYTOM_URL", "http://localhost:1999")
 )
 
-var getBestBlockHashCmd = &cobra.Command{
-       Use:   "get-best-block-hash",
-       Short: "Get the most recent block hash",
+var blockHashCmd = &cobra.Command{
+       Use:   "block-hash",
+       Short: "Get the hash of most recent block",
        Run: func(cmd *cobra.Command, args []string) {
                var response interface{}
                client := mustRPCClient()
@@ -27,6 +27,17 @@ var getBestBlockHashCmd = &cobra.Command{
        },
 }
 
+var blockHeightCmd = &cobra.Command{
+       Use:   "block-height",
+       Short: "Get the number of most recent block",
+       Run: func(cmd *cobra.Command, args []string) {
+               var response interface{}
+               client := mustRPCClient()
+               client.Call(context.Background(), "/block-height", nil, &response)
+               jww.FEEDBACK.Printf("block height: %v\n", response)
+       },
+}
+
 var getBlockByHashCmd = &cobra.Command{
        Use:   "get-block-by-hash",
        Short: "Get a whole block matching the given hash",
@@ -94,3 +105,24 @@ var getBlockByHeightCmd = &cobra.Command{
                jww.FEEDBACK.Printf("%v\n", response)
        },
 }
+
+var getBlockTransactionsCountByHeightCmd = &cobra.Command{
+       Use:   "get-block-transactions-count-by-height",
+       Short: "Get the transactions count of a block matching the given height",
+       Run: func(cmd *cobra.Command, args []string) {
+               if len(args) != 1 {
+                       jww.ERROR.Println("get-block-header-by-height args not valid\nUsage: get-block-transactions-count-by-height [height]")
+                       return
+               }
+
+               ui64, err := strconv.ParseUint(args[0], 10, 64)
+               if err != nil {
+                       jww.ERROR.Printf("Invalid height value")
+                       return
+               }
+               var response interface{}
+               client := mustRPCClient()
+               client.Call(context.Background(), "/get-block-transactions-count-by-height", ui64, &response)
+               jww.FEEDBACK.Printf("transactions count: %v\n", response)
+       },
+}
index 5632b3a..a0d8e16 100644 (file)
@@ -93,11 +93,13 @@ func AddCommands() {
        BytomcliCmd.AddCommand(bindAssetCmd)
        BytomcliCmd.AddCommand(listAssetsCmd)
 
-       BytomcliCmd.AddCommand(getBestBlockHashCmd)
+       BytomcliCmd.AddCommand(blockHeightCmd)
+       BytomcliCmd.AddCommand(blockHashCmd)
        BytomcliCmd.AddCommand(getBlockByHashCmd)
        BytomcliCmd.AddCommand(getBlockHeaderByHashCmd)
        BytomcliCmd.AddCommand(getBlockTransactionsCountByHashCmd)
        BytomcliCmd.AddCommand(getBlockByHeightCmd)
+       BytomcliCmd.AddCommand(getBlockTransactionsCountByHeightCmd)
 
        BytomcliCmd.AddCommand(createKeyCmd)
        BytomcliCmd.AddCommand(deleteKeyCmd)