// This example demonstrates how to create and marshal a command into a JSON-RPC // request. func ExampleMarshalCmd() { // Create a new getblock command. Notice the nil parameter indicates // to use the default parameter for that fields. This is a common // pattern used in all of the New<Foo>Cmd functions in this package for // optional fields. Also, notice the call to btcjson.Bool which is a // convenience function for creating a pointer out of a primitive for // optional parameters. blockHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" gbCmd := btcjson.NewGetBlockCmd(blockHash, btcjson.Bool(false), nil) // Marshal the command to the format suitable for sending to the RPC // server. Typically the client would increment the id here which is // request so the response can be identified. id := 1 marshalledBytes, err := btcjson.MarshalCmd(id, gbCmd) if err != nil { fmt.Println(err) return } // Display the marshalled command. Ordinarily this would be sent across // the wire to the RPC server, but for this example, just display it. fmt.Printf("%s\n", marshalledBytes) // Output: // {"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",false],"id":1} }
// TestMarshalCmdErrors tests the error paths of the MarshalCmd function. func TestMarshalCmdErrors(t *testing.T) { t.Parallel() tests := []struct { name string id interface{} cmd interface{} err btcjson.Error }{ { name: "unregistered type", id: 1, cmd: (*int)(nil), err: btcjson.Error{ErrorCode: btcjson.ErrUnregisteredMethod}, }, { name: "nil instance of registered type", id: 1, cmd: (*btcjson.GetBlockCmd)(nil), err: btcjson.Error{ErrorCode: btcjson.ErrInvalidType}, }, { name: "nil instance of registered type", id: []int{0, 1}, cmd: &btcjson.GetBlockCountCmd{}, err: btcjson.Error{ErrorCode: btcjson.ErrInvalidType}, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { _, err := btcjson.MarshalCmd(test.id, test.cmd) if reflect.TypeOf(err) != reflect.TypeOf(test.err) { t.Errorf("Test #%d (%s) wrong error - got %T (%[2]v), "+ "want %T", i, test.name, err, test.err) continue } gotErrorCode := err.(btcjson.Error).ErrorCode if gotErrorCode != test.err.ErrorCode { t.Errorf("Test #%d (%s) mismatched error code - got "+ "%v (%v), want %v", i, test.name, gotErrorCode, err, test.err.ErrorCode) continue } } }
// TestBtcWalletExtCmds tests all of the btcwallet extended 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 TestBtcWalletExtCmds(t *testing.T) { t.Parallel() testID := int(1) tests := []struct { name string newCmd func() (interface{}, error) staticCmd func() interface{} marshalled string unmarshalled interface{} }{ { name: "createnewaccount", newCmd: func() (interface{}, error) { return btcjson.NewCmd("createnewaccount", "acct") }, staticCmd: func() interface{} { return btcjson.NewCreateNewAccountCmd("acct") }, marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`, unmarshalled: &btcjson.CreateNewAccountCmd{ Account: "acct", }, }, { name: "dumpwallet", newCmd: func() (interface{}, error) { return btcjson.NewCmd("dumpwallet", "filename") }, staticCmd: func() interface{} { return btcjson.NewDumpWalletCmd("filename") }, marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`, unmarshalled: &btcjson.DumpWalletCmd{ Filename: "filename", }, }, { name: "importaddress", newCmd: func() (interface{}, error) { return btcjson.NewCmd("importaddress", "1Address") }, staticCmd: func() interface{} { return btcjson.NewImportAddressCmd("1Address", nil) }, marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address"],"id":1}`, unmarshalled: &btcjson.ImportAddressCmd{ Address: "1Address", Rescan: btcjson.Bool(true), }, }, { name: "importaddress optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("importaddress", "1Address", false) }, staticCmd: func() interface{} { return btcjson.NewImportAddressCmd("1Address", btcjson.Bool(false)) }, marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",false],"id":1}`, unmarshalled: &btcjson.ImportAddressCmd{ Address: "1Address", Rescan: btcjson.Bool(false), }, }, { name: "importpubkey", newCmd: func() (interface{}, error) { return btcjson.NewCmd("importpubkey", "031234") }, staticCmd: func() interface{} { return btcjson.NewImportPubKeyCmd("031234", nil) }, marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234"],"id":1}`, unmarshalled: &btcjson.ImportPubKeyCmd{ PubKey: "031234", Rescan: btcjson.Bool(true), }, }, { name: "importpubkey optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("importpubkey", "031234", false) }, staticCmd: func() interface{} { return btcjson.NewImportPubKeyCmd("031234", btcjson.Bool(false)) }, marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234",false],"id":1}`, unmarshalled: &btcjson.ImportPubKeyCmd{ PubKey: "031234", Rescan: btcjson.Bool(false), }, }, { name: "importwallet", newCmd: func() (interface{}, error) { return btcjson.NewCmd("importwallet", "filename") }, staticCmd: func() interface{} { return btcjson.NewImportWalletCmd("filename") }, marshalled: `{"jsonrpc":"1.0","method":"importwallet","params":["filename"],"id":1}`, unmarshalled: &btcjson.ImportWalletCmd{ Filename: "filename", }, }, { name: "renameaccount", newCmd: func() (interface{}, error) { return btcjson.NewCmd("renameaccount", "oldacct", "newacct") }, staticCmd: func() interface{} { return btcjson.NewRenameAccountCmd("oldacct", "newacct") }, marshalled: `{"jsonrpc":"1.0","method":"renameaccount","params":["oldacct","newacct"],"id":1}`, unmarshalled: &btcjson.RenameAccountCmd{ OldAccount: "oldacct", NewAccount: "newacct", }, }, } 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 } } }
// TestBtcdExtCmds tests all of the btcd extended 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 TestBtcdExtCmds(t *testing.T) { t.Parallel() testID := int(1) tests := []struct { name string newCmd func() (interface{}, error) staticCmd func() interface{} marshalled string unmarshalled interface{} }{ { name: "debuglevel", newCmd: func() (interface{}, error) { return btcjson.NewCmd("debuglevel", "trace") }, staticCmd: func() interface{} { return btcjson.NewDebugLevelCmd("trace") }, marshalled: `{"jsonrpc":"1.0","method":"debuglevel","params":["trace"],"id":1}`, unmarshalled: &btcjson.DebugLevelCmd{ LevelSpec: "trace", }, }, { name: "node", newCmd: func() (interface{}, error) { return btcjson.NewCmd("node", btcjson.NRemove, "1.1.1.1") }, staticCmd: func() interface{} { return btcjson.NewNodeCmd("remove", "1.1.1.1", nil) }, marshalled: `{"jsonrpc":"1.0","method":"node","params":["remove","1.1.1.1"],"id":1}`, unmarshalled: &btcjson.NodeCmd{ SubCmd: btcjson.NRemove, Target: "1.1.1.1", }, }, { name: "node", newCmd: func() (interface{}, error) { return btcjson.NewCmd("node", btcjson.NDisconnect, "1.1.1.1") }, staticCmd: func() interface{} { return btcjson.NewNodeCmd("disconnect", "1.1.1.1", nil) }, marshalled: `{"jsonrpc":"1.0","method":"node","params":["disconnect","1.1.1.1"],"id":1}`, unmarshalled: &btcjson.NodeCmd{ SubCmd: btcjson.NDisconnect, Target: "1.1.1.1", }, }, { name: "node", newCmd: func() (interface{}, error) { return btcjson.NewCmd("node", btcjson.NConnect, "1.1.1.1", "perm") }, staticCmd: func() interface{} { return btcjson.NewNodeCmd("connect", "1.1.1.1", btcjson.String("perm")) }, marshalled: `{"jsonrpc":"1.0","method":"node","params":["connect","1.1.1.1","perm"],"id":1}`, unmarshalled: &btcjson.NodeCmd{ SubCmd: btcjson.NConnect, Target: "1.1.1.1", ConnectSubCmd: btcjson.String("perm"), }, }, { name: "node", newCmd: func() (interface{}, error) { return btcjson.NewCmd("node", btcjson.NConnect, "1.1.1.1", "temp") }, staticCmd: func() interface{} { return btcjson.NewNodeCmd("connect", "1.1.1.1", btcjson.String("temp")) }, marshalled: `{"jsonrpc":"1.0","method":"node","params":["connect","1.1.1.1","temp"],"id":1}`, unmarshalled: &btcjson.NodeCmd{ SubCmd: btcjson.NConnect, Target: "1.1.1.1", ConnectSubCmd: btcjson.String("temp"), }, }, { name: "generate", newCmd: func() (interface{}, error) { return btcjson.NewCmd("generate", 1) }, staticCmd: func() interface{} { return btcjson.NewGenerateCmd(1) }, marshalled: `{"jsonrpc":"1.0","method":"generate","params":[1],"id":1}`, unmarshalled: &btcjson.GenerateCmd{ NumBlocks: 1, }, }, { name: "getbestblock", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getbestblock") }, staticCmd: func() interface{} { return btcjson.NewGetBestBlockCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getbestblock","params":[],"id":1}`, unmarshalled: &btcjson.GetBestBlockCmd{}, }, { name: "getcurrentnet", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getcurrentnet") }, staticCmd: func() interface{} { return btcjson.NewGetCurrentNetCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getcurrentnet","params":[],"id":1}`, unmarshalled: &btcjson.GetCurrentNetCmd{}, }, { name: "version", newCmd: func() (interface{}, error) { return btcjson.NewCmd("version") }, staticCmd: func() interface{} { return btcjson.NewVersionCmd() }, marshalled: `{"jsonrpc":"1.0","method":"version","params":[],"id":1}`, unmarshalled: &btcjson.VersionCmd{}, }, } 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 } } }
// TestChainSvrCmds tests all of the chain 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 TestChainSvrCmds(t *testing.T) { t.Parallel() testID := int(1) tests := []struct { name string newCmd func() (interface{}, error) staticCmd func() interface{} marshalled string unmarshalled interface{} }{ { name: "addnode", newCmd: func() (interface{}, error) { return btcjson.NewCmd("addnode", "127.0.0.1", btcjson.ANRemove) }, staticCmd: func() interface{} { return btcjson.NewAddNodeCmd("127.0.0.1", btcjson.ANRemove) }, marshalled: `{"jsonrpc":"1.0","method":"addnode","params":["127.0.0.1","remove"],"id":1}`, unmarshalled: &btcjson.AddNodeCmd{Addr: "127.0.0.1", SubCmd: btcjson.ANRemove}, }, { name: "createrawtransaction", newCmd: func() (interface{}, error) { return btcjson.NewCmd("createrawtransaction", `[{"txid":"123","vout":1}]`, `{"456":0.0123}`) }, staticCmd: func() interface{} { txInputs := []btcjson.TransactionInput{ {Txid: "123", Vout: 1}, } amounts := map[string]float64{"456": .0123} return btcjson.NewCreateRawTransactionCmd(txInputs, amounts, nil) }, marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1}],{"456":0.0123}],"id":1}`, unmarshalled: &btcjson.CreateRawTransactionCmd{ Inputs: []btcjson.TransactionInput{{Txid: "123", Vout: 1}}, Amounts: map[string]float64{"456": .0123}, }, }, { name: "createrawtransaction optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("createrawtransaction", `[{"txid":"123","vout":1}]`, `{"456":0.0123}`, int64(12312333333)) }, staticCmd: func() interface{} { txInputs := []btcjson.TransactionInput{ {Txid: "123", Vout: 1}, } amounts := map[string]float64{"456": .0123} return btcjson.NewCreateRawTransactionCmd(txInputs, amounts, btcjson.Int64(12312333333)) }, marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1}],{"456":0.0123},12312333333],"id":1}`, unmarshalled: &btcjson.CreateRawTransactionCmd{ Inputs: []btcjson.TransactionInput{{Txid: "123", Vout: 1}}, Amounts: map[string]float64{"456": .0123}, LockTime: btcjson.Int64(12312333333), }, }, { name: "decoderawtransaction", newCmd: func() (interface{}, error) { return btcjson.NewCmd("decoderawtransaction", "123") }, staticCmd: func() interface{} { return btcjson.NewDecodeRawTransactionCmd("123") }, marshalled: `{"jsonrpc":"1.0","method":"decoderawtransaction","params":["123"],"id":1}`, unmarshalled: &btcjson.DecodeRawTransactionCmd{HexTx: "123"}, }, { name: "decodescript", newCmd: func() (interface{}, error) { return btcjson.NewCmd("decodescript", "00") }, staticCmd: func() interface{} { return btcjson.NewDecodeScriptCmd("00") }, marshalled: `{"jsonrpc":"1.0","method":"decodescript","params":["00"],"id":1}`, unmarshalled: &btcjson.DecodeScriptCmd{HexScript: "00"}, }, { name: "getaddednodeinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getaddednodeinfo", true) }, staticCmd: func() interface{} { return btcjson.NewGetAddedNodeInfoCmd(true, nil) }, marshalled: `{"jsonrpc":"1.0","method":"getaddednodeinfo","params":[true],"id":1}`, unmarshalled: &btcjson.GetAddedNodeInfoCmd{DNS: true, Node: nil}, }, { name: "getaddednodeinfo optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getaddednodeinfo", true, "127.0.0.1") }, staticCmd: func() interface{} { return btcjson.NewGetAddedNodeInfoCmd(true, btcjson.String("127.0.0.1")) }, marshalled: `{"jsonrpc":"1.0","method":"getaddednodeinfo","params":[true,"127.0.0.1"],"id":1}`, unmarshalled: &btcjson.GetAddedNodeInfoCmd{ DNS: true, Node: btcjson.String("127.0.0.1"), }, }, { name: "getbestblockhash", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getbestblockhash") }, staticCmd: func() interface{} { return btcjson.NewGetBestBlockHashCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getbestblockhash","params":[],"id":1}`, unmarshalled: &btcjson.GetBestBlockHashCmd{}, }, { name: "getblock", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblock", "123") }, staticCmd: func() interface{} { return btcjson.NewGetBlockCmd("123", nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123"],"id":1}`, unmarshalled: &btcjson.GetBlockCmd{ Hash: "123", Verbose: btcjson.Bool(true), VerboseTx: btcjson.Bool(false), }, }, { name: "getblock required optional1", newCmd: func() (interface{}, error) { // Intentionally use a source param that is // more pointers than the destination to // exercise that path. verbosePtr := btcjson.Bool(true) return btcjson.NewCmd("getblock", "123", &verbosePtr) }, staticCmd: func() interface{} { return btcjson.NewGetBlockCmd("123", btcjson.Bool(true), nil) }, marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",true],"id":1}`, unmarshalled: &btcjson.GetBlockCmd{ Hash: "123", Verbose: btcjson.Bool(true), VerboseTx: btcjson.Bool(false), }, }, { name: "getblock required optional2", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblock", "123", true, true) }, staticCmd: func() interface{} { return btcjson.NewGetBlockCmd("123", btcjson.Bool(true), btcjson.Bool(true)) }, marshalled: `{"jsonrpc":"1.0","method":"getblock","params":["123",true,true],"id":1}`, unmarshalled: &btcjson.GetBlockCmd{ Hash: "123", Verbose: btcjson.Bool(true), VerboseTx: btcjson.Bool(true), }, }, { name: "getblockchaininfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblockchaininfo") }, staticCmd: func() interface{} { return btcjson.NewGetBlockChainInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getblockchaininfo","params":[],"id":1}`, unmarshalled: &btcjson.GetBlockChainInfoCmd{}, }, { name: "getblockcount", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblockcount") }, staticCmd: func() interface{} { return btcjson.NewGetBlockCountCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getblockcount","params":[],"id":1}`, unmarshalled: &btcjson.GetBlockCountCmd{}, }, { name: "getblockhash", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblockhash", 123) }, staticCmd: func() interface{} { return btcjson.NewGetBlockHashCmd(123) }, marshalled: `{"jsonrpc":"1.0","method":"getblockhash","params":[123],"id":1}`, unmarshalled: &btcjson.GetBlockHashCmd{Index: 123}, }, { name: "getblockheader", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblockheader", "123") }, staticCmd: func() interface{} { return btcjson.NewGetBlockHeaderCmd("123", nil) }, marshalled: `{"jsonrpc":"1.0","method":"getblockheader","params":["123"],"id":1}`, unmarshalled: &btcjson.GetBlockHeaderCmd{ Hash: "123", Verbose: btcjson.Bool(true), }, }, { name: "getblocktemplate", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblocktemplate") }, staticCmd: func() interface{} { return btcjson.NewGetBlockTemplateCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"getblocktemplate","params":[],"id":1}`, unmarshalled: &btcjson.GetBlockTemplateCmd{Request: nil}, }, { name: "getblocktemplate optional - template request", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblocktemplate", `{"mode":"template","capabilities":["longpoll","coinbasetxn"]}`) }, staticCmd: func() interface{} { template := btcjson.TemplateRequest{ Mode: "template", Capabilities: []string{"longpoll", "coinbasetxn"}, } return btcjson.NewGetBlockTemplateCmd(&template) }, marshalled: `{"jsonrpc":"1.0","method":"getblocktemplate","params":[{"mode":"template","capabilities":["longpoll","coinbasetxn"]}],"id":1}`, unmarshalled: &btcjson.GetBlockTemplateCmd{ Request: &btcjson.TemplateRequest{ Mode: "template", Capabilities: []string{"longpoll", "coinbasetxn"}, }, }, }, { name: "getblocktemplate optional - template request with tweaks", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblocktemplate", `{"mode":"template","capabilities":["longpoll","coinbasetxn"],"sigoplimit":500,"sizelimit":100000000,"maxversion":2}`) }, staticCmd: func() interface{} { template := btcjson.TemplateRequest{ Mode: "template", Capabilities: []string{"longpoll", "coinbasetxn"}, SigOpLimit: 500, SizeLimit: 100000000, MaxVersion: 2, } return btcjson.NewGetBlockTemplateCmd(&template) }, marshalled: `{"jsonrpc":"1.0","method":"getblocktemplate","params":[{"mode":"template","capabilities":["longpoll","coinbasetxn"],"sigoplimit":500,"sizelimit":100000000,"maxversion":2}],"id":1}`, unmarshalled: &btcjson.GetBlockTemplateCmd{ Request: &btcjson.TemplateRequest{ Mode: "template", Capabilities: []string{"longpoll", "coinbasetxn"}, SigOpLimit: int64(500), SizeLimit: int64(100000000), MaxVersion: 2, }, }, }, { name: "getblocktemplate optional - template request with tweaks 2", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getblocktemplate", `{"mode":"template","capabilities":["longpoll","coinbasetxn"],"sigoplimit":true,"sizelimit":100000000,"maxversion":2}`) }, staticCmd: func() interface{} { template := btcjson.TemplateRequest{ Mode: "template", Capabilities: []string{"longpoll", "coinbasetxn"}, SigOpLimit: true, SizeLimit: 100000000, MaxVersion: 2, } return btcjson.NewGetBlockTemplateCmd(&template) }, marshalled: `{"jsonrpc":"1.0","method":"getblocktemplate","params":[{"mode":"template","capabilities":["longpoll","coinbasetxn"],"sigoplimit":true,"sizelimit":100000000,"maxversion":2}],"id":1}`, unmarshalled: &btcjson.GetBlockTemplateCmd{ Request: &btcjson.TemplateRequest{ Mode: "template", Capabilities: []string{"longpoll", "coinbasetxn"}, SigOpLimit: true, SizeLimit: int64(100000000), MaxVersion: 2, }, }, }, { name: "getchaintips", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getchaintips") }, staticCmd: func() interface{} { return btcjson.NewGetChainTipsCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getchaintips","params":[],"id":1}`, unmarshalled: &btcjson.GetChainTipsCmd{}, }, { name: "getconnectioncount", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getconnectioncount") }, staticCmd: func() interface{} { return btcjson.NewGetConnectionCountCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getconnectioncount","params":[],"id":1}`, unmarshalled: &btcjson.GetConnectionCountCmd{}, }, { name: "getdifficulty", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getdifficulty") }, staticCmd: func() interface{} { return btcjson.NewGetDifficultyCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getdifficulty","params":[],"id":1}`, unmarshalled: &btcjson.GetDifficultyCmd{}, }, { name: "getgenerate", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getgenerate") }, staticCmd: func() interface{} { return btcjson.NewGetGenerateCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getgenerate","params":[],"id":1}`, unmarshalled: &btcjson.GetGenerateCmd{}, }, { name: "gethashespersec", newCmd: func() (interface{}, error) { return btcjson.NewCmd("gethashespersec") }, staticCmd: func() interface{} { return btcjson.NewGetHashesPerSecCmd() }, marshalled: `{"jsonrpc":"1.0","method":"gethashespersec","params":[],"id":1}`, unmarshalled: &btcjson.GetHashesPerSecCmd{}, }, { name: "getinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getinfo") }, staticCmd: func() interface{} { return btcjson.NewGetInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getinfo","params":[],"id":1}`, unmarshalled: &btcjson.GetInfoCmd{}, }, { name: "getmempoolinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getmempoolinfo") }, staticCmd: func() interface{} { return btcjson.NewGetMempoolInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getmempoolinfo","params":[],"id":1}`, unmarshalled: &btcjson.GetMempoolInfoCmd{}, }, { name: "getmininginfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getmininginfo") }, staticCmd: func() interface{} { return btcjson.NewGetMiningInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getmininginfo","params":[],"id":1}`, unmarshalled: &btcjson.GetMiningInfoCmd{}, }, { name: "getnetworkinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getnetworkinfo") }, staticCmd: func() interface{} { return btcjson.NewGetNetworkInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getnetworkinfo","params":[],"id":1}`, unmarshalled: &btcjson.GetNetworkInfoCmd{}, }, { name: "getnettotals", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getnettotals") }, staticCmd: func() interface{} { return btcjson.NewGetNetTotalsCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getnettotals","params":[],"id":1}`, unmarshalled: &btcjson.GetNetTotalsCmd{}, }, { name: "getnetworkhashps", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getnetworkhashps") }, staticCmd: func() interface{} { return btcjson.NewGetNetworkHashPSCmd(nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"getnetworkhashps","params":[],"id":1}`, unmarshalled: &btcjson.GetNetworkHashPSCmd{ Blocks: btcjson.Int(120), Height: btcjson.Int(-1), }, }, { name: "getnetworkhashps optional1", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getnetworkhashps", 200) }, staticCmd: func() interface{} { return btcjson.NewGetNetworkHashPSCmd(btcjson.Int(200), nil) }, marshalled: `{"jsonrpc":"1.0","method":"getnetworkhashps","params":[200],"id":1}`, unmarshalled: &btcjson.GetNetworkHashPSCmd{ Blocks: btcjson.Int(200), Height: btcjson.Int(-1), }, }, { name: "getnetworkhashps optional2", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getnetworkhashps", 200, 123) }, staticCmd: func() interface{} { return btcjson.NewGetNetworkHashPSCmd(btcjson.Int(200), btcjson.Int(123)) }, marshalled: `{"jsonrpc":"1.0","method":"getnetworkhashps","params":[200,123],"id":1}`, unmarshalled: &btcjson.GetNetworkHashPSCmd{ Blocks: btcjson.Int(200), Height: btcjson.Int(123), }, }, { name: "getpeerinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getpeerinfo") }, staticCmd: func() interface{} { return btcjson.NewGetPeerInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getpeerinfo","params":[],"id":1}`, unmarshalled: &btcjson.GetPeerInfoCmd{}, }, { name: "getrawmempool", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getrawmempool") }, staticCmd: func() interface{} { return btcjson.NewGetRawMempoolCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"getrawmempool","params":[],"id":1}`, unmarshalled: &btcjson.GetRawMempoolCmd{ Verbose: btcjson.Bool(false), }, }, { name: "getrawmempool optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getrawmempool", false) }, staticCmd: func() interface{} { return btcjson.NewGetRawMempoolCmd(btcjson.Bool(false)) }, marshalled: `{"jsonrpc":"1.0","method":"getrawmempool","params":[false],"id":1}`, unmarshalled: &btcjson.GetRawMempoolCmd{ Verbose: btcjson.Bool(false), }, }, { name: "getrawtransaction", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getrawtransaction", "123") }, staticCmd: func() interface{} { return btcjson.NewGetRawTransactionCmd("123", nil) }, marshalled: `{"jsonrpc":"1.0","method":"getrawtransaction","params":["123"],"id":1}`, unmarshalled: &btcjson.GetRawTransactionCmd{ Txid: "123", Verbose: btcjson.Int(0), }, }, { name: "getrawtransaction optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getrawtransaction", "123", 1) }, staticCmd: func() interface{} { return btcjson.NewGetRawTransactionCmd("123", btcjson.Int(1)) }, marshalled: `{"jsonrpc":"1.0","method":"getrawtransaction","params":["123",1],"id":1}`, unmarshalled: &btcjson.GetRawTransactionCmd{ Txid: "123", Verbose: btcjson.Int(1), }, }, { name: "gettxout", newCmd: func() (interface{}, error) { return btcjson.NewCmd("gettxout", "123", 1) }, staticCmd: func() interface{} { return btcjson.NewGetTxOutCmd("123", 1, nil) }, marshalled: `{"jsonrpc":"1.0","method":"gettxout","params":["123",1],"id":1}`, unmarshalled: &btcjson.GetTxOutCmd{ Txid: "123", Vout: 1, IncludeMempool: btcjson.Bool(true), }, }, { name: "gettxout optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("gettxout", "123", 1, true) }, staticCmd: func() interface{} { return btcjson.NewGetTxOutCmd("123", 1, btcjson.Bool(true)) }, marshalled: `{"jsonrpc":"1.0","method":"gettxout","params":["123",1,true],"id":1}`, unmarshalled: &btcjson.GetTxOutCmd{ Txid: "123", Vout: 1, IncludeMempool: btcjson.Bool(true), }, }, { name: "gettxoutproof", newCmd: func() (interface{}, error) { return btcjson.NewCmd("gettxoutproof", []string{"123", "456"}) }, staticCmd: func() interface{} { return btcjson.NewGetTxOutProofCmd([]string{"123", "456"}, nil) }, marshalled: `{"jsonrpc":"1.0","method":"gettxoutproof","params":[["123","456"]],"id":1}`, unmarshalled: &btcjson.GetTxOutProofCmd{ TxIDs: []string{"123", "456"}, }, }, { name: "gettxoutproof optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("gettxoutproof", []string{"123", "456"}, btcjson.String("000000000000034a7dedef4a161fa058a2d67a173a90155f3a2fe6fc132e0ebf")) }, staticCmd: func() interface{} { return btcjson.NewGetTxOutProofCmd([]string{"123", "456"}, btcjson.String("000000000000034a7dedef4a161fa058a2d67a173a90155f3a2fe6fc132e0ebf")) }, marshalled: `{"jsonrpc":"1.0","method":"gettxoutproof","params":[["123","456"],` + `"000000000000034a7dedef4a161fa058a2d67a173a90155f3a2fe6fc132e0ebf"],"id":1}`, unmarshalled: &btcjson.GetTxOutProofCmd{ TxIDs: []string{"123", "456"}, BlockHash: btcjson.String("000000000000034a7dedef4a161fa058a2d67a173a90155f3a2fe6fc132e0ebf"), }, }, { name: "gettxoutsetinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("gettxoutsetinfo") }, staticCmd: func() interface{} { return btcjson.NewGetTxOutSetInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"gettxoutsetinfo","params":[],"id":1}`, unmarshalled: &btcjson.GetTxOutSetInfoCmd{}, }, { name: "getwork", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getwork") }, staticCmd: func() interface{} { return btcjson.NewGetWorkCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"getwork","params":[],"id":1}`, unmarshalled: &btcjson.GetWorkCmd{ Data: nil, }, }, { name: "getwork optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getwork", "00112233") }, staticCmd: func() interface{} { return btcjson.NewGetWorkCmd(btcjson.String("00112233")) }, marshalled: `{"jsonrpc":"1.0","method":"getwork","params":["00112233"],"id":1}`, unmarshalled: &btcjson.GetWorkCmd{ Data: btcjson.String("00112233"), }, }, { name: "help", newCmd: func() (interface{}, error) { return btcjson.NewCmd("help") }, staticCmd: func() interface{} { return btcjson.NewHelpCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"help","params":[],"id":1}`, unmarshalled: &btcjson.HelpCmd{ Command: nil, }, }, { name: "help optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("help", "getblock") }, staticCmd: func() interface{} { return btcjson.NewHelpCmd(btcjson.String("getblock")) }, marshalled: `{"jsonrpc":"1.0","method":"help","params":["getblock"],"id":1}`, unmarshalled: &btcjson.HelpCmd{ Command: btcjson.String("getblock"), }, }, { name: "invalidateblock", newCmd: func() (interface{}, error) { return btcjson.NewCmd("invalidateblock", "123") }, staticCmd: func() interface{} { return btcjson.NewInvalidateBlockCmd("123") }, marshalled: `{"jsonrpc":"1.0","method":"invalidateblock","params":["123"],"id":1}`, unmarshalled: &btcjson.InvalidateBlockCmd{ BlockHash: "123", }, }, { name: "ping", newCmd: func() (interface{}, error) { return btcjson.NewCmd("ping") }, staticCmd: func() interface{} { return btcjson.NewPingCmd() }, marshalled: `{"jsonrpc":"1.0","method":"ping","params":[],"id":1}`, unmarshalled: &btcjson.PingCmd{}, }, { name: "preciousblock", newCmd: func() (interface{}, error) { return btcjson.NewCmd("preciousblock", "0123") }, staticCmd: func() interface{} { return btcjson.NewPreciousBlockCmd("0123") }, marshalled: `{"jsonrpc":"1.0","method":"preciousblock","params":["0123"],"id":1}`, unmarshalled: &btcjson.PreciousBlockCmd{ BlockHash: "0123", }, }, { name: "reconsiderblock", newCmd: func() (interface{}, error) { return btcjson.NewCmd("reconsiderblock", "123") }, staticCmd: func() interface{} { return btcjson.NewReconsiderBlockCmd("123") }, marshalled: `{"jsonrpc":"1.0","method":"reconsiderblock","params":["123"],"id":1}`, unmarshalled: &btcjson.ReconsiderBlockCmd{ BlockHash: "123", }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address") }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", nil, nil, nil, nil, nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address"],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(1), Skip: btcjson.Int(0), Count: btcjson.Int(100), VinExtra: btcjson.Int(0), Reverse: btcjson.Bool(false), FilterAddrs: nil, }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address", 0) }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", btcjson.Int(0), nil, nil, nil, nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address",0],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(0), Skip: btcjson.Int(0), Count: btcjson.Int(100), VinExtra: btcjson.Int(0), Reverse: btcjson.Bool(false), FilterAddrs: nil, }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address", 0, 5) }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", btcjson.Int(0), btcjson.Int(5), nil, nil, nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address",0,5],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(0), Skip: btcjson.Int(5), Count: btcjson.Int(100), VinExtra: btcjson.Int(0), Reverse: btcjson.Bool(false), FilterAddrs: nil, }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address", 0, 5, 10) }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", btcjson.Int(0), btcjson.Int(5), btcjson.Int(10), nil, nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address",0,5,10],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(0), Skip: btcjson.Int(5), Count: btcjson.Int(10), VinExtra: btcjson.Int(0), Reverse: btcjson.Bool(false), FilterAddrs: nil, }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address", 0, 5, 10, 1) }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", btcjson.Int(0), btcjson.Int(5), btcjson.Int(10), btcjson.Int(1), nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address",0,5,10,1],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(0), Skip: btcjson.Int(5), Count: btcjson.Int(10), VinExtra: btcjson.Int(1), Reverse: btcjson.Bool(false), FilterAddrs: nil, }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address", 0, 5, 10, 1, true) }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", btcjson.Int(0), btcjson.Int(5), btcjson.Int(10), btcjson.Int(1), btcjson.Bool(true), nil) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address",0,5,10,1,true],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(0), Skip: btcjson.Int(5), Count: btcjson.Int(10), VinExtra: btcjson.Int(1), Reverse: btcjson.Bool(true), FilterAddrs: nil, }, }, { name: "searchrawtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("searchrawtransactions", "1Address", 0, 5, 10, 1, true, []string{"1Address"}) }, staticCmd: func() interface{} { return btcjson.NewSearchRawTransactionsCmd("1Address", btcjson.Int(0), btcjson.Int(5), btcjson.Int(10), btcjson.Int(1), btcjson.Bool(true), &[]string{"1Address"}) }, marshalled: `{"jsonrpc":"1.0","method":"searchrawtransactions","params":["1Address",0,5,10,1,true,["1Address"]],"id":1}`, unmarshalled: &btcjson.SearchRawTransactionsCmd{ Address: "1Address", Verbose: btcjson.Int(0), Skip: btcjson.Int(5), Count: btcjson.Int(10), VinExtra: btcjson.Int(1), Reverse: btcjson.Bool(true), FilterAddrs: &[]string{"1Address"}, }, }, { name: "sendrawtransaction", newCmd: func() (interface{}, error) { return btcjson.NewCmd("sendrawtransaction", "1122") }, staticCmd: func() interface{} { return btcjson.NewSendRawTransactionCmd("1122", nil) }, marshalled: `{"jsonrpc":"1.0","method":"sendrawtransaction","params":["1122"],"id":1}`, unmarshalled: &btcjson.SendRawTransactionCmd{ HexTx: "1122", AllowHighFees: btcjson.Bool(false), }, }, { name: "sendrawtransaction optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("sendrawtransaction", "1122", false) }, staticCmd: func() interface{} { return btcjson.NewSendRawTransactionCmd("1122", btcjson.Bool(false)) }, marshalled: `{"jsonrpc":"1.0","method":"sendrawtransaction","params":["1122",false],"id":1}`, unmarshalled: &btcjson.SendRawTransactionCmd{ HexTx: "1122", AllowHighFees: btcjson.Bool(false), }, }, { name: "setgenerate", newCmd: func() (interface{}, error) { return btcjson.NewCmd("setgenerate", true) }, staticCmd: func() interface{} { return btcjson.NewSetGenerateCmd(true, nil) }, marshalled: `{"jsonrpc":"1.0","method":"setgenerate","params":[true],"id":1}`, unmarshalled: &btcjson.SetGenerateCmd{ Generate: true, GenProcLimit: btcjson.Int(-1), }, }, { name: "setgenerate optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("setgenerate", true, 6) }, staticCmd: func() interface{} { return btcjson.NewSetGenerateCmd(true, btcjson.Int(6)) }, marshalled: `{"jsonrpc":"1.0","method":"setgenerate","params":[true,6],"id":1}`, unmarshalled: &btcjson.SetGenerateCmd{ Generate: true, GenProcLimit: btcjson.Int(6), }, }, { name: "stop", newCmd: func() (interface{}, error) { return btcjson.NewCmd("stop") }, staticCmd: func() interface{} { return btcjson.NewStopCmd() }, marshalled: `{"jsonrpc":"1.0","method":"stop","params":[],"id":1}`, unmarshalled: &btcjson.StopCmd{}, }, { name: "submitblock", newCmd: func() (interface{}, error) { return btcjson.NewCmd("submitblock", "112233") }, staticCmd: func() interface{} { return btcjson.NewSubmitBlockCmd("112233", nil) }, marshalled: `{"jsonrpc":"1.0","method":"submitblock","params":["112233"],"id":1}`, unmarshalled: &btcjson.SubmitBlockCmd{ HexBlock: "112233", Options: nil, }, }, { name: "submitblock optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("submitblock", "112233", `{"workid":"12345"}`) }, staticCmd: func() interface{} { options := btcjson.SubmitBlockOptions{ WorkID: "12345", } return btcjson.NewSubmitBlockCmd("112233", &options) }, marshalled: `{"jsonrpc":"1.0","method":"submitblock","params":["112233",{"workid":"12345"}],"id":1}`, unmarshalled: &btcjson.SubmitBlockCmd{ HexBlock: "112233", Options: &btcjson.SubmitBlockOptions{ WorkID: "12345", }, }, }, { name: "validateaddress", newCmd: func() (interface{}, error) { return btcjson.NewCmd("validateaddress", "1Address") }, staticCmd: func() interface{} { return btcjson.NewValidateAddressCmd("1Address") }, marshalled: `{"jsonrpc":"1.0","method":"validateaddress","params":["1Address"],"id":1}`, unmarshalled: &btcjson.ValidateAddressCmd{ Address: "1Address", }, }, { name: "verifychain", newCmd: func() (interface{}, error) { return btcjson.NewCmd("verifychain") }, staticCmd: func() interface{} { return btcjson.NewVerifyChainCmd(nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"verifychain","params":[],"id":1}`, unmarshalled: &btcjson.VerifyChainCmd{ CheckLevel: btcjson.Int32(3), CheckDepth: btcjson.Int32(288), }, }, { name: "verifychain optional1", newCmd: func() (interface{}, error) { return btcjson.NewCmd("verifychain", 2) }, staticCmd: func() interface{} { return btcjson.NewVerifyChainCmd(btcjson.Int32(2), nil) }, marshalled: `{"jsonrpc":"1.0","method":"verifychain","params":[2],"id":1}`, unmarshalled: &btcjson.VerifyChainCmd{ CheckLevel: btcjson.Int32(2), CheckDepth: btcjson.Int32(288), }, }, { name: "verifychain optional2", newCmd: func() (interface{}, error) { return btcjson.NewCmd("verifychain", 2, 500) }, staticCmd: func() interface{} { return btcjson.NewVerifyChainCmd(btcjson.Int32(2), btcjson.Int32(500)) }, marshalled: `{"jsonrpc":"1.0","method":"verifychain","params":[2,500],"id":1}`, unmarshalled: &btcjson.VerifyChainCmd{ CheckLevel: btcjson.Int32(2), CheckDepth: btcjson.Int32(500), }, }, { name: "verifymessage", newCmd: func() (interface{}, error) { return btcjson.NewCmd("verifymessage", "1Address", "301234", "test") }, staticCmd: func() interface{} { return btcjson.NewVerifyMessageCmd("1Address", "301234", "test") }, marshalled: `{"jsonrpc":"1.0","method":"verifymessage","params":["1Address","301234","test"],"id":1}`, unmarshalled: &btcjson.VerifyMessageCmd{ Address: "1Address", Signature: "301234", Message: "test", }, }, { name: "verifytxoutproof", newCmd: func() (interface{}, error) { return btcjson.NewCmd("verifytxoutproof", "test") }, staticCmd: func() interface{} { return btcjson.NewVerifyTxOutProofCmd("test") }, marshalled: `{"jsonrpc":"1.0","method":"verifytxoutproof","params":["test"],"id":1}`, unmarshalled: &btcjson.VerifyTxOutProofCmd{ Proof: "test", }, }, } 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) t.Errorf("\n%s\n%s", 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 } } }
func main() { cfg, args, err := loadConfig() if err != nil { os.Exit(1) } if len(args) < 1 { usage("No command specified") os.Exit(1) } // Ensure the specified method identifies a valid registered command and // is one of the usable types. method := args[0] usageFlags, err := btcjson.MethodUsageFlags(method) if err != nil { fmt.Fprintf(os.Stderr, "Unrecognized command '%s'\n", method) fmt.Fprintln(os.Stderr, listCmdMessage) os.Exit(1) } if usageFlags&unusableFlags != 0 { fmt.Fprintf(os.Stderr, "The '%s' command can only be used via "+ "websockets\n", method) fmt.Fprintln(os.Stderr, listCmdMessage) os.Exit(1) } // Convert remaining command line args to a slice of interface values // to be passed along as parameters to new command creation function. // // Since some commands, such as submitblock, can involve data which is // too large for the Operating System to allow as a normal command line // parameter, support using '-' as an argument to allow the argument // to be read from a stdin pipe. bio := bufio.NewReader(os.Stdin) params := make([]interface{}, 0, len(args[1:])) for _, arg := range args[1:] { if arg == "-" { param, err := bio.ReadString('\n') if err != nil && err != io.EOF { fmt.Fprintf(os.Stderr, "Failed to read data "+ "from stdin: %v\n", err) os.Exit(1) } if err == io.EOF && len(param) == 0 { fmt.Fprintln(os.Stderr, "Not enough lines "+ "provided on stdin") os.Exit(1) } param = strings.TrimRight(param, "\r\n") params = append(params, param) continue } params = append(params, arg) } // Attempt to create the appropriate command using the arguments // provided by the user. cmd, err := btcjson.NewCmd(method, params...) if err != nil { // Show the error along with its error code when it's a // btcjson.Error as it reallistcally will always be since the // NewCmd function is only supposed to return errors of that // type. if jerr, ok := err.(btcjson.Error); ok { fmt.Fprintf(os.Stderr, "%s command: %v (code: %s)\n", method, err, jerr.ErrorCode) commandUsage(method) os.Exit(1) } // The error is not a btcjson.Error and this really should not // happen. Nevertheless, fallback to just showing the error // if it should happen due to a bug in the package. fmt.Fprintf(os.Stderr, "%s command: %v\n", method, err) commandUsage(method) os.Exit(1) } // Marshal the command into a JSON-RPC byte slice in preparation for // sending it to the RPC server. marshalledJSON, err := btcjson.MarshalCmd(1, cmd) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // Send the JSON-RPC request to the server using the user-specified // connection configuration. result, err := sendPostRequest(marshalledJSON, cfg) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } // Choose how to display the result based on its type. strResult := string(result) if strings.HasPrefix(strResult, "{") || strings.HasPrefix(strResult, "[") { var dst bytes.Buffer if err := json.Indent(&dst, result, "", " "); err != nil { fmt.Fprintf(os.Stderr, "Failed to format result: %v", err) os.Exit(1) } fmt.Println(dst.String()) } else if strings.HasPrefix(strResult, `"`) { var str string if err := json.Unmarshal(result, &str); err != nil { fmt.Fprintf(os.Stderr, "Failed to unmarshal result: %v", err) os.Exit(1) } fmt.Println(str) } else if strResult != "null" { fmt.Println(strResult) } }
// TestWalletSvrWsNtfns tests all of the chain server websocket-specific // notifications 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 TestWalletSvrWsNtfns(t *testing.T) { t.Parallel() tests := []struct { name string newNtfn func() (interface{}, error) staticNtfn func() interface{} marshalled string unmarshalled interface{} }{ { name: "accountbalance", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("accountbalance", "acct", 1.25, true) }, staticNtfn: func() interface{} { return btcjson.NewAccountBalanceNtfn("acct", 1.25, true) }, marshalled: `{"jsonrpc":"1.0","method":"accountbalance","params":["acct",1.25,true],"id":null}`, unmarshalled: &btcjson.AccountBalanceNtfn{ Account: "acct", Balance: 1.25, Confirmed: true, }, }, { name: "btcdconnected", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("btcdconnected", true) }, staticNtfn: func() interface{} { return btcjson.NewBtcdConnectedNtfn(true) }, marshalled: `{"jsonrpc":"1.0","method":"btcdconnected","params":[true],"id":null}`, unmarshalled: &btcjson.BtcdConnectedNtfn{ Connected: true, }, }, { name: "walletlockstate", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("walletlockstate", true) }, staticNtfn: func() interface{} { return btcjson.NewWalletLockStateNtfn(true) }, marshalled: `{"jsonrpc":"1.0","method":"walletlockstate","params":[true],"id":null}`, unmarshalled: &btcjson.WalletLockStateNtfn{ Locked: true, }, }, { name: "newtx", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("newtx", "acct", `{"account":"acct","address":"1Address","category":"send","amount":1.5,"fee":0.0001,"confirmations":1,"txid":"456","walletconflicts":[],"time":12345678,"timereceived":12345876,"vout":789,"otheraccount":"otheracct"}`) }, staticNtfn: func() interface{} { result := btcjson.ListTransactionsResult{ Account: "acct", Address: "1Address", Category: "send", Amount: 1.5, Fee: btcjson.Float64(0.0001), Confirmations: 1, TxID: "456", WalletConflicts: []string{}, Time: 12345678, TimeReceived: 12345876, Vout: 789, OtherAccount: "otheracct", } return btcjson.NewNewTxNtfn("acct", result) }, marshalled: `{"jsonrpc":"1.0","method":"newtx","params":["acct",{"account":"acct","address":"1Address","amount":1.5,"category":"send","confirmations":1,"fee":0.0001,"time":12345678,"timereceived":12345876,"txid":"456","vout":789,"walletconflicts":[],"otheraccount":"otheracct"}],"id":null}`, unmarshalled: &btcjson.NewTxNtfn{ Account: "acct", Details: btcjson.ListTransactionsResult{ Account: "acct", Address: "1Address", Category: "send", Amount: 1.5, Fee: btcjson.Float64(0.0001), Confirmations: 1, TxID: "456", WalletConflicts: []string{}, Time: 12345678, TimeReceived: 12345876, Vout: 789, OtherAccount: "otheracct", }, }, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Marshal the notification as created by the new static // creation function. The ID is nil for notifications. marshalled, err := btcjson.MarshalCmd(nil, test.staticNtfn()) 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 notification is created without error via the // generic new notification creation function. cmd, err := test.newNtfn() if err != nil { t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ", i, test.name, err) } // Marshal the notification as created by the generic new // notification creation function. The ID is nil for // notifications. marshalled, err = btcjson.MarshalCmd(nil, 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 } } }
// TestChainSvrWsCmds tests all of the chain server websocket-specific 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 TestChainSvrWsCmds(t *testing.T) { t.Parallel() testID := int(1) tests := []struct { name string newCmd func() (interface{}, error) staticCmd func() interface{} marshalled string unmarshalled interface{} }{ { name: "authenticate", newCmd: func() (interface{}, error) { return btcjson.NewCmd("authenticate", "user", "pass") }, staticCmd: func() interface{} { return btcjson.NewAuthenticateCmd("user", "pass") }, marshalled: `{"jsonrpc":"1.0","method":"authenticate","params":["user","pass"],"id":1}`, unmarshalled: &btcjson.AuthenticateCmd{Username: "******", Passphrase: "pass"}, }, { name: "notifyblocks", newCmd: func() (interface{}, error) { return btcjson.NewCmd("notifyblocks") }, staticCmd: func() interface{} { return btcjson.NewNotifyBlocksCmd() }, marshalled: `{"jsonrpc":"1.0","method":"notifyblocks","params":[],"id":1}`, unmarshalled: &btcjson.NotifyBlocksCmd{}, }, { name: "stopnotifyblocks", newCmd: func() (interface{}, error) { return btcjson.NewCmd("stopnotifyblocks") }, staticCmd: func() interface{} { return btcjson.NewStopNotifyBlocksCmd() }, marshalled: `{"jsonrpc":"1.0","method":"stopnotifyblocks","params":[],"id":1}`, unmarshalled: &btcjson.StopNotifyBlocksCmd{}, }, { name: "notifynewtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("notifynewtransactions") }, staticCmd: func() interface{} { return btcjson.NewNotifyNewTransactionsCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"notifynewtransactions","params":[],"id":1}`, unmarshalled: &btcjson.NotifyNewTransactionsCmd{ Verbose: btcjson.Bool(false), }, }, { name: "notifynewtransactions optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("notifynewtransactions", true) }, staticCmd: func() interface{} { return btcjson.NewNotifyNewTransactionsCmd(btcjson.Bool(true)) }, marshalled: `{"jsonrpc":"1.0","method":"notifynewtransactions","params":[true],"id":1}`, unmarshalled: &btcjson.NotifyNewTransactionsCmd{ Verbose: btcjson.Bool(true), }, }, { name: "stopnotifynewtransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("stopnotifynewtransactions") }, staticCmd: func() interface{} { return btcjson.NewStopNotifyNewTransactionsCmd() }, marshalled: `{"jsonrpc":"1.0","method":"stopnotifynewtransactions","params":[],"id":1}`, unmarshalled: &btcjson.StopNotifyNewTransactionsCmd{}, }, { name: "notifyreceived", newCmd: func() (interface{}, error) { return btcjson.NewCmd("notifyreceived", []string{"1Address"}) }, staticCmd: func() interface{} { return btcjson.NewNotifyReceivedCmd([]string{"1Address"}) }, marshalled: `{"jsonrpc":"1.0","method":"notifyreceived","params":[["1Address"]],"id":1}`, unmarshalled: &btcjson.NotifyReceivedCmd{ Addresses: []string{"1Address"}, }, }, { name: "stopnotifyreceived", newCmd: func() (interface{}, error) { return btcjson.NewCmd("stopnotifyreceived", []string{"1Address"}) }, staticCmd: func() interface{} { return btcjson.NewStopNotifyReceivedCmd([]string{"1Address"}) }, marshalled: `{"jsonrpc":"1.0","method":"stopnotifyreceived","params":[["1Address"]],"id":1}`, unmarshalled: &btcjson.StopNotifyReceivedCmd{ Addresses: []string{"1Address"}, }, }, { name: "notifyspent", newCmd: func() (interface{}, error) { return btcjson.NewCmd("notifyspent", `[{"hash":"123","index":0}]`) }, staticCmd: func() interface{} { ops := []btcjson.OutPoint{{Hash: "123", Index: 0}} return btcjson.NewNotifySpentCmd(ops) }, marshalled: `{"jsonrpc":"1.0","method":"notifyspent","params":[[{"hash":"123","index":0}]],"id":1}`, unmarshalled: &btcjson.NotifySpentCmd{ OutPoints: []btcjson.OutPoint{{Hash: "123", Index: 0}}, }, }, { name: "stopnotifyspent", newCmd: func() (interface{}, error) { return btcjson.NewCmd("stopnotifyspent", `[{"hash":"123","index":0}]`) }, staticCmd: func() interface{} { ops := []btcjson.OutPoint{{Hash: "123", Index: 0}} return btcjson.NewStopNotifySpentCmd(ops) }, marshalled: `{"jsonrpc":"1.0","method":"stopnotifyspent","params":[[{"hash":"123","index":0}]],"id":1}`, unmarshalled: &btcjson.StopNotifySpentCmd{ OutPoints: []btcjson.OutPoint{{Hash: "123", Index: 0}}, }, }, { name: "rescan", newCmd: func() (interface{}, error) { return btcjson.NewCmd("rescan", "123", `["1Address"]`, `[{"hash":"0000000000000000000000000000000000000000000000000000000000000123","index":0}]`) }, staticCmd: func() interface{} { addrs := []string{"1Address"} ops := []btcjson.OutPoint{{ Hash: "0000000000000000000000000000000000000000000000000000000000000123", Index: 0, }} return btcjson.NewRescanCmd("123", addrs, ops, nil) }, marshalled: `{"jsonrpc":"1.0","method":"rescan","params":["123",["1Address"],[{"hash":"0000000000000000000000000000000000000000000000000000000000000123","index":0}]],"id":1}`, unmarshalled: &btcjson.RescanCmd{ BeginBlock: "123", Addresses: []string{"1Address"}, OutPoints: []btcjson.OutPoint{{Hash: "0000000000000000000000000000000000000000000000000000000000000123", Index: 0}}, EndBlock: nil, }, }, { name: "rescan optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("rescan", "123", `["1Address"]`, `[{"hash":"123","index":0}]`, "456") }, staticCmd: func() interface{} { addrs := []string{"1Address"} ops := []btcjson.OutPoint{{Hash: "123", Index: 0}} return btcjson.NewRescanCmd("123", addrs, ops, btcjson.String("456")) }, marshalled: `{"jsonrpc":"1.0","method":"rescan","params":["123",["1Address"],[{"hash":"123","index":0}],"456"],"id":1}`, unmarshalled: &btcjson.RescanCmd{ BeginBlock: "123", Addresses: []string{"1Address"}, OutPoints: []btcjson.OutPoint{{Hash: "123", Index: 0}}, EndBlock: btcjson.String("456"), }, }, } 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 } } }
// TestWalletSvrWsCmds tests all of the wallet server websocket-specific // 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 TestWalletSvrWsCmds(t *testing.T) { t.Parallel() testID := int(1) tests := []struct { name string newCmd func() (interface{}, error) staticCmd func() interface{} marshalled string unmarshalled interface{} }{ { name: "createencryptedwallet", newCmd: func() (interface{}, error) { return btcjson.NewCmd("createencryptedwallet", "pass") }, staticCmd: func() interface{} { return btcjson.NewCreateEncryptedWalletCmd("pass") }, marshalled: `{"jsonrpc":"1.0","method":"createencryptedwallet","params":["pass"],"id":1}`, unmarshalled: &btcjson.CreateEncryptedWalletCmd{Passphrase: "pass"}, }, { name: "exportwatchingwallet", newCmd: func() (interface{}, error) { return btcjson.NewCmd("exportwatchingwallet") }, staticCmd: func() interface{} { return btcjson.NewExportWatchingWalletCmd(nil, nil) }, marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":[],"id":1}`, unmarshalled: &btcjson.ExportWatchingWalletCmd{ Account: nil, Download: btcjson.Bool(false), }, }, { name: "exportwatchingwallet optional1", newCmd: func() (interface{}, error) { return btcjson.NewCmd("exportwatchingwallet", "acct") }, staticCmd: func() interface{} { return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), nil) }, marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct"],"id":1}`, unmarshalled: &btcjson.ExportWatchingWalletCmd{ Account: btcjson.String("acct"), Download: btcjson.Bool(false), }, }, { name: "exportwatchingwallet optional2", newCmd: func() (interface{}, error) { return btcjson.NewCmd("exportwatchingwallet", "acct", true) }, staticCmd: func() interface{} { return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"), btcjson.Bool(true)) }, marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct",true],"id":1}`, unmarshalled: &btcjson.ExportWatchingWalletCmd{ Account: btcjson.String("acct"), Download: btcjson.Bool(true), }, }, { name: "getunconfirmedbalance", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getunconfirmedbalance") }, staticCmd: func() interface{} { return btcjson.NewGetUnconfirmedBalanceCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":[],"id":1}`, unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{ Account: nil, }, }, { name: "getunconfirmedbalance optional1", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getunconfirmedbalance", "acct") }, staticCmd: func() interface{} { return btcjson.NewGetUnconfirmedBalanceCmd(btcjson.String("acct")) }, marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":["acct"],"id":1}`, unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{ Account: btcjson.String("acct"), }, }, { name: "listaddresstransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("listaddresstransactions", `["1Address"]`) }, staticCmd: func() interface{} { return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, nil) }, marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"]],"id":1}`, unmarshalled: &btcjson.ListAddressTransactionsCmd{ Addresses: []string{"1Address"}, Account: nil, }, }, { name: "listaddresstransactions optional1", newCmd: func() (interface{}, error) { return btcjson.NewCmd("listaddresstransactions", `["1Address"]`, "acct") }, staticCmd: func() interface{} { return btcjson.NewListAddressTransactionsCmd([]string{"1Address"}, btcjson.String("acct")) }, marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"],"acct"],"id":1}`, unmarshalled: &btcjson.ListAddressTransactionsCmd{ Addresses: []string{"1Address"}, Account: btcjson.String("acct"), }, }, { name: "listalltransactions", newCmd: func() (interface{}, error) { return btcjson.NewCmd("listalltransactions") }, staticCmd: func() interface{} { return btcjson.NewListAllTransactionsCmd(nil) }, marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":[],"id":1}`, unmarshalled: &btcjson.ListAllTransactionsCmd{ Account: nil, }, }, { name: "listalltransactions optional", newCmd: func() (interface{}, error) { return btcjson.NewCmd("listalltransactions", "acct") }, staticCmd: func() interface{} { return btcjson.NewListAllTransactionsCmd(btcjson.String("acct")) }, marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":["acct"],"id":1}`, unmarshalled: &btcjson.ListAllTransactionsCmd{ Account: btcjson.String("acct"), }, }, { name: "recoveraddresses", newCmd: func() (interface{}, error) { return btcjson.NewCmd("recoveraddresses", "acct", 10) }, staticCmd: func() interface{} { return btcjson.NewRecoverAddressesCmd("acct", 10) }, marshalled: `{"jsonrpc":"1.0","method":"recoveraddresses","params":["acct",10],"id":1}`, unmarshalled: &btcjson.RecoverAddressesCmd{ Account: "acct", N: 10, }, }, { name: "walletislocked", newCmd: func() (interface{}, error) { return btcjson.NewCmd("walletislocked") }, staticCmd: func() interface{} { return btcjson.NewWalletIsLockedCmd() }, marshalled: `{"jsonrpc":"1.0","method":"walletislocked","params":[],"id":1}`, unmarshalled: &btcjson.WalletIsLockedCmd{}, }, } 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 } } }
// 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, }, }, { 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, 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, }, }, { 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, }, }, { 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: "getwalletinfo", newCmd: func() (interface{}, error) { return btcjson.NewCmd("getwalletinfo") }, staticCmd: func() interface{} { return btcjson.NewGetWalletInfoCmd() }, marshalled: `{"jsonrpc":"1.0","method":"getwalletinfo","params":[],"id":1}`, unmarshalled: &btcjson.GetWalletInfoCmd{}, }, { 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, 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 } } }
// TestChainSvrWsNtfns tests all of the chain server websocket-specific // notifications 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 TestChainSvrWsNtfns(t *testing.T) { t.Parallel() tests := []struct { name string newNtfn func() (interface{}, error) staticNtfn func() interface{} marshalled string unmarshalled interface{} }{ { name: "blockconnected", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("blockconnected", "123", 100000, 123456789) }, staticNtfn: func() interface{} { return btcjson.NewBlockConnectedNtfn("123", 100000, 123456789) }, marshalled: `{"jsonrpc":"1.0","method":"blockconnected","params":["123",100000,123456789],"id":null}`, unmarshalled: &btcjson.BlockConnectedNtfn{ Hash: "123", Height: 100000, Time: 123456789, }, }, { name: "blockdisconnected", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("blockdisconnected", "123", 100000, 123456789) }, staticNtfn: func() interface{} { return btcjson.NewBlockDisconnectedNtfn("123", 100000, 123456789) }, marshalled: `{"jsonrpc":"1.0","method":"blockdisconnected","params":["123",100000,123456789],"id":null}`, unmarshalled: &btcjson.BlockDisconnectedNtfn{ Hash: "123", Height: 100000, Time: 123456789, }, }, { name: "recvtx", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("recvtx", "001122", `{"height":100000,"hash":"123","index":0,"time":12345678}`) }, staticNtfn: func() interface{} { blockDetails := btcjson.BlockDetails{ Height: 100000, Hash: "123", Index: 0, Time: 12345678, } return btcjson.NewRecvTxNtfn("001122", &blockDetails) }, marshalled: `{"jsonrpc":"1.0","method":"recvtx","params":["001122",{"height":100000,"hash":"123","index":0,"time":12345678}],"id":null}`, unmarshalled: &btcjson.RecvTxNtfn{ HexTx: "001122", Block: &btcjson.BlockDetails{ Height: 100000, Hash: "123", Index: 0, Time: 12345678, }, }, }, { name: "redeemingtx", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("redeemingtx", "001122", `{"height":100000,"hash":"123","index":0,"time":12345678}`) }, staticNtfn: func() interface{} { blockDetails := btcjson.BlockDetails{ Height: 100000, Hash: "123", Index: 0, Time: 12345678, } return btcjson.NewRedeemingTxNtfn("001122", &blockDetails) }, marshalled: `{"jsonrpc":"1.0","method":"redeemingtx","params":["001122",{"height":100000,"hash":"123","index":0,"time":12345678}],"id":null}`, unmarshalled: &btcjson.RedeemingTxNtfn{ HexTx: "001122", Block: &btcjson.BlockDetails{ Height: 100000, Hash: "123", Index: 0, Time: 12345678, }, }, }, { name: "rescanfinished", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("rescanfinished", "123", 100000, 12345678) }, staticNtfn: func() interface{} { return btcjson.NewRescanFinishedNtfn("123", 100000, 12345678) }, marshalled: `{"jsonrpc":"1.0","method":"rescanfinished","params":["123",100000,12345678],"id":null}`, unmarshalled: &btcjson.RescanFinishedNtfn{ Hash: "123", Height: 100000, Time: 12345678, }, }, { name: "rescanprogress", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("rescanprogress", "123", 100000, 12345678) }, staticNtfn: func() interface{} { return btcjson.NewRescanProgressNtfn("123", 100000, 12345678) }, marshalled: `{"jsonrpc":"1.0","method":"rescanprogress","params":["123",100000,12345678],"id":null}`, unmarshalled: &btcjson.RescanProgressNtfn{ Hash: "123", Height: 100000, Time: 12345678, }, }, { name: "txaccepted", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("txaccepted", "123", 1.5) }, staticNtfn: func() interface{} { return btcjson.NewTxAcceptedNtfn("123", 1.5) }, marshalled: `{"jsonrpc":"1.0","method":"txaccepted","params":["123",1.5],"id":null}`, unmarshalled: &btcjson.TxAcceptedNtfn{ TxID: "123", Amount: 1.5, }, }, { name: "txacceptedverbose", newNtfn: func() (interface{}, error) { return btcjson.NewCmd("txacceptedverbose", `{"hex":"001122","txid":"123","version":1,"locktime":4294967295,"vin":null,"vout":null,"confirmations":0}`) }, staticNtfn: func() interface{} { txResult := btcjson.TxRawResult{ Hex: "001122", Txid: "123", Version: 1, LockTime: 4294967295, Vin: nil, Vout: nil, Confirmations: 0, } return btcjson.NewTxAcceptedVerboseNtfn(txResult) }, marshalled: `{"jsonrpc":"1.0","method":"txacceptedverbose","params":[{"hex":"001122","txid":"123","version":1,"locktime":4294967295,"vin":null,"vout":null}],"id":null}`, unmarshalled: &btcjson.TxAcceptedVerboseNtfn{ RawTx: btcjson.TxRawResult{ Hex: "001122", Txid: "123", Version: 1, LockTime: 4294967295, Vin: nil, Vout: nil, Confirmations: 0, }, }, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Marshal the notification as created by the new static // creation function. The ID is nil for notifications. marshalled, err := btcjson.MarshalCmd(nil, test.staticNtfn()) 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 notification is created without error via the // generic new notification creation function. cmd, err := test.newNtfn() if err != nil { t.Errorf("Test #%d (%s) unexpected NewCmd error: %v ", i, test.name, err) } // Marshal the notification as created by the generic new // notification creation function. The ID is nil for // notifications. marshalled, err = btcjson.MarshalCmd(nil, 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 } } }