From 908945ed53d102a8627d90b5c0e5ac685c94e017 Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 23 Jan 2014 15:55:52 -0500 Subject: [PATCH] fix getwork API closes #12 --- jsoncmd.go | 20 ++++++++++++++------ jsoncmd_test.go | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/jsoncmd.go b/jsoncmd.go index 63abfbbe..5224d948 100644 --- a/jsoncmd.go +++ b/jsoncmd.go @@ -3691,7 +3691,7 @@ type WorkRequest struct { // unmarshaling of getwork JSON RPC commands. type GetWorkCmd struct { id interface{} - Request WorkRequest + Request *WorkRequest } // Enforce that GetWorkCmd satisifies the Cmd interface. @@ -3699,7 +3699,14 @@ var _ Cmd = &GetWorkCmd{} // NewGetWorkCmd creates a new GetWorkCmd. Optionally a // pointer to a TemplateRequest may be provided. -func NewGetWorkCmd(id interface{}, request WorkRequest) (*GetWorkCmd, error) { +func NewGetWorkCmd(id interface{}, optArgs ...*WorkRequest) (*GetWorkCmd, error) { + var request *WorkRequest + if len(optArgs) > 0 { + if len(optArgs) > 1 { + return nil, ErrTooManyOptArgs + } + request = optArgs[0] + } return &GetWorkCmd{ id: id, Request: request, @@ -3727,9 +3734,10 @@ func (cmd *GetWorkCmd) MarshalJSON() ([]byte, error) { Jsonrpc: "1.0", Method: "getwork", Id: cmd.id, - Params: []interface{}{ - cmd.Request, - }, + Params: []interface{}{}, + } + if cmd.Request != nil { + raw.Params = append(raw.Params, cmd.Request) } return json.Marshal(raw) @@ -3786,7 +3794,7 @@ func (cmd *GetWorkCmd) UnmarshalJSON(b []byte) error { wrequest.Algorithm = salgo } - newCmd, err := NewGetWorkCmd(r.Id, *wrequest) + newCmd, err := NewGetWorkCmd(r.Id, wrequest) if err != nil { return err } diff --git a/jsoncmd_test.go b/jsoncmd_test.go index 282e5ebd..e8385ad3 100644 --- a/jsoncmd_test.go +++ b/jsoncmd_test.go @@ -736,7 +736,7 @@ var jsoncmdtests = []struct { cmd: "getwork", f: func() (Cmd, error) { return NewGetWorkCmd(testId, - WorkRequest{ + &WorkRequest{ Data: "some data", Target: "our target", Algorithm: "algo", @@ -744,7 +744,7 @@ var jsoncmdtests = []struct { }, result: &GetWorkCmd{ id: testId, - Request: WorkRequest{ + Request: &WorkRequest{ Data: "some data", Target: "our target", Algorithm: "algo",