From cd5f64c71c024818ed3a72940efafbeb0eab6098 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Tue, 10 Dec 2024 15:45:38 +0100 Subject: [PATCH] refactor: service query client --- pkg/client/interface.go | 2 ++ pkg/client/query/servicequerier.go | 43 +++++++++++++++++++++++------- x/service/types/query_client.go | 31 +++++++++++++++++++++ 3 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 x/service/types/query_client.go diff --git a/pkg/client/interface.go b/pkg/client/interface.go index 8eb0b8cfa..ab70b189e 100644 --- a/pkg/client/interface.go +++ b/pkg/client/interface.go @@ -353,6 +353,8 @@ type ProofQueryClient interface { // ServiceQueryClient defines an interface that enables the querying of the // on-chain service information type ServiceQueryClient interface { + ParamsQuerier[*servicetypes.Params] + // GetService queries the chain for the details of the service provided GetService(ctx context.Context, serviceId string) (sharedtypes.Service, error) GetServiceRelayDifficulty(ctx context.Context, serviceId string) (servicetypes.RelayMiningDifficulty, error) diff --git a/pkg/client/query/servicequerier.go b/pkg/client/query/servicequerier.go index cb0629681..84beed5ad 100644 --- a/pkg/client/query/servicequerier.go +++ b/pkg/client/query/servicequerier.go @@ -4,7 +4,7 @@ import ( "context" "cosmossdk.io/depinject" - "github.com/cosmos/gogoproto/grpc" + gogogrpc "github.com/cosmos/gogoproto/grpc" "github.com/pokt-network/poktroll/pkg/client" servicetypes "github.com/pokt-network/poktroll/x/service/types" @@ -17,7 +17,9 @@ var _ client.ServiceQueryClient = (*serviceQuerier)(nil) // querying of on-chain service information through a single exposed method // which returns a sharedtypes.Service struct type serviceQuerier struct { - clientConn grpc.ClientConn + client.ParamsQuerier[*servicetypes.Params] + + clientConn gogogrpc.ClientConn serviceQuerier servicetypes.QueryClient } @@ -25,20 +27,39 @@ type serviceQuerier struct { // injecting the dependecies provided by the depinject.Config. // // Required dependencies: -// - clientCtx (grpc.ClientConn) -func NewServiceQuerier(deps depinject.Config) (client.ServiceQueryClient, error) { - servq := &serviceQuerier{} +// - clientCtx (gogogrpc.ClientConn) +func NewServiceQuerier( + deps depinject.Config, + paramsQuerierOpts ...ParamsQuerierOptionFn, +) (client.ServiceQueryClient, error) { + paramsQuerierCfg := DefaultParamsQuerierConfig() + for _, opt := range paramsQuerierOpts { + opt(paramsQuerierCfg) + } + + paramsQuerier, err := NewCachedParamsQuerier[*servicetypes.Params, servicetypes.ServiceQueryClient]( + deps, servicetypes.NewServiceQueryClient, + WithModuleInfo[*servicetypes.Params](servicetypes.ModuleName, servicetypes.ErrServiceParamInvalid), + WithParamsCacheOptions(paramsQuerierCfg.CacheOpts...), + ) + if err != nil { + return nil, err + } + + querier := &serviceQuerier{ + ParamsQuerier: paramsQuerier, + } - if err := depinject.Inject( + if err = depinject.Inject( deps, - &servq.clientConn, + &querier.clientConn, ); err != nil { return nil, err } - servq.serviceQuerier = servicetypes.NewQueryClient(servq.clientConn) + querier.serviceQuerier = servicetypes.NewQueryClient(querier.clientConn) - return servq, nil + return querier, nil } // GetService returns a sharedtypes.Service struct for a given serviceId. @@ -51,6 +72,8 @@ func (servq *serviceQuerier) GetService( Id: serviceId, } + // TODO_IN_THIS_COMMIT: historically cache services... + res, err := servq.serviceQuerier.Service(ctx, req) if err != nil { return sharedtypes.Service{}, ErrQueryRetrieveService.Wrapf( @@ -71,6 +94,8 @@ func (servq *serviceQuerier) GetServiceRelayDifficulty( ServiceId: serviceId, } + // TODO_IN_THIS_COMMIT: historically cache relay mining difficulties... + res, err := servq.serviceQuerier.RelayMiningDifficulty(ctx, req) if err != nil { return servicetypes.RelayMiningDifficulty{}, err diff --git a/x/service/types/query_client.go b/x/service/types/query_client.go new file mode 100644 index 000000000..d321cee0c --- /dev/null +++ b/x/service/types/query_client.go @@ -0,0 +1,31 @@ +package types + +import ( + "context" + + gogogrpc "github.com/cosmos/gogoproto/grpc" +) + +// TODO_IN_THIS_COMMIT: godoc... +type ServiceQueryClient interface { + QueryClient + + GetParams(context.Context) (*Params, error) +} + +// TODO_IN_THIS_COMMIT: godoc... +func NewServiceQueryClient(conn gogogrpc.ClientConn) ServiceQueryClient { + return NewQueryClient(conn).(ServiceQueryClient) +} + +// TODO_IN_THIS_COMMIT: investigate generalization... +// TODO_IN_THIS_COMMIT: godoc... +func (c *queryClient) GetParams(ctx context.Context) (*Params, error) { + res, err := c.Params(ctx, &QueryParamsRequest{}) + if err != nil { + return nil, err + } + + params := res.GetParams() + return ¶ms, nil +}