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(vsmgr): support token-included api infos #361

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions docs/en/04.venus-sector-manager-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,27 @@ We will break down each configurable item one by one.

````
[Common.API]
# Chain service address, required, string type
# Chain service info, required, string type
# Fill in according to the actual situation of the service used
# If the field is valid as a token-included-info-string ("{token}:{multiaddr}"), the token included would be used to construct the rpc client instead of the common token.
Chain = "/ip4/{api_host}/tcp/{api_port}"

# Message service address, required, string type
# Message service info, required, string type
# Fill in according to the actual situation of the service used
# If the field is valid as a token-included-info-string ("{token}:{multiaddr}"), the token included would be used to construct the rpc client instead of the common token.
Messager = "/ip4/{api_host}/tcp/{api_port}"

# Market service address, required, string type
# Market service info, required, string type
# Fill in according to the actual situation of the service used
# If the field is valid as a token-included-info-string ("{token}:{multiaddr}"), the token included would be used to construct the rpc client instead of the common token.
Market = "/ip4/{api_host}/tcp/{api_port}"

# Event gateway service address, required, string type
# Event gateway service infos, required, string type
# Fill in according to the actual situation of the service used
# For each one contained, if the item is valid as a token-included-info-string ("{token}:{multiaddr}"), the token included would be used to construct the rpc client instead of the common token.
Gateway = ["/ip4/{api_host}/tcp/{api_port}"]

# service token, required, string type
# common token for services, required, string type
# Fill in according to the actual situation of the service used
Token = "{some token}"

Expand Down Expand Up @@ -210,7 +214,7 @@ Path = "/mnt/remote/10.0.0.14/store"
# read only, optional, boolean
# Default is false
# From v0.4.0 and above, the persistent storage allocation logic goes to vsmgr
# This configuration can be used to set whether you can continue to write to the storage
# This configuration can be used to set whether you can continue to write to the storage
#ReadOnly = false

# weight, optional, number type
Expand Down
12 changes: 8 additions & 4 deletions docs/zh/04.venus-sector-manager的配置解析.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,27 @@

```
[Common.API]
# 链服务地址,必填项,字符串类型
# 链服务信息,必填项,字符串类型
# 根据所使用的服务实际情况填写
# 如果使用了合法的 "{token}:{multiaddr}" 格式,构造客户端时将使用本字段中提取的 token, 否则使用通用 token
Chain = "/ip4/{api_host}/tcp/{api_port}"

# 消息服务地址,必填项,字符串类型
# 消息服务信息,必填项,字符串类型
# 根据所使用的服务实际情况填写
# 如果使用了合法的 "{token}:{multiaddr}" 格式,构造客户端时将使用本字段中提取的 token, 否则使用通用 token
Messager = "/ip4/{api_host}/tcp/{api_port}"

# 市场服务地址,必填项,字符串类型
# 市场服务信息,必填项,字符串类型
# 根据所使用的服务实际情况填写
# 如果使用了合法的 "{token}:{multiaddr}" 格式,构造客户端时将使用本字段中提取的 token, 否则使用通用 token
Market = "/ip4/{api_host}/tcp/{api_port}"

# 事件网关服务地址,必填项,字符串类型
# 根据所使用的服务实际情况填写
# 对于每一条信息,如果使用了合法的 "{token}:{multiaddr}" 格式,构造客户端时将使用本字段中提取的 token, 否则使用通用 token
Gateway = ["/ip4/{api_host}/tcp/{api_port}"]

# 服务 token, 必填项,字符串类型
# 服务通用 token, 必填项,字符串类型
# 根据所使用的服务实际情况填写
Token = "{some token}"

Expand Down
5 changes: 3 additions & 2 deletions venus-sector-manager/dep/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func StartProofEvent(gctx GlobalContext, lc fx.Lifecycle, prover core.Prover, cf
}

cfg.Lock()
urls, token, miners := cfg.Common.API.Gateway, cfg.Common.API.Token, cfg.Miners
urls, commonToken, miners := cfg.Common.API.Gateway, cfg.Common.API.Token, cfg.Miners
cfg.Unlock()

if len(urls) == 0 {
Expand Down Expand Up @@ -68,7 +68,8 @@ func StartProofEvent(gctx GlobalContext, lc fx.Lifecycle, prover core.Prover, cf
return nil
}

for _, addr := range urls {
for _, u := range urls {
addr, token := extractAPIInfo(u, commonToken)
client, err := miner.NewProofEventClient(lc, addr, token)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions venus-sector-manager/dep/sealer_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func BuildLocalSectorStateManager(online OnlineMetaStore, offline OfflineMetaSto

func BuildMessagerClient(gctx GlobalContext, lc fx.Lifecycle, scfg *modules.Config, locker confmgr.RLocker) (messager.API, error) {
locker.Lock()
api, token := scfg.Common.API.Messager, scfg.Common.API.Token
api, token := extractAPIInfo(scfg.Common.API.Messager, scfg.Common.API.Token)
locker.Unlock()

mcli, mcloser, err := messager.New(gctx, api, token)
Expand Down Expand Up @@ -265,7 +265,7 @@ func buildSealerCliClient(gctx GlobalContext, lc fx.Lifecycle, serverAddr string

func BuildChainClient(gctx GlobalContext, lc fx.Lifecycle, scfg *modules.Config, locker confmgr.RLocker) (chain.API, error) {
locker.Lock()
api, token := scfg.Common.API.Chain, scfg.Common.API.Token
api, token := extractAPIInfo(scfg.Common.API.Chain, scfg.Common.API.Token)
locker.Unlock()

ccli, ccloser, err := chain.New(gctx, api, token)
Expand Down Expand Up @@ -465,7 +465,7 @@ type MarketAPIRelatedComponets struct {

func BuildMarketAPI(gctx GlobalContext, lc fx.Lifecycle, scfg *modules.SafeConfig, infoAPI core.MinerInfoAPI) (market.API, error) {
scfg.Lock()
api, token := scfg.Common.API.Market, scfg.Common.API.Token
api, token := extractAPIInfo(scfg.Common.API.Market, scfg.Common.API.Token)
defer scfg.Unlock()

if api == "" {
Expand Down
23 changes: 23 additions & 0 deletions venus-sector-manager/dep/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dep

import (
"regexp"
"strings"
)

var (
infoWithToken = regexp.MustCompile("^[a-zA-Z0-9\\-_]+?\\.[a-zA-Z0-9\\-_]+?\\.([a-zA-Z0-9\\-_]+)?:.+$")
)

func extractAPIInfo(raw string, commonToken string) (string, string) {
if !infoWithToken.Match([]byte(raw)) {
return raw, commonToken
}

sp := strings.SplitN(raw, ":", 2)
if sp[0] == "" {
return sp[1], commonToken
}

return sp[1], sp[0]
}
81 changes: 81 additions & 0 deletions venus-sector-manager/dep/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package dep

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestExtractAPIInfo(t *testing.T) {
cases := []struct {
raw string
common string

api string
token string
}{
{
raw: "/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: "/ip4/127.0.0.1/tcp/1789",
token: "abcde",
},

{
raw: "x.y.z:/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: "/ip4/127.0.0.1/tcp/1789",
token: "x.y.z",
},

{
raw: "x.y.z:/ip4/127.0.0.1/tcp/1789",
common: "",
api: "/ip4/127.0.0.1/tcp/1789",
token: "x.y.z",
},

{
raw: ":/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: ":/ip4/127.0.0.1/tcp/1789",
token: "abcde",
},

{
raw: "x.y:/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: "x.y:/ip4/127.0.0.1/tcp/1789",
token: "abcde",
},

{
raw: "x:/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: "x:/ip4/127.0.0.1/tcp/1789",
token: "abcde",
},

{
raw: "..:/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: "..:/ip4/127.0.0.1/tcp/1789",
token: "abcde",
},

{
raw: "xyz:/ip4/127.0.0.1/tcp/1789",
common: "abcde",
api: "xyz:/ip4/127.0.0.1/tcp/1789",
token: "abcde",
},
}

for ci := range cases {
c := cases[ci]

api, token := extractAPIInfo(c.raw, c.common)
require.Equalf(t, c.api, api, "api extracted from %s, %s", c.raw, c.common)
require.Equalf(t, c.token, token, "token extracted from %s, %s", c.raw, c.common)
}
}