lbcd/btcjson/walletsvrcmds_test.go

1251 lines
40 KiB
Go
Raw Normal View History

// Copyright (c) 2014 The btcsuite developers
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcjson_test
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/btcsuite/btcd/btcjson"
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
)
// TestWalletSvrCmds tests all of the wallet server commands marshal and
// unmarshal into valid results include handling of optional fields being
// omitted in the marshalled command, while optional fields with defaults have
// the default assigned on unmarshalled commands.
func TestWalletSvrCmds(t *testing.T) {
t.Parallel()
testID := int(1)
tests := []struct {
name string
newCmd func() (interface{}, error)
staticCmd func() interface{}
marshalled string
unmarshalled interface{}
}{
{
name: "addmultisigaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("addmultisigaddress", 2, []string{"031234", "035678"})
},
staticCmd: func() interface{} {
keys := []string{"031234", "035678"}
return btcjson.NewAddMultisigAddressCmd(2, keys, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"addmultisigaddress","params":[2,["031234","035678"]],"id":1}`,
unmarshalled: &btcjson.AddMultisigAddressCmd{
NRequired: 2,
Keys: []string{"031234", "035678"},
Account: nil,
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
},
},
{
name: "addmultisigaddress optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("addmultisigaddress", 2, []string{"031234", "035678"}, "test")
},
staticCmd: func() interface{} {
keys := []string{"031234", "035678"}
return btcjson.NewAddMultisigAddressCmd(2, keys, btcjson.String("test"))
},
marshalled: `{"jsonrpc":"1.0","method":"addmultisigaddress","params":[2,["031234","035678"],"test"],"id":1}`,
unmarshalled: &btcjson.AddMultisigAddressCmd{
NRequired: 2,
Keys: []string{"031234", "035678"},
Account: btcjson.String("test"),
},
},
{
name: "createmultisig",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("createmultisig", 2, []string{"031234", "035678"})
},
staticCmd: func() interface{} {
keys := []string{"031234", "035678"}
return btcjson.NewCreateMultisigCmd(2, keys)
},
marshalled: `{"jsonrpc":"1.0","method":"createmultisig","params":[2,["031234","035678"]],"id":1}`,
unmarshalled: &btcjson.CreateMultisigCmd{
NRequired: 2,
Keys: []string{"031234", "035678"},
},
},
{
name: "dumpprivkey",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("dumpprivkey", "1Address")
},
staticCmd: func() interface{} {
return btcjson.NewDumpPrivKeyCmd("1Address")
},
marshalled: `{"jsonrpc":"1.0","method":"dumpprivkey","params":["1Address"],"id":1}`,
unmarshalled: &btcjson.DumpPrivKeyCmd{
Address: "1Address",
},
},
{
name: "encryptwallet",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("encryptwallet", "pass")
},
staticCmd: func() interface{} {
return btcjson.NewEncryptWalletCmd("pass")
},
marshalled: `{"jsonrpc":"1.0","method":"encryptwallet","params":["pass"],"id":1}`,
unmarshalled: &btcjson.EncryptWalletCmd{
Passphrase: "pass",
},
},
{
name: "estimatefee",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("estimatefee", 6)
},
staticCmd: func() interface{} {
return btcjson.NewEstimateFeeCmd(6)
},
marshalled: `{"jsonrpc":"1.0","method":"estimatefee","params":[6],"id":1}`,
unmarshalled: &btcjson.EstimateFeeCmd{
NumBlocks: 6,
},
},
{
name: "estimatepriority",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("estimatepriority", 6)
},
staticCmd: func() interface{} {
return btcjson.NewEstimatePriorityCmd(6)
},
marshalled: `{"jsonrpc":"1.0","method":"estimatepriority","params":[6],"id":1}`,
unmarshalled: &btcjson.EstimatePriorityCmd{
NumBlocks: 6,
},
},
{
name: "getaccount",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getaccount", "1Address")
},
staticCmd: func() interface{} {
return btcjson.NewGetAccountCmd("1Address")
},
marshalled: `{"jsonrpc":"1.0","method":"getaccount","params":["1Address"],"id":1}`,
unmarshalled: &btcjson.GetAccountCmd{
Address: "1Address",
},
},
{
name: "getaccountaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getaccountaddress", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewGetAccountAddressCmd("acct")
},
marshalled: `{"jsonrpc":"1.0","method":"getaccountaddress","params":["acct"],"id":1}`,
unmarshalled: &btcjson.GetAccountAddressCmd{
Account: "acct",
},
},
{
name: "getaddressesbyaccount",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getaddressesbyaccount", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewGetAddressesByAccountCmd("acct")
},
marshalled: `{"jsonrpc":"1.0","method":"getaddressesbyaccount","params":["acct"],"id":1}`,
unmarshalled: &btcjson.GetAddressesByAccountCmd{
Account: "acct",
},
},
{
name: "getbalance",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getbalance")
},
staticCmd: func() interface{} {
return btcjson.NewGetBalanceCmd(nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":[],"id":1}`,
unmarshalled: &btcjson.GetBalanceCmd{
Account: nil,
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
MinConf: btcjson.Int(1),
},
},
{
name: "getbalance optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getbalance", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewGetBalanceCmd(btcjson.String("acct"), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":["acct"],"id":1}`,
unmarshalled: &btcjson.GetBalanceCmd{
Account: btcjson.String("acct"),
MinConf: btcjson.Int(1),
},
},
{
name: "getbalance optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getbalance", "acct", 6)
},
staticCmd: func() interface{} {
return btcjson.NewGetBalanceCmd(btcjson.String("acct"), btcjson.Int(6))
},
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":["acct",6],"id":1}`,
unmarshalled: &btcjson.GetBalanceCmd{
Account: btcjson.String("acct"),
MinConf: btcjson.Int(6),
},
},
{
name: "getnewaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getnewaddress")
},
staticCmd: func() interface{} {
return btcjson.NewGetNewAddressCmd(nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":[],"id":1}`,
unmarshalled: &btcjson.GetNewAddressCmd{
Account: nil,
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
},
},
{
name: "getnewaddress optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getnewaddress", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewGetNewAddressCmd(btcjson.String("acct"))
},
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":["acct"],"id":1}`,
unmarshalled: &btcjson.GetNewAddressCmd{
Account: btcjson.String("acct"),
},
},
{
name: "getrawchangeaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getrawchangeaddress")
},
staticCmd: func() interface{} {
return btcjson.NewGetRawChangeAddressCmd(nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getrawchangeaddress","params":[],"id":1}`,
unmarshalled: &btcjson.GetRawChangeAddressCmd{
Account: nil,
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
},
},
{
name: "getrawchangeaddress optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getrawchangeaddress", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewGetRawChangeAddressCmd(btcjson.String("acct"))
},
marshalled: `{"jsonrpc":"1.0","method":"getrawchangeaddress","params":["acct"],"id":1}`,
unmarshalled: &btcjson.GetRawChangeAddressCmd{
Account: btcjson.String("acct"),
},
},
{
name: "getreceivedbyaccount",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getreceivedbyaccount", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewGetReceivedByAccountCmd("acct", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaccount","params":["acct"],"id":1}`,
unmarshalled: &btcjson.GetReceivedByAccountCmd{
Account: "acct",
MinConf: btcjson.Int(1),
},
},
{
name: "getreceivedbyaccount optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getreceivedbyaccount", "acct", 6)
},
staticCmd: func() interface{} {
return btcjson.NewGetReceivedByAccountCmd("acct", btcjson.Int(6))
},
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaccount","params":["acct",6],"id":1}`,
unmarshalled: &btcjson.GetReceivedByAccountCmd{
Account: "acct",
MinConf: btcjson.Int(6),
},
},
{
name: "getreceivedbyaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getreceivedbyaddress", "1Address")
},
staticCmd: func() interface{} {
return btcjson.NewGetReceivedByAddressCmd("1Address", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaddress","params":["1Address"],"id":1}`,
unmarshalled: &btcjson.GetReceivedByAddressCmd{
Address: "1Address",
MinConf: btcjson.Int(1),
},
},
{
name: "getreceivedbyaddress optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("getreceivedbyaddress", "1Address", 6)
},
staticCmd: func() interface{} {
return btcjson.NewGetReceivedByAddressCmd("1Address", btcjson.Int(6))
},
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaddress","params":["1Address",6],"id":1}`,
unmarshalled: &btcjson.GetReceivedByAddressCmd{
Address: "1Address",
MinConf: btcjson.Int(6),
},
},
{
name: "gettransaction",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("gettransaction", "123")
},
staticCmd: func() interface{} {
return btcjson.NewGetTransactionCmd("123", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"gettransaction","params":["123"],"id":1}`,
unmarshalled: &btcjson.GetTransactionCmd{
Txid: "123",
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "gettransaction optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("gettransaction", "123", true)
},
staticCmd: func() interface{} {
return btcjson.NewGetTransactionCmd("123", btcjson.Bool(true))
},
marshalled: `{"jsonrpc":"1.0","method":"gettransaction","params":["123",true],"id":1}`,
unmarshalled: &btcjson.GetTransactionCmd{
Txid: "123",
IncludeWatchOnly: btcjson.Bool(true),
},
},
{
name: "importprivkey",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("importprivkey", "abc")
},
staticCmd: func() interface{} {
return btcjson.NewImportPrivKeyCmd("abc", nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"importprivkey","params":["abc"],"id":1}`,
unmarshalled: &btcjson.ImportPrivKeyCmd{
PrivKey: "abc",
Label: nil,
Reimagine btcjson package with version 2. This commit implements a reimagining of the way the btcjson package functions based upon how the project has evolved and lessons learned while using it since it was first written. It therefore contains significant changes to the API. For now, it has been implemented in a v2 subdirectory to prevent breaking existing callers, but the ultimate goal is to update all callers to use the new version and then to replace the old API with the new one. This also removes the need for the btcws completely since those commands have been rolled in. The following is an overview of the changes and some reasoning behind why they were made: - The infrastructure has been completely changed to be reflection based instead of requiring thousands and thousands of lines of manual, and therefore error prone, marshal/unmarshal code - This makes it much easier to add new commands without making marshalling mistakes since it is simply a struct definition and a call to register that new struct (plus a trivial New<foo>Cmd function and tests, of course) - It also makes it much easier to gain a lot of information from simply looking at the struct definition which was previously not possible such as the order of the parameters, which parameters are required versus optional, and what the default values for optional parameters are - Each command now has usage flags associated with them that can be queried which are intended to allow classification of the commands such as for chain server and wallet server and websocket-only - The help infrastructure has been completely redone to provide automatic generation with caller provided description map and result types. This is in contrast to the previous method of providing the help directly which meant it would end up in the binary of anything that imported the package - Many of the structs have been renamed to use the terminology from the JSON-RPC specification: - RawCmd/Message is now only a single struct named Request to reflect the fact it is a JSON-RPC request - Error is now called RPCError to reflect the fact it is specifically an RPC error as opposed to many of the other errors that are possible - All RPC error codes except the standard JSON-RPC 2.0 errors have been converted from full structs to only codes since an audit of the codebase has shown that the messages are overridden the vast majority of the time with specifics (as they should be) and removing them also avoids the temptation to return non-specific, and therefore not as helpful, error messages - There is now an Error which provides a type assertable error with error codes so callers can better ascertain failure reasons programatically - The ID is no longer a part of the command and is instead specified at the time the command is marshalled into a JSON-RPC request. This aligns better with the way JSON-RPC functions since it is the caller who manages the ID that is sent with any given _request_, not the package - All <Foo>Cmd structs now treat non-pointers as required fields and pointers as optional fields - All New<Foo>Cmd functions now accept the exact number of parameters, with pointers to the appropriate type for optional parameters - This is preferrable to the old vararg syntax since it means the code will fail to compile if the optional arguments are changed now which helps prevent errors creep in over time from missed modifications to optional args - All of the connection related code has been completely eliminated since this package is not intended to used a client, rather it is intended to provide the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC requests and replies from static types - The btcrpcclient package provides a robust client with connection management and higher-level types that in turn uses the primitives provided by this package - Even if the caller does not wish to use btcrpcclient for some reason, they should still be responsible for connection management since they might want to use any number of connection features which the package would not necessarily support - Synced a few of the commands that have added new optional fields that have since been added to Bitcoin Core - Includes all of the commands and notifications that were previously in btcws - Now provides 100% test coverage with parallel tests - The code is completely golint and go vet clean This has the side effect of addressing nearly everything in, and therefore closes #26. Also fixes #18 and closes #19.
2014-12-31 08:05:03 +01:00
Rescan: btcjson.Bool(true),
},
},
{
name: "importprivkey optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("importprivkey", "abc", "label")
},
staticCmd: func() interface{} {
return btcjson.NewImportPrivKeyCmd("abc", btcjson.String("label"), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"importprivkey","params":["abc","label"],"id":1}`,
unmarshalled: &btcjson.ImportPrivKeyCmd{
PrivKey: "abc",
Label: btcjson.String("label"),
Rescan: btcjson.Bool(true),
},
},
{
name: "importprivkey optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("importprivkey", "abc", "label", false)
},
staticCmd: func() interface{} {
return btcjson.NewImportPrivKeyCmd("abc", btcjson.String("label"), btcjson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"importprivkey","params":["abc","label",false],"id":1}`,
unmarshalled: &btcjson.ImportPrivKeyCmd{
PrivKey: "abc",
Label: btcjson.String("label"),
Rescan: btcjson.Bool(false),
},
},
{
name: "keypoolrefill",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("keypoolrefill")
},
staticCmd: func() interface{} {
return btcjson.NewKeyPoolRefillCmd(nil)
},
marshalled: `{"jsonrpc":"1.0","method":"keypoolrefill","params":[],"id":1}`,
unmarshalled: &btcjson.KeyPoolRefillCmd{
NewSize: btcjson.Uint(100),
},
},
{
name: "keypoolrefill optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("keypoolrefill", 200)
},
staticCmd: func() interface{} {
return btcjson.NewKeyPoolRefillCmd(btcjson.Uint(200))
},
marshalled: `{"jsonrpc":"1.0","method":"keypoolrefill","params":[200],"id":1}`,
unmarshalled: &btcjson.KeyPoolRefillCmd{
NewSize: btcjson.Uint(200),
},
},
{
name: "listaccounts",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listaccounts")
},
staticCmd: func() interface{} {
return btcjson.NewListAccountsCmd(nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listaccounts","params":[],"id":1}`,
unmarshalled: &btcjson.ListAccountsCmd{
MinConf: btcjson.Int(1),
},
},
{
name: "listaccounts optional",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listaccounts", 6)
},
staticCmd: func() interface{} {
return btcjson.NewListAccountsCmd(btcjson.Int(6))
},
marshalled: `{"jsonrpc":"1.0","method":"listaccounts","params":[6],"id":1}`,
unmarshalled: &btcjson.ListAccountsCmd{
MinConf: btcjson.Int(6),
},
},
{
name: "listaddressgroupings",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listaddressgroupings")
},
staticCmd: func() interface{} {
return btcjson.NewListAddressGroupingsCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"listaddressgroupings","params":[],"id":1}`,
unmarshalled: &btcjson.ListAddressGroupingsCmd{},
},
{
name: "listlockunspent",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listlockunspent")
},
staticCmd: func() interface{} {
return btcjson.NewListLockUnspentCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"listlockunspent","params":[],"id":1}`,
unmarshalled: &btcjson.ListLockUnspentCmd{},
},
{
name: "listreceivedbyaccount",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaccount")
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAccountCmd(nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaccount","params":[],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAccountCmd{
MinConf: btcjson.Int(1),
IncludeEmpty: btcjson.Bool(false),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaccount optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaccount", 6)
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAccountCmd(btcjson.Int(6), nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaccount","params":[6],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAccountCmd{
MinConf: btcjson.Int(6),
IncludeEmpty: btcjson.Bool(false),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaccount optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaccount", 6, true)
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAccountCmd(btcjson.Int(6), btcjson.Bool(true), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaccount","params":[6,true],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAccountCmd{
MinConf: btcjson.Int(6),
IncludeEmpty: btcjson.Bool(true),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaccount optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaccount", 6, true, false)
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAccountCmd(btcjson.Int(6), btcjson.Bool(true), btcjson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaccount","params":[6,true,false],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAccountCmd{
MinConf: btcjson.Int(6),
IncludeEmpty: btcjson.Bool(true),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaddress")
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAddressCmd(nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaddress","params":[],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAddressCmd{
MinConf: btcjson.Int(1),
IncludeEmpty: btcjson.Bool(false),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaddress optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaddress", 6)
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAddressCmd(btcjson.Int(6), nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaddress","params":[6],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAddressCmd{
MinConf: btcjson.Int(6),
IncludeEmpty: btcjson.Bool(false),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaddress optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaddress", 6, true)
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAddressCmd(btcjson.Int(6), btcjson.Bool(true), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaddress","params":[6,true],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAddressCmd{
MinConf: btcjson.Int(6),
IncludeEmpty: btcjson.Bool(true),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listreceivedbyaddress optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listreceivedbyaddress", 6, true, false)
},
staticCmd: func() interface{} {
return btcjson.NewListReceivedByAddressCmd(btcjson.Int(6), btcjson.Bool(true), btcjson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"listreceivedbyaddress","params":[6,true,false],"id":1}`,
unmarshalled: &btcjson.ListReceivedByAddressCmd{
MinConf: btcjson.Int(6),
IncludeEmpty: btcjson.Bool(true),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listsinceblock",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listsinceblock")
},
staticCmd: func() interface{} {
return btcjson.NewListSinceBlockCmd(nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listsinceblock","params":[],"id":1}`,
unmarshalled: &btcjson.ListSinceBlockCmd{
BlockHash: nil,
TargetConfirmations: btcjson.Int(1),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listsinceblock optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listsinceblock", "123")
},
staticCmd: func() interface{} {
return btcjson.NewListSinceBlockCmd(btcjson.String("123"), nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listsinceblock","params":["123"],"id":1}`,
unmarshalled: &btcjson.ListSinceBlockCmd{
BlockHash: btcjson.String("123"),
TargetConfirmations: btcjson.Int(1),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listsinceblock optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listsinceblock", "123", 6)
},
staticCmd: func() interface{} {
return btcjson.NewListSinceBlockCmd(btcjson.String("123"), btcjson.Int(6), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listsinceblock","params":["123",6],"id":1}`,
unmarshalled: &btcjson.ListSinceBlockCmd{
BlockHash: btcjson.String("123"),
TargetConfirmations: btcjson.Int(6),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listsinceblock optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listsinceblock", "123", 6, true)
},
staticCmd: func() interface{} {
return btcjson.NewListSinceBlockCmd(btcjson.String("123"), btcjson.Int(6), btcjson.Bool(true))
},
marshalled: `{"jsonrpc":"1.0","method":"listsinceblock","params":["123",6,true],"id":1}`,
unmarshalled: &btcjson.ListSinceBlockCmd{
BlockHash: btcjson.String("123"),
TargetConfirmations: btcjson.Int(6),
IncludeWatchOnly: btcjson.Bool(true),
},
},
{
name: "listtransactions",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listtransactions")
},
staticCmd: func() interface{} {
return btcjson.NewListTransactionsCmd(nil, nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":[],"id":1}`,
unmarshalled: &btcjson.ListTransactionsCmd{
Account: nil,
Count: btcjson.Int(10),
From: btcjson.Int(0),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listtransactions optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listtransactions", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewListTransactionsCmd(btcjson.String("acct"), nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":["acct"],"id":1}`,
unmarshalled: &btcjson.ListTransactionsCmd{
Account: btcjson.String("acct"),
Count: btcjson.Int(10),
From: btcjson.Int(0),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listtransactions optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listtransactions", "acct", 20)
},
staticCmd: func() interface{} {
return btcjson.NewListTransactionsCmd(btcjson.String("acct"), btcjson.Int(20), nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":["acct",20],"id":1}`,
unmarshalled: &btcjson.ListTransactionsCmd{
Account: btcjson.String("acct"),
Count: btcjson.Int(20),
From: btcjson.Int(0),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listtransactions optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listtransactions", "acct", 20, 1)
},
staticCmd: func() interface{} {
return btcjson.NewListTransactionsCmd(btcjson.String("acct"), btcjson.Int(20),
btcjson.Int(1), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":["acct",20,1],"id":1}`,
unmarshalled: &btcjson.ListTransactionsCmd{
Account: btcjson.String("acct"),
Count: btcjson.Int(20),
From: btcjson.Int(1),
IncludeWatchOnly: btcjson.Bool(false),
},
},
{
name: "listtransactions optional4",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listtransactions", "acct", 20, 1, true)
},
staticCmd: func() interface{} {
return btcjson.NewListTransactionsCmd(btcjson.String("acct"), btcjson.Int(20),
btcjson.Int(1), btcjson.Bool(true))
},
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":["acct",20,1,true],"id":1}`,
unmarshalled: &btcjson.ListTransactionsCmd{
Account: btcjson.String("acct"),
Count: btcjson.Int(20),
From: btcjson.Int(1),
IncludeWatchOnly: btcjson.Bool(true),
},
},
{
name: "listunspent",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listunspent")
},
staticCmd: func() interface{} {
return btcjson.NewListUnspentCmd(nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listunspent","params":[],"id":1}`,
unmarshalled: &btcjson.ListUnspentCmd{
MinConf: btcjson.Int(1),
MaxConf: btcjson.Int(9999999),
Addresses: nil,
},
},
{
name: "listunspent optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listunspent", 6)
},
staticCmd: func() interface{} {
return btcjson.NewListUnspentCmd(btcjson.Int(6), nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listunspent","params":[6],"id":1}`,
unmarshalled: &btcjson.ListUnspentCmd{
MinConf: btcjson.Int(6),
MaxConf: btcjson.Int(9999999),
Addresses: nil,
},
},
{
name: "listunspent optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listunspent", 6, 100)
},
staticCmd: func() interface{} {
return btcjson.NewListUnspentCmd(btcjson.Int(6), btcjson.Int(100), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"listunspent","params":[6,100],"id":1}`,
unmarshalled: &btcjson.ListUnspentCmd{
MinConf: btcjson.Int(6),
MaxConf: btcjson.Int(100),
Addresses: nil,
},
},
{
name: "listunspent optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("listunspent", 6, 100, []string{"1Address", "1Address2"})
},
staticCmd: func() interface{} {
return btcjson.NewListUnspentCmd(btcjson.Int(6), btcjson.Int(100),
&[]string{"1Address", "1Address2"})
},
marshalled: `{"jsonrpc":"1.0","method":"listunspent","params":[6,100,["1Address","1Address2"]],"id":1}`,
unmarshalled: &btcjson.ListUnspentCmd{
MinConf: btcjson.Int(6),
MaxConf: btcjson.Int(100),
Addresses: &[]string{"1Address", "1Address2"},
},
},
{
name: "lockunspent",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("lockunspent", true, `[{"txid":"123","vout":1}]`)
},
staticCmd: func() interface{} {
txInputs := []btcjson.TransactionInput{
{Txid: "123", Vout: 1},
}
return btcjson.NewLockUnspentCmd(true, txInputs)
},
marshalled: `{"jsonrpc":"1.0","method":"lockunspent","params":[true,[{"txid":"123","vout":1}]],"id":1}`,
unmarshalled: &btcjson.LockUnspentCmd{
Unlock: true,
Transactions: []btcjson.TransactionInput{
{Txid: "123", Vout: 1},
},
},
},
{
name: "move",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("move", "from", "to", 0.5)
},
staticCmd: func() interface{} {
return btcjson.NewMoveCmd("from", "to", 0.5, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"move","params":["from","to",0.5],"id":1}`,
unmarshalled: &btcjson.MoveCmd{
FromAccount: "from",
ToAccount: "to",
Amount: 0.5,
MinConf: btcjson.Int(1),
Comment: nil,
},
},
{
name: "move optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("move", "from", "to", 0.5, 6)
},
staticCmd: func() interface{} {
return btcjson.NewMoveCmd("from", "to", 0.5, btcjson.Int(6), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"move","params":["from","to",0.5,6],"id":1}`,
unmarshalled: &btcjson.MoveCmd{
FromAccount: "from",
ToAccount: "to",
Amount: 0.5,
MinConf: btcjson.Int(6),
Comment: nil,
},
},
{
name: "move optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("move", "from", "to", 0.5, 6, "comment")
},
staticCmd: func() interface{} {
return btcjson.NewMoveCmd("from", "to", 0.5, btcjson.Int(6), btcjson.String("comment"))
},
marshalled: `{"jsonrpc":"1.0","method":"move","params":["from","to",0.5,6,"comment"],"id":1}`,
unmarshalled: &btcjson.MoveCmd{
FromAccount: "from",
ToAccount: "to",
Amount: 0.5,
MinConf: btcjson.Int(6),
Comment: btcjson.String("comment"),
},
},
{
name: "sendfrom",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5)
},
staticCmd: func() interface{} {
return btcjson.NewSendFromCmd("from", "1Address", 0.5, nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5],"id":1}`,
unmarshalled: &btcjson.SendFromCmd{
FromAccount: "from",
ToAddress: "1Address",
Amount: 0.5,
MinConf: btcjson.Int(1),
Comment: nil,
CommentTo: nil,
},
},
{
name: "sendfrom optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6)
},
staticCmd: func() interface{} {
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6],"id":1}`,
unmarshalled: &btcjson.SendFromCmd{
FromAccount: "from",
ToAddress: "1Address",
Amount: 0.5,
MinConf: btcjson.Int(6),
Comment: nil,
CommentTo: nil,
},
},
{
name: "sendfrom optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "comment")
},
staticCmd: func() interface{} {
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6),
btcjson.String("comment"), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"comment"],"id":1}`,
unmarshalled: &btcjson.SendFromCmd{
FromAccount: "from",
ToAddress: "1Address",
Amount: 0.5,
MinConf: btcjson.Int(6),
Comment: btcjson.String("comment"),
CommentTo: nil,
},
},
{
name: "sendfrom optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "comment", "commentto")
},
staticCmd: func() interface{} {
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6),
btcjson.String("comment"), btcjson.String("commentto"))
},
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"comment","commentto"],"id":1}`,
unmarshalled: &btcjson.SendFromCmd{
FromAccount: "from",
ToAddress: "1Address",
Amount: 0.5,
MinConf: btcjson.Int(6),
Comment: btcjson.String("comment"),
CommentTo: btcjson.String("commentto"),
},
},
{
name: "sendmany",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`)
},
staticCmd: func() interface{} {
amounts := map[string]float64{"1Address": 0.5}
return btcjson.NewSendManyCmd("from", amounts, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5}],"id":1}`,
unmarshalled: &btcjson.SendManyCmd{
FromAccount: "from",
Amounts: map[string]float64{"1Address": 0.5},
MinConf: btcjson.Int(1),
Comment: nil,
},
},
{
name: "sendmany optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`, 6)
},
staticCmd: func() interface{} {
amounts := map[string]float64{"1Address": 0.5}
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6],"id":1}`,
unmarshalled: &btcjson.SendManyCmd{
FromAccount: "from",
Amounts: map[string]float64{"1Address": 0.5},
MinConf: btcjson.Int(6),
Comment: nil,
},
},
{
name: "sendmany optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`, 6, "comment")
},
staticCmd: func() interface{} {
amounts := map[string]float64{"1Address": 0.5}
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), btcjson.String("comment"))
},
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6,"comment"],"id":1}`,
unmarshalled: &btcjson.SendManyCmd{
FromAccount: "from",
Amounts: map[string]float64{"1Address": 0.5},
MinConf: btcjson.Int(6),
Comment: btcjson.String("comment"),
},
},
{
name: "sendtoaddress",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5)
},
staticCmd: func() interface{} {
return btcjson.NewSendToAddressCmd("1Address", 0.5, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5],"id":1}`,
unmarshalled: &btcjson.SendToAddressCmd{
Address: "1Address",
Amount: 0.5,
Comment: nil,
CommentTo: nil,
},
},
{
name: "sendtoaddress optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "comment", "commentto")
},
staticCmd: func() interface{} {
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("comment"),
btcjson.String("commentto"))
},
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5,"comment","commentto"],"id":1}`,
unmarshalled: &btcjson.SendToAddressCmd{
Address: "1Address",
Amount: 0.5,
Comment: btcjson.String("comment"),
CommentTo: btcjson.String("commentto"),
},
},
{
name: "setaccount",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("setaccount", "1Address", "acct")
},
staticCmd: func() interface{} {
return btcjson.NewSetAccountCmd("1Address", "acct")
},
marshalled: `{"jsonrpc":"1.0","method":"setaccount","params":["1Address","acct"],"id":1}`,
unmarshalled: &btcjson.SetAccountCmd{
Address: "1Address",
Account: "acct",
},
},
{
name: "settxfee",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("settxfee", 0.0001)
},
staticCmd: func() interface{} {
return btcjson.NewSetTxFeeCmd(0.0001)
},
marshalled: `{"jsonrpc":"1.0","method":"settxfee","params":[0.0001],"id":1}`,
unmarshalled: &btcjson.SetTxFeeCmd{
Amount: 0.0001,
},
},
{
name: "signmessage",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("signmessage", "1Address", "message")
},
staticCmd: func() interface{} {
return btcjson.NewSignMessageCmd("1Address", "message")
},
marshalled: `{"jsonrpc":"1.0","method":"signmessage","params":["1Address","message"],"id":1}`,
unmarshalled: &btcjson.SignMessageCmd{
Address: "1Address",
Message: "message",
},
},
{
name: "signrawtransaction",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("signrawtransaction", "001122")
},
staticCmd: func() interface{} {
return btcjson.NewSignRawTransactionCmd("001122", nil, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"signrawtransaction","params":["001122"],"id":1}`,
unmarshalled: &btcjson.SignRawTransactionCmd{
RawTx: "001122",
Inputs: nil,
PrivKeys: nil,
Flags: btcjson.String("ALL"),
},
},
{
name: "signrawtransaction optional1",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("signrawtransaction", "001122", `[{"txid":"123","vout":1,"scriptPubKey":"00","redeemScript":"01"}]`)
},
staticCmd: func() interface{} {
txInputs := []btcjson.RawTxInput{
{
Txid: "123",
Vout: 1,
ScriptPubKey: "00",
RedeemScript: "01",
},
}
return btcjson.NewSignRawTransactionCmd("001122", &txInputs, nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"signrawtransaction","params":["001122",[{"txid":"123","vout":1,"scriptPubKey":"00","redeemScript":"01"}]],"id":1}`,
unmarshalled: &btcjson.SignRawTransactionCmd{
RawTx: "001122",
Inputs: &[]btcjson.RawTxInput{
{
Txid: "123",
Vout: 1,
ScriptPubKey: "00",
RedeemScript: "01",
},
},
PrivKeys: nil,
Flags: btcjson.String("ALL"),
},
},
{
name: "signrawtransaction optional2",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("signrawtransaction", "001122", `[]`, `["abc"]`)
},
staticCmd: func() interface{} {
txInputs := []btcjson.RawTxInput{}
privKeys := []string{"abc"}
return btcjson.NewSignRawTransactionCmd("001122", &txInputs, &privKeys, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"signrawtransaction","params":["001122",[],["abc"]],"id":1}`,
unmarshalled: &btcjson.SignRawTransactionCmd{
RawTx: "001122",
Inputs: &[]btcjson.RawTxInput{},
PrivKeys: &[]string{"abc"},
Flags: btcjson.String("ALL"),
},
},
{
name: "signrawtransaction optional3",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("signrawtransaction", "001122", `[]`, `[]`, "ALL")
},
staticCmd: func() interface{} {
txInputs := []btcjson.RawTxInput{}
privKeys := []string{}
return btcjson.NewSignRawTransactionCmd("001122", &txInputs, &privKeys,
btcjson.String("ALL"))
},
marshalled: `{"jsonrpc":"1.0","method":"signrawtransaction","params":["001122",[],[],"ALL"],"id":1}`,
unmarshalled: &btcjson.SignRawTransactionCmd{
RawTx: "001122",
Inputs: &[]btcjson.RawTxInput{},
PrivKeys: &[]string{},
Flags: btcjson.String("ALL"),
},
},
{
name: "walletlock",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("walletlock")
},
staticCmd: func() interface{} {
return btcjson.NewWalletLockCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"walletlock","params":[],"id":1}`,
unmarshalled: &btcjson.WalletLockCmd{},
},
{
name: "walletpassphrase",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("walletpassphrase", "pass", 60)
},
staticCmd: func() interface{} {
return btcjson.NewWalletPassphraseCmd("pass", 60)
},
marshalled: `{"jsonrpc":"1.0","method":"walletpassphrase","params":["pass",60],"id":1}`,
unmarshalled: &btcjson.WalletPassphraseCmd{
Passphrase: "pass",
Timeout: 60,
},
},
{
name: "walletpassphrasechange",
newCmd: func() (interface{}, error) {
return btcjson.NewCmd("walletpassphrasechange", "old", "new")
},
staticCmd: func() interface{} {
return btcjson.NewWalletPassphraseChangeCmd("old", "new")
},
marshalled: `{"jsonrpc":"1.0","method":"walletpassphrasechange","params":["old","new"],"id":1}`,
unmarshalled: &btcjson.WalletPassphraseChangeCmd{
OldPassphrase: "old",
NewPassphrase: "new",
},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Marshal the command as created by the new static command
// creation function.
marshalled, err := btcjson.MarshalCmd(testID, test.staticCmd())
if err != nil {
t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !bytes.Equal(marshalled, []byte(test.marshalled)) {
t.Errorf("Test #%d (%s) unexpected marshalled data - "+
"got %s, want %s", i, test.name, marshalled,
test.marshalled)
continue
}
// Ensure the command is created without error via the generic
// new command creation function.
cmd, err := test.newCmd()
if err != nil {
t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ",
i, test.name, err)
}
// Marshal the command as created by the generic new command
// creation function.
marshalled, err = btcjson.MarshalCmd(testID, cmd)
if err != nil {
t.Errorf("MarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !bytes.Equal(marshalled, []byte(test.marshalled)) {
t.Errorf("Test #%d (%s) unexpected marshalled data - "+
"got %s, want %s", i, test.name, marshalled,
test.marshalled)
continue
}
var request btcjson.Request
if err := json.Unmarshal(marshalled, &request); err != nil {
t.Errorf("Test #%d (%s) unexpected error while "+
"unmarshalling JSON-RPC request: %v", i,
test.name, err)
continue
}
cmd, err = btcjson.UnmarshalCmd(&request)
if err != nil {
t.Errorf("UnmarshalCmd #%d (%s) unexpected error: %v", i,
test.name, err)
continue
}
if !reflect.DeepEqual(cmd, test.unmarshalled) {
t.Errorf("Test #%d (%s) unexpected unmarshalled command "+
"- got %s, want %s", i, test.name,
fmt.Sprintf("(%T) %+[1]v", cmd),
fmt.Sprintf("(%T) %+[1]v\n", test.unmarshalled))
continue
}
}
}