From 1b07bef6dcb8462e797b03f0d4a3b689dac742e1 Mon Sep 17 00:00:00 2001 From: oysheng Date: Wed, 25 Apr 2018 19:51:35 +0800 Subject: [PATCH] add get-diffculty add get-hash-rate --- api/api.go | 11 ++++++---- api/block_retrieve.go | 42 +++++++++++++++++++++++++++++++++++++++ cmd/bytomcli/commands/block.go | 26 ++++++++++++++++++++++++ cmd/bytomcli/commands/bytomcli.go | 2 ++ 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/api/api.go b/api/api.go index df15b37e..0a331e1b 100644 --- a/api/api.go +++ b/api/api.go @@ -205,8 +205,6 @@ func (a *API) buildHandler() { m.Handle("/", alwaysError(errors.New("not Found"))) m.Handle("/error", jsonHandler(a.walletError)) - m.Handle("/net-info", jsonHandler(a.getNetInfo)) - m.Handle("/create-access-token", jsonHandler(a.createAccessToken)) m.Handle("/list-access-tokens", jsonHandler(a.listAccessTokens)) m.Handle("/delete-access-token", jsonHandler(a.deleteAccessToken)) @@ -225,12 +223,17 @@ func (a *API) buildHandler() { m.Handle("/get-block-header", jsonHandler(a.getBlockHeader)) m.Handle("/get-block", jsonHandler(a.getBlock)) m.Handle("/get-block-count", jsonHandler(a.getBlockCount)) + m.Handle("/get-difficulty", jsonHandler(a.getDifficulty)) + m.Handle("/get-hash-rate", jsonHandler(a.getHashRate)) m.Handle("/is-mining", jsonHandler(a.isMining)) - m.Handle("/gas-rate", jsonHandler(a.gasRate)) + m.Handle("/set-mining", jsonHandler(a.setMining)) + m.Handle("/get-work", jsonHandler(a.getWork)) m.Handle("/submit-work", jsonHandler(a.submitWork)) - m.Handle("/set-mining", jsonHandler(a.setMining)) + + m.Handle("/gas-rate", jsonHandler(a.gasRate)) + m.Handle("/net-info", jsonHandler(a.getNetInfo)) handler := latencyHandler(m, walletEnable) handler = maxBytesHandler(handler) // TODO(tessr): consider moving this to non-core specific mux diff --git a/api/block_retrieve.go b/api/block_retrieve.go index cbefaf56..769efcf5 100644 --- a/api/block_retrieve.go +++ b/api/block_retrieve.go @@ -143,3 +143,45 @@ func (a *API) getBlockHeader(ins BlockReq) Response { } return NewSuccessResponse(resp) } + +// GetDifficultyResp is resp struct for getDifficulty API +type GetDifficultyResp struct { + BlockHash *bc.Hash `json:"hash"` + Bits uint64 `json:"bits"` + Difficulty string `json:"difficulty"` +} + +func (a *API) getDifficulty() Response { + hash := a.chain.BestBlockHash() + block, err := a.chain.GetBlockByHash(hash) + if err != nil { + return NewErrorResponse(err) + } + + resp := &GetDifficultyResp{ + BlockHash: hash, + Bits: block.Bits, + Difficulty: difficulty.CompactToBig(block.Bits).String(), + } + return NewSuccessResponse(resp) +} + +// getHashRateResp is resp struct for getHashRate API +type getHashRateResp struct { + BlockHash *bc.Hash `json:"hash"` + Nonce uint64 `json:"nonce"` +} + +func (a *API) getHashRate() Response { + hash := a.chain.BestBlockHash() + block, err := a.chain.GetBlockByHash(hash) + if err != nil { + return NewErrorResponse(err) + } + + resp := &getHashRateResp{ + BlockHash: hash, + Nonce: block.Nonce, + } + return NewSuccessResponse(resp) +} diff --git a/cmd/bytomcli/commands/block.go b/cmd/bytomcli/commands/block.go index b430ce0b..cab1eb2c 100644 --- a/cmd/bytomcli/commands/block.go +++ b/cmd/bytomcli/commands/block.go @@ -146,3 +146,29 @@ var getBlockHeaderCmd = &cobra.Command{ printJSON(data) }, } + +var getDifficultyCmd = &cobra.Command{ + Use: "get-difficulty", + Short: "Get the difficulty of most recent block", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + data, exitCode := util.ClientCall("/get-difficulty") + if exitCode != util.Success { + os.Exit(exitCode) + } + printJSON(data) + }, +} + +var getHashRateCmd = &cobra.Command{ + Use: "get-hash-rate", + Short: "Get the nonce of most recent block", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + data, exitCode := util.ClientCall("/get-hash-rate") + if exitCode != util.Success { + os.Exit(exitCode) + } + printJSON(data) + }, +} diff --git a/cmd/bytomcli/commands/bytomcli.go b/cmd/bytomcli/commands/bytomcli.go index bc05d5b4..61b8461c 100644 --- a/cmd/bytomcli/commands/bytomcli.go +++ b/cmd/bytomcli/commands/bytomcli.go @@ -141,6 +141,8 @@ func AddCommands() { BytomcliCmd.AddCommand(getBlockHashCmd) BytomcliCmd.AddCommand(getBlockCmd) BytomcliCmd.AddCommand(getBlockHeaderCmd) + BytomcliCmd.AddCommand(getDifficultyCmd) + BytomcliCmd.AddCommand(getHashRateCmd) BytomcliCmd.AddCommand(createKeyCmd) BytomcliCmd.AddCommand(deleteKeyCmd) -- 2.11.0