forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
574 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.