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,19 +47,21 @@ 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 {
Hash string `json:"hash"` Hex string `json:"hex,omitempty"`
Confirmations uint64 `json:"confirmations"` Hash string `json:"hash"`
Size int `json:"size"` Confirmations uint64 `json:"confirmations"`
Height int64 `json:"height"` Size int `json:"size"`
Version uint32 `json:"version"` Height int64 `json:"height"`
MerkleRoot string `json:"merkleroot"` Version uint32 `json:"version"`
Tx []string `json:"tx"` MerkleRoot string `json:"merkleroot"`
Time int64 `json:"time"` Tx []string `json:"tx,omitempty"`
Nonce uint32 `json:"nonce"` RawTx []TxRawResult `json:"rawtx,omitempty"`
Bits string `json:"bits"` Time int64 `json:"time"`
Difficulty float64 `json:"difficulty"` Nonce uint32 `json:"nonce"`
PreviousHash string `json:"previousblockhash"` Bits string `json:"bits"`
NextHash string `json:"nextblockhash"` Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash"`
} }
// TxRawResult models the data from the getrawtransaction command. // TxRawResult models the data from the getrawtransaction command.
@ -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,11 +91,11 @@ 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"`
Sequence float64 `json:"sequence"` Sequence float64 `json:"sequence"`
} }

View file

@ -1705,18 +1705,40 @@ func (cmd *GetBestBlockHashCmd) UnmarshalJSON(b []byte) error {
// GetBlockCmd is a type handling custom marshaling and // GetBlockCmd is a type handling custom marshaling and
// unmarshaling of getblock JSON RPC commands. // unmarshaling of getblock JSON RPC commands.
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
} }