From 75ea378a6a5d3abd73e31c8e5a7f4e1c609c5474 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 2 Jan 2017 18:10:12 -0800 Subject: [PATCH 1/7] Encode dummy query values as hex strings --- example/dummy/dummy.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/dummy/dummy.go b/example/dummy/dummy.go index 438de6a4c..34ba63ecc 100644 --- a/example/dummy/dummy.go +++ b/example/dummy/dummy.go @@ -1,6 +1,7 @@ package dummy import ( + "encoding/hex" "strings" . "github.com/tendermint/go-common" @@ -48,8 +49,7 @@ func (app *DummyApplication) Commit() types.Result { func (app *DummyApplication) Query(query []byte) types.Result { index, value, exists := app.state.Get(query) - - queryResult := QueryResult{index, string(value), exists} + queryResult := QueryResult{index, hex.EncodeToString(value), exists} return types.NewResultOK(wire.JSONBytes(queryResult), "") } From ab211d2dbeab48bb3f5c064199b51cd64af28d24 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 2 Jan 2017 18:20:30 -0800 Subject: [PATCH 2/7] Include 'value' and 'valueHex' fields in dummy query response --- example/dummy/dummy.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/example/dummy/dummy.go b/example/dummy/dummy.go index 34ba63ecc..086aee18f 100644 --- a/example/dummy/dummy.go +++ b/example/dummy/dummy.go @@ -49,12 +49,13 @@ func (app *DummyApplication) Commit() types.Result { func (app *DummyApplication) Query(query []byte) types.Result { index, value, exists := app.state.Get(query) - queryResult := QueryResult{index, hex.EncodeToString(value), exists} + queryResult := QueryResult{index, string(value), hex.EncodeToString(value), exists} return types.NewResultOK(wire.JSONBytes(queryResult), "") } type QueryResult struct { - Index int `json:"index"` - Value string `json:"value"` - Exists bool `json:"exists"` + Index int `json:"index"` + Value string `json:"value"` + ValueHex string `json:"valueHex"` + Exists bool `json:"exists"` } From 115e6939d01909567f7f19b956b17cf9d313c246 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 2 Jan 2017 18:28:47 -0800 Subject: [PATCH 3/7] Require quotes or 0x for string args --- cmd/tmsp-cli/tmsp-cli.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/tmsp-cli/tmsp-cli.go b/cmd/tmsp-cli/tmsp-cli.go index 219d89dfb..5b74f8f91 100644 --- a/cmd/tmsp-cli/tmsp-cli.go +++ b/cmd/tmsp-cli/tmsp-cli.go @@ -285,5 +285,13 @@ func stringOrHexToBytes(s string) []byte { } return b } + + if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") { + fmt.Printf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string\n", s) + return []byte{} + } + + // TODO: return errors + return []byte(s) } From e748127b7f4a8ee489dec5228bbd083533af89b1 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 2 Jan 2017 18:49:10 -0800 Subject: [PATCH 4/7] Don't include quotes in quoted string args --- cmd/tmsp-cli/tmsp-cli.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/cmd/tmsp-cli/tmsp-cli.go b/cmd/tmsp-cli/tmsp-cli.go index 5b74f8f91..cfc56d1e9 100644 --- a/cmd/tmsp-cli/tmsp-cli.go +++ b/cmd/tmsp-cli/tmsp-cli.go @@ -213,7 +213,11 @@ func cmdAppendTx(c *cli.Context) error { if len(args) != 1 { return errors.New("Command append_tx takes 1 argument") } - txBytes := stringOrHexToBytes(c.Args()[0]) + txBytes, err := stringOrHexToBytes(c.Args()[0]) + if err != nil { + fmt.Println(err.Error()) + return nil + } res := client.AppendTxSync(txBytes) printResponse(c, res, string(res.Data), true) return nil @@ -225,7 +229,11 @@ func cmdCheckTx(c *cli.Context) error { if len(args) != 1 { return errors.New("Command check_tx takes 1 argument") } - txBytes := stringOrHexToBytes(c.Args()[0]) + txBytes, err := stringOrHexToBytes(c.Args()[0]) + if err != nil { + fmt.Println(err.Error()) + return nil + } res := client.CheckTxSync(txBytes) printResponse(c, res, string(res.Data), true) return nil @@ -244,7 +252,11 @@ func cmdQuery(c *cli.Context) error { if len(args) != 1 { return errors.New("Command query takes 1 argument") } - queryBytes := stringOrHexToBytes(c.Args()[0]) + queryBytes, err := stringOrHexToBytes(c.Args()[0]) + if err != nil { + fmt.Println(err.Error()) + return nil + } res := client.QuerySync(queryBytes) printResponse(c, res, string(res.Data), true) return nil @@ -277,21 +289,22 @@ func printResponse(c *cli.Context, res types.Result, s string, printCode bool) { } // NOTE: s is interpreted as a string unless prefixed with 0x -func stringOrHexToBytes(s string) []byte { - if len(s) > 2 && s[:2] == "0x" { +func stringOrHexToBytes(s string) ([]byte, error) { + fmt.Printf("string: %s %x\n", s, []byte(s)) + + if len(s) > 2 && strings.ToLower(s[:2]) == "0x" { b, err := hex.DecodeString(s[2:]) if err != nil { - fmt.Println("Error decoding hex argument:", err.Error()) + err = fmt.Errorf("Error decoding hex argument: %s", err.Error()) + return nil, err } - return b + return b, nil } if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") { - fmt.Printf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string\n", s) - return []byte{} + err := fmt.Errorf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string", s) + return nil, err } - // TODO: return errors - - return []byte(s) + return []byte(s[1 : len(s)-1]), nil } From 86a6deba3feef1c5c5df09ddc72979318450b2a4 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Mon, 2 Jan 2017 18:49:54 -0800 Subject: [PATCH 5/7] Remove debug printing --- cmd/tmsp-cli/tmsp-cli.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/tmsp-cli/tmsp-cli.go b/cmd/tmsp-cli/tmsp-cli.go index cfc56d1e9..2fab906a0 100644 --- a/cmd/tmsp-cli/tmsp-cli.go +++ b/cmd/tmsp-cli/tmsp-cli.go @@ -290,8 +290,6 @@ func printResponse(c *cli.Context, res types.Result, s string, printCode bool) { // NOTE: s is interpreted as a string unless prefixed with 0x func stringOrHexToBytes(s string) ([]byte, error) { - fmt.Printf("string: %s %x\n", s, []byte(s)) - if len(s) > 2 && strings.ToLower(s[:2]) == "0x" { b, err := hex.DecodeString(s[2:]) if err != nil { From cd6fa3018c537f8145c490eab918b394443dd54a Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Tue, 3 Jan 2017 10:53:14 -0800 Subject: [PATCH 6/7] Return errors from cmd functions instead of printing --- cmd/tmsp-cli/tmsp-cli.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cmd/tmsp-cli/tmsp-cli.go b/cmd/tmsp-cli/tmsp-cli.go index 2fab906a0..50e954d9f 100644 --- a/cmd/tmsp-cli/tmsp-cli.go +++ b/cmd/tmsp-cli/tmsp-cli.go @@ -215,8 +215,7 @@ func cmdAppendTx(c *cli.Context) error { } txBytes, err := stringOrHexToBytes(c.Args()[0]) if err != nil { - fmt.Println(err.Error()) - return nil + return err } res := client.AppendTxSync(txBytes) printResponse(c, res, string(res.Data), true) @@ -231,8 +230,7 @@ func cmdCheckTx(c *cli.Context) error { } txBytes, err := stringOrHexToBytes(c.Args()[0]) if err != nil { - fmt.Println(err.Error()) - return nil + return err } res := client.CheckTxSync(txBytes) printResponse(c, res, string(res.Data), true) @@ -254,8 +252,7 @@ func cmdQuery(c *cli.Context) error { } queryBytes, err := stringOrHexToBytes(c.Args()[0]) if err != nil { - fmt.Println(err.Error()) - return nil + return err } res := client.QuerySync(queryBytes) printResponse(c, res, string(res.Data), true) From 4bb65366f4cbc3442b82eeb99efe1f9f1242dc26 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Tue, 3 Jan 2017 19:26:20 -0800 Subject: [PATCH 7/7] Update CLI test string args --- tests/test_cli/ex1.tmsp | 8 ++++---- tests/test_cli/ex1.tmsp.out | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_cli/ex1.tmsp b/tests/test_cli/ex1.tmsp index de61c8b2f..132512671 100644 --- a/tests/test_cli/ex1.tmsp +++ b/tests/test_cli/ex1.tmsp @@ -1,10 +1,10 @@ echo hello info commit -append_tx abc +append_tx "abc" info commit -query abc -append_tx def=xyz +query "abc" +append_tx "def=xyz" commit -query def +query "def" diff --git a/tests/test_cli/ex1.tmsp.out b/tests/test_cli/ex1.tmsp.out index eabeabd1f..51cee99f7 100644 --- a/tests/test_cli/ex1.tmsp.out +++ b/tests/test_cli/ex1.tmsp.out @@ -7,7 +7,7 @@ > commit -> data: 0x -> append_tx abc +> append_tx "abc" -> code: OK > info @@ -16,17 +16,17 @@ > commit -> data: 0x750502FC7E84BBD788ED589624F06CFA871845D1 -> query abc +> query "abc" -> code: OK --> data: {"index":0,"value":"abc","exists":true} +-> data: {"index":0,"value":"abc","valueHex":"616263","exists":true} -> append_tx def=xyz +> append_tx "def=xyz" -> code: OK > commit -> data: 0x76393B8A182E450286B0694C629ECB51B286EFD5 -> query def +> query "def" -> code: OK --> data: {"index":1,"value":"xyz","exists":true} +-> data: {"index":1,"value":"xyz","valueHex":"78797a","exists":true}