From 34178423dfb3ae62cd4ff541c87f503da6ecb126 Mon Sep 17 00:00:00 2001 From: tom <69969590+simlecode@users.noreply.github.com> Date: Fri, 11 Feb 2022 15:10:19 +0800 Subject: [PATCH] feat: venus-shared: Expose RPC client options (#4756) * Expose RPC client options --- venus-devtool/api-gen/client.go | 4 +- venus-shared/api/chain/v0/client_gen.go | 4 +- venus-shared/api/chain/v1/client_gen.go | 4 +- venus-shared/api/client/client.go | 31 --------------- venus-shared/api/client/client_test.go | 53 ------------------------- venus-shared/api/messager/client_gen.go | 4 +- venus-shared/api/proxy_util_test.go | 4 -- venus-shared/api/wallet/client_gen.go | 4 +- 8 files changed, 10 insertions(+), 98 deletions(-) delete mode 100644 venus-shared/api/client/client.go delete mode 100644 venus-shared/api/client/client_test.go diff --git a/venus-devtool/api-gen/client.go b/venus-devtool/api-gen/client.go index b200213772..88a2207dde 100644 --- a/venus-devtool/api-gen/client.go +++ b/venus-devtool/api-gen/client.go @@ -38,14 +38,14 @@ import ( ) // New{{ .APIName }}RPC creates a new httpparse jsonrpc remotecli. -func New{{ .APIName }}RPC(ctx context.Context, addr string, requestHeader http.Header) ({{ .APIName }}, jsonrpc.ClientCloser, error) { +func New{{ .APIName }}RPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) ({{ .APIName }}, jsonrpc.ClientCloser, error) { if requestHeader == nil { requestHeader = http.Header{} } requestHeader.Set(api.VenusAPINamespaceHeader, "{{ .APINs }}") var res {{ .APIStruct }} - closer, err := jsonrpc.NewMergeClient(ctx, addr, "{{ .MethNs }}", api.GetInternalStructs(&res), requestHeader) + closer, err := jsonrpc.NewMergeClient(ctx, addr, "{{ .MethNs }}", api.GetInternalStructs(&res), requestHeader, opts...) return &res, closer, err } diff --git a/venus-shared/api/chain/v0/client_gen.go b/venus-shared/api/chain/v0/client_gen.go index bbb39e63f8..a7215fcb4c 100644 --- a/venus-shared/api/chain/v0/client_gen.go +++ b/venus-shared/api/chain/v0/client_gen.go @@ -11,14 +11,14 @@ import ( ) // NewFullNodeRPC creates a new httpparse jsonrpc remotecli. -func NewFullNodeRPC(ctx context.Context, addr string, requestHeader http.Header) (FullNode, jsonrpc.ClientCloser, error) { +func NewFullNodeRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (FullNode, jsonrpc.ClientCloser, error) { if requestHeader == nil { requestHeader = http.Header{} } requestHeader.Set(api.VenusAPINamespaceHeader, "v0.FullNode") var res FullNodeStruct - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&res), requestHeader) + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&res), requestHeader, opts...) return &res, closer, err } diff --git a/venus-shared/api/chain/v1/client_gen.go b/venus-shared/api/chain/v1/client_gen.go index 1813606a5f..285364777e 100644 --- a/venus-shared/api/chain/v1/client_gen.go +++ b/venus-shared/api/chain/v1/client_gen.go @@ -11,14 +11,14 @@ import ( ) // NewFullNodeRPC creates a new httpparse jsonrpc remotecli. -func NewFullNodeRPC(ctx context.Context, addr string, requestHeader http.Header) (FullNode, jsonrpc.ClientCloser, error) { +func NewFullNodeRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (FullNode, jsonrpc.ClientCloser, error) { if requestHeader == nil { requestHeader = http.Header{} } requestHeader.Set(api.VenusAPINamespaceHeader, "v1.FullNode") var res FullNodeStruct - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&res), requestHeader) + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&res), requestHeader, opts...) return &res, closer, err } diff --git a/venus-shared/api/client/client.go b/venus-shared/api/client/client.go deleted file mode 100644 index 93328500fd..0000000000 --- a/venus-shared/api/client/client.go +++ /dev/null @@ -1,31 +0,0 @@ -package client - -import ( - "context" - "net/http" - - "github.com/filecoin-project/go-jsonrpc" - "github.com/filecoin-project/venus/venus-shared/api" - v0 "github.com/filecoin-project/venus/venus-shared/api/chain/v0" - v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1" -) - -func NewFullRPCV0(ctx context.Context, addr string, header http.Header) (v0.FullNode, jsonrpc.ClientCloser, error) { - var full v0.FullNodeStruct - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&full), - header) - return &full, closer, err -} - -func NewFullRPCV1(ctx context.Context, addr string, header http.Header) (v1.FullNode, jsonrpc.ClientCloser, error) { - var full v1.FullNodeStruct - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&full), - header) - return &full, closer, err -} - -func NewWalletRPC(ctx context.Context, addr string, header http.Header) (v1.IWallet, jsonrpc.ClientCloser, error) { - var wallet v1.IWalletStruct - closer, err := jsonrpc.NewClient(ctx, addr, "Filecoin", &wallet.Internal, header) - return &wallet, closer, err -} diff --git a/venus-shared/api/client/client_test.go b/venus-shared/api/client/client_test.go deleted file mode 100644 index 579c47998a..0000000000 --- a/venus-shared/api/client/client_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package client - -import ( - "context" - "net/http" - "testing" - - "github.com/stretchr/testify/assert" - - tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags" -) - -func TestAPIClient(t *testing.T) { - t.SkipNow() - tf.UnitTest(t) - token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBbGxvdyI6WyJyZWFkIiwid3JpdGUiLCJzaWduIiwiYWRtaW4iXX0.J9r8fhNJpf1bD4G4_tqT9UR81S5CNJHS6fD86rqxfpQ" - addr := "ws://127.0.0.1:3453/rpc/v0" - - ctx := context.Background() - header := http.Header{} - header.Add("Authorization", "Bearer "+token) - - v0cli, v0close, err := NewFullRPCV0(ctx, addr, header) - assert.Nil(t, err) - defer v0close() - - v1cli, v1close, err := NewFullRPCV1(ctx, addr, header) - assert.Nil(t, err) - defer v1close() - - head, err := v0cli.ChainHead(context.Background()) - assert.Nil(t, err) - head2, err := v1cli.ChainHead(context.Background()) - assert.Nil(t, err) - assert.Equal(t, head, head2) - - v0version, err := v0cli.Version(context.Background()) - assert.Nil(t, err) - t.Log(v0version) - - v1version, err := v1cli.Version(context.Background()) - assert.Nil(t, err) - t.Log(v1version) - - wcli, wclose, err := NewWalletRPC(ctx, addr, header) - if err != nil { - t.Fatal(err) - } - defer wclose() - - addrs := wcli.WalletAddresses(context.Background()) - t.Log(addrs) -} diff --git a/venus-shared/api/messager/client_gen.go b/venus-shared/api/messager/client_gen.go index 837950b80d..d98757bb18 100644 --- a/venus-shared/api/messager/client_gen.go +++ b/venus-shared/api/messager/client_gen.go @@ -11,14 +11,14 @@ import ( ) // NewIMessagerRPC creates a new httpparse jsonrpc remotecli. -func NewIMessagerRPC(ctx context.Context, addr string, requestHeader http.Header) (IMessager, jsonrpc.ClientCloser, error) { +func NewIMessagerRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (IMessager, jsonrpc.ClientCloser, error) { if requestHeader == nil { requestHeader = http.Header{} } requestHeader.Set(api.VenusAPINamespaceHeader, "messager.IMessager") var res IMessagerStruct - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Message", api.GetInternalStructs(&res), requestHeader) + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Message", api.GetInternalStructs(&res), requestHeader, opts...) return &res, closer, err } diff --git a/venus-shared/api/proxy_util_test.go b/venus-shared/api/proxy_util_test.go index a57d44ff21..945fe58f1c 100644 --- a/venus-shared/api/proxy_util_test.go +++ b/venus-shared/api/proxy_util_test.go @@ -29,10 +29,6 @@ type StrC struct { } } -type StrD struct { - StrA -} - func TestGetInternalStructs(t *testing.T) { tf.UnitTest(t) var proxy StrA diff --git a/venus-shared/api/wallet/client_gen.go b/venus-shared/api/wallet/client_gen.go index cc8f767295..95e0cff4c4 100644 --- a/venus-shared/api/wallet/client_gen.go +++ b/venus-shared/api/wallet/client_gen.go @@ -11,14 +11,14 @@ import ( ) // NewIFullAPIRPC creates a new httpparse jsonrpc remotecli. -func NewIFullAPIRPC(ctx context.Context, addr string, requestHeader http.Header) (IFullAPI, jsonrpc.ClientCloser, error) { +func NewIFullAPIRPC(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (IFullAPI, jsonrpc.ClientCloser, error) { if requestHeader == nil { requestHeader = http.Header{} } requestHeader.Set(api.VenusAPINamespaceHeader, "wallet.IFullAPI") var res IFullAPIStruct - closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&res), requestHeader) + closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", api.GetInternalStructs(&res), requestHeader, opts...) return &res, closer, err }