Fixed TxRawResult/Vin structure and added GetBlockCmd verbose flags

Changed TxRawResult to omitempty block info for mempool tx as well as
moved Txid to Vin.TxId from Vin.ScriptSig.Txid (both match bitcoind
output)

Added support for new bitcoind [verbose=true] and added non-standard
optional verboseTx to return TxRawResults intead Txids
This commit is contained in:
Francis Lam 2013-12-08 14:51:18 -05:00
parent 6325bd9df2
commit 91a19dda85
2 changed files with 86 additions and 29 deletions

View file

@ -47,13 +47,15 @@ type InfoResult struct {
// BlockResult models the data from the getblock command.
type BlockResult struct {
Hex string `json:"hex,omitempty"`
Hash string `json:"hash"`
Confirmations uint64 `json:"confirmations"`
Size int `json:"size"`
Height int64 `json:"height"`
Version uint32 `json:"version"`
MerkleRoot string `json:"merkleroot"`
Tx []string `json:"tx"`
Tx []string `json:"tx,omitempty"`
RawTx []TxRawResult `json:"rawtx,omitempty"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
@ -70,10 +72,10 @@ type TxRawResult struct {
LockTime uint32 `json:"locktime"`
Vin []Vin `json:"vin"`
Vout []Vout `json:"vout"`
BlockHash string `json:"blockhash"`
Confirmations uint64 `json:"confirmations"`
Time int64 `json:"time"`
Blocktime int64 `json:"blocktime"`
BlockHash string `json:"blockhash,omitempty"`
Confirmations uint64 `json:"confirmations,omitempty"`
Time int64 `json:"time,omitempty"`
Blocktime int64 `json:"blocktime,omitempty"`
}
// TxRawDecodeResult models the data from the decoderawtransaction command.
@ -89,9 +91,9 @@ type TxRawDecodeResult struct {
// getrawtransaction and decoderawtransaction use the same structure.
type Vin struct {
Coinbase string `json:"coinbase,omitempty"`
Txid string `json:"txid,omitempty"`
Vout int `json:"vout,omitempty"`
ScriptSig struct {
Txid string `json:"txid"`
Asm string `json:"asm"`
Hex string `json:"hex"`
} `json:"scriptSig,omitempty"`

View file

@ -1707,16 +1707,38 @@ func (cmd *GetBestBlockHashCmd) UnmarshalJSON(b []byte) error {
type GetBlockCmd struct {
id interface{}
Hash string
Verbose bool
VerboseTx bool
}
// Enforce that GetBlockCmd satisifies the Cmd interface.
var _ Cmd = &GetBlockCmd{}
// NewGetBlockCmd creates a new GetBlockCmd.
func NewGetBlockCmd(id interface{}, hash string) (*GetBlockCmd, error) {
func NewGetBlockCmd(id interface{}, hash string, optArgs ...bool) (*GetBlockCmd, error) {
// default verbose is set to true to match old behavior
verbose, verboseTx := true, false
optArgsLen := len(optArgs)
if optArgsLen > 0 {
if optArgsLen > 2 {
return nil, ErrTooManyOptArgs
}
verbose = optArgs[0]
if optArgsLen > 1 {
verboseTx = optArgs[1]
if !verbose && verboseTx {
return nil, ErrInvalidParams
}
}
}
return &GetBlockCmd{
id: id,
Hash: hash,
Verbose: verbose,
VerboseTx: verboseTx,
}, nil
}
@ -1734,14 +1756,28 @@ func (cmd *GetBlockCmd) Method() string {
func (cmd *GetBlockCmd) MarshalJSON() ([]byte, error) {
// Fill and marshal a RawCmd.
return json.Marshal(RawCmd{
raw := RawCmd{
Jsonrpc: "1.0",
Method: "getblock",
Id: cmd.id,
Params: []interface{}{
cmd.Hash,
},
})
}
if !cmd.Verbose {
// set optional verbose argument to false
raw.Params = append(raw.Params, false)
} else {
if cmd.VerboseTx {
// set optional verbose argument to true
raw.Params = append(raw.Params, true)
// set optional verboseTx argument to true
raw.Params = append(raw.Params, true)
}
}
return json.Marshal(raw)
}
// UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of
@ -1753,15 +1789,34 @@ func (cmd *GetBlockCmd) UnmarshalJSON(b []byte) error {
return err
}
if len(r.Params) != 1 {
if len(r.Params) > 3 || len(r.Params) < 1 {
return ErrWrongNumberOfParams
}
hash, ok := r.Params[0].(string)
if !ok {
return errors.New("first parameter hash must be a string")
}
newCmd, err := NewGetBlockCmd(r.Id, hash)
optArgs := make([]bool, 0, 1)
if len(r.Params) > 1 {
verbose, ok := r.Params[1].(bool)
if !ok {
return errors.New("second optional parameter verbose must be a bool")
}
optArgs = append(optArgs, verbose)
}
if len(r.Params) == 3 {
verboseTx, ok := r.Params[2].(bool)
if !ok {
return errors.New("third optional parameter verboseTx must be a bool")
}
optArgs = append(optArgs, verboseTx)
}
newCmd, err := NewGetBlockCmd(r.Id, hash, optArgs...)
if err != nil {
return err
}