From 32be74f12d6226c66dad7765a9bd7ddb82fe5d60 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Tue, 10 Dec 2024 15:45:28 +0100 Subject: [PATCH] refactor: app query client --- pkg/client/interface.go | 2 ++ pkg/client/query/appquerier.go | 34 +++++++++++++++++++---- x/application/types/expected_keepers.go | 4 +++ x/application/types/query_client.go | 31 +++++++++++++++++++++ x/proof/types/application_query_client.go | 10 ++++++- x/proof/types/expected_keepers.go | 1 + x/tokenomics/types/expected_keepers.go | 1 + 7 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 x/application/types/query_client.go diff --git a/pkg/client/interface.go b/pkg/client/interface.go index 93956d6a7..f946ad2fe 100644 --- a/pkg/client/interface.go +++ b/pkg/client/interface.go @@ -268,6 +268,8 @@ type AccountQueryClient interface { // ApplicationQueryClient defines an interface that enables the querying of the // on-chain application information type ApplicationQueryClient interface { + ParamsQuerier[*apptypes.Params] + // GetApplication queries the chain for the details of the application provided GetApplication(ctx context.Context, appAddress string) (apptypes.Application, error) diff --git a/pkg/client/query/appquerier.go b/pkg/client/query/appquerier.go index 9477c35f9..b578532f3 100644 --- a/pkg/client/query/appquerier.go +++ b/pkg/client/query/appquerier.go @@ -4,10 +4,11 @@ import ( "context" "cosmossdk.io/depinject" - grpc "github.com/cosmos/gogoproto/grpc" + gogogrpc "github.com/cosmos/gogoproto/grpc" "github.com/pokt-network/poktroll/pkg/client" apptypes "github.com/pokt-network/poktroll/x/application/types" + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" ) var _ client.ApplicationQueryClient = (*appQuerier)(nil) @@ -16,7 +17,9 @@ var _ client.ApplicationQueryClient = (*appQuerier)(nil) // querying of on-chain application information through a single exposed method // which returns an apptypes.Application interface type appQuerier struct { - clientConn grpc.ClientConn + client.ParamsQuerier[*apptypes.Params] + + clientConn gogogrpc.ClientConn applicationQuerier apptypes.QueryClient } @@ -24,11 +27,30 @@ type appQuerier struct { // by injecting the dependecies provided by the depinject.Config // // Required dependencies: -// - clientCtx -func NewApplicationQuerier(deps depinject.Config) (client.ApplicationQueryClient, error) { - aq := &appQuerier{} +// - clientCtx (gogogrpc.ClientConn) +func NewApplicationQuerier( + deps depinject.Config, + opts ...ParamsQuerierOptionFn, +) (client.ApplicationQueryClient, error) { + cfg := DefaultParamsQuerierConfig() + for _, opt := range opts { + opt(cfg) + } + + paramsQuerier, err := NewCachedParamsQuerier[*apptypes.Params, apptypes.ApplicationQueryClient]( + deps, apptypes.NewAppQueryClient, + WithModuleInfo(sharedtypes.ModuleName, sharedtypes.ErrSharedParamInvalid), + WithQueryCacheOptions(cfg.CacheOpts...), + ) + if err != nil { + return nil, err + } + + aq := &appQuerier{ + ParamsQuerier: paramsQuerier, + } - if err := depinject.Inject( + if err = depinject.Inject( deps, &aq.clientConn, ); err != nil { diff --git a/x/application/types/expected_keepers.go b/x/application/types/expected_keepers.go index 3c908df63..55756ad3e 100644 --- a/x/application/types/expected_keepers.go +++ b/x/application/types/expected_keepers.go @@ -37,3 +37,7 @@ type SharedKeeper interface { GetParams(ctx context.Context) sharedtypes.Params GetSessionEndHeight(ctx context.Context, queryHeight int64) int64 } + +type ApplicationKeeper interface { + GetParams(ctx context.Context) Params +} diff --git a/x/application/types/query_client.go b/x/application/types/query_client.go new file mode 100644 index 000000000..682da5594 --- /dev/null +++ b/x/application/types/query_client.go @@ -0,0 +1,31 @@ +package types + +import ( + "context" + + gogogrpc "github.com/cosmos/gogoproto/grpc" +) + +// TODO_IN_THIS_COMMIT: godoc... +type ApplicationQueryClient interface { + QueryClient + + GetParams(context.Context) (*Params, error) +} + +// TODO_IN_THIS_COMMIT: godoc... +func NewAppQueryClient(conn gogogrpc.ClientConn) ApplicationQueryClient { + return NewQueryClient(conn).(ApplicationQueryClient) +} + +// 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 +} diff --git a/x/proof/types/application_query_client.go b/x/proof/types/application_query_client.go index 1cd887314..fd329007d 100644 --- a/x/proof/types/application_query_client.go +++ b/x/proof/types/application_query_client.go @@ -5,6 +5,7 @@ import ( "github.com/pokt-network/poktroll/pkg/client" apptypes "github.com/pokt-network/poktroll/x/application/types" + sharedkeeper "github.com/pokt-network/poktroll/x/shared/keeper" ) var _ client.ApplicationQueryClient = (*AppKeeperQueryClient)(nil) @@ -13,6 +14,8 @@ var _ client.ApplicationQueryClient = (*AppKeeperQueryClient)(nil) // It does not rely on the QueryClient, and therefore does not make any // network requests as in the off-chain implementation. type AppKeeperQueryClient struct { + client.ParamsQuerier[*apptypes.Params] + keeper ApplicationKeeper } @@ -22,7 +25,12 @@ type AppKeeperQueryClient struct { // has delegated its signing power to. // It should be injected into the RingClient when initialized from within the a keeper. func NewAppKeeperQueryClient(appKeeper ApplicationKeeper) client.ApplicationQueryClient { - return &AppKeeperQueryClient{keeper: appKeeper} + keeperParamsQuerier := sharedkeeper.NewKeeperParamsQuerier[apptypes.Params](appKeeper) + + return &AppKeeperQueryClient{ + keeper: appKeeper, + ParamsQuerier: keeperParamsQuerier, + } } // GetApplication returns the application corresponding to the given address. diff --git a/x/proof/types/expected_keepers.go b/x/proof/types/expected_keepers.go index 9d1fd765e..fb06c6ac8 100644 --- a/x/proof/types/expected_keepers.go +++ b/x/proof/types/expected_keepers.go @@ -49,6 +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 } // SharedKeeper defines the expected interface needed to retrieve shared information. diff --git a/x/tokenomics/types/expected_keepers.go b/x/tokenomics/types/expected_keepers.go index c7c0de0c5..0d864409d 100644 --- a/x/tokenomics/types/expected_keepers.go +++ b/x/tokenomics/types/expected_keepers.go @@ -46,6 +46,7 @@ type ApplicationKeeper interface { GetAllApplications(ctx context.Context) []apptypes.Application UnbondApplication(ctx context.Context, app *apptypes.Application) error EndBlockerUnbondApplications(ctx context.Context) error + GetParams(ctx context.Context) apptypes.Params } type ProofKeeper interface {