Skip to content

Commit

Permalink
feat: implements debug blocks proto base64 decode
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Apr 12, 2022
1 parent 8f5d669 commit 1749e70
Showing 1 changed file with 61 additions and 0 deletions.
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
},
}
}

0 comments on commit 1749e70

Please sign in to comment.