Skip to content

Commit

Permalink
Implement query for SynchronyParams
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Jan 14, 2025
1 parent 949b46c commit 0543cde
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 35 deletions.
14 changes: 14 additions & 0 deletions proto/dydxprotocol/blocktime/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ service Query {
option (google.api.http).get =
"/dydxprotocol/v4/blocktime/all_downtime_info";
}

// Queries the SynchronyParams.
rpc SynchronyParams(QuerySynchronyParamsRequest)
returns (QuerySynchronyParamsResponse) {
option (google.api.http).get = "/dydxprotocol/v4/blocktime/synchrony_params";
}
}

// QuerySynchronyParamsRequest is a request type for the SynchronyParams
message QuerySynchronyParamsRequest {}

// QuerySynchronyParamsResponse is a response type for the SynchronyParams
message QuerySynchronyParamsResponse {
SynchronyParams params = 1 [ (gogoproto.nullable) = false ];
}

// QueryDowntimeParamsRequest is a request type for the DowntimeParams
Expand Down
24 changes: 24 additions & 0 deletions protocol/x/blocktime/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryDowntimeParams())
cmd.AddCommand(CmdQueryAllDowntimeInfo())
cmd.AddCommand(CmdQueryPreviousBlockInfo())
cmd.AddCommand(CmdQuerySynchronyParams())

return cmd
}
Expand Down Expand Up @@ -96,3 +97,26 @@ func CmdQueryPreviousBlockInfo() *cobra.Command {

return cmd
}

func CmdQuerySynchronyParams() *cobra.Command {
cmd := &cobra.Command{
Use: "sychrony-params",
Short: "get synchrony params",
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.SynchronyParams(
context.Background(),
&types.QuerySynchronyParamsRequest{},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
18 changes: 18 additions & 0 deletions protocol/x/blocktime/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import (

var _ types.QueryServer = Keeper{}

func (k Keeper) SynchronyParams(
c context.Context,
req *types.QuerySynchronyParamsRequest,
) (
*types.QuerySynchronyParamsResponse,
error,
) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := lib.UnwrapSDKContext(c, types.ModuleName)
params := k.GetSynchronyParams(ctx)
return &types.QuerySynchronyParamsResponse{
Params: params,
}, nil
}

func (k Keeper) DowntimeParams(
c context.Context,
req *types.QueryDowntimeParamsRequest,
Expand Down
35 changes: 35 additions & 0 deletions protocol/x/blocktime/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/x/blocktime/types"
)

func TestSynchronyParams(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.BlockTimeKeeper

for name, tc := range map[string]struct {
req *types.QuerySynchronyParamsRequest
res *types.QuerySynchronyParamsResponse
err error
}{
"Default": {
req: &types.QuerySynchronyParamsRequest{},
res: &types.QuerySynchronyParamsResponse{
Params: types.DefaultSynchronyParams(),
},
err: nil,
},
"Nil": {
req: nil,
res: nil,
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
t.Run(name, func(t *testing.T) {
res, err := k.SynchronyParams(ctx, tc.req)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
require.Equal(t, tc.res, res)
}
})
}
}

func TestDowntimeParams(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
Expand Down
Loading

0 comments on commit 0543cde

Please sign in to comment.