Change searchrawtransaction to be compatible with bitcoind

* Pluralise `searchrawtransaction` -> `searchrawtransactions`
 * Change the `verbose` parameter from a bool to an int.
This commit is contained in:
Olaoluwa Osuntokun 2015-02-05 11:35:18 -08:00
parent dc4989461a
commit 1b57f5bf87
7 changed files with 44 additions and 44 deletions

View file

@ -652,7 +652,7 @@ getpeerinfo.`,
"reconsiderblock": `reconsiderblock "hash"
Remove invalid mark from block specified by "hash" so it is considered again.`,
"searchrawtransaction": `searchrawtransaction "address" (verbose=1 skip=0 count=100)
"searchrawtransactions": `searchrawtransactions "address" (verbose=1 skip=0 count=100)
Returns raw tx data related to credits or debits to "address". Skip indicates
the number of leading transactions to leave out of the final result. Count
represents the max number of transactions to return. If verbose is false, a

View file

@ -619,7 +619,7 @@ func CreateMessageWithId(message string, id interface{}, args ...interface{}) ([
*/
finalMessage, err = jsonWithArgs(message, id, []interface{}{args[0].(string), txList})
// one required string (address), one optional bool, two optional ints.
case "searchrawtransaction":
case "searchrawtransactions":
if len(args) > 4 || len(args) == 0 {
err = fmt.Errorf("wrong number of arguments for %s", message)
return finalMessage, err
@ -629,7 +629,7 @@ func CreateMessageWithId(message string, id interface{}, args ...interface{}) ([
ok3 := true
ok4 := true
if len(args) >= 2 {
_, ok2 = args[1].(bool)
_, ok2 = args[1].(int)
}
if len(args) >= 3 {
_, ok3 = args[2].(int)

View file

@ -181,12 +181,12 @@ var cmdtests = []struct {
{"signrawtransaction", []interface{}{1.2, "test", "test2", "test3", "test4"}, false},
{"signrawtransaction", []interface{}{"hexstring", 1, "test2", "test3", "test4"}, false},
{"signrawtransaction", []interface{}{"hexstring", "test", "test2", 3, "test4"}, false},
{"searchrawtransaction", []interface{}{"someaddr"}, true},
{"searchrawtransaction", []interface{}{"someaddr", true}, true},
{"searchrawtransaction", []interface{}{"someaddr", false, 1}, true},
{"searchrawtransaction", []interface{}{"someaddr", true, 5, 500}, true},
{"searchrawtransaction", []interface{}{"someaddr", true, 5, "test"}, false},
{"searchrawtransaction", []interface{}{}, false},
{"searchrawtransactions", []interface{}{"someaddr"}, true},
{"searchrawtransactions", []interface{}{"someaddr", 1}, true},
{"searchrawtransactions", []interface{}{"someaddr", 0, 1}, true},
{"searchrawtransactions", []interface{}{"someaddr", 1, 5, 500}, true},
{"searchrawtransactions", []interface{}{"someaddr", 1, 5, "test"}, false},
{"searchrawtransactions", []interface{}{}, false},
{"listsinceblock", []interface{}{"test", "test"}, true},
{"listsinceblock", []interface{}{"test", "test", "test"}, false},
{"listsinceblock", []interface{}{"test"}, true},

View file

@ -286,8 +286,8 @@ func ParseMarshaledCmd(b []byte) (Cmd, error) {
case "reconsiderblock":
cmd = new(ReconsiderBlockCmd)
case "searchrawtransaction":
cmd = new(SearchRawTransactionCmd)
case "searchrawtransactions":
cmd = new(SearchRawTransactionsCmd)
case "sendfrom":
cmd = new(SendFromCmd)
@ -5322,21 +5322,21 @@ func (cmd *ReconsiderBlockCmd) UnmarshalJSON(b []byte) error {
return nil
}
// SearchRawTransactionCmd is a type handling custom marshaling and
// unmarshaling of sendrawtransaction JSON RPC commands.
type SearchRawTransactionCmd struct {
// SearchRawTransactionsCmd is a type handling custom marshaling and
// unmarshaling of sendrawtransactions JSON RPC commands.
type SearchRawTransactionsCmd struct {
id interface{}
Address string
Verbose bool
Verbose int
Skip int
Count int
}
// NewSearchRawTransactionCmd creates a new SearchRawTransactionCmd.
func NewSearchRawTransactionCmd(id interface{}, address string,
optArgs ...interface{}) (*SearchRawTransactionCmd, error) {
verbose := true
// NewSearchRawTransactionsCmd creates a new SearchRawTransactionsCmd.
func NewSearchRawTransactionsCmd(id interface{}, address string,
optArgs ...interface{}) (*SearchRawTransactionsCmd, error) {
var skip int
verbose := 1
count := 100
if len(optArgs) > 3 {
@ -5344,9 +5344,9 @@ func NewSearchRawTransactionCmd(id interface{}, address string,
}
if len(optArgs) > 0 {
v, ok := optArgs[0].(bool)
v, ok := optArgs[0].(int)
if !ok {
return nil, errors.New("first optional argument verbose is not a bool")
return nil, errors.New("first optional argument verbose is not a int")
}
verbose = v
@ -5367,7 +5367,7 @@ func NewSearchRawTransactionCmd(id interface{}, address string,
count = c
}
return &SearchRawTransactionCmd{
return &SearchRawTransactionsCmd{
id: id,
Address: address,
Verbose: verbose,
@ -5377,20 +5377,20 @@ func NewSearchRawTransactionCmd(id interface{}, address string,
}
// Id satisfies the Cmd interface by returning the id of the command.
func (cmd *SearchRawTransactionCmd) Id() interface{} {
func (cmd *SearchRawTransactionsCmd) Id() interface{} {
return cmd.id
}
// Method satisfies the Cmd interface by returning the json method.
func (cmd *SearchRawTransactionCmd) Method() string {
return "searchrawtransaction"
func (cmd *SearchRawTransactionsCmd) Method() string {
return "searchrawtransactions"
}
// MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface.
func (cmd *SearchRawTransactionCmd) MarshalJSON() ([]byte, error) {
func (cmd *SearchRawTransactionsCmd) MarshalJSON() ([]byte, error) {
params := make([]interface{}, 1, 4)
params[0] = cmd.Address
if !cmd.Verbose || cmd.Skip != 0 || cmd.Count != 100 {
if cmd.Verbose != 1 || cmd.Skip != 0 || cmd.Count != 100 {
params = append(params, cmd.Verbose)
}
if cmd.Skip != 0 || cmd.Count != 100 {
@ -5411,7 +5411,7 @@ func (cmd *SearchRawTransactionCmd) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of
// the Cmd interface.
func (cmd *SearchRawTransactionCmd) UnmarshalJSON(b []byte) error {
func (cmd *SearchRawTransactionsCmd) UnmarshalJSON(b []byte) error {
// Unmarshal into a RawCmd
var r RawCmd
if err := json.Unmarshal(b, &r); err != nil {
@ -5429,9 +5429,9 @@ func (cmd *SearchRawTransactionCmd) UnmarshalJSON(b []byte) error {
optArgs := make([]interface{}, 0, 3)
if len(r.Params) > 1 {
var verbose bool
var verbose int
if err := json.Unmarshal(r.Params[1], &verbose); err != nil {
return fmt.Errorf("second optional parameter 'verbose' must be an bool: %v", err)
return fmt.Errorf("second optional parameter 'verbose' must be an int: %v", err)
}
optArgs = append(optArgs, verbose)
}
@ -5450,7 +5450,7 @@ func (cmd *SearchRawTransactionCmd) UnmarshalJSON(b []byte) error {
optArgs = append(optArgs, count)
}
newCmd, err := NewSearchRawTransactionCmd(r.Id, address, optArgs...)
newCmd, err := NewSearchRawTransactionsCmd(r.Id, address, optArgs...)
if err != nil {
return err
}
@ -5459,8 +5459,8 @@ func (cmd *SearchRawTransactionCmd) UnmarshalJSON(b []byte) error {
return nil
}
// Enforce that SearchRawTransactionCmd satisifies the Cmd interface.
var _ Cmd = &SearchRawTransactionCmd{}
// Enforce that SearchRawTransactionsCmd satisifies the Cmd interface.
var _ Cmd = &SearchRawTransactionsCmd{}
// SendFromCmd is a type handling custom marshaling and
// unmarshaling of sendfrom JSON RPC commands.

View file

@ -1210,15 +1210,15 @@ var jsoncmdtests = []struct {
},
{
name: "basic + optionals",
cmd: "searchrawtransaction",
cmd: "searchrawtransactions",
f: func() (Cmd, error) {
return NewSearchRawTransactionCmd(testID,
"someaddr", true, 5, 200)
return NewSearchRawTransactionsCmd(testID,
"someaddr", 1, 5, 200)
},
result: &SearchRawTransactionCmd{
result: &SearchRawTransactionsCmd{
id: testID,
Address: "someaddr",
Verbose: true,
Verbose: 1,
Skip: 5,
Count: 200,
},
@ -1725,7 +1725,7 @@ func TestHelps(t *testing.T) {
"move",
"ping",
"reconsiderblock",
"searchrawtransaction",
"searchrawtransactions",
"sendfrom",
"sendmany",
"sendrawtransaction",

View file

@ -690,8 +690,8 @@ func ReadResultCmd(cmd string, message []byte) (Reply, error) {
result.Result = res
}
case "searchrawtransaction":
// searchrawtransaction can either return a list of JSON objects
case "searchrawtransactions":
// searchrawtransactions can either return a list of JSON objects
// or a list of hex-encoded strings depending on the verbose flag.
// Choose the right form accordingly.
if bytes.IndexByte(objmap["result"], '{') > -1 {

View file

@ -61,9 +61,9 @@ var resulttests = []struct {
{"signrawtransaction", []byte(`{"error":null,"id":1,"result":{false}}`), false, false},
{"listunspent", []byte(`{"error":null,"id":1,"result":[{"txid":"something"}]}`), false, true},
{"listunspent", []byte(`{"error":null,"id":1,"result":[{"txid"}]}`), false, false},
{"searchrawtransaction", []byte(`{"error":null,"id":1,"result":{"a":"b"}}`), false, false},
{"searchrawtransaction", []byte(`{"error":null,"id":1,"result":["sometxhex"]}`), false, true},
{"searchrawtransaction", []byte(`{"error":null,"id":1,"result":[{"hex":"somejunk","version":1}]}`), false, true},
{"searchrawtransactions", []byte(`{"error":null,"id":1,"result":{"a":"b"}}`), false, false},
{"searchrawtransactions", []byte(`{"error":null,"id":1,"result":["sometxhex"]}`), false, true},
{"searchrawtransactions", []byte(`{"error":null,"id":1,"result":[{"hex":"somejunk","version":1}]}`), false, true},
}
// TestReadResultCmd tests that readResultCmd can properly unmarshall the