Skip to content

Commit

Permalink
GetBlockTemplate RPC client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottminns committed Sep 8, 2020
1 parent 2547246 commit 95a83d1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ type TemplateRequest struct {
// "proposal".
Data string `json:"data,omitempty"`
WorkID string `json:"workid,omitempty"`

// "rules"
Rules []string `json:"rules,omitempty"`
}

// convertTemplateRequestField potentially converts the provided value as
Expand Down
37 changes: 36 additions & 1 deletion rpcclient/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,39 @@ func (c *Client) SubmitBlock(block *btcutil.Block, options *btcjson.SubmitBlockO
return c.SubmitBlockAsync(block, options).Receive()
}

// TODO(davec): Implement GetBlockTemplate
// FutureGetBlockTemplateResponse is a future promise to deliver the result of a
// GetBlockTemplateAsync RPC invocation (or an applicable error).
type FutureGetBlockTemplateResponse chan *response

// Receive waits for the response promised by the future and returns an error if
// any occurred when retrieving the block template.
func (r FutureGetBlockTemplateResponse) Receive() (*btcjson.GetBlockTemplateResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}

// Unmarshal result as a getwork result object.
var result btcjson.GetBlockTemplateResult
err = json.Unmarshal(res, &result)
if err != nil {
return nil, err
}

return &result, nil
}

// GetBlockTemplateAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See GetBlockTemplate for the blocking version and more details.
func (c *Client) GetBlockTemplateAsync(req *btcjson.TemplateRequest) FutureGetBlockTemplateResponse {
cmd := btcjson.NewGetBlockTemplateCmd(req)
return c.sendCmd(cmd)
}

// GetBlockTemplate returns a new block template for mining.
func (c *Client) GetBlockTemplate(req *btcjson.TemplateRequest) (*btcjson.GetBlockTemplateResult, error) {
return c.GetBlockTemplateAsync(req).Receive()
}
2 changes: 2 additions & 0 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ var helpDescsEnUS = map[string]string{
"templaterequest-data": "Hex-encoded block data (only for mode=proposal)",
"templaterequest-workid": "The server provided workid if provided in block template (not applicable)",

"templaterequest-rules": "Specific block rules that are to be enforced e.g. '[\"segwit\"]",

// GetBlockTemplateResultTx help.
"getblocktemplateresulttx-data": "Hex-encoded transaction data (byte-for-byte)",
"getblocktemplateresulttx-hash": "Hex-encoded transaction hash (little endian if treated as a 256-bit number)",
Expand Down

0 comments on commit 95a83d1

Please sign in to comment.