Skip to content

Commit

Permalink
GetBlockTemplate RPC client implementation (#1629)
Browse files Browse the repository at this point in the history
* GetBlockTemplate RPC client implementation

* Txid added to the getblocktemplate result

* Omitempty for TxID and improved comment for GetBlockTemplate 'rules' field
  • Loading branch information
elliottminns authored Sep 21, 2020
1 parent f402416 commit 6daaf73
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions btcjson/chainsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ type TemplateRequest struct {
// "proposal".
Data string `json:"data,omitempty"`
WorkID string `json:"workid,omitempty"`

// list of supported softfork deployments, by name
// Ref: https://en.bitcoin.it/wiki/BIP_0009#getblocktemplate_changes.
Rules []string `json:"rules,omitempty"`
}

// convertTemplateRequestField potentially converts the provided value as
Expand Down
6 changes: 4 additions & 2 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ type GetBlockFilterResult struct {
// GetBlockTemplateResultTx models the transactions field of the
// getblocktemplate command.
type GetBlockTemplateResultTx struct {
Data string `json:"data"`
Hash string `json:"hash"`
Data string `json:"data"`
Hash string `json:"hash"`
// TODO: remove omitempty once implemented in rpcserver
TxID string `json:"txid,omitempty"`
Depends []int64 `json:"depends"`
Fee int64 `json:"fee"`
SigOps int64 `json:"sigops"`
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 @@ -295,13 +295,15 @@ var helpDescsEnUS = map[string]string{
"templaterequest-target": "The desired target for the block template (this parameter is ignored)",
"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)",
"getblocktemplateresulttx-depends": "Other transactions before this one (by 1-based index in the 'transactions' list) that must be present in the final block if this one is",
"getblocktemplateresulttx-fee": "Difference in value between transaction inputs and outputs (in Satoshi)",
"getblocktemplateresulttx-sigops": "Total number of signature operations as counted for purposes of block limits",
"getblocktemplateresulttx-txid": "The transaction id, can be different from hash.",
"getblocktemplateresulttx-weight": "The weight of the transaction",

// GetBlockTemplateResultAux help.
Expand Down

0 comments on commit 6daaf73

Please sign in to comment.