From 5b376b3b5e63b8a452adf7ba287f3e608ab09564 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sun, 11 May 2014 02:21:27 -0500 Subject: [PATCH] Expose getcurrentnet and getbestblock to HTTP POST. This commit makes the getcurrentnet and getbestblock RPCs available to clients making HTTP POST requests. Closes #127. --- rpcserver.go | 25 +++++++++++++++++++++++++ rpcwebsocket.go | 26 -------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index b272756d..e13c03d5 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -20,6 +20,7 @@ import ( "github.com/conformal/btcscript" "github.com/conformal/btcutil" "github.com/conformal/btcwire" + "github.com/conformal/btcws" "github.com/conformal/fastsha256" "io/ioutil" "math/big" @@ -81,11 +82,13 @@ var rpcHandlersBeforeInit = map[string]commandHandler{ "decoderawtransaction": handleDecodeRawTransaction, "decodescript": handleDecodeScript, "getaddednodeinfo": handleGetAddedNodeInfo, + "getbestblock": handleGetBestBlock, "getbestblockhash": handleGetBestBlockHash, "getblock": handleGetBlock, "getblockcount": handleGetBlockCount, "getblockhash": handleGetBlockHash, "getconnectioncount": handleGetConnectionCount, + "getcurrentnet": handleGetCurrentNet, "getdifficulty": handleGetDifficulty, "getgenerate": handleGetGenerate, "gethashespersec": handleGetHashesPerSec, @@ -939,6 +942,23 @@ func handleGetAddedNodeInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) return results, nil } +// handleGetBestBlock implements the getbestblock command. +func handleGetBestBlock(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { + // All other "get block" commands give either the height, the + // hash, or both but require the block SHA. This gets both for + // the best block. + sha, height, err := s.server.db.NewestSha() + if err != nil { + return nil, btcjson.ErrBestBlockHash + } + + result := &btcws.GetBestBlockResult{ + Hash: sha.String(), + Height: int32(height), + } + return result, nil +} + // handleGetBestBlockHash implements the getbestblockhash command. func handleGetBestBlockHash(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { sha, _, err := s.server.db.NewestSha() @@ -1074,6 +1094,11 @@ func handleGetConnectionCount(s *rpcServer, cmd btcjson.Cmd) (interface{}, error return s.server.ConnectedCount(), nil } +// handleGetCurrentNet implements the getcurrentnet command. +func handleGetCurrentNet(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { + return s.server.btcnet, nil +} + // handleGetDifficulty implements the getdifficulty command. func handleGetDifficulty(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { sha, _, err := s.server.db.NewestSha() diff --git a/rpcwebsocket.go b/rpcwebsocket.go index 64c49db6..07d8e1d7 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -46,8 +46,6 @@ type wsCommandHandler func(*wsClient, btcjson.Cmd) (interface{}, *btcjson.Error) // wsHandlers maps RPC command strings to appropriate websocket handler // functions. var wsHandlers = map[string]wsCommandHandler{ - "getbestblock": handleGetBestBlock, - "getcurrentnet": handleGetCurrentNet, "notifyblocks": handleNotifyBlocks, "notifynewtransactions": handleNotifyNewTransactions, "notifyreceived": handleNotifyReceived, @@ -1380,30 +1378,6 @@ func newWebsocketClient(server *rpcServer, conn *websocket.Conn, } } -// handleGetBestBlock implements the getbestblock command extension -// for websocket connections. -func handleGetBestBlock(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjson.Error) { - // All other "get block" commands give either the height, the - // hash, or both but require the block SHA. This gets both for - // the best block. - sha, height, err := wsc.server.server.db.NewestSha() - if err != nil { - return nil, &btcjson.ErrBestBlockHash - } - - result := &btcws.GetBestBlockResult{ - Hash: sha.String(), - Height: int32(height), - } - return result, nil -} - -// handleGetCurrentNet implements the getcurrentnet command extension -// for websocket connections. -func handleGetCurrentNet(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjson.Error) { - return wsc.server.server.btcnet, nil -} - // handleNotifyBlocks implements the notifyblocks command extension for // websocket connections. func handleNotifyBlocks(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjson.Error) {