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: venus-shared: api version / namespaces & helper func for rpc endpoint #4782

Merged
merged 4 commits into from
Feb 18, 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
25 changes: 18 additions & 7 deletions venus-devtool/api-gen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions venus-devtool/api-gen/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "Message",
},
},
Expand All @@ -39,6 +40,9 @@ func init() {
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/wallet",
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
},
},
util.APIMeta{
Type: reflect.TypeOf((*gatewayv1.IGateway)(nil)).Elem(),
Expand All @@ -47,6 +51,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 1,
MethodNamespace: "Gateway",
},
},
Expand All @@ -57,6 +62,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "Gateway",
},
},
Expand All @@ -67,6 +73,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "VENUS_MARKET",
},
},
Expand All @@ -77,6 +84,7 @@ func init() {
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
Version: 0,
MethodNamespace: "VENUS_MARKET_CLIENT",
},
},
Expand Down
7 changes: 7 additions & 0 deletions venus-devtool/util/api_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var ChainAPIPairs = []struct {
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v0",
IncludeAll: true,
},
RPCMeta: RPCMeta{
Version: 0,
},
},
},
{
Expand All @@ -48,13 +51,17 @@ var ChainAPIPairs = []struct {
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v1",
IncludeAll: true,
},
RPCMeta: RPCMeta{
Version: 1,
},
},
},
}

var LatestChainAPIPair = ChainAPIPairs[len(ChainAPIPairs)-1]

type RPCMeta struct {
Version uint32
Namespace string
MethodNamespace string
}
Expand Down
14 changes: 12 additions & 2 deletions venus-shared/api/chain/v0/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions venus-shared/api/chain/v1/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions venus-shared/api/endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"fmt"
"net/url"
)

func Endpoint(raw string, ver uint32) (string, error) {
u, err := url.Parse(raw)
if err != nil {
return "", fmt.Errorf("invalid url: %w", err)
}

if u.Scheme == "" {
return "", fmt.Errorf("scheme is required")
}

if u.Host == "" {
return "", fmt.Errorf("host is required")
}

// raw url contains more than just scheme://host(:prot)
if u.Path != "" && u.Path != "/" {
return raw, nil
}

return fmt.Sprintf("%s://%s/rpc/v%d", u.Scheme, u.Host, ver), nil
}
95 changes: 95 additions & 0 deletions venus-shared/api/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package api

import (
"testing"

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

func TestParseHost(t *testing.T) {

cases := []struct {
raw string
ver uint32
expected string
}{
// valid
{
raw: "http://api.venus.io:1234",
ver: 1,
expected: "http://api.venus.io:1234/rpc/v1",
},
{
raw: "http://api.venus.io:1234/",
ver: 1,
expected: "http://api.venus.io:1234/rpc/v1",
},
{
raw: "http://api.venus.io:1234/rpc",
ver: 1,
expected: "http://api.venus.io:1234/rpc",
},

{
raw: "http://api.venus.io:1234/rpc/v0",
ver: 1,
expected: "http://api.venus.io:1234/rpc/v0",
},

// invalid: no scheme
{
raw: "://api.venus.io:1234",
ver: 1,
expected: "",
},
{
raw: "://api.venus.io:1234/",
ver: 1,
expected: "",
},
{
raw: "://api.venus.io:1234/rpc",
ver: 1,
expected: "",
},
{
raw: "://api.venus.io:1234/rpc/v0",
ver: 1,
expected: "",
},

// invalid: no scheme 2
{
raw: "api.venus.io:1234",
ver: 1,
expected: "",
},
{
raw: "api.venus.io:1234/",
ver: 1,
expected: "",
},
{
raw: "api.venus.io:1234/rpc",
ver: 1,
expected: "",
},
{
raw: "api.venus.io:1234/rpc/v0",
ver: 1,
expected: "",
},
}

for i := range cases {
c := cases[i]
got, err := Endpoint(c.raw, c.ver)
if c.expected == "" {
require.Errorf(t, err, "%s is expected to be invalid, got %s", c.raw, got)
continue
}

require.NoErrorf(t, err, "%s is expected to be valid", c.raw)
require.Equal(t, c.expected, got, "converted endpoint")
}
}
2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

type IGateway interface {
IProofEvent
Expand Down
16 changes: 13 additions & 3 deletions venus-shared/api/gateway/v0/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/market_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/proof_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion venus-shared/api/gateway/v0/wallet_event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v0
package gateway

import (
"context"
Expand Down
Loading