本文整理汇总了Golang中github.com/decred/dcrd/dcrjson.NewCmd函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCmd函数的具体用法?Golang NewCmd怎么用?Golang NewCmd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewCmd函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestNewCmdErrors
// TestNewCmdErrors ensures the error paths of NewCmd behave as expected.
func TestNewCmdErrors(t *testing.T) {
t.Parallel()
tests := []struct {
name string
method string
args []interface{}
err dcrjson.Error
}{
{
name: "unregistered command",
method: "boguscommand",
args: []interface{}{},
err: dcrjson.Error{Code: dcrjson.ErrUnregisteredMethod},
},
{
name: "too few parameters to command with required + optional",
method: "getblock",
args: []interface{}{},
err: dcrjson.Error{Code: dcrjson.ErrNumParams},
},
{
name: "too many parameters to command with no optional",
method: "getblockcount",
args: []interface{}{"123"},
err: dcrjson.Error{Code: dcrjson.ErrNumParams},
},
{
name: "incorrect parameter type",
method: "getblock",
args: []interface{}{1},
err: dcrjson.Error{Code: dcrjson.ErrInvalidType},
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
_, err := dcrjson.NewCmd(test.method, test.args...)
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.(dcrjson.Error).Code
if gotErrorCode != test.err.Code {
t.Errorf("Test #%d (%s) mismatched error code - got "+
"%v (%v), want %v", i, test.name, gotErrorCode,
err, test.err.Code)
continue
}
}
}
示例2: TestDcrWalletExtCmds
// TestDcrWalletExtCmds 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 TestDcrWalletExtCmds(t *testing.T) {
t.Parallel()
testID := int(1)
tests := []struct {
name string
newCmd func() (interface{}, error)
staticCmd func() interface{}
marshalled string
unmarshalled interface{}
}{
{
name: "notifywinningtickets",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifywinningtickets")
},
staticCmd: func() interface{} {
return dcrjson.NewNotifyWinningTicketsCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"notifywinningtickets","params":[],"id":1}`,
unmarshalled: &dcrjson.NotifyWinningTicketsCmd{},
},
{
name: "notifyspentandmissedtickets",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifyspentandmissedtickets")
},
staticCmd: func() interface{} {
return dcrjson.NewNotifySpentAndMissedTicketsCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"notifyspentandmissedtickets","params":[],"id":1}`,
unmarshalled: &dcrjson.NotifySpentAndMissedTicketsCmd{},
},
{
name: "notifynewtickets",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifynewtickets")
},
staticCmd: func() interface{} {
return dcrjson.NewNotifyNewTicketsCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"notifynewtickets","params":[],"id":1}`,
unmarshalled: &dcrjson.NotifyNewTicketsCmd{},
},
{
name: "notifystakedifficulty",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifystakedifficulty")
},
staticCmd: func() interface{} {
return dcrjson.NewNotifyStakeDifficultyCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"notifystakedifficulty","params":[],"id":1}`,
unmarshalled: &dcrjson.NotifyStakeDifficultyCmd{},
},
}
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 := dcrjson.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 = dcrjson.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)
//.........这里部分代码省略.........
示例3: execute
func execute(protected *bool, cfg *config, line string, clear *bool) bool {
switch line {
case "h":
fmt.Printf("[h]elp print this message\n")
fmt.Printf("[l]ist list all available commands\n")
fmt.Printf("[p]rotect toggle protected mode (for passwords)\n")
fmt.Printf("[c]lear clear command history\n")
fmt.Printf("[q]uit/ctrl+d exit\n")
fmt.Printf("Enter commands with arguments to execute them.\n")
case "l":
fallthrough
case "list":
listCommands()
case "q":
fallthrough
case "quit":
return true
case "p":
fallthrough
case "protect":
if *protected {
*protected = false
return false
}
*protected = true
return false
case "c":
fallthrough
case "clear":
*clear = true
default:
args := strings.Split(line, " ")
if len(args) < 1 {
usage("No command specified")
return false
}
// Ensure the specified method identifies a valid registered command and
// is one of the usable types.
listCmdMessageLocal := "Enter [l]ist to list commands"
method := args[0]
usageFlags, err := dcrjson.MethodUsageFlags(method)
if err != nil {
fmt.Fprintf(os.Stderr, "Unrecognized command '%s'\n", method)
fmt.Fprintln(os.Stderr, listCmdMessageLocal)
return false
}
if usageFlags&unusableFlags != 0 {
fmt.Fprintf(os.Stderr, "The '%s' command can only be used via "+
"websockets\n", method)
fmt.Fprintln(os.Stderr, listCmdMessageLocal)
return false
}
// 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)
return false
}
if err == io.EOF && len(param) == 0 {
fmt.Fprintln(os.Stderr, "Not enough lines "+
"provided on stdin")
return false
}
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 := dcrjson.NewCmd(method, params...)
if err != nil {
// Show the error along with its error code when it's a
// dcrjson.Error as it reallistcally will always be since the
// NewCmd function is only supposed to return errors of that
// type.
if jerr, ok := err.(dcrjson.Error); ok {
fmt.Fprintf(os.Stderr, "%s command: %v (code: %s)\n",
method, err, jerr.Code)
commandUsage(method)
return false
}
//.........这里部分代码省略.........
示例4: TestChainSvrWsNtfns
// 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 dcrjson.NewCmd("blockconnected", "header", []string{"tx0", "tx1"})
},
staticNtfn: func() interface{} {
return dcrjson.NewBlockConnectedNtfn("header", []string{"tx0", "tx1"})
},
marshalled: `{"jsonrpc":"1.0","method":"blockconnected","params":["header",["tx0","tx1"]],"id":null}`,
unmarshalled: &dcrjson.BlockConnectedNtfn{
Header: "header",
SubscribedTxs: []string{"tx0", "tx1"},
},
},
{
name: "blockdisconnected",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("blockdisconnected", "header")
},
staticNtfn: func() interface{} {
return dcrjson.NewBlockDisconnectedNtfn("header")
},
marshalled: `{"jsonrpc":"1.0","method":"blockdisconnected","params":["header"],"id":null}`,
unmarshalled: &dcrjson.BlockDisconnectedNtfn{
Header: "header",
},
},
{
name: "relevanttxaccepted",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("relevanttxaccepted", "001122")
},
staticNtfn: func() interface{} {
return dcrjson.NewRelevantTxAcceptedNtfn("001122")
},
marshalled: `{"jsonrpc":"1.0","method":"relevanttxaccepted","params":["001122"],"id":null}`,
unmarshalled: &dcrjson.RelevantTxAcceptedNtfn{
Transaction: "001122",
},
},
{
name: "txaccepted",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("txaccepted", "123", 1.5)
},
staticNtfn: func() interface{} {
return dcrjson.NewTxAcceptedNtfn("123", 1.5)
},
marshalled: `{"jsonrpc":"1.0","method":"txaccepted","params":["123",1.5],"id":null}`,
unmarshalled: &dcrjson.TxAcceptedNtfn{
TxID: "123",
Amount: 1.5,
},
},
{
name: "txacceptedverbose",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("txacceptedverbose", `{"hex":"001122","txid":"123","version":1,"locktime":4294967295,"vin":null,"vout":null,"confirmations":0}`)
},
staticNtfn: func() interface{} {
txResult := dcrjson.TxRawResult{
Hex: "001122",
Txid: "123",
Version: 1,
LockTime: 4294967295,
Vin: nil,
Vout: nil,
Confirmations: 0,
}
return dcrjson.NewTxAcceptedVerboseNtfn(txResult)
},
marshalled: `{"jsonrpc":"1.0","method":"txacceptedverbose","params":[{"hex":"001122","txid":"123","version":1,"locktime":4294967295,"expiry":0,"vin":null,"vout":null,"blockheight":0}],"id":null}`,
unmarshalled: &dcrjson.TxAcceptedVerboseNtfn{
RawTx: dcrjson.TxRawResult{
Hex: "001122",
Txid: "123",
Version: 1,
LockTime: 4294967295,
Vin: nil,
Vout: nil,
Confirmations: 0,
},
},
},
}
//.........这里部分代码省略.........
示例5: TestBtcWalletExtCmds
// 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 dcrjson.NewCmd("createnewaccount", "acct")
},
staticCmd: func() interface{} {
return dcrjson.NewCreateNewAccountCmd("acct")
},
marshalled: `{"jsonrpc":"1.0","method":"createnewaccount","params":["acct"],"id":1}`,
unmarshalled: &dcrjson.CreateNewAccountCmd{
Account: "acct",
},
},
{
name: "dumpwallet",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("dumpwallet", "filename")
},
staticCmd: func() interface{} {
return dcrjson.NewDumpWalletCmd("filename")
},
marshalled: `{"jsonrpc":"1.0","method":"dumpwallet","params":["filename"],"id":1}`,
unmarshalled: &dcrjson.DumpWalletCmd{
Filename: "filename",
},
},
{
name: "importaddress",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("importaddress", "1Address")
},
staticCmd: func() interface{} {
return dcrjson.NewImportAddressCmd("1Address", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address"],"id":1}`,
unmarshalled: &dcrjson.ImportAddressCmd{
Address: "1Address",
Rescan: dcrjson.Bool(true),
},
},
{
name: "importaddress optional",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("importaddress", "1Address", false)
},
staticCmd: func() interface{} {
return dcrjson.NewImportAddressCmd("1Address", dcrjson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"importaddress","params":["1Address",false],"id":1}`,
unmarshalled: &dcrjson.ImportAddressCmd{
Address: "1Address",
Rescan: dcrjson.Bool(false),
},
},
{
name: "importpubkey",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("importpubkey", "031234")
},
staticCmd: func() interface{} {
return dcrjson.NewImportPubKeyCmd("031234", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234"],"id":1}`,
unmarshalled: &dcrjson.ImportPubKeyCmd{
PubKey: "031234",
Rescan: dcrjson.Bool(true),
},
},
{
name: "importpubkey optional",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("importpubkey", "031234", false)
},
staticCmd: func() interface{} {
return dcrjson.NewImportPubKeyCmd("031234", dcrjson.Bool(false))
},
marshalled: `{"jsonrpc":"1.0","method":"importpubkey","params":["031234",false],"id":1}`,
unmarshalled: &dcrjson.ImportPubKeyCmd{
PubKey: "031234",
Rescan: dcrjson.Bool(false),
},
},
{
name: "importwallet",
newCmd: func() (interface{}, error) {
//.........这里部分代码省略.........
示例6: TestBtcdExtCmds
// 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 dcrjson.NewCmd("debuglevel", "trace")
},
staticCmd: func() interface{} {
return dcrjson.NewDebugLevelCmd("trace")
},
marshalled: `{"jsonrpc":"1.0","method":"debuglevel","params":["trace"],"id":1}`,
unmarshalled: &dcrjson.DebugLevelCmd{
LevelSpec: "trace",
},
},
{
name: "node",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("node", dcrjson.NRemove, "1.1.1.1")
},
staticCmd: func() interface{} {
return dcrjson.NewNodeCmd("remove", "1.1.1.1", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"node","params":["remove","1.1.1.1"],"id":1}`,
unmarshalled: &dcrjson.NodeCmd{
SubCmd: dcrjson.NRemove,
Target: "1.1.1.1",
},
},
{
name: "node",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("node", dcrjson.NDisconnect, "1.1.1.1")
},
staticCmd: func() interface{} {
return dcrjson.NewNodeCmd("disconnect", "1.1.1.1", nil)
},
marshalled: `{"jsonrpc":"1.0","method":"node","params":["disconnect","1.1.1.1"],"id":1}`,
unmarshalled: &dcrjson.NodeCmd{
SubCmd: dcrjson.NDisconnect,
Target: "1.1.1.1",
},
},
{
name: "node",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("node", dcrjson.NConnect, "1.1.1.1", "perm")
},
staticCmd: func() interface{} {
return dcrjson.NewNodeCmd("connect", "1.1.1.1", dcrjson.String("perm"))
},
marshalled: `{"jsonrpc":"1.0","method":"node","params":["connect","1.1.1.1","perm"],"id":1}`,
unmarshalled: &dcrjson.NodeCmd{
SubCmd: dcrjson.NConnect,
Target: "1.1.1.1",
ConnectSubCmd: dcrjson.String("perm"),
},
},
{
name: "node",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("node", dcrjson.NConnect, "1.1.1.1", "temp")
},
staticCmd: func() interface{} {
return dcrjson.NewNodeCmd("connect", "1.1.1.1", dcrjson.String("temp"))
},
marshalled: `{"jsonrpc":"1.0","method":"node","params":["connect","1.1.1.1","temp"],"id":1}`,
unmarshalled: &dcrjson.NodeCmd{
SubCmd: dcrjson.NConnect,
Target: "1.1.1.1",
ConnectSubCmd: dcrjson.String("temp"),
},
},
{
name: "generate",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("generate", 1)
},
staticCmd: func() interface{} {
return dcrjson.NewGenerateCmd(1)
},
marshalled: `{"jsonrpc":"1.0","method":"generate","params":[1],"id":1}`,
unmarshalled: &dcrjson.GenerateCmd{
NumBlocks: 1,
},
},
{
//.........这里部分代码省略.........
示例7: TestChainSvrCmds
// 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 dcrjson.NewCmd("addnode", "127.0.0.1", dcrjson.ANRemove)
},
staticCmd: func() interface{} {
return dcrjson.NewAddNodeCmd("127.0.0.1", dcrjson.ANRemove)
},
marshalled: `{"jsonrpc":"1.0","method":"addnode","params":["127.0.0.1","remove"],"id":1}`,
unmarshalled: &dcrjson.AddNodeCmd{Addr: "127.0.0.1", SubCmd: dcrjson.ANRemove},
},
{
name: "createrawtransaction",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("createrawtransaction", `[{"txid":"123","vout":1}]`,
`{"456":0.0123}`)
},
staticCmd: func() interface{} {
txInputs := []dcrjson.TransactionInput{
{Txid: "123", Vout: 1},
}
amounts := map[string]float64{"456": .0123}
return dcrjson.NewCreateRawTransactionCmd(txInputs, amounts, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1,"tree":0}],{"456":0.0123}],"id":1}`,
unmarshalled: &dcrjson.CreateRawTransactionCmd{
Inputs: []dcrjson.TransactionInput{{Txid: "123", Vout: 1}},
Amounts: map[string]float64{"456": .0123},
},
},
{
name: "createrawtransaction optional",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("createrawtransaction", `[{"txid":"123","vout":1,"tree":0}]`,
`{"456":0.0123}`, int64(12312333333))
},
staticCmd: func() interface{} {
txInputs := []dcrjson.TransactionInput{
{Txid: "123", Vout: 1},
}
amounts := map[string]float64{"456": .0123}
return dcrjson.NewCreateRawTransactionCmd(txInputs, amounts, dcrjson.Int64(12312333333))
},
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1,"tree":0}],{"456":0.0123},12312333333],"id":1}`,
unmarshalled: &dcrjson.CreateRawTransactionCmd{
Inputs: []dcrjson.TransactionInput{{Txid: "123", Vout: 1}},
Amounts: map[string]float64{"456": .0123},
LockTime: dcrjson.Int64(12312333333),
},
},
{
name: "decoderawtransaction",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("decoderawtransaction", "123")
},
staticCmd: func() interface{} {
return dcrjson.NewDecodeRawTransactionCmd("123")
},
marshalled: `{"jsonrpc":"1.0","method":"decoderawtransaction","params":["123"],"id":1}`,
unmarshalled: &dcrjson.DecodeRawTransactionCmd{HexTx: "123"},
},
{
name: "decodescript",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("decodescript", "00")
},
staticCmd: func() interface{} {
return dcrjson.NewDecodeScriptCmd("00")
},
marshalled: `{"jsonrpc":"1.0","method":"decodescript","params":["00"],"id":1}`,
unmarshalled: &dcrjson.DecodeScriptCmd{HexScript: "00"},
},
{
name: "getaddednodeinfo",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("getaddednodeinfo", true)
},
staticCmd: func() interface{} {
return dcrjson.NewGetAddedNodeInfoCmd(true, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getaddednodeinfo","params":[true],"id":1}`,
unmarshalled: &dcrjson.GetAddedNodeInfoCmd{DNS: true, Node: nil},
},
{
name: "getaddednodeinfo optional",
//.........这里部分代码省略.........
示例8: TestWalletSvrWsNtfns
// 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 dcrjson.NewCmd("accountbalance", "acct", 1.25, true)
},
staticNtfn: func() interface{} {
return dcrjson.NewAccountBalanceNtfn("acct", 1.25, true)
},
marshalled: `{"jsonrpc":"1.0","method":"accountbalance","params":["acct",1.25,true],"id":null}`,
unmarshalled: &dcrjson.AccountBalanceNtfn{
Account: "acct",
Balance: 1.25,
Confirmed: true,
},
},
{
name: "dcrdconnected",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("dcrdconnected", true)
},
staticNtfn: func() interface{} {
return dcrjson.NewBtcdConnectedNtfn(true)
},
marshalled: `{"jsonrpc":"1.0","method":"dcrdconnected","params":[true],"id":null}`,
unmarshalled: &dcrjson.BtcdConnectedNtfn{
Connected: true,
},
},
{
name: "walletlockstate",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("walletlockstate", true)
},
staticNtfn: func() interface{} {
return dcrjson.NewWalletLockStateNtfn(true)
},
marshalled: `{"jsonrpc":"1.0","method":"walletlockstate","params":[true],"id":null}`,
unmarshalled: &dcrjson.WalletLockStateNtfn{
Locked: true,
},
},
{
name: "newtx",
newNtfn: func() (interface{}, error) {
return dcrjson.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 := dcrjson.ListTransactionsResult{
Account: "acct",
Address: "1Address",
Category: "send",
Amount: 1.5,
Fee: dcrjson.Float64(0.0001),
Confirmations: 1,
TxID: "456",
WalletConflicts: []string{},
Time: 12345678,
TimeReceived: 12345876,
Vout: 789,
OtherAccount: "otheracct",
}
return dcrjson.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: &dcrjson.NewTxNtfn{
Account: "acct",
Details: dcrjson.ListTransactionsResult{
Account: "acct",
Address: "1Address",
Category: "send",
Amount: 1.5,
Fee: dcrjson.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 {
//.........这里部分代码省略.........
示例9: TestChainSvrWsCmds
// 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 dcrjson.NewCmd("authenticate", "user", "pass")
},
staticCmd: func() interface{} {
return dcrjson.NewAuthenticateCmd("user", "pass")
},
marshalled: `{"jsonrpc":"1.0","method":"authenticate","params":["user","pass"],"id":1}`,
unmarshalled: &dcrjson.AuthenticateCmd{Username: "user", Passphrase: "pass"},
},
{
name: "notifyblocks",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifyblocks")
},
staticCmd: func() interface{} {
return dcrjson.NewNotifyBlocksCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"notifyblocks","params":[],"id":1}`,
unmarshalled: &dcrjson.NotifyBlocksCmd{},
},
{
name: "stopnotifyblocks",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("stopnotifyblocks")
},
staticCmd: func() interface{} {
return dcrjson.NewStopNotifyBlocksCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"stopnotifyblocks","params":[],"id":1}`,
unmarshalled: &dcrjson.StopNotifyBlocksCmd{},
},
{
name: "notifynewtransactions",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifynewtransactions")
},
staticCmd: func() interface{} {
return dcrjson.NewNotifyNewTransactionsCmd(nil)
},
marshalled: `{"jsonrpc":"1.0","method":"notifynewtransactions","params":[],"id":1}`,
unmarshalled: &dcrjson.NotifyNewTransactionsCmd{
Verbose: dcrjson.Bool(false),
},
},
{
name: "notifynewtransactions optional",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("notifynewtransactions", true)
},
staticCmd: func() interface{} {
return dcrjson.NewNotifyNewTransactionsCmd(dcrjson.Bool(true))
},
marshalled: `{"jsonrpc":"1.0","method":"notifynewtransactions","params":[true],"id":1}`,
unmarshalled: &dcrjson.NotifyNewTransactionsCmd{
Verbose: dcrjson.Bool(true),
},
},
{
name: "stopnotifynewtransactions",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("stopnotifynewtransactions")
},
staticCmd: func() interface{} {
return dcrjson.NewStopNotifyNewTransactionsCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"stopnotifynewtransactions","params":[],"id":1}`,
unmarshalled: &dcrjson.StopNotifyNewTransactionsCmd{},
},
{
name: "rescan",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("rescan", "0000000000000000000000000000000000000000000000000000000000000123")
},
staticCmd: func() interface{} {
return dcrjson.NewRescanCmd("0000000000000000000000000000000000000000000000000000000000000123")
},
marshalled: `{"jsonrpc":"1.0","method":"rescan","params":["0000000000000000000000000000000000000000000000000000000000000123"],"id":1}`,
unmarshalled: &dcrjson.RescanCmd{
BlockHashes: "0000000000000000000000000000000000000000000000000000000000000123",
},
},
}
//.........这里部分代码省略.........
示例10: TestWalletSvrCmds
// 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 dcrjson.NewCmd("addmultisigaddress", 2, []string{"031234", "035678"})
},
staticCmd: func() interface{} {
keys := []string{"031234", "035678"}
return dcrjson.NewAddMultisigAddressCmd(2, keys, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"addmultisigaddress","params":[2,["031234","035678"]],"id":1}`,
unmarshalled: &dcrjson.AddMultisigAddressCmd{
NRequired: 2,
Keys: []string{"031234", "035678"},
Account: nil,
},
},
{
name: "addmultisigaddress optional",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("addmultisigaddress", 2, []string{"031234", "035678"}, "test")
},
staticCmd: func() interface{} {
keys := []string{"031234", "035678"}
return dcrjson.NewAddMultisigAddressCmd(2, keys, dcrjson.String("test"))
},
marshalled: `{"jsonrpc":"1.0","method":"addmultisigaddress","params":[2,["031234","035678"],"test"],"id":1}`,
unmarshalled: &dcrjson.AddMultisigAddressCmd{
NRequired: 2,
Keys: []string{"031234", "035678"},
Account: dcrjson.String("test"),
},
},
{
name: "createmultisig",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("createmultisig", 2, []string{"031234", "035678"})
},
staticCmd: func() interface{} {
keys := []string{"031234", "035678"}
return dcrjson.NewCreateMultisigCmd(2, keys)
},
marshalled: `{"jsonrpc":"1.0","method":"createmultisig","params":[2,["031234","035678"]],"id":1}`,
unmarshalled: &dcrjson.CreateMultisigCmd{
NRequired: 2,
Keys: []string{"031234", "035678"},
},
},
{
name: "dumpprivkey",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("dumpprivkey", "1Address")
},
staticCmd: func() interface{} {
return dcrjson.NewDumpPrivKeyCmd("1Address")
},
marshalled: `{"jsonrpc":"1.0","method":"dumpprivkey","params":["1Address"],"id":1}`,
unmarshalled: &dcrjson.DumpPrivKeyCmd{
Address: "1Address",
},
},
{
name: "encryptwallet",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("encryptwallet", "pass")
},
staticCmd: func() interface{} {
return dcrjson.NewEncryptWalletCmd("pass")
},
marshalled: `{"jsonrpc":"1.0","method":"encryptwallet","params":["pass"],"id":1}`,
unmarshalled: &dcrjson.EncryptWalletCmd{
Passphrase: "pass",
},
},
{
name: "estimatefee",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("estimatefee", 6)
},
staticCmd: func() interface{} {
return dcrjson.NewEstimateFeeCmd(6)
},
marshalled: `{"jsonrpc":"1.0","method":"estimatefee","params":[6],"id":1}`,
unmarshalled: &dcrjson.EstimateFeeCmd{
NumBlocks: 6,
},
//.........这里部分代码省略.........
示例11: main
func main() {
cfg, args, err := loadConfig()
if err != nil {
os.Exit(1)
}
if cfg.Terminal {
startTerminal(cfg)
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 := dcrjson.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 := dcrjson.NewCmd(method, params...)
if err != nil {
// Show the error along with its error code when it's a
// dcrjson.Error as it reallistcally will always be since the
// NewCmd function is only supposed to return errors of that
// type.
if jerr, ok := err.(dcrjson.Error); ok {
fmt.Fprintf(os.Stderr, "%s command: %v (code: %s)\n",
method, err, jerr.Code)
commandUsage(method)
os.Exit(1)
}
// The error is not a dcrjson.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 := dcrjson.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)
}
//.........这里部分代码省略.........
示例12: TestWalletSvrWsCmds
// 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 dcrjson.NewCmd("createencryptedwallet", "pass")
},
staticCmd: func() interface{} {
return dcrjson.NewCreateEncryptedWalletCmd("pass")
},
marshalled: `{"jsonrpc":"1.0","method":"createencryptedwallet","params":["pass"],"id":1}`,
unmarshalled: &dcrjson.CreateEncryptedWalletCmd{Passphrase: "pass"},
},
{
name: "exportwatchingwallet",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("exportwatchingwallet")
},
staticCmd: func() interface{} {
return dcrjson.NewExportWatchingWalletCmd(nil, nil)
},
marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":[],"id":1}`,
unmarshalled: &dcrjson.ExportWatchingWalletCmd{
Account: nil,
Download: dcrjson.Bool(false),
},
},
{
name: "exportwatchingwallet optional1",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("exportwatchingwallet", "acct")
},
staticCmd: func() interface{} {
return dcrjson.NewExportWatchingWalletCmd(dcrjson.String("acct"), nil)
},
marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct"],"id":1}`,
unmarshalled: &dcrjson.ExportWatchingWalletCmd{
Account: dcrjson.String("acct"),
Download: dcrjson.Bool(false),
},
},
{
name: "exportwatchingwallet optional2",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("exportwatchingwallet", "acct", true)
},
staticCmd: func() interface{} {
return dcrjson.NewExportWatchingWalletCmd(dcrjson.String("acct"),
dcrjson.Bool(true))
},
marshalled: `{"jsonrpc":"1.0","method":"exportwatchingwallet","params":["acct",true],"id":1}`,
unmarshalled: &dcrjson.ExportWatchingWalletCmd{
Account: dcrjson.String("acct"),
Download: dcrjson.Bool(true),
},
},
{
name: "getunconfirmedbalance",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("getunconfirmedbalance")
},
staticCmd: func() interface{} {
return dcrjson.NewGetUnconfirmedBalanceCmd(nil)
},
marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":[],"id":1}`,
unmarshalled: &dcrjson.GetUnconfirmedBalanceCmd{
Account: nil,
},
},
{
name: "getunconfirmedbalance optional1",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("getunconfirmedbalance", "acct")
},
staticCmd: func() interface{} {
return dcrjson.NewGetUnconfirmedBalanceCmd(dcrjson.String("acct"))
},
marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":["acct"],"id":1}`,
unmarshalled: &dcrjson.GetUnconfirmedBalanceCmd{
Account: dcrjson.String("acct"),
},
},
{
name: "listaddresstransactions",
newCmd: func() (interface{}, error) {
return dcrjson.NewCmd("listaddresstransactions", `["1Address"]`)
},
//.........这里部分代码省略.........
示例13: TestDcrwalletChainSvrWsNtfns
// 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 TestDcrwalletChainSvrWsNtfns(t *testing.T) {
t.Parallel()
tests := []struct {
name string
newNtfn func() (interface{}, error)
staticNtfn func() interface{}
marshalled string
unmarshalled interface{}
}{
{
name: "ticketpurchase",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("ticketpurchased", "123", 5)
},
staticNtfn: func() interface{} {
return dcrjson.NewTicketPurchasedNtfn("123", 5)
},
marshalled: `{"jsonrpc":"1.0","method":"ticketpurchased","params":["123",5],"id":null}`,
unmarshalled: &dcrjson.TicketPurchasedNtfn{
TxHash: "123",
Amount: 5,
},
},
{
name: "votecreated",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("votecreated", "123", "1234", 100, "12345", 1)
},
staticNtfn: func() interface{} {
return dcrjson.NewVoteCreatedNtfn("123", "1234", 100, "12345", 1)
},
marshalled: `{"jsonrpc":"1.0","method":"votecreated","params":["123","1234",100,"12345",1],"id":null}`,
unmarshalled: &dcrjson.VoteCreatedNtfn{
TxHash: "123",
BlockHash: "1234",
Height: 100,
SStxIn: "12345",
VoteBits: 1,
},
},
{
name: "revocationcreated",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("revocationcreated", "123", "1234")
},
staticNtfn: func() interface{} {
return dcrjson.NewRevocationCreatedNtfn("123", "1234")
},
marshalled: `{"jsonrpc":"1.0","method":"revocationcreated","params":["123","1234"],"id":null}`,
unmarshalled: &dcrjson.RevocationCreatedNtfn{
TxHash: "123",
SStxIn: "1234",
},
},
{
name: "winningtickets",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("winningtickets", "123", 100, map[string]string{"a": "b"})
},
staticNtfn: func() interface{} {
return dcrjson.NewWinningTicketsNtfn("123", 100, map[string]string{"a": "b"})
},
marshalled: `{"jsonrpc":"1.0","method":"winningtickets","params":["123",100,{"a":"b"}],"id":null}`,
unmarshalled: &dcrjson.WinningTicketsNtfn{
BlockHash: "123",
BlockHeight: 100,
Tickets: map[string]string{"a": "b"},
},
},
{
name: "spentandmissedtickets",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("spentandmissedtickets", "123", 100, 3, map[string]string{"a": "b"})
},
staticNtfn: func() interface{} {
return dcrjson.NewSpentAndMissedTicketsNtfn("123", 100, 3, map[string]string{"a": "b"})
},
marshalled: `{"jsonrpc":"1.0","method":"spentandmissedtickets","params":["123",100,3,{"a":"b"}],"id":null}`,
unmarshalled: &dcrjson.SpentAndMissedTicketsNtfn{
Hash: "123",
Height: 100,
StakeDiff: 3,
Tickets: map[string]string{"a": "b"},
},
},
{
name: "newtickets",
newNtfn: func() (interface{}, error) {
return dcrjson.NewCmd("newtickets", "123", 100, 3, []string{"a", "b"})
},
staticNtfn: func() interface{} {
return dcrjson.NewNewTicketsNtfn("123", 100, 3, []string{"a", "b"})
},
marshalled: `{"jsonrpc":"1.0","method":"newtickets","params":["123",100,3,["a","b"]],"id":null}`,
unmarshalled: &dcrjson.NewTicketsNtfn{
//.........这里部分代码省略.........