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:
parent
2a8122ffde
commit
6212a7daff
1 changed files with 75 additions and 1 deletions
76
rpcserver.go
76
rpcserver.go
|
@ -173,12 +173,86 @@ func jsonRPCRead(w http.ResponseWriter, r *http.Request, s *rpcServer) {
|
|||
}
|
||||
s.server.Stop()
|
||||
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{
|
||||
Result: maxidx,
|
||||
Error: nil,
|
||||
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.
|
||||
case "getgenerate":
|
||||
rawReply = btcjson.Reply{
|
||||
|
|
Loading…
Reference in a new issue