Skip to content

Commit

Permalink
refactor: service query client
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 11, 2024
1 parent bb9a3ab commit cd5f64c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
2 changes: 2 additions & 0 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 34 additions & 9 deletions pkg/client/query/servicequerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,28 +17,49 @@ 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
}

// NewServiceQuerier returns a new instance of a client.ServiceQueryClient by
// 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.
Expand All @@ -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(
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions x/service/types/query_client.go
Original file line number Diff line number Diff line change
@@ -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 &params, nil
}

0 comments on commit cd5f64c

Please sign in to comment.