Skip to content

Commit

Permalink
Merge pull request #52 from okp4/feat/debug-proto
Browse files Browse the repository at this point in the history
Feat/debug proto
  • Loading branch information
amimart authored Apr 19, 2022
2 parents c7781a3 + 2959444 commit fc274b5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
vue/
release/
target/
okp4d
.idea/
.vscode/
.DS_Store
61 changes: 61 additions & 0 deletions cmd/okp4d/debug_extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/json"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/proto/tendermint/types"
)

func ExtendDebugCmd(getCmd func(name string) (*cobra.Command, error)) error {
debugCmd, err := getCmd("debug")
if err != nil {
return err
}

debugCmd.AddCommand(DecodeBlocksCmd())
return nil
}

func DecodeBlocksCmd() *cobra.Command {
return &cobra.Command{
Use: "decode-blocks",
Short: "Decode base64 protobuf encoded blocks in JSON.",
Long: "Read base64 protobuf encoded blocks from stdin and write the corresponding JSON representation on stdout.",
RunE: func(cmd *cobra.Command, args []string) error {
scanner := bufio.NewScanner(cmd.InOrStdin())
for scanner.Scan() {
strB64 := scanner.Text()
if strB64[0] == '"' {
strB64 = strB64[1:]
}
if strB64[len(strB64)-1] == '"' {
strB64 = strB64[:len(strB64)-1]
}

bytes, err := base64.StdEncoding.DecodeString(strB64)
if err != nil {
cmd.PrintErrln("could not decode base64 block:", err.Error())
continue
}

block := new(types.Block)
if err := block.Unmarshal(bytes); err != nil {
cmd.PrintErrln("could not decode block:", err.Error())
continue
}

json, err := json.Marshal(block)
if err != nil {
cmd.PrintErrln("could not marshal block in JSON:", err.Error())
continue
}
cmd.Println(string(json))
}

return nil
},
}
}
25 changes: 25 additions & 0 deletions cmd/okp4d/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"fmt"
"os"

svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/okp4/okp4d/app"
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/cosmoscmd"
)

Expand All @@ -18,7 +20,30 @@ func main() {
app.New,
// this line is used by starport scaffolding # root/arguments
)

if err := Extend(rootCmd); err != nil {
os.Exit(1)
}

if err := svrcmd.Execute(rootCmd, app.DefaultNodeHome); err != nil {
os.Exit(1)
}
}

func Extend(cmd *cobra.Command) error {
cmdGetter := func(name string) (*cobra.Command, error) {
return getSubCommand(cmd, name)
}

return ExtendDebugCmd(cmdGetter)
}

func getSubCommand(cmd *cobra.Command, name string) (*cobra.Command, error) {
for i, v := range cmd.Commands() {
if v.Name() == name {
return cmd.Commands()[i], nil
}
}

return nil, fmt.Errorf("cannot find '%s' command", name)
}

0 comments on commit fc274b5

Please sign in to comment.