Skip to content

Commit

Permalink
rpcclient: Export receiveFuture
Browse files Browse the repository at this point in the history
This facilitates using custom commands with rpcclient.

See #1083
  • Loading branch information
JeremyRand committed May 19, 2020
1 parent 00c1cdf commit 78aca1e
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 108 deletions.
40 changes: 20 additions & 20 deletions rpcclient/chain.go
Expand Up @@ -22,7 +22,7 @@ type FutureGetBestBlockHashResult chan *Response
// Receive waits for the Response promised by the future and returns the hash of
// the best block in the longest block chain.
func (r FutureGetBestBlockHashResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (c *Client) legacyGetBlockRequest(hash string, verbose,
func (c *Client) waitForGetBlockRes(respChan chan *Response, hash string,
verbose, verboseTx bool) ([]byte, error) {

res, err := receiveFuture(respChan)
res, err := ReceiveFuture(respChan)

// If we receive an invalid parameter error, then we may be
// communicating with a btcd node which only understands the legacy
Expand Down Expand Up @@ -278,7 +278,7 @@ type FutureGetBlockCountResult chan *Response
// Receive waits for the Response promised by the future and returns the number
// of blocks in the longest block chain.
func (r FutureGetBlockCountResult) Receive() (int64, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -314,7 +314,7 @@ type FutureGetDifficultyResult chan *Response
// Receive waits for the Response promised by the future and returns the
// proof-of-work difficulty as a multiple of the minimum difficulty.
func (r FutureGetDifficultyResult) Receive() (float64, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func unmarshalGetBlockChainInfoResultSoftForks(chainInfo *btcjson.GetBlockChainI
// Receive waits for the Response promised by the future and returns chain info
// result provided by the server.
func (r FutureGetBlockChainInfoResult) Receive() (*btcjson.GetBlockChainInfoResult, error) {
res, err := receiveFuture(r.Response)
res, err := ReceiveFuture(r.Response)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -442,7 +442,7 @@ type FutureGetBlockHashResult chan *Response
// Receive waits for the Response promised by the future and returns the hash of
// the block in the best block chain at the given height.
func (r FutureGetBlockHashResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -479,7 +479,7 @@ type FutureGetBlockHeaderResult chan *Response
// Receive waits for the Response promised by the future and returns the
// blockheader requested from the server given its hash.
func (r FutureGetBlockHeaderResult) Receive() (*wire.BlockHeader, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -536,7 +536,7 @@ type FutureGetBlockHeaderVerboseResult chan *Response
// Receive waits for the Response promised by the future and returns the
// data structure of the blockheader requested from the server given its hash.
func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -582,7 +582,7 @@ type FutureGetMempoolEntryResult chan *Response
// structure with information about the transaction in the memory pool given
// its hash.
func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -620,7 +620,7 @@ type FutureGetRawMempoolResult chan *Response
// Receive waits for the Response promised by the future and returns the hashes
// of all transactions in the memory pool.
func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -671,7 +671,7 @@ type FutureGetRawMempoolVerboseResult chan *Response
// transaction hashes to an associated data structure with information about the
// transaction for all transactions in the memory pool.
func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -712,7 +712,7 @@ type FutureEstimateFeeResult chan *Response
// Receive waits for the Response promised by the future and returns the info
// provided by the server.
func (r FutureEstimateFeeResult) Receive() (float64, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return -1, err
}
Expand Down Expand Up @@ -749,7 +749,7 @@ type FutureEstimateSmartFeeResult chan *Response
// Receive waits for the response promised by the future and returns the
// estimated fee.
func (r FutureEstimateSmartFeeResult) Receive() (*btcjson.EstimateSmartFeeResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -786,7 +786,7 @@ type FutureVerifyChainResult chan *Response
// or not the chain verified based on the check level and number of blocks
// to verify specified in the original call.
func (r FutureVerifyChainResult) Receive() (bool, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -873,7 +873,7 @@ type FutureGetTxOutResult chan *Response
// Receive waits for the Response promised by the future and returns a
// transaction given its hash.
func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -928,7 +928,7 @@ type FutureRescanBlocksResult chan *Response
// NOTE: This is a btcsuite extension ported from
// github.com/decred/dcrrpcclient.
func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -977,7 +977,7 @@ type FutureInvalidateBlockResult chan *Response
// Receive waits for the Response promised by the future and returns the raw
// block requested from the server given its hash.
func (r FutureInvalidateBlockResult) Receive() error {
_, err := receiveFuture(r)
_, err := ReceiveFuture(r)

return err
}
Expand Down Expand Up @@ -1009,7 +1009,7 @@ type FutureGetCFilterResult chan *Response
// Receive waits for the Response promised by the future and returns the raw
// filter requested from the server given its block hash.
func (r FutureGetCFilterResult) Receive() (*wire.MsgCFilter, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ type FutureGetCFilterHeaderResult chan *Response
// Receive waits for the Response promised by the future and returns the raw
// filter header requested from the server given its block hash.
func (r FutureGetCFilterHeaderResult) Receive() (*wire.MsgCFHeaders, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1118,7 +1118,7 @@ type FutureGetBlockStatsResult chan *Response
// Receive waits for the response promised by the future and returns statistics
// of a block at a certain height.
func (r FutureGetBlockStatsResult) Receive() (*btcjson.GetBlockStatsResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down
18 changes: 9 additions & 9 deletions rpcclient/extensions.go
Expand Up @@ -26,7 +26,7 @@ type FutureDebugLevelResult chan *Response
// of setting the debug logging level to the passed level specification or the
// list of of the available subsystems for the special keyword 'show'.
func (r FutureDebugLevelResult) Receive() (string, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -72,7 +72,7 @@ type FutureCreateEncryptedWalletResult chan *Response

// Receive waits for and returns the error Response promised by the future.
func (r FutureCreateEncryptedWalletResult) Receive() error {
_, err := receiveFuture(r)
_, err := ReceiveFuture(r)
return err
}

Expand Down Expand Up @@ -107,7 +107,7 @@ type FutureListAddressTransactionsResult chan *Response
// Receive waits for the Response promised by the future and returns information
// about all transactions associated with the provided addresses.
func (r FutureListAddressTransactionsResult) Receive() ([]btcjson.ListTransactionsResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -153,7 +153,7 @@ type FutureGetBestBlockResult chan *Response
// Receive waits for the Response promised by the future and returns the hash
// and height of the block in the longest (best) chain.
func (r FutureGetBestBlockResult) Receive() (*chainhash.Hash, int32, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -201,7 +201,7 @@ type FutureGetCurrentNetResult chan *Response
// Receive waits for the Response promised by the future and returns the network
// the server is running on.
func (r FutureGetCurrentNetResult) Receive() (wire.BitcoinNet, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -248,7 +248,7 @@ type FutureGetHeadersResult chan *Response
// NOTE: This is a btcsuite extension ported from
// github.com/decred/dcrrpcclient.
func (r FutureGetHeadersResult) Receive() ([]wire.BlockHeader, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -312,7 +312,7 @@ type FutureExportWatchingWalletResult chan *Response
// Receive waits for the Response promised by the future and returns the
// exported wallet.
func (r FutureExportWatchingWalletResult) Receive() ([]byte, []byte, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -381,7 +381,7 @@ type FutureSessionResult chan *Response
// Receive waits for the Response promised by the future and returns the
// session result.
func (r FutureSessionResult) Receive() (*btcjson.SessionResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -436,7 +436,7 @@ type FutureVersionResult chan *Response
// github.com/decred/dcrrpcclient.
func (r FutureVersionResult) Receive() (map[string]btcjson.VersionResult,
error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions rpcclient/infrastructure.go
Expand Up @@ -819,11 +819,11 @@ func newFutureError(err error) chan *Response {
return responseChan
}

// receiveFuture receives from the passed futureResult channel to extract a
// ReceiveFuture receives from the passed futureResult channel to extract a
// reply or any errors. The examined errors include an error in the
// futureResult and the error in the reply from the server. This will block
// until the result is available on the passed channel.
func receiveFuture(f chan *Response) ([]byte, error) {
func ReceiveFuture(f chan *Response) ([]byte, error) {
// Wait for a response on the returned channel.
r := <-f
return r.result, r.err
Expand Down Expand Up @@ -929,7 +929,7 @@ func (c *Client) SendCmd(cmd interface{}) chan *Response {
func (c *Client) SendCmdAndWait(cmd interface{}) (interface{}, error) {
// Marshal the command to JSON-RPC, send it to the connected server, and
// wait for a response on the returned channel.
return receiveFuture(c.SendCmd(cmd))
return ReceiveFuture(c.SendCmd(cmd))
}

// Disconnected returns whether or not the server is disconnected. If a
Expand Down
20 changes: 10 additions & 10 deletions rpcclient/mining.go
Expand Up @@ -21,7 +21,7 @@ type FutureGenerateResult chan *Response
// Receive waits for the Response promised by the future and returns a list of
// block hashes generated by the call.
func (r FutureGenerateResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -68,7 +68,7 @@ type FutureGenerateToAddressResult chan *Response
// Receive waits for the response promised by the future and returns the hashes of
// of the generated blocks.
func (f FutureGenerateToAddressResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(f)
res, err := ReceiveFuture(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,7 +115,7 @@ type FutureGetGenerateResult chan *Response
// Receive waits for the Response promised by the future and returns true if the
// server is set to mine, otherwise false.
func (r FutureGetGenerateResult) Receive() (bool, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -152,7 +152,7 @@ type FutureSetGenerateResult chan *Response
// Receive waits for the Response promised by the future and returns an error if
// any occurred when setting the server to generate coins (mine) or not.
func (r FutureSetGenerateResult) Receive() error {
_, err := receiveFuture(r)
_, err := ReceiveFuture(r)
return err
}

Expand All @@ -179,7 +179,7 @@ type FutureGetHashesPerSecResult chan *Response
// hashes per second performance measurement while generating coins (mining).
// Zero is returned if the server is not mining.
func (r FutureGetHashesPerSecResult) Receive() (int64, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return -1, err
}
Expand Down Expand Up @@ -218,7 +218,7 @@ type FutureGetMiningInfoResult chan *Response
// Receive waits for the Response promised by the future and returns the mining
// information.
func (r FutureGetMiningInfoResult) Receive() (*btcjson.GetMiningInfoResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ type FutureGetNetworkHashPS chan *Response
// estimated network hashes per second for the block heights provided by the
// parameters.
func (r FutureGetNetworkHashPS) Receive() (int64, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return -1, err
}
Expand Down Expand Up @@ -338,7 +338,7 @@ type FutureGetWork chan *Response
// Receive waits for the Response promised by the future and returns the hash
// data to work on.
func (r FutureGetWork) Receive() (*btcjson.GetWorkResult, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -377,7 +377,7 @@ type FutureGetWorkSubmit chan *Response
// Receive waits for the Response promised by the future and returns whether
// or not the submitted block header was accepted.
func (r FutureGetWorkSubmit) Receive() (bool, error) {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -417,7 +417,7 @@ type FutureSubmitBlockResult chan *Response
// Receive waits for the Response promised by the future and returns an error if
// any occurred when submitting the block.
func (r FutureSubmitBlockResult) Receive() error {
res, err := receiveFuture(r)
res, err := ReceiveFuture(r)
if err != nil {
return err
}
Expand Down

0 comments on commit 78aca1e

Please sign in to comment.