From 020444906be64188fc3fa00093dd3a1760e05a3d Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Fri, 25 Oct 2013 19:00:33 +0100 Subject: [PATCH] Getblockhash and getblock template Fix some type wranging in unmarshal for getblocktemplate --- jsoncmd.go | 15 ++++++++++--- jsoncmd_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/jsoncmd.go b/jsoncmd.go index dda08fd5..9cd2d0f7 100644 --- a/jsoncmd.go +++ b/jsoncmd.go @@ -1853,11 +1853,20 @@ func (cmd *GetBlockTemplateCmd) UnmarshalJSON(b []byte) error { capabilities, ok := rmap["capabilities"] if ok { - scap, ok := capabilities.([]string) + icap, ok := capabilities.([]interface{}) if !ok { - return errors.New("TemplateRequest mode must be a string array") + return errors.New("TemplateRequest mode must be an array") } - trequest.Capabilities = scap + + cap := make([]string, len(icap)) + for i, val := range icap { + cap[i], ok = val.(string) + if !ok { + return errors.New("TemplateRequest mode must be an aray of strings") + } + } + + trequest.Capabilities = cap } optArgs = append(optArgs, trequest) diff --git a/jsoncmd_test.go b/jsoncmd_test.go index 0e7d9c63..4163644c 100644 --- a/jsoncmd_test.go +++ b/jsoncmd_test.go @@ -304,6 +304,62 @@ var jsoncmdtests = []struct { id: float64(1), }, }, + { + name: "basic getblockhash", + f: func() (Cmd, error) { + return NewGetBlockHashCmd(float64(1), 1234) + }, + result: &GetBlockHashCmd{ + id: float64(1), + Index: 1234, + }, + }, + { + name: "basic getblocktemplate", + f: func() (Cmd, error) { + return NewGetBlockTemplateCmd(float64(1)) + }, + result: &GetBlockTemplateCmd{ + id: float64(1), + }, + }, + { + name: "basic getblocktemplate + request", + f: func() (Cmd, error) { + return NewGetBlockTemplateCmd(float64(1), + &TemplateRequest{Mode:"mode", + Capabilities: []string{"one", "two", "three"}}) + }, + result: &GetBlockTemplateCmd{ + id: float64(1), + Request: &TemplateRequest{ + Mode:"mode", + Capabilities: []string{ + "one", + "two", + "three", + }, + }, + }, + }, + { + name: "basic getblocktemplate + request no mode", + f: func() (Cmd, error) { + return NewGetBlockTemplateCmd(float64(1), + &TemplateRequest{ + Capabilities: []string{"one", "two", "three"}}) + }, + result: &GetBlockTemplateCmd{ + id: float64(1), + Request: &TemplateRequest{ + Capabilities: []string{ + "one", + "two", + "three", + }, + }, + }, + }, { name: "basic ping", f: func() (Cmd, error) {