Skip to content

Commit

Permalink
Clean up marshaller added hex support
Browse files Browse the repository at this point in the history
  • Loading branch information
jubeless committed Dec 5, 2023
1 parent bf490db commit 126fe12
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 85 deletions.
6 changes: 1 addition & 5 deletions cmd/tools/print/tools_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,5 @@ func SetupJsonEncoder(cmd *cobra.Command) (*jsonencoder.Encoder, error) {
}

pbregistry.Extends(protoregistry.WellKnownRegistry)

options := []jsonencoder.Option{
jsonencoder.WithBytesAsHex(),
}
return jsonencoder.New(pbregistry, options...), nil
return jsonencoder.New(pbregistry), nil
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
buf.build/gen/go/bufbuild/reflect/connectrpc/go v1.12.0-20230822193137-310c9c4845dd.1
buf.build/gen/go/bufbuild/reflect/protocolbuffers/go v1.31.0-20230822193137-310c9c4845dd.2
connectrpc.com/connect v1.12.0
github.com/ShinyTrinkets/overseer v0.3.0
github.com/dustin/go-humanize v1.0.1
github.com/go-json-experiment/json v0.0.0-20231013223334-54c864be5b8d
Expand Down Expand Up @@ -40,7 +41,6 @@ require (
)

require (
connectrpc.com/connect v1.12.0 // indirect
github.com/bufbuild/protocompile v0.4.0 // indirect
github.com/google/s2a-go v0.1.4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
Expand Down Expand Up @@ -75,7 +75,7 @@ require (
github.com/bits-and-blooms/bitset v1.3.1 // indirect
github.com/blendle/zapdriver v1.3.2-0.20200203083823-9200777f8a3d // indirect
github.com/bobg/go-generics/v2 v2.1.1 // indirect
github.com/bufbuild/connect-go v1.10.0
github.com/bufbuild/connect-go v1.10.0 // indirect
github.com/bufbuild/connect-grpchealth-go v1.1.1 // indirect
github.com/bufbuild/connect-grpcreflect-go v1.0.0 // indirect
github.com/bufbuild/connect-opentelemetry-go v0.3.0 // indirect
Expand Down Expand Up @@ -228,5 +228,5 @@ require (
replace (
github.com/ShinyTrinkets/overseer => github.com/streamingfast/overseer v0.2.1-0.20210326144022-ee491780e3ef
github.com/bytecodealliance/wasmtime-go/v4 => github.com/streamingfast/wasmtime-go/v4 v4.0.0-freemem3
github.com/jhump/protoreflect => github.com/streamingfast/protoreflect v0.0.0-20230414203421-018294174fdc
github.com/jhump/protoreflect => github.com/streamingfast/protoreflect v0.0.0-20231205191344-4b629d20ce8d
)
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/streamingfast/bstream v0.0.2-0.20231205163051-ade2f311eca3 h1:u8orpRssS8rYceziOQ/mbBQHlYh5w06oOtTXK90/yMc=
github.com/streamingfast/bstream v0.0.2-0.20231205163051-ade2f311eca3/go.mod h1:08GVb+DXyz6jVNIsbf+2zlaC81UeEGu5o1h49KrSR3Y=
github.com/streamingfast/bstream v0.0.2-0.20231205174934-869fb7d64fd2 h1:TQPPxjBXflVinpSSbYaiMuNgw1HB1YnMcFiR52M8EVo=
github.com/streamingfast/bstream v0.0.2-0.20231205174934-869fb7d64fd2/go.mod h1:08GVb+DXyz6jVNIsbf+2zlaC81UeEGu5o1h49KrSR3Y=
github.com/streamingfast/bstream v0.0.2-0.20231205175345-609448673b00 h1:U/8aQZOpOzLTVcuEVdbEVffVu00ixotkTe8DRhEXxao=
github.com/streamingfast/bstream v0.0.2-0.20231205175345-609448673b00/go.mod h1:08GVb+DXyz6jVNIsbf+2zlaC81UeEGu5o1h49KrSR3Y=
github.com/streamingfast/bstream v0.0.2-0.20231205185208-7e21cc7e64bc h1:ioohiLa+d59fqToa2OhbUx418YMrqt2bLT+m+fmjOG8=
github.com/streamingfast/bstream v0.0.2-0.20231205185208-7e21cc7e64bc/go.mod h1:08GVb+DXyz6jVNIsbf+2zlaC81UeEGu5o1h49KrSR3Y=
github.com/streamingfast/cli v0.0.4-0.20230825151644-8cc84512cd80 h1:UxJUTcEVkdZy8N77E3exz0iNlgQuxl4m220GPvzdZ2s=
Expand Down Expand Up @@ -619,8 +613,8 @@ github.com/streamingfast/overseer v0.2.1-0.20210326144022-ee491780e3ef h1:9IVFHR
github.com/streamingfast/overseer v0.2.1-0.20210326144022-ee491780e3ef/go.mod h1:cq8CvbZ3ioFmGrHokSAJalS0lC+pVXLKhITScItUGXY=
github.com/streamingfast/pbgo v0.0.6-0.20231120172814-537d034aad5e h1:8hoT2QUwh+YNgIcCPux9xd4u9XojHR8hbyAzz7rQuEM=
github.com/streamingfast/pbgo v0.0.6-0.20231120172814-537d034aad5e/go.mod h1:fZuijmeFrqxW2YnnXmGrkQpUTHx3eHCaJUKwdvXAYKM=
github.com/streamingfast/protoreflect v0.0.0-20230414203421-018294174fdc h1:poYChURzYXislOzzeo44FKipd3wWvxhlz966qzO9kZk=
github.com/streamingfast/protoreflect v0.0.0-20230414203421-018294174fdc/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo=
github.com/streamingfast/protoreflect v0.0.0-20231205191344-4b629d20ce8d h1:33VIARqUqBUKXJcuQoOS1rVSms54tgxhhNCmrLptpLg=
github.com/streamingfast/protoreflect v0.0.0-20231205191344-4b629d20ce8d/go.mod h1:aBJivEdekmFWYSQ29EE/fN9IanJWJXbtjy3ky0XD/jE=
github.com/streamingfast/sf-tracing v0.0.0-20230616174903-cd2ade641ca9 h1:YRwpVvLYa+FEJlTy0S7mk4UptYjk5zac+A+ZE1phOeA=
github.com/streamingfast/sf-tracing v0.0.0-20230616174903-cd2ade641ca9/go.mod h1:ktzt1BUj3GF+SKQHEmn3ShryJ7y87JeCHtaTGaDVATs=
github.com/streamingfast/shutter v1.5.0 h1:NpzDYzj0HVpSiDJVO/FFSL6QIK/YKOxY0gJAtyaTOgs=
Expand Down
9 changes: 2 additions & 7 deletions jsonencoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ type Encoder struct {
marshallers []*json.Marshalers
}

func New(files *protoregistry.Registry, opts ...Option) *Encoder {
func New(files *protoregistry.Registry) *Encoder {
e := &Encoder{
protoRegistry: files,
}

e.marshallers = []*json.Marshalers{
json.MarshalFuncV2(e.protoAny),
}

for _, opt := range opts {
opt(e)
json.MarshalFuncV2(e.anypb),
}
return e
}
Expand Down
45 changes: 45 additions & 0 deletions jsonencoder/marshallers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jsonencoder

import (
"encoding/hex"
"fmt"
"strings"

"github.com/jhump/protoreflect/dynamic"

"github.com/mr-tron/base58"

"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"google.golang.org/protobuf/types/known/anypb"
)

func (e *Encoder) anypb(encoder *jsontext.Encoder, t *anypb.Any, options json.Options) error {
msg, err := e.protoRegistry.Unmarshall(t)
if err != nil {
return fmt.Errorf("unmarshalling proto any: %w", err)
}
setBytesEncoder(t.TypeUrl)
cnt, err := json.Marshal(msg, json.WithMarshalers(json.NewMarshalers(e.marshallers...)))
if err != nil {
return fmt.Errorf("json marshalling proto any: %w", err)
}
return encoder.WriteValue(cnt)
}

func (e *Encoder) base58Bytes(encoder *jsontext.Encoder, t []byte, options json.Options) error {
return encoder.WriteToken(jsontext.String(base58.Encode(t)))
}

func (e *Encoder) hexBytes(encoder *jsontext.Encoder, t []byte, options json.Options) error {
return encoder.WriteToken(jsontext.String(hex.EncodeToString(t)))
}

func setBytesEncoder(typeURL string) {
if strings.Contains(typeURL, "solana") {
dynamic.SetDefaultBytesRepresentation(dynamic.BytesAsBase58)
return
} else {
dynamic.SetDefaultBytesRepresentation(dynamic.BytesAsHex)
}
}
32 changes: 0 additions & 32 deletions jsonencoder/options.go

This file was deleted.

21 changes: 0 additions & 21 deletions jsonencoder/proto.go

This file was deleted.

2 changes: 1 addition & 1 deletion protoregistry/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
for _, file := range fileDescriptorSet.Msg.FileDescriptorSet.File {
cnt, err := proto.Marshal(file)
if err != nil {
log.Fatalf("failed to marshall proto file %s: %v", file.Name, err)
log.Fatalf("failed to marshall proto file %s: %v", file.GetName(), err)
return
}
name := ""
Expand Down
10 changes: 6 additions & 4 deletions protoregistry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

"google.golang.org/protobuf/types/known/anypb"

"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic"
)
Expand Down Expand Up @@ -37,18 +39,18 @@ func (r *Registry) RegisterFileDescriptor(f *desc.FileDescriptor) {
r.filesDescriptors = append(r.filesDescriptors, f)
}

func (r *Registry) Unmarshall(typeURL string, value []byte) (*dynamic.Message, error) {
func (r *Registry) Unmarshall(t *anypb.Any) (*dynamic.Message, error) {
for _, fd := range r.filesDescriptors {
md := fd.FindSymbol(cleanTypeURL(typeURL))
md := fd.FindSymbol(cleanTypeURL(t.TypeUrl))
if md != nil {
dynMsg := dynamic.NewMessageFactoryWithDefaults().NewDynamicMessage(md.(*desc.MessageDescriptor))
if err := dynMsg.Unmarshal(value); err != nil {
if err := dynMsg.Unmarshal(t.Value); err != nil {
return nil, fmt.Errorf("unmarshalling proto: %w", err)
}
return dynMsg, nil
}
}
return nil, fmt.Errorf("no message descriptor in registry for type url: %s", typeURL)
return nil, fmt.Errorf("no message descriptor in registry for type url: %s", t.TypeUrl)
}

func (r *Registry) Extends(registry *Registry) {
Expand Down
4 changes: 0 additions & 4 deletions unsafe_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ package firecore
import (
"context"

pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/streamingfast/dlauncher/launcher"
"go.uber.org/zap"
)

var UnsafePayloadKind pbbstream.Protocol = pbbstream.Protocol_UNKNOWN
var UnsafeJsonBytesEncoder = "hex"

// UnsafeResolveReaderNodeStartBlock is a function that resolved the reader node start block num, by default it simply
// returns the value of the 'reader-node-start-block-num'. However, the function may be overwritten in certain chains
// to perform a more complex resolution logic.
Expand Down

0 comments on commit 126fe12

Please sign in to comment.