Skip to content

Commit

Permalink
refactor: proof query client
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 13, 2024
1 parent 32be74f commit 06c7c0b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
5 changes: 3 additions & 2 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ type BlockQueryClient interface {
// protobuf message. Since the generated go types don't include interface types, this
// is necessary to prevent dependency cycles.
type ProofParams interface {
cosmostypes.Msg

GetProofRequestProbability() float64
GetProofRequirementThreshold() *cosmostypes.Coin
GetProofMissingPenalty() *cosmostypes.Coin
Expand All @@ -345,8 +347,7 @@ type ProofParams interface {
// ProofQueryClient defines an interface that enables the querying of the
// on-chain proof module params.
type ProofQueryClient interface {
// GetParams queries the chain for the current shared module parameters.
GetParams(ctx context.Context) (ProofParams, error)
ParamsQuerier[ProofParams]
}

// ServiceQueryClient defines an interface that enables the querying of the
Expand Down
51 changes: 34 additions & 17 deletions pkg/client/query/proofquerier.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package query

import (
"context"

"cosmossdk.io/depinject"
"github.com/cosmos/gogoproto/grpc"

"github.com/pokt-network/poktroll/pkg/client"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
)

// TODO_IN_THIS_COMMIT: comment explaining why we can't use client.ProofQueryClient;
// tl;dr, it defines ian interface for ProofParams to avoid a dependency cycle
// (i.e. instead of importing prooftypes).
var _ client.ProofQueryClient = (*proofQuerier)(nil)

// proofQuerier is a wrapper around the prooftypes.QueryClient that enables the
// querying of on-chain proof module params.
type proofQuerier struct {
//client.ParamsQuerier[*prooftypes.Params]
client.ParamsQuerier[client.ProofParams]

clientConn grpc.ClientConn
proofQuerier prooftypes.QueryClient
}
Expand All @@ -22,10 +28,33 @@ type proofQuerier struct {
//
// Required dependencies:
// - grpc.ClientConn
func NewProofQuerier(deps depinject.Config) (client.ProofQueryClient, error) {
querier := &proofQuerier{}
func NewProofQuerier(
deps depinject.Config,
paramsQuerierOpts ...ParamsQuerierOptionFn,
// TODO_IN_THIS_COMMIT: comment explaining why we can't use client.ProofQueryClient;
// tl;dr, it defines ian interface for ProofParams to avoid a dependency cycle
// (i.e. instead of importing prooftypes).
// ) (paramsQuerierIface[*prooftypes.Params], error) {
) (paramsQuerierIface[client.ProofParams], error) {
paramsQuerierCfg := DefaultParamsQuerierConfig()
for _, opt := range paramsQuerierOpts {
opt(paramsQuerierCfg)
}

paramsQuerier, err := NewCachedParamsQuerier[client.ProofParams, prooftypes.ProofQueryClient](
deps, prooftypes.NewProofQueryClient,
WithModuleInfo[*prooftypes.Params](prooftypes.ModuleName, prooftypes.ErrProofParamInvalid),

Check failure on line 46 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

invalid operation: cannot index WithModuleInfo (value of type func(moduleName string, moduleParamError *"cosmossdk.io/errors".Error) ParamsQuerierOptionFn)

Check failure on line 46 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

invalid operation: cannot index WithModuleInfo (value of type func(moduleName string, moduleParamError *"cosmossdk.io/errors".Error) ParamsQuerierOptionFn)

Check failure on line 46 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

invalid operation: cannot index WithModuleInfo (value of type func(moduleName string, moduleParamError *"cosmossdk.io/errors".Error) ParamsQuerierOptionFn)

Check failure on line 46 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

invalid operation: cannot index WithModuleInfo (value of type func(moduleName string, moduleParamError *"cosmossdk.io/errors".Error) ParamsQuerierOptionFn)
WithParamsCacheOptions(paramsQuerierCfg.CacheOpts...),

Check failure on line 47 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

undefined: WithParamsCacheOptions (typecheck)

Check failure on line 47 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

undefined: WithParamsCacheOptions) (typecheck)

Check failure on line 47 in pkg/client/query/proofquerier.go

View workflow job for this annotation

GitHub Actions / go-test

undefined: WithParamsCacheOptions) (typecheck)
)
if err != nil {
return nil, err
}

if err := depinject.Inject(
querier := &proofQuerier{
ParamsQuerier: paramsQuerier,
}

if err = depinject.Inject(
deps,
&querier.clientConn,
); err != nil {
Expand All @@ -36,15 +65,3 @@ func NewProofQuerier(deps depinject.Config) (client.ProofQueryClient, error) {

return querier, nil
}

// GetParams queries the chain for the current proof module parameters.
func (pq *proofQuerier) GetParams(
ctx context.Context,
) (client.ProofParams, error) {
req := &prooftypes.QueryParamsRequest{}
res, err := pq.proofQuerier.Params(ctx, req)
if err != nil {
return nil, err
}
return &res.Params, nil
}
2 changes: 1 addition & 1 deletion x/proof/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ApplicationKeeper interface {
GetApplication(ctx context.Context, address string) (app apptypes.Application, found bool)
GetAllApplications(ctx context.Context) []apptypes.Application
SetApplication(context.Context, apptypes.Application)
GetParams(context.Context) apptypes.Params
GetParams(ctx context.Context) apptypes.Params
}

// SharedKeeper defines the expected interface needed to retrieve shared information.
Expand Down
33 changes: 33 additions & 0 deletions x/proof/types/query_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package types

import (
"context"

gogogrpc "github.com/cosmos/gogoproto/grpc"

"github.com/pokt-network/poktroll/pkg/client"
)

// TODO_IN_THIS_COMMIT: godoc...
type ProofQueryClient interface {
QueryClient

GetParams(context.Context) (client.ProofParams, error)
}

// TODO_IN_THIS_COMMIT: godoc...
func NewProofQueryClient(conn gogogrpc.ClientConn) ProofQueryClient {
return NewQueryClient(conn).(ProofQueryClient)
}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
func (c *queryClient) GetParams(ctx context.Context) (client.ProofParams, error) {
res, err := c.Params(ctx, &QueryParamsRequest{})
if err != nil {
return nil, err
}

params := res.GetParams()
return &params, nil
}

0 comments on commit 06c7c0b

Please sign in to comment.