Skip to content

Commit

Permalink
add querier
Browse files Browse the repository at this point in the history
  • Loading branch information
lkh1434 committed Sep 14, 2024
1 parent dd64424 commit 04355a4
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 0 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/v26/app"
"github.com/osmosis-labs/osmosis/v26/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
}
115 changes: 115 additions & 0 deletions cmd/osmosisd/cmd/decoder/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package decoder

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

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

"github.com/osmosis-labs/osmosis/v26/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
codec := d.EncodingConfig.Marshaler

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

signersBytes, _, err := codec.GetMsgV1Signers(msg)
if err != nil {
fmt.Println(err)
return nil, err
}

signers := convertSignersToAddresses(signersBytes, codec)
fmt.Println("signers: ", signers)

msgs = append(msgs, &GeneralCosmosMsg{
Type: msgType,
Message: msgString,
Signers: signers,
})
}

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
// }

func convertSignersToAddresses(signers [][]byte, cdc codec.Codec) []string {
var results []string
sMap := map[string]bool{}
for _, bytes := range signers {
signer, err := cdc.InterfaceRegistry().SigningContext().AddressCodec().BytesToString(bytes)
if err != nil {
// TODO: 일단 테스트보고 나중에 panic 제거
panic(err)
//fmt.Println(err)
//return nil
}
if _, ok := sMap[signer]; ok {
continue
}
sMap[signer] = true
results = append(results, signer)
}
return results
}
Loading

0 comments on commit 04355a4

Please sign in to comment.