GetBlockTemplate RPC client implementation (#1629)

* GetBlockTemplate RPC client implementation

* Txid added to the getblocktemplate result

* Omitempty for TxID and improved comment for GetBlockTemplate 'rules' field
This commit is contained in:
Elliott Minns 2020-09-21 08:42:35 -05:00 committed by GitHub
parent f4024160f3
commit 6daaf73544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 3 deletions

View file

@ -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

View file

@ -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"`

View file

@ -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()
}

View file

@ -295,6 +295,7 @@ 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)",
@ -302,6 +303,7 @@ var helpDescsEnUS = map[string]string{
"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.