mirror of
https://github.com/btcsuite/btcd.git
synced 2025-02-22 14:22:49 +01:00
Switch rpcserver.go over to using the jsoncmd apis.
This commit is contained in:
parent
3c405563bd
commit
975640c6b3
1 changed files with 46 additions and 116 deletions
162
rpcserver.go
162
rpcserver.go
|
@ -313,8 +313,8 @@ func jsonRPCRead(w http.ResponseWriter, r *http.Request, s *rpcServer) {
|
||||||
// wallet notification channel can be used to automatically register
|
// wallet notification channel can be used to automatically register
|
||||||
// various notifications for the wallet.
|
// various notifications for the wallet.
|
||||||
func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply btcjson.Reply, err error) {
|
func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply btcjson.Reply, err error) {
|
||||||
var message btcjson.Message
|
cmd, err := btcjson.ParseMarshaledCmd(body)
|
||||||
if err := json.Unmarshal(body, &message); err != nil {
|
if err != nil {
|
||||||
jsonError := btcjson.ErrParse
|
jsonError := btcjson.ErrParse
|
||||||
|
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
|
@ -327,15 +327,16 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
|
|
||||||
return reply, jsonError
|
return reply, jsonError
|
||||||
}
|
}
|
||||||
log.Tracef("RPCS: received: %v", message)
|
log.Tracef("RPCS: received: %v", cmd)
|
||||||
|
|
||||||
// Set final reply based on error if non-nil.
|
// Set final reply based on error if non-nil.
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
id := cmd.Id()
|
||||||
if jsonErr, ok := err.(btcjson.Error); ok {
|
if jsonErr, ok := err.(btcjson.Error); ok {
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Error: &jsonErr,
|
Error: &jsonErr,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
err = errors.New(jsonErr.Message)
|
err = errors.New(jsonErr.Message)
|
||||||
} else {
|
} else {
|
||||||
|
@ -348,22 +349,24 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Error: &rawJSONError,
|
Error: &rawJSONError,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
id := cmd.Id()
|
||||||
|
|
||||||
// Deal with commands
|
// Deal with commands
|
||||||
switch message.Method {
|
switch c := cmd.(type) {
|
||||||
case "stop":
|
case *btcjson.StopCmd:
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: "btcd stopping.",
|
Result: "btcd stopping.",
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
s.server.Stop()
|
s.server.Stop()
|
||||||
|
|
||||||
case "getblockcount":
|
case *btcjson.GetBlockCountCmd:
|
||||||
var maxidx int64
|
var maxidx int64
|
||||||
_, maxidx, err = s.server.db.NewestSha()
|
_, maxidx, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -373,10 +376,10 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: maxidx,
|
Result: maxidx,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getbestblockhash":
|
case *btcjson.GetBestBlockHashCmd:
|
||||||
var sha *btcwire.ShaHash
|
var sha *btcwire.ShaHash
|
||||||
sha, _, err = s.server.db.NewestSha()
|
sha, _, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -386,18 +389,18 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: sha.String(),
|
Result: sha.String(),
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getconnectioncount":
|
case *btcjson.GetConnectionCountCmd:
|
||||||
var count int
|
var count int
|
||||||
|
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: count,
|
Result: count,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getdifficulty":
|
case *btcjson.GetDifficultyCmd:
|
||||||
var sha *btcwire.ShaHash
|
var sha *btcwire.ShaHash
|
||||||
sha, _, err = s.server.db.NewestSha()
|
sha, _, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -415,44 +418,31 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
blockHeader := &blk.MsgBlock().Header
|
blockHeader := &blk.MsgBlock().Header
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: getDifficultyRatio(blockHeader.Bits),
|
Result: getDifficultyRatio(blockHeader.Bits),
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
// btcd does not do mining so we can hardcode replies here.
|
// btcd does not do mining so we can hardcode replies here.
|
||||||
case "getgenerate":
|
case *btcjson.GetGenerateCmd:
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: false,
|
Result: false,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "setgenerate":
|
case *btcjson.SetGenerateCmd:
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: nil,
|
Result: nil,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "gethashespersec":
|
case *btcjson.GetHashesPerSecCmd:
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: 0,
|
Result: 0,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getblockhash":
|
case *btcjson.GetBlockHashCmd:
|
||||||
var f interface{}
|
|
||||||
err = json.Unmarshal(body, &f)
|
|
||||||
m := f.(map[string]interface{})
|
|
||||||
var idx float64
|
|
||||||
for _, v := range m {
|
|
||||||
switch vv := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
for _, u := range vv {
|
|
||||||
idx, _ = u.(float64)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var sha *btcwire.ShaHash
|
var sha *btcwire.ShaHash
|
||||||
sha, err = s.server.db.FetchBlockShaByHeight(int64(idx))
|
sha, err = s.server.db.FetchBlockShaByHeight(c.Index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[RCPS] Error getting block: %v", err)
|
log.Errorf("[RCPS] Error getting block: %v", err)
|
||||||
err = btcjson.ErrOutOfRange
|
err = btcjson.ErrOutOfRange
|
||||||
|
@ -460,25 +450,12 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: sha.String(),
|
Result: sha.String(),
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getblock":
|
case *btcjson.GetBlockCmd:
|
||||||
var f interface{}
|
|
||||||
err = json.Unmarshal(body, &f)
|
|
||||||
m := f.(map[string]interface{})
|
|
||||||
var hash string
|
|
||||||
for _, v := range m {
|
|
||||||
switch vv := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
for _, u := range vv {
|
|
||||||
hash, _ = u.(string)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var sha *btcwire.ShaHash
|
var sha *btcwire.ShaHash
|
||||||
sha, err = btcwire.NewShaHashFromStr(hash)
|
sha, err = btcwire.NewShaHashFromStr(c.Hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error generating sha: %v", err)
|
log.Errorf("RPCS: Error generating sha: %v", err)
|
||||||
err = btcjson.ErrBlockNotFound
|
err = btcjson.ErrBlockNotFound
|
||||||
|
@ -517,7 +494,7 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
|
|
||||||
blockHeader := &blk.MsgBlock().Header
|
blockHeader := &blk.MsgBlock().Header
|
||||||
blockReply := btcjson.BlockResult{
|
blockReply := btcjson.BlockResult{
|
||||||
Hash: hash,
|
Hash: c.Hash,
|
||||||
Version: blockHeader.Version,
|
Version: blockHeader.Version,
|
||||||
MerkleRoot: blockHeader.MerkleRoot.String(),
|
MerkleRoot: blockHeader.MerkleRoot.String(),
|
||||||
PreviousHash: blockHeader.PrevBlock.String(),
|
PreviousHash: blockHeader.PrevBlock.String(),
|
||||||
|
@ -546,10 +523,10 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: blockReply,
|
Result: blockReply,
|
||||||
Error: nil,
|
Error: nil,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getrawmempool":
|
case *btcjson.GetRawMempoolCmd:
|
||||||
hashes := s.server.txMemPool.TxShas()
|
hashes := s.server.txMemPool.TxShas()
|
||||||
hashStrings := make([]string, len(hashes))
|
hashStrings := make([]string, len(hashes))
|
||||||
for i := 0; i < len(hashes); i++ {
|
for i := 0; i < len(hashes); i++ {
|
||||||
|
@ -557,36 +534,14 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: hashStrings,
|
Result: hashStrings,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "getrawtransaction":
|
case *btcjson.GetRawTransactionCmd:
|
||||||
// TODO: Perform smarter paramter parsing.
|
if c.Verbose {
|
||||||
var f interface{}
|
|
||||||
err = json.Unmarshal(body, &f)
|
|
||||||
m := f.(map[string]interface{})
|
|
||||||
var tx string
|
|
||||||
var verbose float64
|
|
||||||
for _, v := range m {
|
|
||||||
switch vv := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
for _, u := range vv {
|
|
||||||
switch uu := u.(type) {
|
|
||||||
case string:
|
|
||||||
tx = uu
|
|
||||||
case float64:
|
|
||||||
verbose = uu
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if int(verbose) != 0 {
|
|
||||||
// TODO: check error code. tx is not checked before
|
// TODO: check error code. tx is not checked before
|
||||||
// this point.
|
// this point.
|
||||||
txSha, _ := btcwire.NewShaHashFromStr(tx)
|
txSha, _ := btcwire.NewShaHashFromStr(c.Txid)
|
||||||
var txS *btcwire.MsgTx
|
var txS *btcwire.MsgTx
|
||||||
var txList []*btcdb.TxListReply
|
var txList []*btcdb.TxListReply
|
||||||
txList, err = s.server.db.FetchTxBySha(txSha)
|
txList, err = s.server.db.FetchTxBySha(txSha)
|
||||||
|
@ -652,7 +607,7 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
|
|
||||||
blockHeader := &blk.MsgBlock().Header
|
blockHeader := &blk.MsgBlock().Header
|
||||||
txReply := btcjson.TxRawResult{
|
txReply := btcjson.TxRawResult{
|
||||||
Txid: tx,
|
Txid: c.Txid,
|
||||||
Vout: voutList,
|
Vout: voutList,
|
||||||
Vin: vinList,
|
Vin: vinList,
|
||||||
Version: txS.Version,
|
Version: txS.Version,
|
||||||
|
@ -667,52 +622,27 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: txReply,
|
Result: txReply,
|
||||||
Error: nil,
|
Error: nil,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Don't return details
|
// Don't return details
|
||||||
// not used yet
|
// not used yet
|
||||||
}
|
}
|
||||||
|
|
||||||
case "decoderawtransaction":
|
case *btcjson.DecodeRawTransactionCmd:
|
||||||
// TODO: Perform smarter paramter parsing.
|
// TODO: use c.HexTx and fill result with info.
|
||||||
var f interface{}
|
|
||||||
err = json.Unmarshal(body, &f)
|
|
||||||
m := f.(map[string]interface{})
|
|
||||||
var hash string
|
|
||||||
for _, v := range m {
|
|
||||||
switch vv := v.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
for _, u := range vv {
|
|
||||||
hash, _ = u.(string)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO: use hash and fill result with info.
|
|
||||||
_ = hash
|
|
||||||
txReply := btcjson.TxRawDecodeResult{}
|
txReply := btcjson.TxRawDecodeResult{}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: txReply,
|
Result: txReply,
|
||||||
Error: nil,
|
Error: nil,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
case "sendrawtransaction":
|
case *btcjson.SendRawTransactionCmd:
|
||||||
params, ok := message.Params.([]interface{})
|
|
||||||
if !ok || len(params) != 1 {
|
|
||||||
err = btcjson.ErrInvalidParams
|
|
||||||
return
|
|
||||||
}
|
|
||||||
serializedtxhex, ok := params[0].(string)
|
|
||||||
if !ok {
|
|
||||||
err = btcjson.ErrRawTxString
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize and send off to tx relay
|
// Deserialize and send off to tx relay
|
||||||
var serializedTx []byte
|
var serializedTx []byte
|
||||||
serializedTx, err = hex.DecodeString(serializedtxhex)
|
serializedTx, err = hex.DecodeString(c.HexTx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = btcjson.ErrDecodeHexString
|
err = btcjson.ErrDecodeHexString
|
||||||
return
|
return
|
||||||
|
@ -750,7 +680,7 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: result,
|
Result: result,
|
||||||
Error: nil,
|
Error: nil,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -761,7 +691,7 @@ func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: nil,
|
Result: nil,
|
||||||
Error: &jsonError,
|
Error: &jsonError,
|
||||||
Id: &message.Id,
|
Id: &id,
|
||||||
}
|
}
|
||||||
err = ErrMethodNotImplemented
|
err = ErrMethodNotImplemented
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue