Browse Source

Improve rpc to properly format any alias for []byte in URIClient

pull/456/head
Ethan Frey 7 years ago
parent
commit
bd93f76950
2 changed files with 44 additions and 4 deletions
  1. +39
    -0
      rpc/client/args_test.go
  2. +5
    -4
      rpc/client/http_client.go

+ 39
- 0
rpc/client/args_test.go View File

@ -0,0 +1,39 @@
package rpcclient
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Tx []byte
type Foo struct {
Bar int
Baz string
}
func TestArgToJSON(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
cases := []struct {
input interface{}
expected string
}{
{[]byte("1234"), "0x31323334"},
{Tx("654"), "0x363534"},
{Foo{7, "hello"}, `{"Bar":7,"Baz":"hello"}`},
}
for i, tc := range cases {
args := map[string]interface{}{"data": tc.input}
err := argsToJson(args)
require.Nil(err, "%d: %+v", i, err)
require.Equal(1, len(args), "%d", i)
data, ok := args["data"].(string)
require.True(ok, "%d: %#v", i, args["data"])
assert.Equal(tc.expected, data, "%d", i)
}
}

+ 5
- 4
rpc/client/http_client.go View File

@ -12,8 +12,8 @@ import (
"strings"
"github.com/pkg/errors"
types "github.com/tendermint/tendermint/rpc/types"
wire "github.com/tendermint/go-wire"
types "github.com/tendermint/tendermint/rpc/types"
)
// HTTPClient is a common interface for JSONRPCClient and URIClient.
@ -179,10 +179,11 @@ func argsToJson(args map[string]interface{}) error {
var n int
var err error
for k, v := range args {
// Convert byte slices to "0x"-prefixed hex
byteSlice, isByteSlice := reflect.ValueOf(v).Interface().([]byte)
rt := reflect.TypeOf(v)
isByteSlice := rt.Kind() == reflect.Slice && rt.Elem().Kind() == reflect.Uint8
if isByteSlice {
args[k] = fmt.Sprintf("0x%X", byteSlice)
bytes := reflect.ValueOf(v).Bytes()
args[k] = fmt.Sprintf("0x%X", bytes)
continue
}


Loading…
Cancel
Save