Skip to content

Commit

Permalink
fix: fix incorrect output in cmd (#229)
Browse files Browse the repository at this point in the history
* fix: fix incorrect output in cmd

* chore: mod tidy
  • Loading branch information
simlecode authored Jul 27, 2022
1 parent f700cb8 commit 4386d21
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 15 deletions.
17 changes: 15 additions & 2 deletions cli/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strconv"
"time"

"github.com/filecoin-project/venus-messager/utils/actor_parser"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/exitcode"
Expand All @@ -19,6 +17,8 @@ import (
"github.com/urfave/cli/v2"

"github.com/filecoin-project/venus-messager/cli/tablewriter"
"github.com/filecoin-project/venus-messager/utils"
"github.com/filecoin-project/venus-messager/utils/actor_parser"
types "github.com/filecoin-project/venus/venus-shared/types/messager"
)

Expand Down Expand Up @@ -128,13 +128,26 @@ var searchCmd = &cli.Command{
}

if params != nil {
res, err := utils.TryConvertParams(params)
if err != nil {
fmt.Printf("try convert params failed %v\n", err)
} else {
params = res
}

bytes, _ := json.MarshalIndent(params, "", "\t")
if len(bytes) != 0 {
fmt.Printf("- params information:\n%s\n", string(bytes))
}
}

if rets != nil {
res, err := utils.TryConvertParams(rets)
if err != nil {
fmt.Printf("try convert ret failed %v\n", err)
} else {
rets = res
}
bytes, _ := json.MarshalIndent(rets, "", "\t")
if len(bytes) != 0 {
fmt.Printf("- returns information:\n%s\n", string(bytes))
Expand Down
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module github.com/filecoin-project/venus-messager
go 1.17

require (
contrib.go.opencensus.io/exporter/graphite v0.0.0-20200424223504-26b90655e0ce // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/fatih/color v1.13.0
github.com/filecoin-project/go-address v0.0.6
github.com/filecoin-project/go-bitfield v0.2.4
github.com/filecoin-project/go-cbor-util v0.0.1
github.com/filecoin-project/go-jsonrpc v0.1.5
github.com/filecoin-project/go-state-types v0.1.10
github.com/filecoin-project/specs-actors v0.9.15
github.com/filecoin-project/specs-actors/v5 v5.0.6
github.com/filecoin-project/specs-actors/v6 v6.0.2
github.com/filecoin-project/venus v1.6.1-0.20220718091042-a51da69e1731
github.com/filecoin-project/venus-auth v1.6.0
Expand All @@ -25,21 +27,19 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml v1.9.4
github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.9.0 // indirect
github.com/stretchr/testify v1.7.1
github.com/urfave/cli/v2 v2.3.0
github.com/whyrusleeping/cbor-gen v0.0.0-20220323183124-98fa8256a799
go.opencensus.io v0.23.0
go.uber.org/fx v1.15.0
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
gorm.io/driver/mysql v1.1.1
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.12
modernc.org/mathutil v1.1.1
)

require go.opencensus.io v0.23.0

require (
contrib.go.opencensus.io/exporter/graphite v0.0.0-20200424223504-26b90655e0ce // indirect
contrib.go.opencensus.io/exporter/jaeger v0.2.1 // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
github.com/BurntSushi/toml v0.4.1 // indirect
Expand All @@ -62,15 +62,12 @@ require (
github.com/filecoin-project/go-amt-ipld/v2 v2.1.1-0.20201006184820-924ee87a1349 // indirect
github.com/filecoin-project/go-amt-ipld/v3 v3.1.0 // indirect
github.com/filecoin-project/go-amt-ipld/v4 v4.0.0 // indirect
github.com/filecoin-project/go-bitfield v0.2.4 // indirect
github.com/filecoin-project/go-hamt-ipld v0.1.5 // indirect
github.com/filecoin-project/go-hamt-ipld/v2 v2.0.0 // indirect
github.com/filecoin-project/go-hamt-ipld/v3 v3.1.0 // indirect
github.com/filecoin-project/specs-actors v0.9.15 // indirect
github.com/filecoin-project/specs-actors/v2 v2.3.6 // indirect
github.com/filecoin-project/specs-actors/v3 v3.1.2 // indirect
github.com/filecoin-project/specs-actors/v4 v4.0.2 // indirect
github.com/filecoin-project/specs-actors/v5 v5.0.6 // indirect
github.com/filecoin-project/specs-actors/v7 v7.0.1 // indirect
github.com/filecoin-project/specs-actors/v8 v8.0.1 // indirect
github.com/filecoin-project/specs-storage v0.4.1 // indirect
Expand Down Expand Up @@ -163,6 +160,7 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/uber/jaeger-client-go v2.25.0+incompatible // indirect
github.com/ugorji/go/codec v1.2.4 // indirect
Expand All @@ -177,6 +175,7 @@ require (
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/api v0.56.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand Down
6 changes: 1 addition & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM7
cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE=
cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk=
cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk=
cloud.google.com/go/firestore v1.6.0/go.mod h1:afJwI0vaXwAG54kI7A//lP/lSPDkQORQuMkv56TxEPU=
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw=
cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA=
Expand Down Expand Up @@ -1760,7 +1759,6 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/crypt v0.1.0/go.mod h1:B/mN0msZuINBtQ1zZLEQcegFJJf9vnYIR88KRMEuODE=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sercand/kuberesolver v2.1.0+incompatible/go.mod h1:lWF3GL0xptCB/vCiJPl/ZshwPsX/n4Y7u0CW9E7aQIQ=
Expand Down Expand Up @@ -1837,9 +1835,8 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.9.0 h1:yR6EXjTp0y0cLN8OZg1CRZmOBdI88UcGkhgyJhu6nZk=
github.com/spf13/viper v1.9.0/go.mod h1:+i6ajR7OX2XaiBkrcZJFK21htRk7eDeLg7+O6bhUPP4=
github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
Expand Down Expand Up @@ -2083,7 +2080,6 @@ golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
Expand Down
162 changes: 162 additions & 0 deletions utils/convert_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package utils

import (
"bytes"
"fmt"
"math"
"reflect"

"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"
market6 "github.com/filecoin-project/specs-actors/v6/actors/builtin/market"
)

var bitFieldTyp = reflect.TypeOf(bitfield.BitField{})

var poStPartitionTyp = reflect.TypeOf([]miner.PoStPartition{})

func TryConvertParams(in interface{}) (interface{}, error) {
switch val := in.(type) {
case *miner.ExtendSectorExpirationParams:
out := make(map[string][]map[string]interface{}, len(val.Extensions))
for _, extension := range val.Extensions {
res, err := convertStructToMap(extension)
if err != nil {
return nil, err
}
out["Extensions"] = append(out["Extensions"], res)
}
return out, nil
case *miner.DeclareFaultsRecoveredParams:
out := make(map[string][]map[string]interface{}, len(val.Recoveries))
for _, recoverie := range val.Recoveries {
res, err := convertStructToMap(recoverie)
if err != nil {
return nil, err
}
out["Recoveries"] = append(out["Recoveries"], res)
}
return out, nil
case *miner.DeclareFaultsParams:
out := make(map[string][]map[string]interface{}, len(val.Faults))
for _, fault := range val.Faults {
res, err := convertStructToMap(fault)
if err != nil {
return nil, err
}
out["Faults"] = append(out["Faults"], res)
}
return out, nil
case *miner5.ProveCommitAggregateParams:
return convertStructToMap(val)
case *miner.TerminateSectorsParams:
out := make(map[string][]map[string]interface{}, len(val.Terminations))
for _, termination := range val.Terminations {
res, err := convertStructToMap(termination)
if err != nil {
return nil, err
}
out["Terminations"] = append(out["Terminations"], res)
}
return out, nil
case *miner.CompactPartitionsParams:
return convertStructToMap(val)
case *miner.CompactSectorNumbersParams:
return convertStructToMap(val)
case *miner.SubmitWindowedPoStParams:
return convertStructToMap(val)
case *market6.PublishStorageDealsReturn:
return convertStructToMap(val)
}

return in, nil
}

func convertStructToMap(in interface{}) (map[string]interface{}, error) {
rt := reflect.TypeOf(in)
rv := reflect.ValueOf(in)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
rv = rv.Elem()
}
m := make(map[string]interface{}, rt.NumField())
for i := 0; i < rt.NumField(); i++ {
if rt.Field(i).Type.AssignableTo(bitFieldTyp) {
res, err := convertBitFieldToString(rv.Field(i).Interface().(bitfield.BitField))
if err != nil {
return nil, err
}
m[rt.Field(i).Name] = res
continue
}
if rt.Field(i).Type.AssignableTo(poStPartitionTyp) {
partitions := rv.Field(i).Interface().([]miner.PoStPartition)
tmp := make([]map[string]interface{}, 0, len(partitions))
for idx := range partitions {
res, err := convertBitFieldToString(partitions[idx].Skipped)
if err != nil {
return nil, err
}
tmp = append(tmp, map[string]interface{}{
"Index": partitions[idx].Index,
"Skipped": res,
})
}
m[rt.Field(i).Name] = tmp
continue
}
m[rt.Field(i).Name] = rv.Field(i).Interface()
}

return m, nil
}

func convertBitFieldToString(val bitfield.BitField) (string, error) {
list, err := val.All(math.MaxInt64)
if err != nil {
return "", err
}

if len(list) == 0 {
return "", nil
}

buf := bytes.Buffer{}
mergeRes := merge(list)
for i := range mergeRes {
if len(mergeRes[i]) == 2 {
buf.WriteString(fmt.Sprintf("%d-%d", mergeRes[i][0], mergeRes[i][1]))
} else {
buf.WriteString(fmt.Sprintf("%d", mergeRes[i][0]))
}
if i < len(mergeRes)-1 {
buf.WriteString(", ")
}
}

return buf.String(), nil
}

func merge(list []uint64) [][]uint64 {
listLen := len(list)
res := make([][]uint64, 0, listLen)

start := 0
for start < listLen {
curr := list[start]
end := start + 1
for end < listLen && curr+1 == list[end] {
curr = list[end]
end++
}
if start+1 < end {
res = append(res, []uint64{list[start], curr})
} else {
res = append(res, []uint64{list[start]})
}
start = end
}

return res
}
Loading

0 comments on commit 4386d21

Please sign in to comment.