From e0e4c8bdb67b20a0d99bdfe29984937d4e1fab52 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 26 Dec 2013 10:16:23 -0600 Subject: [PATCH] Allow getblock result to be string or BlockResult. The getblock command has recently added a verbose flag which alters the output. The previous code added a new field "Hex" to the BlockResult, but this is not consistent with the origin getblock RPC call which returns a string when verbose is false and the BlockResult JSON object when it is true. This commit corrects that by first removing the Hex field from the BlockResult and second allowing the result for getblock to be either form. --- jsonapi.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/jsonapi.go b/jsonapi.go index e64bd7f6..d32c454d 100644 --- a/jsonapi.go +++ b/jsonapi.go @@ -45,9 +45,10 @@ type InfoResult struct { Errors string `json:"errors,omitempty"` } -// BlockResult models the data from the getblock command. +// BlockResult models the data from the getblock command when the verbose flag +// is set. When the verbose flag is not set, getblock return a hex-encoded +// string. type BlockResult struct { - Hex string `json:"hex,omitempty"` Hash string `json:"hash"` Confirmations uint64 `json:"confirmations"` Size int `json:"size"` @@ -776,10 +777,21 @@ func ReadResultCmd(cmd string, message []byte) (Reply, error) { result.Result = res } case "getblock": - var res BlockResult - err = json.Unmarshal(objmap["result"], &res) - if err == nil { - result.Result = res + // getblock can either return a JSON object or a hex-encoded + // string depending on the verbose flag. Choose the right form + // accordingly. + if strings.Contains(string(objmap["result"]), "{") { + var res BlockResult + err = json.Unmarshal(objmap["result"], &res) + if err == nil { + result.Result = res + } + } else { + var res string + err = json.Unmarshal(objmap["result"], &res) + if err == nil { + result.Result = res + } } case "getrawtransaction": var res TxRawResult