Skip to content

Commit

Permalink
fix: remove dependency on venus-common-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Aug 3, 2022
1 parent 38f382d commit b08ac8f
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 79 deletions.
15 changes: 1 addition & 14 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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) {
Expand Down
8 changes: 1 addition & 7 deletions cmds/client_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
63 changes: 18 additions & 45 deletions gateway/gateway_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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{}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}),
Expand Down
6 changes: 3 additions & 3 deletions service/address_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions service/message_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions service/message_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tools/internal/batch_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b08ac8f

Please sign in to comment.