From 74f5a757064e2a394f24fc578239c2edae32627c Mon Sep 17 00:00:00 2001 From: hunjixin <1084400399@qq.com> Date: Wed, 4 Jan 2023 17:53:29 +0800 Subject: [PATCH] feat: fix cli --- .gitignore | 3 +- cli/actor_cfg.go | 124 +++++++++++++++++++++++++------- go.mod | 4 +- go.sum | 8 +-- models/mysql/actor_cfg.go | 8 +-- models/mysql/actor_cfg_test.go | 8 +-- models/sqlite/actor_cfg.go | 8 +-- models/sqlite/actor_cfg_test.go | 2 +- service/message_selector.go | 3 +- 9 files changed, 123 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index a70c1750..5aee4de3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,8 @@ *.txt tipset.json test_sqlite_db -./cmds/cfg_example/cfg_example +cmds/cfg_example/cfg_example +cmds/client_example/client_example venus-messager *.log ./venus-messager-tools diff --git a/cli/actor_cfg.go b/cli/actor_cfg.go index c35d7d3f..bf28221e 100644 --- a/cli/actor_cfg.go +++ b/cli/actor_cfg.go @@ -3,9 +3,13 @@ package cli import ( "bytes" "encoding/json" + "errors" "fmt" "os" "sort" + "strconv" + + "github.com/filecoin-project/go-state-types/big" actors2 "github.com/filecoin-project/venus/venus-shared/actors" @@ -23,7 +27,7 @@ import ( ) var ActorCfgCmds = &cli.Command{ - Name: "actor-cfg", + Name: "actor", Usage: "actor config", Subcommands: []*cli.Command{ listActorCfgCmd, @@ -47,6 +51,16 @@ var listActorCfgCmd = &cli.Command{ } defer closer() + nodeAPI, nodeAPICloser, err := getNodeAPI(ctx) + if err != nil { + return err + } + defer nodeAPICloser() + + if err := LoadBuiltinActors(ctx.Context, nodeAPI); err != nil { + return err + } + actorCfgs, err := client.ListActorCfg(ctx.Context) if err != nil { return err @@ -83,68 +97,82 @@ var updateActorCfgCmd = &cli.Command{ } defer closer() + nodeAPI, nodeAPICloser, err := getNodeAPI(ctx) + if err != nil { + return err + } + defer nodeAPICloser() + + if err := LoadBuiltinActors(ctx.Context, nodeAPI); err != nil { + return err + } + + if ctx.NArg() != 1 { + return errors.New("must specify one uid argument") + } uuidStr := ctx.Args().Get(0) id, err := types2.ParseUUID(uuidStr) if err != nil { return err } + hasUpdate := false changeGasSpecParams := &types.ChangeGasSpecParams{} if ctx.IsSet(gasOverEstimationFlag.Name) { + hasUpdate = true gasOverEstimation := ctx.Float64(gasOverEstimationFlag.Name) changeGasSpecParams.GasOverEstimation = &gasOverEstimation } if ctx.IsSet(GasOverPremiumFlag.Name) { + hasUpdate = true gasOverPremium := ctx.Float64(GasOverPremiumFlag.Name) changeGasSpecParams.GasOverPremium = &gasOverPremium } if ctx.IsSet(gasFeeCapFlag.Name) { + hasUpdate = true gasFeeCap, err := types2.BigFromString(ctx.String(gasFeeCapFlag.Name)) if err != nil { return fmt.Errorf("parsing feecap failed %v", err) } - changeGasSpecParams.GasFeeCap = gasFeeCap + changeGasSpecParams.GasFeeCap = big.Int(gasFeeCap) } if ctx.IsSet(maxFeeFlag.Name) { + hasUpdate = true maxfee, err := types2.BigFromString(ctx.String(maxFeeFlag.Name)) if err != nil { return fmt.Errorf("parsing maxfee failed %v", err) } - changeGasSpecParams.MaxFee = maxfee + changeGasSpecParams.MaxFee = big.Int(maxfee) } if ctx.IsSet(basefeeFlag.Name) { + hasUpdate = true basefee, err := types2.BigFromString(ctx.String(basefeeFlag.Name)) if err != nil { return fmt.Errorf("parsing maxfee failed %v", err) } - changeGasSpecParams.BaseFee = basefee + changeGasSpecParams.BaseFee = big.Int(basefee) + } + if !hasUpdate { + return errors.New("no new params to update") } return client.UpdateActorCfg(ctx.Context, id, changeGasSpecParams) }, } var addActorCfgCmd = &cli.Command{ - Name: "add", - Usage: "add new actor config", + Name: "add", + Usage: "add new actor config", + ArgsUsage: " ", Flags: []cli.Flag{ gasOverEstimationFlag, gasFeeCapFlag, maxFeeFlag, basefeeFlag, GasOverPremiumFlag, - - &cli.StringFlag{ - Name: "code", - Required: true, - }, - &cli.Uint64Flag{ - Name: "method", - Required: true, - }, &cli.IntFlag{ Name: "version", }, @@ -156,16 +184,33 @@ var addActorCfgCmd = &cli.Command{ } defer closer() - codeCid, err := cid.Decode(ctx.String("code")) + nodeAPI, nodeAPICloser, err := getNodeAPI(ctx) + if err != nil { + return err + } + defer nodeAPICloser() + + if err := LoadBuiltinActors(ctx.Context, nodeAPI); err != nil { + return err + } + + if ctx.NArg() != 2 { + return errors.New("must specify code and method argument") + } + codeCid, err := cid.Decode(ctx.Args().Get(0)) if err != nil { return err } + method, err := strconv.ParseUint(ctx.Args().Get(1), 10, 64) + if err != nil { + return err + } newActorCfg := &types.ActorCfg{ ID: types2.NewUUID(), MethodType: types.MethodType{ Code: codeCid, - Method: abi.MethodNum(ctx.Uint64("method")), + Method: abi.MethodNum(method), }, FeeSpec: types.FeeSpec{}, } @@ -181,46 +226,56 @@ var addActorCfgCmd = &cli.Command{ newActorCfg.ActorVersion = methodMeta.Version } + hasArgument := false if ctx.IsSet(gasOverEstimationFlag.Name) { + hasArgument = true gasOverEstimation := ctx.Float64(gasOverEstimationFlag.Name) newActorCfg.GasOverEstimation = gasOverEstimation } if ctx.IsSet(GasOverPremiumFlag.Name) { + hasArgument = true gasOverPremium := ctx.Float64(GasOverPremiumFlag.Name) newActorCfg.GasOverPremium = gasOverPremium } if ctx.IsSet(gasFeeCapFlag.Name) { + hasArgument = true gasFeeCap, err := types2.BigFromString(ctx.String(gasFeeCapFlag.Name)) if err != nil { return fmt.Errorf("parsing feecap failed %v", err) } - newActorCfg.GasFeeCap = gasFeeCap + newActorCfg.GasFeeCap = big.Int(gasFeeCap) } if ctx.IsSet(maxFeeFlag.Name) { + hasArgument = true maxfee, err := types2.BigFromString(ctx.String(maxFeeFlag.Name)) if err != nil { return fmt.Errorf("parsing maxfee failed %v", err) } - newActorCfg.MaxFee = maxfee + newActorCfg.MaxFee = big.Int(maxfee) } if ctx.IsSet(basefeeFlag.Name) { + hasArgument = true basefee, err := types2.BigFromString(ctx.String(basefeeFlag.Name)) if err != nil { return fmt.Errorf("parsing maxfee failed %v", err) } - newActorCfg.BaseFee = basefee + newActorCfg.BaseFee = big.Int(basefee) + } + if !hasArgument { + return errors.New("no argument to save") } return client.SaveActorCfg(ctx.Context, newActorCfg) }, } var getActorCfgCmd = &cli.Command{ - Name: "list", - Usage: "list actor config", + Name: "get", + Usage: "get actor config", + ArgsUsage: "", Flags: []cli.Flag{ outputTypeFlag, }, @@ -231,6 +286,19 @@ var getActorCfgCmd = &cli.Command{ } defer closer() + nodeAPI, nodeAPICloser, err := getNodeAPI(ctx) + if err != nil { + return err + } + defer nodeAPICloser() + + if err := LoadBuiltinActors(ctx.Context, nodeAPI); err != nil { + return err + } + + if ctx.NArg() != 1 { + return errors.New("must specific one uid argument") + } uuidStr := ctx.Args().Get(0) id, err := types2.ParseUUID(uuidStr) if err != nil { @@ -263,7 +331,6 @@ var listBuiltinActorCmd = &cli.Command{ Name: "version", }, }, - Action: func(ctx *cli.Context) error { type MethodMeta struct { Name string @@ -272,6 +339,16 @@ var listBuiltinActorCmd = &cli.Command{ Version actors.Version } + nodeAPI, nodeAPICloser, err := getNodeAPI(ctx) + if err != nil { + return err + } + defer nodeAPICloser() + + if err := LoadBuiltinActors(ctx.Context, nodeAPI); err != nil { + return err + } + methodsByActorsVersion := make(map[actors.Version]map[cid.Cid][]MethodMeta) var maxVersion actors.Version for codeCid, methodMap := range utils.MethodsMap { @@ -329,7 +406,6 @@ var listBuiltinActorCmd = &cli.Command{ } actorCfgTw.Write(row) } - } return actorCfgTw.Flush(os.Stdout) }, diff --git a/go.mod b/go.mod index c00cc5f7..ef55b155 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/filecoin-project/go-jsonrpc v0.1.5 github.com/filecoin-project/go-state-types v0.9.8 github.com/filecoin-project/specs-actors/v5 v5.0.6 - github.com/filecoin-project/venus v1.9.0-rc1.0.20230104064901-387e152c1d91 + github.com/filecoin-project/venus v1.9.0-rc1.0.20230105013945-4a030c5cb47e github.com/filecoin-project/venus-auth v1.9.0 github.com/golang/mock v1.6.0 github.com/google/uuid v1.3.0 @@ -233,7 +233,7 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/hraban/lrucache v0.0.0-20201130153820-17052bf09781 // indirect github.com/ipfs/go-block-format v0.0.3 // indirect - github.com/libp2p/go-libp2p-core v0.20.0 // indirect + github.com/libp2p/go-libp2p-core v0.20.1 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index 3754a389..a143bd2b 100644 --- a/go.sum +++ b/go.sum @@ -418,8 +418,8 @@ github.com/filecoin-project/specs-storage v0.4.1/go.mod h1:Z2eK6uMwAOSLjek6+sy0j github.com/filecoin-project/storetheindex v0.3.5/go.mod h1:0r3d0kSpK63O6AvLr1CjAINLi+nWD49clzcnKV+GLpI= github.com/filecoin-project/test-vectors/schema v0.0.5/go.mod h1:iQ9QXLpYWL3m7warwvK1JC/pTri8mnfEmKygNDqqY6E= github.com/filecoin-project/venus v1.2.4/go.mod h1:hJULXHGAnWuq5S5KRtPkwbT8DqgM9II7NwyNU7t59D0= -github.com/filecoin-project/venus v1.9.0-rc1.0.20230104064901-387e152c1d91 h1:4chMUNd3Ty5N9T2hU81oqkoEmcYVkJqISF7EplgHh5U= -github.com/filecoin-project/venus v1.9.0-rc1.0.20230104064901-387e152c1d91/go.mod h1:BJn0TSnkDTX8R3duiCpgPKDYU5ybrPCgJlkxZi/wmTs= +github.com/filecoin-project/venus v1.9.0-rc1.0.20230105013945-4a030c5cb47e h1:oPfZqAEzwmRjLaAUjYXu/hyCnxWrx1Zet01CN+Kge3M= +github.com/filecoin-project/venus v1.9.0-rc1.0.20230105013945-4a030c5cb47e/go.mod h1:BJn0TSnkDTX8R3duiCpgPKDYU5ybrPCgJlkxZi/wmTs= github.com/filecoin-project/venus-auth v1.3.2/go.mod h1:m5Jog2GYxztwP7w3m/iJdv/V1/bTcAVU9rm/CbhxRQU= github.com/filecoin-project/venus-auth v1.9.0 h1:GH0o/jPdF55/U/uLoMzrqR9+DOsMf5oWM/X4UPuyWPA= github.com/filecoin-project/venus-auth v1.9.0/go.mod h1:Ckj8F/iuSgXnCb9LvH0IiPR7swJZQAhabDOxVycLGWs= @@ -1175,8 +1175,8 @@ github.com/libp2p/go-libp2p-core v0.11.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQR github.com/libp2p/go-libp2p-core v0.12.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= github.com/libp2p/go-libp2p-core v0.13.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= github.com/libp2p/go-libp2p-core v0.14.0/go.mod h1:tLasfcVdTXnixsLB0QYaT1syJOhsbrhG7q6pGrHtBg8= -github.com/libp2p/go-libp2p-core v0.20.0 h1:PGKM74+T+O/FaZNARNW32i90RMBHCcgd/hkum2UQ5eY= -github.com/libp2p/go-libp2p-core v0.20.0/go.mod h1:6zR8H7CvQWgYLsbG4on6oLNSGcyKaYFSEYyDt51+bIY= +github.com/libp2p/go-libp2p-core v0.20.1 h1:fQz4BJyIFmSZAiTbKV8qoYhEH5Dtv/cVhZbG3Ib/+Cw= +github.com/libp2p/go-libp2p-core v0.20.1/go.mod h1:6zR8H7CvQWgYLsbG4on6oLNSGcyKaYFSEYyDt51+bIY= github.com/libp2p/go-libp2p-crypto v0.0.1/go.mod h1:yJkNyDmO341d5wwXxDUGO0LykUVT72ImHNUqh5D/dBE= github.com/libp2p/go-libp2p-crypto v0.0.2/go.mod h1:eETI5OUfBnvARGOHrJz2eWNyTUxEGZnBxMcbUjfIj4I= github.com/libp2p/go-libp2p-crypto v0.1.0/go.mod h1:sPUokVISZiy+nNuTTH/TY+leRSxnFj/2GLjtOTW90hI= diff --git a/models/mysql/actor_cfg.go b/models/mysql/actor_cfg.go index 024c2949..b3f9b895 100644 --- a/models/mysql/actor_cfg.go +++ b/models/mysql/actor_cfg.go @@ -21,8 +21,8 @@ import ( type mysqlActorCfg struct { ID shared.UUID `gorm:"column:id;type:varchar(256);primary_key;"` // 主键 ActorVersion int `gorm:"column:actor_v;type:unsigned int;NOT NULL"` - Code mtypes.DBCid `gorm:"column:code;type:varchar(256);index:idx_code_cid_method,unique;NOT NULL;"` - Method uint64 `gorm:"column:method;type:unsigned bigint;index:idx_code_cid_method,unique;NOT NULL"` + Code mtypes.DBCid `gorm:"column:code;type:varchar(256);index:idx_code_method,unique;NOT NULL;"` + Method uint64 `gorm:"column:method;type:unsigned bigint;index:idx_code_method,unique;NOT NULL"` FeeSpec @@ -88,7 +88,7 @@ func (s *mysqlActorCfgRepo) SaveActorCfg(ctx context.Context, actorCfg *types.Ac func (s *mysqlActorCfgRepo) GetActorCfgByMethodType(ctx context.Context, methodType *types.MethodType) (*types.ActorCfg, error) { var a mysqlActorCfg - if err := s.DB.Take(&a, "code_cid = ? and method = ?", methodType.Code.String(), methodType.Method).Error; err != nil { + if err := s.DB.Take(&a, "code = ? and method = ?", methodType.Code.String(), methodType.Method).Error; err != nil { return nil, err } @@ -119,7 +119,7 @@ func (s *mysqlActorCfgRepo) ListActorCfg(ctx context.Context) ([]*types.ActorCfg } func (s *mysqlActorCfgRepo) DelActorCfgByMethodType(ctx context.Context, methodType *types.MethodType) error { - return s.DB.Delete(mysqlActorCfg{}, "code_cid = ? and method = ?", methodType.Code.String(), methodType.Method).Error + return s.DB.Delete(mysqlActorCfg{}, "code = ? and method = ?", methodType.Code.String(), methodType.Method).Error } func (s *mysqlActorCfgRepo) DelActorCfgById(ctx context.Context, id shared.UUID) error { diff --git a/models/mysql/actor_cfg_test.go b/models/mysql/actor_cfg_test.go index fed9fe56..765921c0 100644 --- a/models/mysql/actor_cfg_test.go +++ b/models/mysql/actor_cfg_test.go @@ -84,7 +84,7 @@ func testGetActorTypeByMethodType(t *testing.T, r repo.Repo, mock sqlmock.Sqlmoc var actorCfg types.ActorCfg testutil.Provide(t, &actorCfg) - mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `actor_cfg` WHERE code_cid = ? and method = ? LIMIT 1")). + mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `actor_cfg` WHERE code = ? and method = ? LIMIT 1")). WithArgs(mtypes.NewDBCid(actorCfg.Code), actorCfg.Method). WillReturnRows(sqlmock.NewRows([]string{"id"})) @@ -94,7 +94,7 @@ func testGetActorTypeByMethodType(t *testing.T, r repo.Repo, mock sqlmock.Sqlmoc }) assert.Equal(t, repo.ErrRecordNotFound, err) - mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `actor_cfg` WHERE code_cid = ? and method = ? LIMIT 1")). + mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `actor_cfg` WHERE code = ? and method = ? LIMIT 1")). WithArgs(mtypes.NewDBCid(actorCfg.Code), actorCfg.Method). WillReturnRows(genSelectResult(fromActorCfg(&actorCfg))) @@ -145,7 +145,7 @@ func testDeleteActorCfgByMethodType(t *testing.T, r repo.Repo, mock sqlmock.Sqlm testutil.Provide(t, &actorCfg) mock.ExpectBegin() - mock.ExpectExec(regexp.QuoteMeta("DELETE FROM `actor_cfg` WHERE code_cid = ? and method = ?")). + mock.ExpectExec(regexp.QuoteMeta("DELETE FROM `actor_cfg` WHERE code = ? and method = ?")). WithArgs(mtypes.NewDBCid(actorCfg.Code), actorCfg.Method). WillReturnResult(driverResult{0, 1}) mock.ExpectCommit() @@ -195,7 +195,7 @@ func testUpdateSelectSpec(t *testing.T, r repo.Repo, mock sqlmock.Sqlmock) { //only update select num mock.ExpectBegin() - mock.ExpectExec(regexp.QuoteMeta("UPDATE `actor_cfg` SET `gasOverPremium`=?,`updated_at`=? WHERE id = ?")). + mock.ExpectExec(regexp.QuoteMeta("UPDATE `actor_cfg` SET `gas_over_premium`=?,`updated_at`=? WHERE id = ?")). WithArgs(actorCfg.GasOverPremium, anyTime{}, actorCfg.ID). WillReturnResult(driverResult{0, 1}) mock.ExpectCommit() diff --git a/models/sqlite/actor_cfg.go b/models/sqlite/actor_cfg.go index e2ed4757..7f105b81 100644 --- a/models/sqlite/actor_cfg.go +++ b/models/sqlite/actor_cfg.go @@ -21,8 +21,8 @@ import ( type sqliteActorCfg struct { ID shared.UUID `gorm:"column:id;type:varchar(256);primary_key;"` // 主键 ActorVersion int `gorm:"column:actor_v;type:INTEGER;NOT NULL"` - Code mtypes.DBCid `gorm:"column:code;type:varchar(256);index:idx_code_cid_method,unique;NOT NULL"` - Method sqliteUint64 `gorm:"column:method;type:INTEGER;index:idx_code_cid_method,unique;NOT NULL"` + Code mtypes.DBCid `gorm:"column:code;type:varchar(256);index:idx_code_method,unique;NOT NULL"` + Method sqliteUint64 `gorm:"column:method;type:INTEGER;index:idx_code_method,unique;NOT NULL"` FeeSpec @@ -88,7 +88,7 @@ func (s *sqliteActorCfgRepo) SaveActorCfg(ctx context.Context, actorCfg *types.A func (s *sqliteActorCfgRepo) GetActorCfgByMethodType(ctx context.Context, methodType *types.MethodType) (*types.ActorCfg, error) { var a sqliteActorCfg - if err := s.DB.Take(&a, "code_cid = ? and method = ?", methodType.Code.String(), sqliteUint64(methodType.Method)).Error; err != nil { + if err := s.DB.Take(&a, "code = ? and method = ?", methodType.Code.String(), sqliteUint64(methodType.Method)).Error; err != nil { return nil, err } @@ -119,7 +119,7 @@ func (s *sqliteActorCfgRepo) ListActorCfg(ctx context.Context) ([]*types.ActorCf } func (s *sqliteActorCfgRepo) DelActorCfgByMethodType(ctx context.Context, methodType *types.MethodType) error { - return s.DB.Delete(sqliteActorCfg{}, "code_cid = ? and method = ?", methodType.Code.String(), sqliteUint64(methodType.Method)).Error + return s.DB.Delete(sqliteActorCfg{}, "code = ? and method = ?", methodType.Code.String(), sqliteUint64(methodType.Method)).Error } func (s *sqliteActorCfgRepo) DelActorCfgById(ctx context.Context, id shared.UUID) error { diff --git a/models/sqlite/actor_cfg_test.go b/models/sqlite/actor_cfg_test.go index 81d0798b..f5fec9ae 100644 --- a/models/sqlite/actor_cfg_test.go +++ b/models/sqlite/actor_cfg_test.go @@ -26,7 +26,7 @@ func Test_fromActorCfg(t *testing.T) { actorCfgCp := actorCfg actorCfgCp.ID = shared.NewUUID() err := actorCfgRepo.SaveActorCfg(ctx, &actorCfgCp) - assert.EqualError(t, err, "UNIQUE constraint failed: actor_cfg.code_cid, actor_cfg.method") + assert.EqualError(t, err, "UNIQUE constraint failed: actor_cfg.code, actor_cfg.method") actorCfg2, err := actorCfgRepo.GetActorCfgByID(ctx, actorCfg.ID) assert.NoError(t, err) diff --git a/service/message_selector.go b/service/message_selector.go index 565c2088..f959e80f 100644 --- a/service/message_selector.go +++ b/service/message_selector.go @@ -590,7 +590,7 @@ func (w *work) saveSelectedMessages(ctx context.Context, selectResult *MsgSelect } func (w *work) getActorCfg(ctx context.Context, msg *types.Message, nv network.Version) (*types.ActorCfg, error) { - key := msg.To.String() + strconv.Itoa(int(nv)) + key := msg.To.String() + "-" + strconv.Itoa(int(nv)) var actor *venusTypes.Actor actorI, has := w.actorCache.Get(key) if has { @@ -604,6 +604,7 @@ func (w *work) getActorCfg(ctx context.Context, msg *types.Message, nv network.V w.actorCache.Add(key, actor) } + fmt.Println(actor.Code) actorCfg, err := w.repo.ActorCfgRepo().GetActorCfgByMethodType(ctx, &types.MethodType{ Code: actor.Code, Method: msg.Method,