Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/tanlang/add sign recorder #123

Merged
merged 12 commits into from
Feb 28, 2023
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ coverage.*
.vscode
dockerfile
*_bin
*test.db
storage/sqlite/test.db
hunjixin marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 3 additions & 1 deletion build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs-force-community/venus-gateway/types"
"go.uber.org/fx"
"gorm.io/gorm"

wallet_api "github.com/filecoin-project/venus/venus-shared/api/wallet"
)
Expand Down Expand Up @@ -62,10 +63,11 @@ func WalletOpt(repo filemgr.Repo, walletPwd string) Option {
Override(new(filemgr.Repo), repo),
Override(new(*config.DBConfig), c.DB),
Override(new(EventBus.Bus), EventBus.New),
Override(new(*sqlite.Conn), sqlite.NewSQLiteConn),
Override(new(*gorm.DB), sqlite.NewDB),
Override(new(*config.CryptoFactor), c.Factor),
Override(new(storage.KeyMiddleware), storage.NewKeyMiddleware),
Override(new(storage.KeyStore), sqlite.NewKeyStore),
Override(new(storage.IRecorder), sqlite.NewSqliteRecorder),
Override(new(wallet.GetPwdFunc), func() wallet.GetPwdFunc {
return func() string {
return walletPwd
Expand Down
1 change: 1 addition & 0 deletions cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ var Commands = []*cli.Command{
walletLock,
walletLockState,
supportCmds,
recordCmd,
}
6 changes: 6 additions & 0 deletions cli/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package helper
import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"text/tabwriter"
"time"

jsonrpc "github.com/filecoin-project/go-jsonrpc"
Expand Down Expand Up @@ -196,3 +198,7 @@ func (e *PrintHelpErr) Is(o error) bool {
_, ok := o.(*PrintHelpErr)
return ok
}

func NewTabWriter(w io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)
}
243 changes: 243 additions & 0 deletions cli/record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
package cli

import (
"encoding/hex"
"fmt"
"reflect"
"time"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-fil-markets/storagemarket/network"
"github.com/filecoin-project/go-state-types/builtin/v8/market"
"github.com/filecoin-project/go-state-types/builtin/v8/paych"
"github.com/filecoin-project/venus-wallet/cli/helper"
"github.com/filecoin-project/venus-wallet/storage/wallet"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/urfave/cli/v2"

types2 "github.com/filecoin-project/venus/venus-shared/types/wallet"
)

var recordCmd = &cli.Command{
Name: "record",
Usage: "manipulate sign record",
Subcommands: []*cli.Command{
recordQuery,
},
}

var recordQuery = &cli.Command{
hunjixin marked this conversation as resolved.
Show resolved Hide resolved
Name: "query",
Usage: "query sign record",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Usage: "address to query",
},
&cli.StringFlag{
Name: "type",
Usage: "sign type to query",
},
&cli.TimestampFlag{
Name: "from",
Aliases: []string{"after", "f"},
Usage: "from time to query",
Timezone: time.Local,
Layout: "2006-1-2-15:04:05",
},
&cli.TimestampFlag{
Name: "to",
Aliases: []string{"before"},
Timezone: time.Local,
Usage: "to time to query",
Layout: "2006-1-2-15:04:05",
},
&cli.IntFlag{
Name: "limit",
Usage: "limit to query",
},
&cli.IntFlag{
Name: "offset",
Aliases: []string{"skip"},
Usage: "offset to query",
},
&cli.BoolFlag{
Name: "error",
Usage: "query error record",
},
&cli.StringFlag{
Name: "id",
Usage: "query record by id",
},
&cli.BoolFlag{
Name: "verbose",
Usage: "verbose output",
Aliases: []string{"v"},
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := helper.GetAPI(cctx)
if err != nil {
return err
}
defer closer()

ctx := helper.ReqContext(cctx)

QueryParams := types.QuerySignRecordParams{}

if cctx.IsSet("address") {
addrStr := cctx.String("address")
addr, err := address.NewFromString(addrStr)
if err != nil {
return fmt.Errorf("parse address %s : %w", addrStr, err)
}
QueryParams.Signer = addr
}

if cctx.IsSet("type") {
t := types.MsgType(cctx.String("type"))
_, ok := wallet.SupportedMsgTypes[t]
if !ok {

fmt.Println("supported types:")
for k := range wallet.SupportedMsgTypes {
fmt.Println(k)
}
return fmt.Errorf("unsupported type %s", t)
}
QueryParams.Type = t
}
if cctx.IsSet("from") {
from := cctx.Timestamp("from")
QueryParams.After = *from
}
if cctx.IsSet("to") {
to := cctx.Timestamp("to")
QueryParams.Before = *to
}
if cctx.IsSet("limit") {
limit := cctx.Int("limit")
QueryParams.Limit = limit
}
if cctx.IsSet("offset") {
offset := cctx.Int("offset")
QueryParams.Skip = offset
}
if cctx.IsSet("error") {
QueryParams.IsError = cctx.Bool("error")
}
if cctx.IsSet("id") {
QueryParams.ID = cctx.String("id")
}

records, err := api.QuerySignRecord(ctx, &QueryParams)
if err != nil {
return fmt.Errorf("query sign record: %w", err)
}
// output in table format
w := helper.NewTabWriter(cctx.App.Writer)
if cctx.Bool("verbose") {
fmt.Fprintln(w, "ID\tSIGNER\tTYPE\tTIME\tDETAIL\tERROR")
for _, r := range records {
errStr := "no error"
if r.Err != nil {
errStr = r.Err.Error()
}
detail, err := getDetail(&r)
hunjixin marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("get detail: %w", err)
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n", r.ID, r.Signer, r.Type, r.CreateAt, detail, errStr)
}
} else {
fmt.Fprintln(w, "SIGNER\tTYPE\tTIME\tERROR")
for _, r := range records {
errStr := "no error"
if r.Err != nil {
errStr = r.Err.Error()
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", r.Signer, r.Type, r.CreateAt, errStr)
}
}
w.Flush()

return nil
},
}

func getDetail(r *types.SignRecord) (string, error) {
var ret string
t, ok := wallet.SupportedMsgTypes[r.Type]
if !ok {
return "", fmt.Errorf("unsupported type %s", r.Type)
}

wrap := func(err error) error {
return fmt.Errorf("get detail: %w", err)
}

if r.Msg == nil {
return "", wrap(fmt.Errorf("msg is nil"))
}

if r.Type == types.MTVerifyAddress || r.Type == types.MTUnknown {
// encode into hex string
hs := hex.EncodeToString(r.Msg)
return fmt.Sprintf("Hex:%s.", hs), nil
}

signObj := reflect.New(t.Type).Interface()
if err := wallet.CborDecodeInto(r.Msg, signObj); err != nil {
return "", fmt.Errorf("decode msg:%w", err)
}
switch r.Type {
case types.MTDealProposal:
deal := signObj.(*market.DealProposal)
cid, err := deal.Cid()
if err != nil {
return "", wrap(err)
}
ret = fmt.Sprintf("DealProposal:%s; Client:%s; Provider:%s.", cid.String(), deal.Client.String(), deal.Provider.String())
case types.MTClientDeal:
deal := signObj.(*market.ClientDealProposal)
cid, err := deal.Proposal.Cid()
if err != nil {
return "", wrap(err)
}
ret = fmt.Sprintf("ClientDeal:%s; Client:%s; Provider:%s.", cid.String(), deal.Proposal.Client.String(), deal.Proposal.Provider.String())
case types.MTDrawRandomParam:
param := signObj.(*types2.DrawRandomParams)
ret = fmt.Sprintf("Pers:%d ; Round:%s; Entropy :%s.", param.Pers, param.Round, param.Entropy)
case types.MTSignedVoucher:
voucher := signObj.(*paych.SignedVoucher)
ret = fmt.Sprintf("Channel:%s; Amount:%s; Lane:%d .", voucher.ChannelAddr.String(), voucher.Amount.String(), voucher.Lane)
case types.MTStorageAsk:
ask := signObj.(*storagemarket.StorageAsk)
ret = fmt.Sprintf("Miner:%s; Price:%s; VerifiedPrice:%s.", ask.Miner.String(), ask.Price.String(), ask.VerifiedPrice.String())
case types.MTAskResponse:
resp := signObj.(*network.AskResponse)
ret = fmt.Sprintf("Miner:%s; Price:%s; VerifiedPrice:%s.", resp.Ask.Ask.Miner.String(), resp.Ask.Ask.Price.String(), resp.Ask.Ask.VerifiedPrice.String())
return ret, nil
case types.MTNetWorkResponse:
resp := signObj.(*network.Response)
if resp.State != storagemarket.StorageDealUnknown {
resp := signObj.(network.Response)
ret = fmt.Sprintf("State:%s; ProposalCid:%s.", storagemarket.DealStates[resp.State], resp.Proposal)
}
case types.MTBlock:
block := signObj.(*types.BlockHeader)
ret = fmt.Sprintf("Height:%d ; Miner:%s.", block.Height, block.Miner.String())
case types.MTChainMsg:
msg := signObj.(*types.Message)
ret = fmt.Sprintf("To:%s; Value:%s; Method:%s.", msg.To.String(), msg.Value.String(), msg.Method)
case types.MTProviderDealState:
deal := signObj.(*storagemarket.ProviderDealState)
ret = fmt.Sprintf("ProposalCid:%s; State:%s.", deal.ProposalCid.String(), storagemarket.DealStates[deal.State])
default:
ret = "unknown message type"
}

return ret, nil
}
7 changes: 7 additions & 0 deletions common/api_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/gbrlsnchs/jwt/v3"
logging "github.com/ipfs/go-log/v2"
"go.uber.org/fx"

"github.com/filecoin-project/venus-wallet/storage"
)

type ICommon = api.ICommon
Expand All @@ -20,6 +22,7 @@ var _ ICommon = &Common{}
type Common struct {
fx.In
APISecret *jwt.HMACSHA
Recorder storage.IRecorder
}

type jwtPayload struct {
Expand Down Expand Up @@ -55,3 +58,7 @@ func (a *Common) LogList(context.Context) ([]string, error) {
func (a *Common) LogSetLevel(ctx context.Context, subsystem, level string) error {
return logging.SetLogLevel(subsystem, level)
}

func (a *Common) QuerySignRecord(ctx context.Context, param *types.QuerySignRecordParams) ([]types.SignRecord, error) {
hunjixin marked this conversation as resolved.
Show resolved Hide resolved
return a.Recorder.QueryRecord(param)
}
2 changes: 1 addition & 1 deletion filemgr/config_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (fsr *FsRepo) defConfig() *config.Config {
ListenAddress: "/ip4/127.0.0.1/tcp/5678/http",
},
DB: &config.DBConfig{
Conn: filepath.Join(fsr.path, skKeyStore),
Conn: filepath.Join(fsr.path, dbName),
Type: "sqlite",
DebugMode: true,
},
Expand Down
4 changes: 2 additions & 2 deletions filemgr/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package filemgr
type systemKeyword = string

const (
skConfig systemKeyword = "config.toml"
skKeyStore systemKeyword = "keystore.sqlit"
skConfig systemKeyword = "config.toml"
dbName systemKeyword = "keystore.sqlit"
)
8 changes: 4 additions & 4 deletions filemgr/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func (fsr *FsRepo) init() error {
}

func (fsr *FsRepo) exists() (bool, error) {
_, err := os.Stat(filepath.Join(fsr.path, skKeyStore))
notexist := os.IsNotExist(err)
if notexist {
_, err := os.Stat(filepath.Join(fsr.path, dbName))
notExist := os.IsNotExist(err)
if notExist {
err = nil
}
return !notexist, err
return !notExist, err
}

func (fsr *FsRepo) Config() *config.Config {
Expand Down
Loading