From b69d3d7141e743f5fe86d4caf2516227e20dec06 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 | 42 +++++++++++++++++++---- x/application/types/expected_keepers.go | 4 +++ x/application/types/query_client.go | 31 +++++++++++++++++ x/proof/types/application_query_client.go | 10 +++++- 5 files changed, 82 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 535f40b0f..2eb7e2592 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..09eccadc2 100644 --- a/pkg/client/query/appquerier.go +++ b/pkg/client/query/appquerier.go @@ -4,10 +4,13 @@ import ( "context" "cosmossdk.io/depinject" - grpc "github.com/cosmos/gogoproto/grpc" + cosmostypes "github.com/cosmos/cosmos-sdk/types" + accounttypes "github.com/cosmos/cosmos-sdk/x/auth/types" + 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 +19,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 +29,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) + } - if err := depinject.Inject( + paramsQuerier, err := NewCachedParamsQuerier[*apptypes.Params, apptypes.ApplicationQueryClient]( + deps, apptypes.NewAppQueryClient, + WithModuleInfo[*sharedtypes.Params](sharedtypes.ModuleName, sharedtypes.ErrSharedParamInvalid), + WithParamsCacheOptions(cfg.CacheOpts...), + ) + if err != nil { + return nil, err + } + + aq := &appQuerier{ + ParamsQuerier: paramsQuerier, + } + + if err = depinject.Inject( deps, &aq.clientConn, ); err != nil { @@ -45,6 +69,12 @@ func (aq *appQuerier) GetApplication( ctx context.Context, appAddress string, ) (apptypes.Application, error) { + defer client.AllQueriesTotalCounter.With( + "method", "account", + "client_type", "account", + "msg_type", cosmostypes.MsgTypeURL(new(accounttypes.QueryAccountRequest)), + ).Add(1) + req := apptypes.QueryGetApplicationRequest{Address: appAddress} res, err := aq.applicationQuerier.Application(ctx, &req) if 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..7c6d09997 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 { + *sharedkeeper.KeeperParamsQuerier[apptypes.Params, ApplicationKeeper] + 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, + KeeperParamsQuerier: keeperParamsQuerier, + } } // GetApplication returns the application corresponding to the given address.