Improve rpcserver.

Add getdifficulty command to rpcserver.

Add missing error check in an old bit of rpcserver.

Add getbestblockhash command.
This commit is contained in:
John C. Vernaleo 2013-10-11 16:31:19 -04:00
parent 2a8122ffde
commit 6212a7daff

View file

@ -173,12 +173,86 @@ func jsonRPCRead(w http.ResponseWriter, r *http.Request, s *rpcServer) {
} }
s.server.Stop() s.server.Stop()
case "getblockcount": case "getblockcount":
_, maxidx, _ := s.server.db.NewestSha() _, maxidx, err := s.server.db.NewestSha()
if err != nil {
log.Errorf("RPCS: Error getting newest sha: %v", err)
jsonError := btcjson.Error{
Code: -5,
Message: "Error getting block count",
}
rawReply = btcjson.Reply{
Result: nil,
Error: &jsonError,
Id: &message.Id,
}
log.Tracef("RPCS: reply: %v", rawReply)
break
}
rawReply = btcjson.Reply{ rawReply = btcjson.Reply{
Result: maxidx, Result: maxidx,
Error: nil, Error: nil,
Id: &message.Id, Id: &message.Id,
} }
case "getbestblockhash":
sha, _, err := s.server.db.NewestSha()
if err != nil {
log.Errorf("RPCS: Error getting newest sha: %v", err)
jsonError := btcjson.Error{
Code: -5,
Message: "Error getting best block hash",
}
rawReply = btcjson.Reply{
Result: nil,
Error: &jsonError,
Id: &message.Id,
}
log.Tracef("RPCS: reply: %v", rawReply)
break
}
rawReply = btcjson.Reply{
Result: sha,
Error: nil,
Id: &message.Id,
}
case "getdifficulty":
sha, _, err := s.server.db.NewestSha()
if err != nil {
log.Errorf("RPCS: Error getting sha: %v", err)
jsonError := btcjson.Error{
Code: -5,
Message: "Error Getting difficulty",
}
rawReply = btcjson.Reply{
Result: nil,
Error: &jsonError,
Id: &message.Id,
}
log.Tracef("RPCS: reply: %v", rawReply)
break
}
blk, err := s.server.db.FetchBlockBySha(sha)
if err != nil {
log.Errorf("RPCS: Error getting block: %v", err)
jsonError := btcjson.Error{
Code: -5,
Message: "Error Getting difficulty",
}
rawReply = btcjson.Reply{
Result: nil,
Error: &jsonError,
Id: &message.Id,
}
log.Tracef("RPCS: reply: %v", rawReply)
break
}
blockHeader := &blk.MsgBlock().Header
rawReply = btcjson.Reply{
Result: getDifficultyRatio(blockHeader.Bits),
Error: nil,
Id: &message.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 "getgenerate":
rawReply = btcjson.Reply{ rawReply = btcjson.Reply{