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.
type BlockResult struct {
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"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash"`
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,omitempty"`
RawTx []TxRawResult `json:"rawtx,omitempty"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash"`
}
// TxRawResult models the data from the getrawtransaction command.
@ -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,11 +91,11 @@ 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"`
Asm string `json:"asm"`
Hex string `json:"hex"`
} `json:"scriptSig,omitempty"`
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
// unmarshaling of getblock JSON RPC commands.
type GetBlockCmd struct {
id interface{}
Hash string
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,
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
}