Skip to content

Commit

Permalink
feat(textual): bytes value renderer (#12734)
Browse files Browse the repository at this point in the history
## Description

Closes: #12711

Implements bytes value renderer for SIGN_MODE_TEXTUAL

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
joeabbey committed Jul 27, 2022
1 parent f770f8c commit 3343c57
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 35 deletions.
10 changes: 10 additions & 0 deletions tx/textual/internal/testdata/bytes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
["", ""],
["00", "AA=="],
["66", "Zg=="],
["666F", "Zm8="],
["666F6F", "Zm9v"],
["666F6F62", "Zm9vYg=="],
["666F6F6261", "Zm9vYmE="],
["666F6F626172", "Zm9vYmFy"]
]
1 change: 1 addition & 0 deletions tx/textual/internal/testpb/1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ message A {
string SDKDEC = 6 [(cosmos_proto.scalar) = "cosmos.Dec"];
cosmos.base.v1beta1.Coin COIN = 7;
repeated cosmos.base.v1beta1.Coin COINS = 8;
bytes BYTES = 9;
}

// B contains fields that are not parseable by SIGN_MODE_TEXTUAL, some fields
Expand Down
134 changes: 105 additions & 29 deletions tx/textual/internal/testpb/1.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions tx/textual/internal/testpb/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ deps:
- remote: buf.build
owner: cosmos
repository: cosmos-proto
branch: main
commit: 1935555c206d4afb9e94615dfd0fad31
digest: b1-TNqW6xj2Pjha5Uoj9a-5uOeRo4mwswKfyqMcN3I_gZ0=
create_time: 2021-12-02T22:04:00.31049Z
- remote: buf.build
owner: cosmos
repository: gogo-proto
branch: main
commit: bee5511075b7499da6178d9e4aaa628b
digest: b1-rrBIustouD-S80cVoZ_rM0qJsmei9AgbXy9GPQu6vxg=
create_time: 2021-12-02T20:01:17.069307Z
34 changes: 34 additions & 0 deletions tx/textual/valuerenderer/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package valuerenderer

import (
"context"
"encoding/base64"
"io"

"google.golang.org/protobuf/reflect/protoreflect"
)

//bytesValueRenderer implements ValueRenderer for bytes
type bytesValueRenderer struct {
}

var _ ValueRenderer = bytesValueRenderer{}

func (vr bytesValueRenderer) Format(ctx context.Context, v protoreflect.Value, w io.Writer) error {
_, err := w.Write([]byte(base64.StdEncoding.EncodeToString(v.Bytes())))
return err
}

func (vr bytesValueRenderer) Parse(_ context.Context, r io.Reader) (protoreflect.Value, error) {
formatted, err := io.ReadAll(r)
if err != nil {
return protoreflect.ValueOfBytes([]byte{}), err
}

data, err := base64.StdEncoding.DecodeString(string(formatted))
if err != nil {
return protoreflect.ValueOfBytes([]byte{}), err
}

return protoreflect.ValueOfBytes(data), nil
}
45 changes: 45 additions & 0 deletions tx/textual/valuerenderer/bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package valuerenderer_test

import (
"context"
"encoding/hex"
"encoding/json"
"io/ioutil"
"strings"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/reflect/protoreflect"
)

func TestFormatBytes(t *testing.T) {
var testcases []bytesTest
raw, err := ioutil.ReadFile("../internal/testdata/bytes.json")
require.NoError(t, err)

err = json.Unmarshal(raw, &testcases)
require.NoError(t, err)

for _, tc := range testcases {
data, err := hex.DecodeString(tc.hex)
require.NoError(t, err)

r, err := valueRendererOf(data)
require.NoError(t, err)

b := new(strings.Builder)
err = r.Format(context.Background(), protoreflect.ValueOfBytes(data), b)
require.NoError(t, err)
require.Equal(t, tc.expRes, b.String())
}
}

type bytesTest struct {
hex string
expRes string
}

func (t *bytesTest) UnmarshalJSON(b []byte) error {
a := []interface{}{&t.hex, &t.expRes}
return json.Unmarshal(b, &a)
}
2 changes: 2 additions & 0 deletions tx/textual/valuerenderer/valuerenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (r Textual) GetValueRenderer(fd protoreflect.FieldDescriptor) (ValueRendere

return vr, nil
}
case fd.Kind() == protoreflect.BytesKind:
return bytesValueRenderer{}, nil

// Integers
case fd.Kind() == protoreflect.Uint32Kind ||
Expand Down
3 changes: 3 additions & 0 deletions tx/textual/valuerenderer/valuerenderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestGetADR050ValueRenderer(t *testing.T) {
{"uint64", uint64(1), false},
{"sdk.Int", math.NewInt(1), false},
{"sdk.Dec", math.LegacyNewDec(1), false},
{"[]byte", []byte{1}, false},
{"float32", float32(1), true},
{"float64", float64(1), true},
}
Expand Down Expand Up @@ -128,6 +129,8 @@ func valueRendererOf(v interface{}) (valuerenderer.ValueRenderer, error) {
return textual.GetValueRenderer(a.ByName(protoreflect.Name("INT32")))
case int64:
return textual.GetValueRenderer(a.ByName(protoreflect.Name("INT64")))
case []byte:
return textual.GetValueRenderer(a.ByName(protoreflect.Name("BYTES")))
case math.Int:
return textual.GetValueRenderer(a.ByName(protoreflect.Name("SDKINT")))
case math.LegacyDec:
Expand Down

0 comments on commit 3343c57

Please sign in to comment.