Esempio n. 1
0
// ListSinceBlock handles a listsinceblock request by returning an array of maps
// with details of sent and received wallet transactions since the given block.
func ListSinceBlock(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
	cmd, ok := icmd.(*btcjson.ListSinceBlockCmd)
	if !ok {
		return nil, &btcjson.ErrInternal
	}

	height := int32(-1)
	if cmd.BlockHash != "" {
		br, err := GetBlock(CurrentRPCConn(), cmd.BlockHash)
		if err != nil {
			return nil, err
		}
		height = int32(br.Height)
	}

	bs, err := GetCurBlock()
	if err != nil {
		return nil, &btcjson.Error{
			Code:    btcjson.ErrWallet.Code,
			Message: err.Error(),
		}
	}

	// For the result we need the block hash for the last block counted
	// in the blockchain due to confirmations. We send this off now so that
	// it can arrive asynchronously while we figure out the rest.
	gbh, err := btcjson.NewGetBlockHashCmd(<-NewJSONID,
		int64(bs.Height)+1-int64(cmd.TargetConfirmations))
	if err != nil {
		return nil, &btcjson.Error{
			Code:    btcjson.ErrWallet.Code,
			Message: err.Error(),
		}
	}

	bhChan := CurrentRPCConn().SendRequest(NewRPCRequest(gbh, new(string)))

	txInfoList, err := accountstore.ListSinceBlock(height, bs.Height,
		cmd.TargetConfirmations)
	if err != nil {
		return nil, &btcjson.Error{
			Code:    btcjson.ErrWallet.Code,
			Message: err.Error(),
		}
	}

	// Done with work, get the response.
	response := <-bhChan
	if response.Err != nil {
		return nil, response.Err
	}

	hash := response.Result.(*string)

	res := make(map[string]interface{})
	res["transactions"] = txInfoList
	res["lastblock"] = *hash

	return res, nil
}
Esempio n. 2
0
// GetBlockHashAsync 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 GetBlockHash for the blocking version and more details.
func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult {
	id := c.NextID()
	cmd, err := btcjson.NewGetBlockHashCmd(id, blockHeight)
	if err != nil {
		return newFutureError(err)
	}

	return c.sendCmd(cmd)
}
Esempio n. 3
0
func getBlockHash(idx int64) (string, error) {
	cmd, err := btcjson.NewGetBlockHashCmd("blocksafari", idx)
	if err != nil {
		return "", err
	}

	msg, err := json.Marshal(cmd)
	if err != nil {
		return "", err
	}

	reply, err := btcjson.TlsRpcCommand(cfg.RPCUser, cfg.RPCPassword, cfg.RPCServer, msg, pem, false)
	if err != nil {
		return "", err
	}

	if reply.Error != nil {
		return "", reply.Error
	}

	return reply.Result.(string), nil
}
Esempio n. 4
0
// makeGetBlockHash generates the cmd structure for getblockhash commands.
func makeGetBlockHash(args []interface{}) (btcjson.Cmd, error) {
	return btcjson.NewGetBlockHashCmd("btcctl", args[0].(int64))
}