OSDN Git Service

Add rpc interface: get-current-block-hash (#80)
authorGuanghua Guo <1536310027@qq.com>
Thu, 2 Nov 2017 08:51:36 +0000 (16:51 +0800)
committerGitHub <noreply@github.com>
Thu, 2 Nov 2017 08:51:36 +0000 (16:51 +0800)
* Add rpc interface: get-current-block-hash

* Update according review

blockchain/reactor.go
cmd/bytomcli/main.go
protocol/protocol.go

index 11c7df3..d4893ba 100644 (file)
@@ -18,6 +18,7 @@ import (
        "github.com/bytom/blockchain/rpc"
        ctypes "github.com/bytom/blockchain/rpc/types"
        "github.com/bytom/blockchain/txfeed"
+       "github.com/bytom/protocol/bc"
        "github.com/bytom/encoding/json"
        "github.com/bytom/errors"
        "github.com/bytom/mining/cpuminer"
@@ -161,6 +162,7 @@ func (bcr *BlockchainReactor) BuildHander() {
        m.Handle("/reset-password", jsonHandler(bcr.pseudohsmResetPassword))
        m.Handle("/update-alias", jsonHandler(bcr.pseudohsmUpdateAlias))
        m.Handle("/net-info", jsonHandler(bcr.getNetInfo))
+       m.Handle("/get-best-block-hash", jsonHandler(bcr.getBestBlockHash))
 
        latencyHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
                if l := latency(m, req); l != nil {
@@ -340,6 +342,10 @@ func (bcR *BlockchainReactor) getNetInfo() (*ctypes.ResultNetInfo, error) {
        return rpc.NetInfo(bcR.sw)
 }
 
+func (bcR *BlockchainReactor) getBestBlockHash() *bc.Hash {
+       return bcR.chain.BestBlockHash()
+}
+
 // BroadcastStatusRequest broadcasts `BlockStore` height.
 func (bcR *BlockchainReactor) BroadcastStatusResponse() {
        block, _ := bcR.chain.State()
index 38892cc..6bb77e6 100644 (file)
@@ -89,6 +89,7 @@ var commands = map[string]*command{
        "reset-password":          {resetPassword},
        "update-alias":            {updateAlias},
        "net-info":                {netInfo},
+       "get-best-block-hash":   {getBestBlockHash},
 }
 
 func main() {
@@ -1111,3 +1112,9 @@ func netInfo(client *rpc.Client, args []string) {
        client.Call(context.Background(), "/net-info", nil, &response)
        fmt.Printf("net info:%v\n", response)
 }
+
+func getBestBlockHash(client *rpc.Client, args []string) {
+       var response interface{}
+       client.Call(context.Background(), "/get-best-block-hash", nil, &response)
+       fmt.Printf("best-block-hash:%v\n", response)
+}
index 5f29f5d..f8c8449 100644 (file)
@@ -167,6 +167,12 @@ func (c *Chain) Height() uint64 {
        return c.state.height
 }
 
+func (c *Chain) BestBlockHash() *bc.Hash {
+       c.state.cond.L.Lock()
+       defer c.state.cond.L.Unlock()
+       return c.state.hash
+}
+
 func (c *Chain) inMainchain(block *legacy.Block) bool {
        hash, ok := c.state.mainChain[block.Height]
        if !ok {