Skip to content

Commit

Permalink
Fixed wrong handling of bytes-encoding in some situations
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Dec 17, 2024
1 parent 0f6b3c3 commit ef6ce68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
12 changes: 6 additions & 6 deletions cmd/tools/compare/tools_compare_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/streamingfast/dstore"
firecore "github.com/streamingfast/firehose-core"
"github.com/streamingfast/firehose-core/cmd/tools/check"
"github.com/streamingfast/firehose-core/json"
fcjson "github.com/streamingfast/firehose-core/json"
fcproto "github.com/streamingfast/firehose-core/proto"
"github.com/streamingfast/firehose-core/types"
"go.uber.org/multierr"
Expand Down Expand Up @@ -361,16 +361,16 @@ func Compare(reference proto.Message, current proto.Message, includeUnknownField

//todo: check if there is a equals that do not compare unknown fields
if !proto.Equal(reference, current) {
var opts []json.MarshallerOption
var opts []fcjson.MarshallerOption
if !includeUnknownFields {
opts = append(opts, json.WithoutUnknownFields())
opts = append(opts, fcjson.WithoutUnknownFields())
}

if bytesEncoding == "base58" {
opts = append(opts, json.WithBytesEncoderFunc(json.ToBase58))
if bytesEncoding != "" {
opts = append(opts, fcjson.WithBytesEncoding(bytesEncoding))
}

encoder := json.NewMarshaller(registry, opts...)
encoder := fcjson.NewMarshaller(registry, opts...)

referenceAsJSON, err := encoder.MarshalToString(reference)
cli.NoError(err, "marshal JSON reference")
Expand Down
8 changes: 2 additions & 6 deletions cmd/tools/print/printer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ type JSONOutputPrinter struct {
func NewJSONOutputPrinter(bytesEncoding string, singleLine bool, registry *fcproto.Registry) (OutputPrinter, error) {
var options []fcjson.MarshallerOption

if bytesEncoding == "base58" {
options = append(options, fcjson.WithBytesEncoderFunc(fcjson.ToBase58))
}

if bytesEncoding == "base64" {
options = append(options, fcjson.WithBytesEncoderFunc(fcjson.ToBase64))
if bytesEncoding != "" {
options = append(options, fcjson.WithBytesEncoding(bytesEncoding))
}

return &JSONOutputPrinter{
Expand Down
44 changes: 37 additions & 7 deletions json/marshallers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ type kv struct {
value any
}

type CustomEncoderFunc func(encoder *jsontext.Encoder, t []byte, options json.Options) error

type Marshaller struct {
marshallers *json.Marshalers
includeUnknownFields bool
registry *proto.Registry
bytesEncoderFunc func(encoder *jsontext.Encoder, t []byte, options json.Options) error
bytesEncoderFunc CustomEncoderFunc
}

type MarshallerOption func(*Marshaller)
Expand All @@ -40,7 +42,15 @@ func WithoutUnknownFields() MarshallerOption {
e.includeUnknownFields = false
}
}
func WithBytesEncoderFunc(f func(encoder *jsontext.Encoder, t []byte, options json.Options) error) MarshallerOption {

func WithBytesEncoding(bytesEncoding string) MarshallerOption {
return withBytesEncoderFunc(jsonEncoder(bytesEncoding))
}

// Deprecated: Use WithBytesEncoding instead passing the encoding directly.
var WithBytesEncoderFunc = withBytesEncoderFunc

func withBytesEncoderFunc(f CustomEncoderFunc) MarshallerOption {
return func(e *Marshaller) {
e.bytesEncoderFunc = f
}
Expand Down Expand Up @@ -125,8 +135,7 @@ func (m *Marshaller) dynamicpbMessage(encoder *jsontext.Encoder, msg *dynamicpb.
x := msg.GetUnknown()
fieldNumber, ofType, l := protowire.ConsumeField(x)
if l > 0 {
var unknownValue []byte
unknownValue = x[:l]
unknownValue := x[:l]
kvl = append(kvl, &kv{
key: fmt.Sprintf("__unknown_fields_%d_with_type_%d__", fieldNumber, ofType),
value: hex.EncodeToString(unknownValue),
Expand Down Expand Up @@ -213,15 +222,36 @@ func EncodeHex(bytes []byte) string {

// Encode encodes the given bytes using the specified encoding.
func Encode(bytesEncoding string, bytes []byte) string {
return Encoder(bytesEncoding)(bytes)
}

// Encoder returns the encoder function that will converts received bytes
// into a string of the specified encoding.
func Encoder(bytesEncoding string) func([]byte) string {
switch {
case strings.EqualFold(bytesEncoding, "base58"):
return base58.Encode

case strings.EqualFold(bytesEncoding, "base64"):
return base64.StdEncoding.EncodeToString

case strings.EqualFold(bytesEncoding, "hex"):
return hex.EncodeToString
}

panic(fmt.Errorf("unsupported bytes encoding: %s", bytesEncoding))
}

func jsonEncoder(bytesEncoding string) jsonEncoderFunc {
switch {
case strings.EqualFold(bytesEncoding, "base58"):
return base58.Encode(bytes)
return ToBase58

case strings.EqualFold(bytesEncoding, "base64"):
return base64.StdEncoding.EncodeToString(bytes)
return ToBase64

case strings.EqualFold(bytesEncoding, "hex"):
return hex.EncodeToString(bytes)
return ToHex
}

panic(fmt.Errorf("unsupported bytes encoding: %s", bytesEncoding))
Expand Down

0 comments on commit ef6ce68

Please sign in to comment.