generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implements debug blocks proto base64 decode
- Loading branch information
Showing
1 changed file
with
61 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,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 | ||
}, | ||
} | ||
} |