Expose getcurrentnet and getbestblock to HTTP POST.
This commit makes the getcurrentnet and getbestblock RPCs available to clients making HTTP POST requests. Closes #127.
This commit is contained in:
parent
6a325f4c6a
commit
5b376b3b5e
2 changed files with 25 additions and 26 deletions
25
rpcserver.go
25
rpcserver.go
|
@ -20,6 +20,7 @@ import (
|
||||||
"github.com/conformal/btcscript"
|
"github.com/conformal/btcscript"
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
|
"github.com/conformal/btcws"
|
||||||
"github.com/conformal/fastsha256"
|
"github.com/conformal/fastsha256"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
@ -81,11 +82,13 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
||||||
"decoderawtransaction": handleDecodeRawTransaction,
|
"decoderawtransaction": handleDecodeRawTransaction,
|
||||||
"decodescript": handleDecodeScript,
|
"decodescript": handleDecodeScript,
|
||||||
"getaddednodeinfo": handleGetAddedNodeInfo,
|
"getaddednodeinfo": handleGetAddedNodeInfo,
|
||||||
|
"getbestblock": handleGetBestBlock,
|
||||||
"getbestblockhash": handleGetBestBlockHash,
|
"getbestblockhash": handleGetBestBlockHash,
|
||||||
"getblock": handleGetBlock,
|
"getblock": handleGetBlock,
|
||||||
"getblockcount": handleGetBlockCount,
|
"getblockcount": handleGetBlockCount,
|
||||||
"getblockhash": handleGetBlockHash,
|
"getblockhash": handleGetBlockHash,
|
||||||
"getconnectioncount": handleGetConnectionCount,
|
"getconnectioncount": handleGetConnectionCount,
|
||||||
|
"getcurrentnet": handleGetCurrentNet,
|
||||||
"getdifficulty": handleGetDifficulty,
|
"getdifficulty": handleGetDifficulty,
|
||||||
"getgenerate": handleGetGenerate,
|
"getgenerate": handleGetGenerate,
|
||||||
"gethashespersec": handleGetHashesPerSec,
|
"gethashespersec": handleGetHashesPerSec,
|
||||||
|
@ -939,6 +942,23 @@ func handleGetAddedNodeInfo(s *rpcServer, cmd btcjson.Cmd) (interface{}, error)
|
||||||
return results, nil
|
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.
|
// handleGetBestBlockHash implements the getbestblockhash command.
|
||||||
func handleGetBestBlockHash(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
func handleGetBestBlockHash(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
||||||
sha, _, err := s.server.db.NewestSha()
|
sha, _, err := s.server.db.NewestSha()
|
||||||
|
@ -1074,6 +1094,11 @@ func handleGetConnectionCount(s *rpcServer, cmd btcjson.Cmd) (interface{}, error
|
||||||
return s.server.ConnectedCount(), nil
|
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.
|
// handleGetDifficulty implements the getdifficulty command.
|
||||||
func handleGetDifficulty(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
func handleGetDifficulty(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
|
||||||
sha, _, err := s.server.db.NewestSha()
|
sha, _, err := s.server.db.NewestSha()
|
||||||
|
|
|
@ -46,8 +46,6 @@ type wsCommandHandler func(*wsClient, btcjson.Cmd) (interface{}, *btcjson.Error)
|
||||||
// wsHandlers maps RPC command strings to appropriate websocket handler
|
// wsHandlers maps RPC command strings to appropriate websocket handler
|
||||||
// functions.
|
// functions.
|
||||||
var wsHandlers = map[string]wsCommandHandler{
|
var wsHandlers = map[string]wsCommandHandler{
|
||||||
"getbestblock": handleGetBestBlock,
|
|
||||||
"getcurrentnet": handleGetCurrentNet,
|
|
||||||
"notifyblocks": handleNotifyBlocks,
|
"notifyblocks": handleNotifyBlocks,
|
||||||
"notifynewtransactions": handleNotifyNewTransactions,
|
"notifynewtransactions": handleNotifyNewTransactions,
|
||||||
"notifyreceived": handleNotifyReceived,
|
"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
|
// handleNotifyBlocks implements the notifyblocks command extension for
|
||||||
// websocket connections.
|
// websocket connections.
|
||||||
func handleNotifyBlocks(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
func handleNotifyBlocks(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue