Rough and ready cut over of btcctl to use the btcjson command api.

This commit is contained in:
Owain G. Ainsworth 2013-10-28 16:45:44 +00:00
parent 08fc3050a3
commit 9f96e59392

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/conformal/btcjson" "github.com/conformal/btcjson"
@ -34,6 +35,7 @@ type handlerData struct {
optionalArgs int optionalArgs int
displayHandler displayHandler displayHandler displayHandler
conversionHandlers []conversionHandler conversionHandlers []conversionHandler
makeCmd func([]interface{}) (btcjson.Cmd, error)
usage string usage string
} }
@ -46,19 +48,20 @@ var (
// commandHandlers is a map of commands and associated handler data that is used // commandHandlers is a map of commands and associated handler data that is used
// to validate correctness and perform the command. // to validate correctness and perform the command.
var commandHandlers = map[string]*handlerData{ var commandHandlers = map[string]*handlerData{
"addnode": &handlerData{2, 0, displaySpewDump, nil, "<ip> <add/remove/onetry>"}, "addnode": &handlerData{2, 0, displaySpewDump, nil, makeAddNode, "<ip> <add/remove/onetry>"},
"decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, "<txhash>"}, "decoderawtransaction": &handlerData{1, 0, displaySpewDump, nil, makeDecodeRawTransaction, "<txhash>"},
"getbestblockhash": &handlerData{0, 0, displayGeneric, nil, ""}, "getbestblockhash": &handlerData{0, 0, displayGeneric, nil, makeGetBestBlockHash, ""},
"getblock": &handlerData{1, 0, displaySpewDump, nil, "<blockhash>"}, "getblock": &handlerData{1, 0, displaySpewDump, nil, makeGetBlock,
"getblockcount": &handlerData{0, 0, displayFloat64, nil, ""}, "<blockhash>"},
"getblockhash": &handlerData{1, 0, displayGeneric, []conversionHandler{toInt}, "<blocknumber>"}, "getblockcount": &handlerData{0, 0, displayFloat64, nil, makeGetBlockCount, ""},
"getconnectioncount": &handlerData{0, 0, displayFloat64, nil, ""}, "getblockhash": &handlerData{1, 0, displayGeneric, []conversionHandler{toInt}, makeGetBlockHash, "<blocknumber>"},
"getdifficulty": &handlerData{0, 0, displayFloat64, nil, ""}, "getconnectioncount": &handlerData{0, 0, displayFloat64, nil, makeGetConnectionCount, ""},
"getgenerate": &handlerData{0, 0, displayGeneric, nil, ""}, "getdifficulty": &handlerData{0, 0, displayFloat64, nil, makeGetDifficulty, ""},
"getpeerinfo": &handlerData{0, 0, displaySpewDump, nil, ""}, "getgenerate": &handlerData{0, 0, displayGeneric, nil, makeGetGenerate, ""},
"getrawmempool": &handlerData{0, 0, displaySpewDump, nil, ""}, "getpeerinfo": &handlerData{0, 0, displaySpewDump, nil, makeGetPeerInfo, ""},
"getrawtransaction": &handlerData{1, 1, displaySpewDump, []conversionHandler{nil, toInt}, "<txhash> [verbose=0]"}, "getrawmempool": &handlerData{0, 0, displaySpewDump, nil, makeGetRawMempool, ""},
"stop": &handlerData{0, 0, displayGeneric, nil, ""}, "getrawtransaction": &handlerData{1, 1, displaySpewDump, []conversionHandler{nil, toInt}, makeGetRawTransaction, "<txhash> [verbose=0]"},
"stop": &handlerData{0, 0, displayGeneric, nil, makeStop, ""},
} }
// toInt attempts to convert the passed string to an integer. It returns the // toInt attempts to convert the passed string to an integer. It returns the
@ -100,6 +103,83 @@ func displaySpewDump(reply interface{}) error {
return nil return nil
} }
// makeAddNode generates the cmd structure for addnode comands.
func makeAddNode(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewAddNodeCmd("btcctl", args[0].(string),
args[1].(string))
}
// makeDecodeRawTransaction generates the cmd structure for
// decoderawtransaction comands.
func makeDecodeRawTransaction(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewDecodeRawTransactionCmd("btcctl", args[0].(string))
}
// makeGetBestBlockHash generates the cmd structure for
// makebestblockhash comands.
func makeGetBestBlockHash(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBestBlockHashCmd("btcctl")
}
// makeGetBlock generates the cmd structure for getblock comands.
func makeGetBlock(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBlockCmd("btcctl", args[0].(string))
}
// makeGetBlockCount generates the cmd structure for getblockcount comands.
func makeGetBlockCount(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBlockCountCmd("btcctl")
}
// makeGetBlockHash generates the cmd structure for getblockhash comands.
func makeGetBlockHash(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetBlockHashCmd("btcctl", args[0].(int64))
}
// makeGetConnectionCount generates the cmd structure for
// getconnectioncount comands.
func makeGetConnectionCount(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetConnectionCountCmd("btcctl")
}
// makeGetDifficulty generates the cmd structure for
// getdifficulty comands.
func makeGetDifficulty(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetDifficultyCmd("btcctl")
}
// makeGetGenerate generates the cmd structure for
// getgenerate comands.
func makeGetGenerate(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetGenerateCmd("btcctl")
}
// makePeerInfo generates the cmd structure for
// getpeerinfo comands.
func makeGetPeerInfo(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetPeerInfoCmd("btcctl")
}
// makeRawMempool generates the cmd structure for
// getrawmempool comands.
func makeGetRawMempool(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewGetRawMempoolCmd("btcctl")
}
// makeRawTransaction generates the cmd structure for
// getrawtransaction comands.
func makeGetRawTransaction(args []interface{}) (btcjson.Cmd, error) {
i := args[1].(int)
bi := i != 0
return btcjson.NewGetRawTransactionCmd("btcctl", args[0].(string), bi)
}
// makeStop generates the cmd structure for stop comands.
func makeStop(args []interface{}) (btcjson.Cmd, error) {
return btcjson.NewStopCmd("btcctl")
}
// send sends a JSON-RPC command to the specified RPC server and examines the // send sends a JSON-RPC command to the specified RPC server and examines the
// results for various error conditions. It either returns a valid result or // results for various error conditions. It either returns a valid result or
// an appropriate error. // an appropriate error.
@ -123,8 +203,8 @@ func send(cfg *config, msg []byte) (interface{}, error) {
// sendCommand creates a JSON-RPC command using the passed command and arguments // sendCommand creates a JSON-RPC command using the passed command and arguments
// and then sends it. A prefix is added to any errors that occur indicating // and then sends it. A prefix is added to any errors that occur indicating
// what step failed. // what step failed.
func sendCommand(cfg *config, command string, args ...interface{}) (interface{}, error) { func sendCommand(cfg *config, command btcjson.Cmd) (interface{}, error) {
msg, err := btcjson.CreateMessage(command, args...) msg, err := json.Marshal(command)
if err != nil { if err != nil {
return nil, fmt.Errorf("CreateMessage: %v", err.Error()) return nil, fmt.Errorf("CreateMessage: %v", err.Error())
} }
@ -177,9 +257,13 @@ func commandHandler(cfg *config, command string, data *handlerData, args []strin
} }
} }
} }
cmd, err := data.makeCmd(iargs)
if err != nil {
return err
}
// Create and send the appropriate JSON-RPC command. // Create and send the appropriate JSON-RPC command.
reply, err := sendCommand(cfg, command, iargs...) reply, err := sendCommand(cfg, cmd)
if err != nil { if err != nil {
return err return err
} }