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
6 changed files
with
602 additions
and
0 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/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 | ||
} |
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,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 | ||
} |
Oops, something went wrong.