Skip to content

Commit

Permalink
add querier
Browse files Browse the repository at this point in the history
  • Loading branch information
lkh1434 committed Apr 11, 2024
1 parent c5cdf79 commit d448fe1
Show file tree
Hide file tree
Showing 8 changed files with 574 additions and 3 deletions.
45 changes: 45 additions & 0 deletions cmd/osmosisd/cmd/decode_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

// DONTCOVER

import (
"fmt"

"github.com/spf13/cobra"

"github.com/osmosis-labs/osmosis/v24/app"
"github.com/osmosis-labs/osmosis/v24/cmd/osmosisd/cmd/decoder"
)

const (
decodeServerPort = "decodeServerPort"
)

// decoderCmd gets cmd to run decode server
func decoderCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "decoder",
Short: "Example osmosisd decode -p 8888, which would run decoder server on specified port",
Long: "decoder command runs decoder server to decode byte array to General Cosmos messages",
RunE: func(cmd *cobra.Command, args []string) error {
decodeServerFlag, err := cmd.Flags().GetString(decodeServerPort)
if err != nil {
return err
}

d := decoder.Decoder{
EncodingConfig: app.MakeEncodingConfig(),
}
err = d.ListenAndServe(decodeServerFlag)
if err != nil {
fmt.Println(err)
return err
}
return nil
},
}

cmd.Flags().StringP(decodeServerPort, "p", "", "port to listen to")
cmd.MarkFlagRequired(decodeServerPort)
return cmd
}
84 changes: 84 additions & 0 deletions cmd/osmosisd/cmd/decoder/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package decoder

import (
"context"
"fmt"
"log"
"net"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/jsonpb"
"google.golang.org/grpc"

"github.com/osmosis-labs/osmosis/v24/app/params"
)

type Decoder struct {
EncodingConfig params.EncodingConfig
}

func (d *Decoder) ListenAndServe(port string) error {
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%s", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
return err
}
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
RegisterCosmosDecoderServer(grpcServer, d)
return grpcServer.Serve(lis)
}

func (d *Decoder) mustEmbedUnimplementedCosmosDecoderServer() {
panic("Forward-compatibility: Unknown method!")
}

func (d *Decoder) Decode(ctx context.Context, request *DecodeRequest) (*DecodeResponse, error) {
cosmosTx, err := d.EncodingConfig.TxConfig.TxDecoder()(request.TxByte)

if err != nil {
fmt.Println(err)
return nil, err
}
jsonpbMarshaller := jsonpb.Marshaler{}

var msgs []*GeneralCosmosMsg

for _, msg := range cosmosTx.GetMsgs() {
msgString, err := jsonpbMarshaller.MarshalToString(msg)
if err != nil {
return nil, err
}
msgType := sdk.MsgTypeURL(msg)

msgs = append(msgs, &GeneralCosmosMsg{
Type: msgType,
Message: msgString,
Signers: getSliceFromAccAddress(msg.GetSigners()),
})
}

resultString, err := d.EncodingConfig.TxConfig.TxJSONEncoder()(cosmosTx)

if err != nil {
return nil, err
}

return &DecodeResponse{
TxResult: string(resultString),
Msgs: msgs,
}, nil
}

func getSliceFromAccAddress(addrs []sdk.AccAddress) []string {
var results []string
sMap := map[string]string{}
for _, s := range addrs {
if _, ok := sMap[s.String()]; ok {
continue
}
sMap[s.String()] = "exists"
results = append(results, s.String())
}
return results
}
Loading

0 comments on commit d448fe1

Please sign in to comment.