Skip to content

Commit

Permalink
refactor: app query client
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 11, 2024
1 parent 48f5762 commit b69d3d7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
42 changes: 36 additions & 6 deletions pkg/client/query/appquerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -16,19 +19,40 @@ 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
}

// NewApplicationQuerier returns a new instance of a client.ApplicationQueryClient
// 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 {
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions x/application/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
31 changes: 31 additions & 0 deletions x/application/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 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 &params, nil
}
10 changes: 9 additions & 1 deletion x/proof/types/application_query_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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.
Expand Down

0 comments on commit b69d3d7

Please sign in to comment.