From 6daaf73544d70aea67efe468651648a6d03c7b25 Mon Sep 17 00:00:00 2001 From: Elliott Minns Date: Mon, 21 Sep 2020 08:42:35 -0500 Subject: [PATCH] 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 --- btcjson/chainsvrcmds.go | 4 ++++ btcjson/chainsvrresults.go | 6 ++++-- rpcclient/mining.go | 37 ++++++++++++++++++++++++++++++++++++- rpcserverhelp.go | 2 ++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/btcjson/chainsvrcmds.go b/btcjson/chainsvrcmds.go index b15bce89..0e683af0 100644 --- a/btcjson/chainsvrcmds.go +++ b/btcjson/chainsvrcmds.go @@ -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 diff --git a/btcjson/chainsvrresults.go b/btcjson/chainsvrresults.go index bdd135b5..9cafe334 100644 --- a/btcjson/chainsvrresults.go +++ b/btcjson/chainsvrresults.go @@ -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"` diff --git a/rpcclient/mining.go b/rpcclient/mining.go index 8717d327..431054e1 100644 --- a/rpcclient/mining.go +++ b/rpcclient/mining.go @@ -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() +} diff --git a/rpcserverhelp.go b/rpcserverhelp.go index ca6d2e79..75a63be0 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -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.