Add a framewowrk for parsing and creating json commands.
Initial prototype for one command mostly from jrick, with discussion with me and jcv. Other commands mocked up in a couple of long boring days by yours truly. Code isn't fully tested yet so may well still be buggy in the type conversions. Test will be added over the next few days to get this up to snuff.
This commit is contained in:
parent
cbd3d730a9
commit
6227c446d3
2 changed files with 6657 additions and 0 deletions
6464
jsoncmd.go
Normal file
6464
jsoncmd.go
Normal file
File diff suppressed because it is too large
Load diff
193
jsoncmd_test.go
Normal file
193
jsoncmd_test.go
Normal file
|
@ -0,0 +1,193 @@
|
||||||
|
// Copyright (c) 2013 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// this has to be in the real json subpackage so we can mock up structs
|
||||||
|
package btcjson
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var jsoncmdtests = []struct {
|
||||||
|
name string
|
||||||
|
f func() (Cmd, error)
|
||||||
|
result Cmd // after marshal and unmarshal
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic addmultisigaddress",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewAddMultisigAddressCmd(float64(1), 1,
|
||||||
|
[]string{"foo", "bar"})
|
||||||
|
},
|
||||||
|
result: &AddMultisigAddressCmd{
|
||||||
|
id: float64(1),
|
||||||
|
NRequired: 1,
|
||||||
|
Keys: []string{"foo", "bar"},
|
||||||
|
Account: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "addmultisigaddress + optional",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewAddMultisigAddressCmd(float64(1), 1,
|
||||||
|
[]string{"foo", "bar"}, "address")
|
||||||
|
},
|
||||||
|
result: &AddMultisigAddressCmd{
|
||||||
|
id: float64(1),
|
||||||
|
NRequired: 1,
|
||||||
|
Keys: []string{"foo", "bar"},
|
||||||
|
Account: "address",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// TODO(oga) Too many arguments to newaddmultisigaddress
|
||||||
|
{
|
||||||
|
name: "basic addnode add",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewAddNodeCmd(float64(1), "address",
|
||||||
|
"add")
|
||||||
|
},
|
||||||
|
result: &AddNodeCmd{
|
||||||
|
id: float64(1),
|
||||||
|
Addr: "address",
|
||||||
|
SubCmd: "add",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic addnode remoe",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewAddNodeCmd(float64(1), "address",
|
||||||
|
"remove")
|
||||||
|
},
|
||||||
|
result: &AddNodeCmd{
|
||||||
|
id: float64(1),
|
||||||
|
Addr: "address",
|
||||||
|
SubCmd: "remove",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic addnode onetry",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewAddNodeCmd(float64(1), "address",
|
||||||
|
"onetry")
|
||||||
|
},
|
||||||
|
result: &AddNodeCmd{
|
||||||
|
id: float64(1),
|
||||||
|
Addr: "address",
|
||||||
|
SubCmd: "onetry",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// TODO(oga) try invalid subcmds
|
||||||
|
{
|
||||||
|
name: "basic backupwallet",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewBackupWalletCmd(float64(1), "destination")
|
||||||
|
},
|
||||||
|
result: &BackupWalletCmd{
|
||||||
|
id: float64(1),
|
||||||
|
Destination: "destination",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic createmultisig",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewCreateMultisigCmd(float64(1), 1,
|
||||||
|
[]string{"key1", "key2", "key3"})
|
||||||
|
},
|
||||||
|
result: &CreateMultisigCmd{
|
||||||
|
id: float64(1),
|
||||||
|
NRequired: 1,
|
||||||
|
Keys: []string{"key1", "key2", "key3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic createrawtransaction",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewCreateRawTransactionCmd(float64(1),
|
||||||
|
[]TransactionInput{
|
||||||
|
TransactionInput{Txid: "tx1", Vout: 1},
|
||||||
|
TransactionInput{Txid: "tx2", Vout: 3}},
|
||||||
|
map[string]int64{"bob": 1, "bill": 2})
|
||||||
|
},
|
||||||
|
result: &CreateRawTransactionCmd{
|
||||||
|
id: float64(1),
|
||||||
|
Inputs: []TransactionInput{
|
||||||
|
TransactionInput{Txid: "tx1", Vout: 1},
|
||||||
|
TransactionInput{Txid: "tx2", Vout: 3},
|
||||||
|
},
|
||||||
|
Amounts: map[string]int64{
|
||||||
|
"bob": 1,
|
||||||
|
"bill": 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic ping",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewPingCmd(float64(1))
|
||||||
|
},
|
||||||
|
result: &PingCmd{
|
||||||
|
id: float64(1),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic getblockcount",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewGetBlockCountCmd(float64(1))
|
||||||
|
},
|
||||||
|
result: &GetBlockCountCmd{
|
||||||
|
id: float64(1),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "basic getblock",
|
||||||
|
f: func() (Cmd, error) {
|
||||||
|
return NewGetBlockCmd(float64(1),
|
||||||
|
"somehash")
|
||||||
|
},
|
||||||
|
result: &GetBlockCmd{
|
||||||
|
id: float64(1),
|
||||||
|
Hash: "somehash",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCmds(t *testing.T) {
|
||||||
|
for _, test := range jsoncmdtests {
|
||||||
|
c, err := test.f()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: failed to run func: %v",
|
||||||
|
test.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := json.Marshal(c)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: failed to marshal cmd: %v",
|
||||||
|
test.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
c2, err := ParseMarshaledCmd(msg)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: failed to ummarshal cmd: %v",
|
||||||
|
test.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(test.result, c2) {
|
||||||
|
t.Errorf("%s: unmarshal not as expected. "+
|
||||||
|
"got %v wanted %v", test.name, spew.Sdump(c2),
|
||||||
|
spew.Sdump(test.result))
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(c, c2) {
|
||||||
|
t.Errorf("%s: unmarshal not as we started with. "+
|
||||||
|
"got %v wanted %v", test.name, spew.Sdump(c2),
|
||||||
|
spew.Sdump(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue