From b08ac8f8a0ee9b713a521f6f7d789ed2533c100c Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Wed, 3 Aug 2022 16:17:04 +0800 Subject: [PATCH] fix: remove dependency on venus-common-utils --- cli/cmd.go | 15 +------- cmds/client_example/main.go | 8 +---- gateway/gateway_client.go | 63 ++++++++++----------------------- go.mod | 1 - go.sum | 1 - main.go | 4 +-- service/address_service.go | 6 ++-- service/message_selector.go | 4 +-- service/message_service.go | 4 +-- tools/internal/batch_replace.go | 3 +- 10 files changed, 30 insertions(+), 79 deletions(-) diff --git a/cli/cmd.go b/cli/cmd.go index 11a780f9..7d98a327 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -8,7 +8,6 @@ import ( "github.com/filecoin-project/venus-messager/filestore" v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1" "github.com/filecoin-project/venus/venus-shared/utils" - "github.com/ipfs-force-community/venus-common-utils/apiinfo" "github.com/mitchellh/go-homedir" "github.com/urfave/cli/v2" @@ -24,19 +23,7 @@ func getAPI(ctx *cli.Context) (messager.IMessager, jsonrpc.ClientCloser, error) return nil, func() {}, err } - return NewMessagerAPI(ctx.Context, cfg.API.Address, cfg.JWT.Local.Token) -} - -func NewMessagerAPI(ctx context.Context, addr, token string) (messager.IMessager, jsonrpc.ClientCloser, error) { - apiInfo := apiinfo.NewAPIInfo(addr, token) - addr, err := apiInfo.DialArgs("v0") - if err != nil { - return nil, nil, err - } - - client, closer, err := messager.NewIMessagerRPC(ctx, addr, apiInfo.AuthHeader()) - - return client, closer, err + return messager.DialIMessagerRPC(ctx.Context, cfg.API.Address, cfg.JWT.Local.Token, nil) } func getNodeAPI(ctx *cli.Context) (v1.FullNode, jsonrpc.ClientCloser, error) { diff --git a/cmds/client_example/main.go b/cmds/client_example/main.go index 6e7f3bbb..e159016b 100644 --- a/cmds/client_example/main.go +++ b/cmds/client_example/main.go @@ -11,7 +11,6 @@ import ( "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" venustypes "github.com/filecoin-project/venus/venus-shared/types" - "github.com/ipfs-force-community/venus-common-utils/apiinfo" "github.com/filecoin-project/venus-messager/config" "github.com/filecoin-project/venus-messager/utils" @@ -81,12 +80,7 @@ func main() { fmt.Println("count : ", count) fmt.Println("value : ", value) - apiInfo := apiinfo.NewAPIInfo(apiAddress, token) - addr, err := apiInfo.DialArgs("v0") - if err != nil { - log.Fatal(err) - } - client, closer, err := messager.NewIMessagerRPC(context.Background(), addr, apiInfo.AuthHeader()) + client, closer, err := messager.DialIMessagerRPC(context.Background(), apiAddress, token, nil) if err != nil { log.Fatal(err) return diff --git a/gateway/gateway_client.go b/gateway/gateway_client.go index 7f519d8b..28130404 100644 --- a/gateway/gateway_client.go +++ b/gateway/gateway_client.go @@ -8,36 +8,20 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-jsonrpc" "github.com/filecoin-project/go-state-types/crypto" + "github.com/filecoin-project/venus/venus-shared/api/gateway/v1" venusTypes "github.com/filecoin-project/venus/venus-shared/types" - "github.com/ipfs-force-community/venus-common-utils/apiinfo" + gtypes "github.com/filecoin-project/venus/venus-shared/types/gateway" "github.com/filecoin-project/venus-messager/config" "github.com/filecoin-project/venus-messager/log" ) -type IWalletClient interface { - WalletHas(ctx context.Context, supportAccount string, addr address.Address) (bool, error) - WalletSign(ctx context.Context, account string, addr address.Address, toSign []byte, meta venusTypes.MsgMeta) (*crypto.Signature, error) -} - -// IWalletCli *api.MessageImp and *gateway.WalletClient both implement IWalletClient, so injection will fail -type IWalletCli struct { - IWalletClient -} - -type WalletClient struct { - Internal struct { - WalletHas func(ctx context.Context, supportAccount string, addr address.Address) (bool, error) - WalletSign func(ctx context.Context, account string, addr address.Address, toSign []byte, meta venusTypes.MsgMeta) (*crypto.Signature, error) - } -} - type WalletProxy struct { - clients map[string]*WalletClient + clients map[string]gateway.IWalletClient logger *log.Logger mutx sync.RWMutex - avaliabeClientCache map[cacheKey]*WalletClient + avaliabeClientCache map[cacheKey]gateway.IWalletClient } type cacheKey string @@ -46,7 +30,7 @@ func newCacheKey(account string, addr address.Address) cacheKey { return cacheKey("walletClientCache:" + account + addr.String()) } -func (w *WalletProxy) putCache(account string, addr address.Address, client *WalletClient) { +func (w *WalletProxy) putCache(account string, addr address.Address, client gateway.IWalletClient) { w.mutx.Lock() defer w.mutx.Unlock() w.avaliabeClientCache[newCacheKey(account, addr)] = client @@ -63,7 +47,7 @@ func (w *WalletProxy) delCache(account string, addr address.Address) bool { return exist } -func (w *WalletProxy) getCachedClient(account string, addr address.Address) *WalletClient { +func (w *WalletProxy) getCachedClient(account string, addr address.Address) gateway.IWalletClient { key := newCacheKey(account, addr) w.mutx.RLock() defer w.mutx.RUnlock() @@ -72,12 +56,12 @@ func (w *WalletProxy) getCachedClient(account string, addr address.Address) *Wal // todo: think about 'fastSelectAvaClient' was called parallelly, // input the same params('account', 'address') -func (w *WalletProxy) fastSelectAvaClient(ctx context.Context, account string, addr address.Address) (*WalletClient, error) { +func (w *WalletProxy) fastSelectAvaClient(ctx context.Context, account string, addr address.Address) (gateway.IWalletClient, error) { var g = &sync.WaitGroup{} - var ch = make(chan *WalletClient, 1) + var ch = make(chan gateway.IWalletClient, 1) for url, c := range w.clients { g.Add(1) - go func(url string, c *WalletClient) { + go func(url string, c gateway.IWalletClient) { has, err := c.WalletHas(ctx, account, addr) if err != nil { w.logger.Errorf("fastSelectAvaClient, call %s:'WalletHas' failed:%s", url, err) @@ -147,25 +131,25 @@ func (w *WalletProxy) WalletSign(ctx context.Context, account string, return s, err } -func (w *WalletClient) WalletHas(ctx context.Context, supportAccount string, addr address.Address) (bool, error) { - return w.Internal.WalletHas(ctx, supportAccount, addr) +func (w *WalletProxy) ListWalletInfo(ctx context.Context) ([]*gtypes.WalletDetail, error) { + panic("implement me") } -func (w *WalletClient) WalletSign(ctx context.Context, account string, addr address.Address, toSign []byte, meta venusTypes.MsgMeta) (*crypto.Signature, error) { - return w.Internal.WalletSign(ctx, account, addr, toSign, meta) +func (w *WalletProxy) ListWalletInfoByWallet(ctx context.Context, account string) (*gtypes.WalletDetail, error) { + panic("implement me") } -func NewWalletClient(cfg *config.GatewayConfig, logger *log.Logger) (*WalletProxy, jsonrpc.ClientCloser, error) { +func NewWalletClient(cfg *config.GatewayConfig, logger *log.Logger) (gateway.IWalletClient, jsonrpc.ClientCloser, error) { var proxy = &WalletProxy{ - clients: make(map[string]*WalletClient), - avaliabeClientCache: make(map[cacheKey]*WalletClient), + clients: make(map[string]gateway.IWalletClient), + avaliabeClientCache: make(map[cacheKey]gateway.IWalletClient), logger: logger} var ctx = context.Background() var closers []jsonrpc.ClientCloser for _, url := range cfg.Url { - c, cls, err := newWalletClient(ctx, cfg.Token, url) + c, cls, err := gateway.DialIGatewayRPC(ctx, url, cfg.Token, nil) if err != nil { return nil, nil, fmt.Errorf("create geteway client with url:%s failed:%w", url, err) @@ -188,15 +172,4 @@ func NewWalletClient(cfg *config.GatewayConfig, logger *log.Logger) (*WalletProx return proxy, totalCloser, nil } -func newWalletClient(ctx context.Context, token, url string) (*WalletClient, jsonrpc.ClientCloser, error) { - apiInfo := apiinfo.NewAPIInfo(url, token) - addr, err := apiInfo.DialArgs("v0") - if err != nil { - return nil, nil, err - } - - walletClient := WalletClient{} - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Gateway", []interface{}{&walletClient.Internal}, apiInfo.AuthHeader()) - - return &walletClient, closer, err -} +var _ gateway.IWalletClient = &WalletProxy{} diff --git a/go.mod b/go.mod index fd78a8ae..f84a0b3f 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,6 @@ require ( github.com/google/uuid v1.3.0 github.com/hunjixin/automapper v0.0.0-20191127090318-9b979ce72ce2 github.com/ipfs-force-community/metrics v1.0.1-0.20220719063006-2c54bb379466 - github.com/ipfs-force-community/venus-common-utils v0.0.0-20210924063144-1d3a5b30de87 github.com/ipfs-force-community/venus-gateway v1.6.0 github.com/ipfs/go-cid v0.1.0 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 5dd3d367..39428631 100644 --- a/go.sum +++ b/go.sum @@ -702,7 +702,6 @@ github.com/ipfs-force-community/metrics v1.0.1-0.20211022060227-11142a08b729/go. github.com/ipfs-force-community/metrics v1.0.1-0.20211228055608-9462dc86e157/go.mod h1:mn40SioMuKtjmRumHFy/fJ26Pn028XuDjUJE9dorjyw= github.com/ipfs-force-community/metrics v1.0.1-0.20220719063006-2c54bb379466 h1:oPQtpBVmYot48fGuUwqVuzD1ilMnHUXkkGSWy5VzUL8= github.com/ipfs-force-community/metrics v1.0.1-0.20220719063006-2c54bb379466/go.mod h1:MOGtsAHKkSXi/ce9Ggci7nXq5jShePzwPDvdBSGDETk= -github.com/ipfs-force-community/venus-common-utils v0.0.0-20210924063144-1d3a5b30de87 h1:1PTFrWfxN5b8y1Cnw403PZ9Gy5RLHN4FXmUM4nTt9Yg= github.com/ipfs-force-community/venus-common-utils v0.0.0-20210924063144-1d3a5b30de87/go.mod h1:RTVEOzM+hkpqmcEWpyLDkx1oGO5r9ZWCgYxG/CsXzJQ= github.com/ipfs-force-community/venus-gateway v1.6.0 h1:uFx01GI78ZhpfgIBXsoa0hK1p0zuARuK69weQ9ZHkZU= github.com/ipfs-force-community/venus-gateway v1.6.0/go.mod h1:BvtUgOPZZSdVKzDmyABPY9D6CjavWnK9iYs+QjTG3LE= diff --git a/main.go b/main.go index b4b3f501..f64ff9a7 100644 --- a/main.go +++ b/main.go @@ -174,9 +174,7 @@ func runAction(ctx *cli.Context) error { return err } - var walletClient *gateway.IWalletCli walletCli, walletCliCloser, err := gateway.NewWalletClient(&cfg.Gateway, log) - walletClient = &gateway.IWalletCli{IWalletClient: walletCli} if err != nil { return err } @@ -197,7 +195,7 @@ func runAction(ctx *cli.Context) error { &cfg.MessageState, &cfg.Gateway, &cfg.RateLimit, cfg.Trace, cfg.Metrics), fx.Supply(log), fx.Supply(client), - fx.Supply(walletClient), + fx.Supply(walletCli), fx.Provide(func() v1.FullNode { return client }), diff --git a/service/address_service.go b/service/address_service.go index e681a701..c8d2e0bc 100644 --- a/service/address_service.go +++ b/service/address_service.go @@ -8,11 +8,11 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/big" - "github.com/filecoin-project/venus-messager/gateway" "github.com/filecoin-project/venus-messager/log" "github.com/filecoin-project/venus-messager/models/repo" v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1" + "github.com/filecoin-project/venus/venus-shared/api/gateway/v1" venusTypes "github.com/filecoin-project/venus/venus-shared/types" types "github.com/filecoin-project/venus/venus-shared/types/messager" ) @@ -25,13 +25,13 @@ type AddressService struct { sps *SharedParamsService nodeClient v1.FullNode - walletClient *gateway.IWalletCli + walletClient gateway.IWalletClient } func NewAddressService(repo repo.Repo, logger *log.Logger, sps *SharedParamsService, - walletClient *gateway.IWalletCli, + walletClient gateway.IWalletClient, nodeClient v1.FullNode) *AddressService { addressService := &AddressService{ repo: repo, diff --git a/service/message_selector.go b/service/message_selector.go index 81686dab..731ac19d 100644 --- a/service/message_selector.go +++ b/service/message_selector.go @@ -12,11 +12,11 @@ import ( "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/venus/pkg/crypto" v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1" + "github.com/filecoin-project/venus/venus-shared/api/gateway/v1" venusTypes "github.com/filecoin-project/venus/venus-shared/types" "modernc.org/mathutil" "github.com/filecoin-project/venus-messager/config" - "github.com/filecoin-project/venus-messager/gateway" "github.com/filecoin-project/venus-messager/log" "github.com/filecoin-project/venus-messager/models/repo" "github.com/filecoin-project/venus-messager/utils" @@ -57,7 +57,7 @@ func NewMessageSelector(repo repo.Repo, nodeClient v1.FullNode, addressService *AddressService, sps *SharedParamsService, - walletClient *gateway.IWalletCli) *MessageSelector { + walletClient gateway.IWalletClient) *MessageSelector { return &MessageSelector{repo: repo, log: logger, cfg: cfg, diff --git a/service/message_service.go b/service/message_service.go index 8e19ff7c..12bc9438 100644 --- a/service/message_service.go +++ b/service/message_service.go @@ -20,13 +20,13 @@ import ( "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/venus-messager/filestore" - "github.com/filecoin-project/venus-messager/gateway" "github.com/filecoin-project/venus-messager/log" "github.com/filecoin-project/venus-messager/metrics" "github.com/filecoin-project/venus-messager/models/repo" "github.com/filecoin-project/venus/pkg/constants" v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1" + "github.com/filecoin-project/venus/venus-shared/api/gateway/v1" venusTypes "github.com/filecoin-project/venus/venus-shared/types" types "github.com/filecoin-project/venus/venus-shared/types/messager" ) @@ -84,7 +84,7 @@ func NewMessageService(repo repo.Repo, addressService *AddressService, sps *SharedParamsService, nodeService *NodeService, - walletClient *gateway.IWalletCli) (*MessageService, error) { + walletClient gateway.IWalletClient) (*MessageService, error) { selector := NewMessageSelector(repo, logger, &fsRepo.Config().MessageService, nc, addressService, sps, walletClient) ms := &MessageService{ repo: repo, diff --git a/tools/internal/batch_replace.go b/tools/internal/batch_replace.go index 11e72dfb..fe668feb 100644 --- a/tools/internal/batch_replace.go +++ b/tools/internal/batch_replace.go @@ -10,6 +10,7 @@ import ( cli2 "github.com/filecoin-project/venus-messager/cli" "github.com/filecoin-project/venus-messager/tools/config" "github.com/filecoin-project/venus-messager/utils" + mapi "github.com/filecoin-project/venus/venus-shared/api/messager" "github.com/filecoin-project/venus/venus-shared/types" "github.com/filecoin-project/venus/venus-shared/types/messager" "github.com/urfave/cli/v2" @@ -49,7 +50,7 @@ var BatchReplaceCmd = &cli.Command{ return fmt.Errorf("read config failed: %v", err) } - messagerAPI, closer, err := cli2.NewMessagerAPI(ctx, cfg.Messager.URL, cfg.Messager.Token) + messagerAPI, closer, err := mapi.DialIMessagerRPC(ctx, cfg.Messager.URL, cfg.Messager.Token, nil) if err != nil { return err }