Skip to content

Commit

Permalink
Improve mpdumpdiag
Browse files Browse the repository at this point in the history
- Add flag to change format of payload.
  • Loading branch information
hayarobi committed Jun 19, 2024
1 parent 21555a7 commit ab6293a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ add_custom_target(brick GO111MODULE=on GOBIN=${BIN_DIR} go install ${GCFLAGS} ${
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
DEPENDS libtool)

add_custom_target(mpdumpdiag GO111MODULE=on GOBIN=${BIN_DIR} go install ${GCFLAGS} -ldflags \"-X main.githash=`git describe --tags`\" ./tools/mpdumpdiag/...
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

add_custom_target(deps DEPENDS libtool)

add_custom_target(check GO111MODULE=on go test -timeout 600s ./...
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif

BUILD_RULES := \
deps \
aergocli aergosvr aergoluac polaris colaris brick \
aergocli aergosvr aergoluac polaris colaris brick mpdumpdiag\
libtool libtool-clean \
libluajit liblmdb libgmp \
libluajit-clean liblmdb-clean libgmp-clean \
Expand Down
9 changes: 7 additions & 2 deletions tools/mpdumpdiag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/spf13/cobra"
)

var payloadType string

var (
rootCmd = &cobra.Command{
Use: "mpdumpdiag",
Expand All @@ -30,9 +32,10 @@ var (
)

func init() {
rootCmd.SetOutput(os.Stdout)
rootCmd.SetOut(os.Stdout)
rootCmd.AddCommand(printCmd)
rootCmd.AddCommand(genCmd)
printCmd.Flags().StringVar(&payloadType, "payload", "base58", "output format of payload")
}

func main() {
Expand All @@ -44,6 +47,8 @@ func main() {
func runPrintCmd(cmd *cobra.Command, args []string) {
filename := args[0]

payloadEncodingType := jsonrpc.ParseEncodingType(payloadType)

file, err := os.Open(filename)
if err != nil {
cmd.Printf("error: failed to open file %s\n", filename)
Expand Down Expand Up @@ -82,7 +87,7 @@ func runPrintCmd(cmd *cobra.Command, args []string) {
count++
//mp.put(types.NewTransaction(&buf)) // nolint: errcheck

out = append(out, jsonrpc.ConvTx(types.NewTransaction(&buf).GetTx(), jsonrpc.Base58))
out = append(out, jsonrpc.ConvTx(types.NewTransaction(&buf).GetTx(), payloadEncodingType))
}
b, e := json.MarshalIndent(out, "", " ")
if e == nil {
Expand Down
16 changes: 16 additions & 0 deletions types/jsonrpc/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@ package jsonrpc
import (
"encoding/json"
"fmt"
"strings"
)

type EncodingType int

const (
Raw EncodingType = 0 + iota
Base58
Obj
)

func ParseEncodingType(s string) EncodingType {
flag := strings.ToLower(s)
switch flag {
case "raw":
return Raw
case "obj":
return Obj
case "base58":
fallthrough
default:
return Base58
}
}

func MarshalJSON(i interface{}) string {
jsonout, err := json.MarshalIndent(i, "", " ")
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions types/jsonrpc/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package jsonrpc

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseEncodingType(t *testing.T) {
tests := []struct {
name string
args string
want EncodingType
}{
{"raw", "raW", Raw},
{"raw2", "raw", Raw},
{"obj", "OBJ", Obj},
{"base58", "base58", Base58},
{"nope", "base58", Base58},
{"", "", Base58},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, ParseEncodingType(tt.args), "ParseEncodingType(%v)", tt.args)
})
}
}
42 changes: 35 additions & 7 deletions types/jsonrpc/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func ConvTxBody(msg *types.TxBody, payloadType EncodingType) *InOutTxBody {
switch payloadType {
case Raw:
tb.Payload = string(msg.Payload)
case Obj:
var v map[string]interface{}
err := json.Unmarshal(msg.Payload, &v)
if err != nil {
tb.Payload = ""
} else {
tb.Payload = v
}
case Base58:
tb.Payload = base58.Encode(msg.Payload)
}
Expand Down Expand Up @@ -141,11 +149,16 @@ func ParseTxBody(tb *InOutTxBody) (msg *types.TxBody, err error) {
}
msg.Amount = amount.Bytes()
}
if tb.Payload != "" {
msg.Payload, err = base58.Decode(tb.Payload)
if err != nil {
return nil, err
switch v := tb.Payload.(type) {
case string:
if tb.Payload != "" {
msg.Payload, err = base58.Decode(v)
if err != nil {
return nil, err
}
}
default:
msg.Payload, _ = json.Marshal(v)
}

if tb.PayloadJson != nil && tb.PayloadJson.Name != "" {
Expand Down Expand Up @@ -192,7 +205,7 @@ type InOutTxBody struct {
Account string `json:"account,omitempty"`
Recipient string `json:"recipient,omitempty"`
Amount string `json:"amount,omitempty"`
Payload string `json:"payload,omitempty"`
Payload any `json:"payload,omitempty"`
PayloadJson *types.CallInfo `json:"payloadJson,omitempty"`
GasLimit uint64 `json:"gasLimit,omitempty"`
GasPrice string `json:"gasPrice,omitempty"`
Expand Down Expand Up @@ -239,9 +252,24 @@ func (t *InOutTxInBlock) String() string {
return MarshalJSON(t)
}

// CovPayloadJson unmarshal payload bytes and store it to member variable PayloadJson .
func CovPayloadJson(tx *InOutTx) {
if tx.Body.Payload != "" {
payload, err := base58.Decode(tx.Body.Payload)
var payload []byte
var err error
switch v := tx.Body.Payload.(type) {
case string:
if v != "" {
payload, err = base58.Decode(v)
if err != nil {
return
}
}
default:
payload, _ = json.Marshal(v)
}
if len(payload) > 0 {
payloadString := string(payload)
payload, err := base58.Decode(payloadString)
if err == nil {
tx.Body.PayloadJson = &types.CallInfo{}
_ = json.Unmarshal(payload, tx.Body.PayloadJson)
Expand Down

0 comments on commit ab6293a

Please sign in to comment.