From 9d9c3432477eaf4d5bc02a97bcb4369334b5813c Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 21 May 2014 19:53:46 -0500 Subject: [PATCH] Make nil pointer handling consistent across RPCs. There are several RPCs which accept a pointer to a hash, transaction, block, etc. Previously not all RPCs handled being passed a nil pointer consistently. Closes #4. --- chain.go | 15 +++++++-- mining.go | 14 +++++--- rawtransactions.go | 79 ++++++++++++++++++++++++++++++---------------- wallet.go | 9 +++++- 4 files changed, 81 insertions(+), 36 deletions(-) diff --git a/chain.go b/chain.go index 9e580824..0b18faa5 100644 --- a/chain.go +++ b/chain.go @@ -96,8 +96,13 @@ func (r FutureGetBlockResult) Receive() (*btcutil.Block, error) { // // See GetBlock for the blocking version and more details. func (c *Client) GetBlockAsync(blockHash *btcwire.ShaHash) FutureGetBlockResult { + hash := "" + if blockHash != nil { + hash = blockHash.String() + } + id := c.NextID() - cmd, err := btcjson.NewGetBlockCmd(id, blockHash.String(), false) + cmd, err := btcjson.NewGetBlockCmd(id, hash, false) if err != nil { return newFutureError(err) } @@ -141,9 +146,13 @@ func (r FutureGetBlockVerboseResult) Receive() (*btcjson.BlockResult, error) { // // See GetBlockVerbose for the blocking version and more details. func (c *Client) GetBlockVerboseAsync(blockHash *btcwire.ShaHash, verboseTx bool) FutureGetBlockVerboseResult { + hash := "" + if blockHash != nil { + hash = blockHash.String() + } + id := c.NextID() - cmd, err := btcjson.NewGetBlockCmd(id, blockHash.String(), true, - verboseTx) + cmd, err := btcjson.NewGetBlockCmd(id, hash, true, verboseTx) if err != nil { return newFutureError(err) } diff --git a/mining.go b/mining.go index 924c3caf..8336d239 100644 --- a/mining.go +++ b/mining.go @@ -384,13 +384,17 @@ func (r FutureSubmitBlockResult) Receive() error { // // See SubmitBlock for the blocking version and more details. func (c *Client) SubmitBlockAsync(block *btcutil.Block, options *btcjson.SubmitBlockOptions) FutureSubmitBlockResult { - id := c.NextID() - blockBytes, err := block.Bytes() - if err != nil { - return newFutureError(err) + blockHex := "" + if block != nil { + blockBytes, err := block.Bytes() + if err != nil { + return newFutureError(err) + } + + blockHex = hex.EncodeToString(blockBytes) } - blockHex := hex.EncodeToString(blockBytes) + id := c.NextID() cmd, err := btcjson.NewSubmitBlockCmd(id, blockHex, options) if err != nil { return newFutureError(err) diff --git a/rawtransactions.go b/rawtransactions.go index a1ef6cf8..d1657ff9 100644 --- a/rawtransactions.go +++ b/rawtransactions.go @@ -95,8 +95,13 @@ func (r FutureGetRawTransactionResult) Receive() (*btcutil.Tx, error) { // // See GetRawTransaction for the blocking version and more details. func (c *Client) GetRawTransactionAsync(txHash *btcwire.ShaHash) FutureGetRawTransactionResult { + hash := "" + if txHash != nil { + hash = txHash.String() + } + id := c.NextID() - cmd, err := btcjson.NewGetRawTransactionCmd(id, txHash.String(), 0) + cmd, err := btcjson.NewGetRawTransactionCmd(id, hash, 0) if err != nil { return newFutureError(err) } @@ -141,8 +146,13 @@ func (r FutureGetRawTransactionVerboseResult) Receive() (*btcjson.TxRawResult, e // // See GetRawTransactionVerbose for the blocking version and more details. func (c *Client) GetRawTransactionVerboseAsync(txHash *btcwire.ShaHash) FutureGetRawTransactionVerboseResult { + hash := "" + if txHash != nil { + hash = txHash.String() + } + id := c.NextID() - cmd, err := btcjson.NewGetRawTransactionCmd(id, txHash.String(), 1) + cmd, err := btcjson.NewGetRawTransactionCmd(id, hash, 1) if err != nil { return newFutureError(err) } @@ -294,12 +304,15 @@ func (r FutureSendRawTransactionResult) Receive() (*btcwire.ShaHash, error) { // // See SendRawTransaction for the blocking version and more details. func (c *Client) SendRawTransactionAsync(tx *btcwire.MsgTx, allowHighFees bool) FutureSendRawTransactionResult { - // Serialize the transaction and convert to hex string. - buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) - if err := tx.Serialize(buf); err != nil { - return newFutureError(err) + txHex := "" + if tx != nil { + // Serialize the transaction and convert to hex string. + buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) + if err := tx.Serialize(buf); err != nil { + return newFutureError(err) + } + txHex = hex.EncodeToString(buf.Bytes()) } - txHex := hex.EncodeToString(buf.Bytes()) id := c.NextID() cmd, err := btcjson.NewSendRawTransactionCmd(id, txHex, allowHighFees) @@ -357,12 +370,15 @@ func (r FutureSignRawTransactionResult) Receive() (*btcwire.MsgTx, bool, error) // // See SignRawTransaction for the blocking version and more details. func (c *Client) SignRawTransactionAsync(tx *btcwire.MsgTx) FutureSignRawTransactionResult { - // Serialize the transaction and convert to hex string. - buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) - if err := tx.Serialize(buf); err != nil { - return newFutureError(err) + txHex := "" + if tx != nil { + // Serialize the transaction and convert to hex string. + buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) + if err := tx.Serialize(buf); err != nil { + return newFutureError(err) + } + txHex = hex.EncodeToString(buf.Bytes()) } - txHex := hex.EncodeToString(buf.Bytes()) id := c.NextID() cmd, err := btcjson.NewSignRawTransactionCmd(id, txHex) @@ -390,12 +406,15 @@ func (c *Client) SignRawTransaction(tx *btcwire.MsgTx) (*btcwire.MsgTx, bool, er // // See SignRawTransaction2 for the blocking version and more details. func (c *Client) SignRawTransaction2Async(tx *btcwire.MsgTx, inputs []btcjson.RawTxInput) FutureSignRawTransactionResult { - // Serialize the transaction and convert to hex string. - buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) - if err := tx.Serialize(buf); err != nil { - return newFutureError(err) + txHex := "" + if tx != nil { + // Serialize the transaction and convert to hex string. + buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) + if err := tx.Serialize(buf); err != nil { + return newFutureError(err) + } + txHex = hex.EncodeToString(buf.Bytes()) } - txHex := hex.EncodeToString(buf.Bytes()) id := c.NextID() cmd, err := btcjson.NewSignRawTransactionCmd(id, txHex, inputs) @@ -429,12 +448,15 @@ func (c *Client) SignRawTransaction3Async(tx *btcwire.MsgTx, inputs []btcjson.RawTxInput, privKeysWIF []string) FutureSignRawTransactionResult { - // Serialize the transaction and convert to hex string. - buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) - if err := tx.Serialize(buf); err != nil { - return newFutureError(err) + txHex := "" + if tx != nil { + // Serialize the transaction and convert to hex string. + buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) + if err := tx.Serialize(buf); err != nil { + return newFutureError(err) + } + txHex = hex.EncodeToString(buf.Bytes()) } - txHex := hex.EncodeToString(buf.Bytes()) id := c.NextID() cmd, err := btcjson.NewSignRawTransactionCmd(id, txHex, inputs, @@ -479,12 +501,15 @@ func (c *Client) SignRawTransaction4Async(tx *btcwire.MsgTx, inputs []btcjson.RawTxInput, privKeysWIF []string, hashType SigHashType) FutureSignRawTransactionResult { - // Serialize the transaction and convert to hex string. - buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) - if err := tx.Serialize(buf); err != nil { - return newFutureError(err) + txHex := "" + if tx != nil { + // Serialize the transaction and convert to hex string. + buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize())) + if err := tx.Serialize(buf); err != nil { + return newFutureError(err) + } + txHex = hex.EncodeToString(buf.Bytes()) } - txHex := hex.EncodeToString(buf.Bytes()) id := c.NextID() cmd, err := btcjson.NewSignRawTransactionCmd(id, txHex, inputs, diff --git a/wallet.go b/wallet.go index 486fc7be..47f812e6 100644 --- a/wallet.go +++ b/wallet.go @@ -44,8 +44,13 @@ func (r FutureGetTransactionResult) Receive() (*btcjson.GetTransactionResult, er // // See GetTransaction for the blocking version and more details. func (c *Client) GetTransactionAsync(txHash *btcwire.ShaHash) FutureGetTransactionResult { + hash := "" + if txHash != nil { + hash = txHash.String() + } + id := c.NextID() - cmd, err := btcjson.NewGetTransactionCmd(id, txHash.String()) + cmd, err := btcjson.NewGetTransactionCmd(id, hash) if err != nil { return newFutureError(err) } @@ -316,6 +321,7 @@ func (c *Client) ListSinceBlockAsync(blockHash *btcwire.ShaHash) FutureListSince if blockHash != nil { hash = blockHash.String() } + id := c.NextID() cmd, err := btcjson.NewListSinceBlockCmd(id, hash) if err != nil { @@ -344,6 +350,7 @@ func (c *Client) ListSinceBlockMinConfAsync(blockHash *btcwire.ShaHash, minConfi if blockHash != nil { hash = blockHash.String() } + id := c.NextID() cmd, err := btcjson.NewListSinceBlockCmd(id, hash, minConfirms) if err != nil {