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.
This commit is contained in:
Dave Collins 2014-05-21 19:53:46 -05:00
parent 57738b1920
commit 9d9c343247
4 changed files with 81 additions and 36 deletions

View file

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

View file

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

View file

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

View file

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