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

View file

@ -1707,16 +1707,38 @@ func (cmd *GetBestBlockHashCmd) UnmarshalJSON(b []byte) error {
type GetBlockCmd struct { type GetBlockCmd struct {
id interface{} id interface{}
Hash string Hash string
Verbose bool
VerboseTx bool
} }
// Enforce that GetBlockCmd satisifies the Cmd interface. // Enforce that GetBlockCmd satisifies the Cmd interface.
var _ Cmd = &GetBlockCmd{} var _ Cmd = &GetBlockCmd{}
// NewGetBlockCmd creates a new 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{ return &GetBlockCmd{
id: id, id: id,
Hash: hash, Hash: hash,
Verbose: verbose,
VerboseTx: verboseTx,
}, nil }, nil
} }
@ -1734,14 +1756,28 @@ func (cmd *GetBlockCmd) Method() string {
func (cmd *GetBlockCmd) MarshalJSON() ([]byte, error) { func (cmd *GetBlockCmd) MarshalJSON() ([]byte, error) {
// Fill and marshal a RawCmd. // Fill and marshal a RawCmd.
return json.Marshal(RawCmd{ raw := RawCmd{
Jsonrpc: "1.0", Jsonrpc: "1.0",
Method: "getblock", Method: "getblock",
Id: cmd.id, Id: cmd.id,
Params: []interface{}{ Params: []interface{}{
cmd.Hash, 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 // UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of
@ -1753,15 +1789,34 @@ func (cmd *GetBlockCmd) UnmarshalJSON(b []byte) error {
return err return err
} }
if len(r.Params) != 1 { if len(r.Params) > 3 || len(r.Params) < 1 {
return ErrWrongNumberOfParams return ErrWrongNumberOfParams
} }
hash, ok := r.Params[0].(string) hash, ok := r.Params[0].(string)
if !ok { if !ok {
return errors.New("first parameter hash must be a string") 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 { if err != nil {
return err return err
} }