Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex Encode Data in Tx Responses #4028

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#3916 Hex encode data in tx responses
24 changes: 18 additions & 6 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,9 @@ func TestSubmitProposal(t *testing.T) {
require.Equal(t, uint32(0), resultTx.Code)

var proposalID uint64
cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.Data, &proposalID)
bz, err := hex.DecodeString(resultTx.Data)
require.NoError(t, err)
cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID)

// verify balance
acc = getAccount(t, port, addr)
Expand Down Expand Up @@ -646,7 +648,9 @@ func TestDeposit(t *testing.T) {
require.Equal(t, uint32(0), resultTx.Code)

var proposalID uint64
cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.Data, &proposalID)
bz, err := hex.DecodeString(resultTx.Data)
require.NoError(t, err)
cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID)

// verify balance
acc = getAccount(t, port, addr)
Expand Down Expand Up @@ -703,7 +707,9 @@ func TestVote(t *testing.T) {
require.Equal(t, uint32(0), resultTx.Code)

var proposalID uint64
cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.Data, &proposalID)
bz, err := hex.DecodeString(resultTx.Data)
require.NoError(t, err)
cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID)

// verify balance
acc = getAccount(t, port, addr)
Expand Down Expand Up @@ -804,18 +810,24 @@ func TestProposalsQuery(t *testing.T) {
// Addr1 proposes (and deposits) proposals #1 and #2
resultTx := doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], halfMinDeposit, fees)
var proposalID1 uint64
cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.Data, &proposalID1)
bz, err := hex.DecodeString(resultTx.Data)
require.NoError(t, err)
cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID1)
tests.WaitForHeight(resultTx.Height+1, port)

resultTx = doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], halfMinDeposit, fees)
var proposalID2 uint64
cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.Data, &proposalID2)
bz, err = hex.DecodeString(resultTx.Data)
require.NoError(t, err)
cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID2)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 proposes (and deposits) proposals #3
resultTx = doSubmitProposal(t, port, seeds[1], names[1], passwords[1], addrs[1], halfMinDeposit, fees)
var proposalID3 uint64
cdc.MustUnmarshalBinaryLengthPrefixed(resultTx.Data, &proposalID3)
bz, err = hex.DecodeString(resultTx.Data)
require.NoError(t, err)
cdc.MustUnmarshalBinaryLengthPrefixed(bz, &proposalID3)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 deposits on proposals #2 & #3
Expand Down
15 changes: 8 additions & 7 deletions types/result.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -67,7 +68,7 @@ type TxResponse struct {
Height int64 `json:"height"`
TxHash string `json:"txhash"`
Code uint32 `json:"code,omitempty"`
Data []byte `json:"data,omitempty"`
Data string `json:"data,omitempty"`
RawLog string `json:"raw_log,omitempty"`
Logs ABCIMessageLogs `json:"logs,omitempty"`
Info string `json:"info,omitempty"`
Expand All @@ -90,7 +91,7 @@ func NewResponseResultTx(res *ctypes.ResultTx, tx Tx) TxResponse {
TxHash: res.Hash.String(),
Height: res.Height,
Code: res.TxResult.Code,
Data: res.TxResult.Data,
Data: strings.ToUpper(hex.EncodeToString(res.TxResult.Data)),
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
RawLog: res.TxResult.Log,
Logs: parsedLogs,
Info: res.TxResult.Info,
Expand Down Expand Up @@ -127,7 +128,7 @@ func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
Height: res.Height,
TxHash: txHash,
Code: res.CheckTx.Code,
Data: res.CheckTx.Data,
Data: strings.ToUpper(hex.EncodeToString(res.CheckTx.Data)),
RawLog: res.CheckTx.Log,
Logs: parsedLogs,
Info: res.CheckTx.Info,
Expand All @@ -154,7 +155,7 @@ func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
Height: res.Height,
TxHash: txHash,
Code: res.DeliverTx.Code,
Data: res.DeliverTx.Data,
Data: strings.ToUpper(hex.EncodeToString(res.DeliverTx.Data)),
RawLog: res.DeliverTx.Log,
Logs: parsedLogs,
Info: res.DeliverTx.Info,
Expand All @@ -175,7 +176,7 @@ func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) TxResponse {

return TxResponse{
Code: res.Code,
Data: res.Data.Bytes(),
Data: res.Data.String(),
RawLog: res.Log,
Logs: parsedLogs,
TxHash: res.Hash.String(),
Expand All @@ -198,8 +199,8 @@ func (r TxResponse) String() string {
sb.WriteString(fmt.Sprintf(" Code: %d\n", r.Code))
}

if r.Data != nil {
sb.WriteString(fmt.Sprintf(" Data: %s\n", string(r.Data)))
if r.Data != "" {
sb.WriteString(fmt.Sprintf(" Data: %s\n", r.Data))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use tab \t in this function?

Suggested change
sb.WriteString(fmt.Sprintf(" Data: %s\n", r.Data))
sb.WriteString(fmt.Sprintf("\tData: %s\n", r.Data))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no, please don't.... I'd even remove the trailing \n

Copy link
Contributor Author

@alexanderbez alexanderbez Apr 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol...the tab vs space war begins. idc really (go's standard is tabs actually), but I'd rather be consistent. What to do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz no \t 🙏

}

if r.RawLog != "" {
Expand Down