From bb9a3abba292c140d5360f6440f1e0fa015b02b5 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Tue, 10 Dec 2024 15:45:34 +0100 Subject: [PATCH] refactor: proof query client --- pkg/client/interface.go | 5 +-- pkg/client/query/proofquerier.go | 51 ++++++++++++++++++++----------- x/proof/types/expected_keepers.go | 2 +- x/proof/types/query_client.go | 33 ++++++++++++++++++++ 4 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 x/proof/types/query_client.go diff --git a/pkg/client/interface.go b/pkg/client/interface.go index 0eba6740e..8eb0b8cfa 100644 --- a/pkg/client/interface.go +++ b/pkg/client/interface.go @@ -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 @@ -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 diff --git a/pkg/client/query/proofquerier.go b/pkg/client/query/proofquerier.go index 30c2984cd..dd4a2218e 100644 --- a/pkg/client/query/proofquerier.go +++ b/pkg/client/query/proofquerier.go @@ -1,8 +1,6 @@ package query import ( - "context" - "cosmossdk.io/depinject" "github.com/cosmos/gogoproto/grpc" @@ -10,9 +8,17 @@ import ( 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 } @@ -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), + WithParamsCacheOptions(paramsQuerierCfg.CacheOpts...), + ) + if err != nil { + return nil, err + } - if err := depinject.Inject( + querier := &proofQuerier{ + ParamsQuerier: paramsQuerier, + } + + if err = depinject.Inject( deps, &querier.clientConn, ); err != nil { @@ -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 -} diff --git a/x/proof/types/expected_keepers.go b/x/proof/types/expected_keepers.go index fb06c6ac8..22bfc9522 100644 --- a/x/proof/types/expected_keepers.go +++ b/x/proof/types/expected_keepers.go @@ -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. diff --git a/x/proof/types/query_client.go b/x/proof/types/query_client.go new file mode 100644 index 000000000..de5e5e3b4 --- /dev/null +++ b/x/proof/types/query_client.go @@ -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 ¶ms, nil +}