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.
This commit is contained in:
Dave Collins 2013-12-26 10:16:23 -06:00
parent 4cb318ac02
commit e0e4c8bdb6

View file

@ -45,9 +45,10 @@ type InfoResult struct {
Errors string `json:"errors,omitempty"` 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 { type BlockResult struct {
Hex string `json:"hex,omitempty"`
Hash string `json:"hash"` Hash string `json:"hash"`
Confirmations uint64 `json:"confirmations"` Confirmations uint64 `json:"confirmations"`
Size int `json:"size"` Size int `json:"size"`
@ -776,10 +777,21 @@ func ReadResultCmd(cmd string, message []byte) (Reply, error) {
result.Result = res result.Result = res
} }
case "getblock": case "getblock":
var res BlockResult // getblock can either return a JSON object or a hex-encoded
err = json.Unmarshal(objmap["result"], &res) // string depending on the verbose flag. Choose the right form
if err == nil { // accordingly.
result.Result = res 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": case "getrawtransaction":
var res TxRawResult var res TxRawResult