mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 18:00:11 +01:00
Use rpc errors defined in btcjson.
This commit is contained in:
parent
9ded3fa2cf
commit
1420eccf73
89
rpcserver.go
89
rpcserver.go
@ -282,10 +282,7 @@ func jsonRPCRead(w http.ResponseWriter, r *http.Request, s *rpcServer) {
|
|||||||
func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
||||||
var message btcjson.Message
|
var message btcjson.Message
|
||||||
if err := json.Unmarshal(body, &message); err != nil {
|
if err := json.Unmarshal(body, &message); err != nil {
|
||||||
jsonError := btcjson.Error{
|
jsonError := btcjson.ErrParse
|
||||||
Code: -32700,
|
|
||||||
Message: "Parse error",
|
|
||||||
}
|
|
||||||
|
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
Result: nil,
|
Result: nil,
|
||||||
@ -309,6 +306,9 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
}
|
}
|
||||||
err = errors.New(jsonErr.Message)
|
err = errors.New(jsonErr.Message)
|
||||||
} else {
|
} else {
|
||||||
|
// In the case where we did not have a btcjson
|
||||||
|
// error to begin with, make a new one to send,
|
||||||
|
// but this really should not happen.
|
||||||
rawJSONError := btcjson.Error{
|
rawJSONError := btcjson.Error{
|
||||||
Code: -32603,
|
Code: -32603,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
@ -335,10 +335,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
_, maxidx, err = s.server.db.NewestSha()
|
_, maxidx, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error getting newest sha: %v", err)
|
log.Errorf("RPCS: Error getting newest sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBlockCount
|
||||||
Code: -5,
|
|
||||||
Message: "Error getting block count",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
@ -351,10 +348,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
sha, _, err = s.server.db.NewestSha()
|
sha, _, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error getting newest sha: %v", err)
|
log.Errorf("RPCS: Error getting newest sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBestBlockHash
|
||||||
Code: -5,
|
|
||||||
Message: "Error getting best block hash",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
@ -375,20 +369,14 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
sha, _, err = s.server.db.NewestSha()
|
sha, _, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error getting sha: %v", err)
|
log.Errorf("RPCS: Error getting sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrDifficulty
|
||||||
Code: -5,
|
|
||||||
Message: "Error Getting difficulty",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var blk *btcutil.Block
|
var blk *btcutil.Block
|
||||||
blk, err = s.server.db.FetchBlockBySha(sha)
|
blk, err = s.server.db.FetchBlockBySha(sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error getting block: %v", err)
|
log.Errorf("RPCS: Error getting block: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrDifficulty
|
||||||
Code: -5,
|
|
||||||
Message: "Error Getting difficulty",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
blockHeader := &blk.MsgBlock().Header
|
blockHeader := &blk.MsgBlock().Header
|
||||||
@ -434,10 +422,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
sha, err = s.server.db.FetchBlockShaByHeight(int64(idx))
|
sha, err = s.server.db.FetchBlockShaByHeight(int64(idx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[RCPS] Error getting block: %v", err)
|
log.Errorf("[RCPS] Error getting block: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrOutOfRange
|
||||||
Code: -1,
|
|
||||||
Message: "Block number out of range.",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reply = btcjson.Reply{
|
reply = btcjson.Reply{
|
||||||
@ -463,31 +448,21 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
sha, err = btcwire.NewShaHashFromStr(hash)
|
sha, err = btcwire.NewShaHashFromStr(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.Error{
|
err = btcjson.ErrBlockNotFound
|
||||||
Code: -5,
|
|
||||||
Message: "Block not found",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var blk *btcutil.Block
|
var blk *btcutil.Block
|
||||||
blk, err = s.server.db.FetchBlockBySha(sha)
|
blk, err = s.server.db.FetchBlockBySha(sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error fetching sha: %v", err)
|
log.Errorf("RPCS: Error fetching sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBlockNotFound
|
||||||
Code: -5,
|
|
||||||
Message: "Block not found",
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
idx := blk.Height()
|
idx := blk.Height()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
buf, err = blk.Bytes()
|
buf, err = blk.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error fetching block: %v", err)
|
log.Errorf("RPCS: Error fetching block: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBlockNotFound
|
||||||
Code: -5,
|
|
||||||
Message: "Block not found",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -502,10 +477,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
_, maxidx, err = s.server.db.NewestSha()
|
_, maxidx, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Cannot get newest sha: %v", err)
|
log.Errorf("RPCS: Cannot get newest sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBlockNotFound
|
||||||
Code: -5,
|
|
||||||
Message: "Block not found",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -531,10 +503,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
shaNext, err = s.server.db.FetchBlockShaByHeight(int64(idx + 1))
|
shaNext, err = s.server.db.FetchBlockShaByHeight(int64(idx + 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: No next block: %v", err)
|
log.Errorf("RPCS: No next block: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBlockNotFound
|
||||||
Code: -5,
|
|
||||||
Message: "Block not found",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
blockReply.NextHash = shaNext.String()
|
blockReply.NextHash = shaNext.String()
|
||||||
@ -589,10 +558,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
txList, err = s.server.db.FetchTxBySha(txSha)
|
txList, err = s.server.db.FetchTxBySha(txSha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error fetching tx: %v", err)
|
log.Errorf("RPCS: Error fetching tx: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrNoTxInfo
|
||||||
Code: -5,
|
|
||||||
Message: "No information available about transaction",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -603,10 +569,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
blk, err = s.server.db.FetchBlockBySha(blksha)
|
blk, err = s.server.db.FetchBlockBySha(blksha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Error fetching sha: %v", err)
|
log.Errorf("RPCS: Error fetching sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrBlockNotFound
|
||||||
Code: -5,
|
|
||||||
Message: "Block not found",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
idx := blk.Height()
|
idx := blk.Height()
|
||||||
@ -648,10 +611,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
_, maxidx, err = s.server.db.NewestSha()
|
_, maxidx, err = s.server.db.NewestSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("RPCS: Cannot get newest sha: %v", err)
|
log.Errorf("RPCS: Cannot get newest sha: %v", err)
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrNoNewestBlockInfo
|
||||||
Code: -5,
|
|
||||||
Message: "No information about newest block",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
confirmations := uint64(1 + maxidx - idx)
|
confirmations := uint64(1 + maxidx - idx)
|
||||||
@ -707,18 +667,12 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
case "sendrawtransaction":
|
case "sendrawtransaction":
|
||||||
params, ok := message.Params.([]interface{})
|
params, ok := message.Params.([]interface{})
|
||||||
if !ok || len(params) != 1 {
|
if !ok || len(params) != 1 {
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrInvalidParams
|
||||||
Code: -32602,
|
|
||||||
Message: "Invalid parameters",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
serializedtxhex, ok := params[0].(string)
|
serializedtxhex, ok := params[0].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrRawTxString
|
||||||
Code: -32602,
|
|
||||||
Message: "Raw tx is not a string",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -726,10 +680,7 @@ func jsonRead(body []byte, s *rpcServer) (reply btcjson.Reply, err error) {
|
|||||||
var serializedTx []byte
|
var serializedTx []byte
|
||||||
serializedTx, err = hex.DecodeString(serializedtxhex)
|
serializedTx, err = hex.DecodeString(serializedtxhex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = btcjson.Error{
|
err = btcjson.ErrDecodeHexString
|
||||||
Code: -22,
|
|
||||||
Message: "Unable to decode hex string",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msgtx := btcwire.NewMsgTx()
|
msgtx := btcwire.NewMsgTx()
|
||||||
|
Loading…
Reference in New Issue
Block a user