Skip to content

Commit

Permalink
Add x/rollup query server (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
natebeauregard authored Nov 7, 2024
1 parent a51efc6 commit eef648c
Show file tree
Hide file tree
Showing 10 changed files with 1,009 additions and 20 deletions.
40 changes: 40 additions & 0 deletions proto/rollup/v1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";

package rollup.v1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "rollup/v1/rollup.proto";

option go_package = "github.com/polymerdao/monomer/x/rollup/types";

// Query defines all query endpoints for the rollup module.
service Query {
// Params returns all rollup module parameters.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/rollup/v1/params";
}

// L1BlockInfo returns the block info derived from L1.
rpc L1BlockInfo(QueryL1BlockInfoRequest) returns (QueryL1BlockInfoResponse) {
option (google.api.http).get = "/rollup/v1/l1_block_info";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is response type for the Query/Params RPC method.
message QueryParamsResponse {
// params holds all the parameters for the rollup module.
Params params = 1 [(gogoproto.nullable) = false];
}

// QueryL1BlockInfoRequest is the request type for the Query/L1BlockInfo RPC method.
message QueryL1BlockInfoRequest {}

// QueryL1BlockInfoResponse is response type for the Query/L1BlockInfo RPC method.
message QueryL1BlockInfoResponse {
// l1_block_info holds the block info derived from L1.
L1BlockInfo l1_block_info = 1 [(gogoproto.nullable) = false];
}
17 changes: 1 addition & 16 deletions x/rollup/keeper/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,6 @@ import (
"github.com/samber/lo"
)

// setL1BlockInfo sets the L1 block info to the app state
//
// Persisted data conforms to optimism specs on L1 attributes:
// https://github.com/ethereum-optimism/optimism/blob/develop/specs/deposits.md#l1-attributes-predeployed-contract
func (k *Keeper) setL1BlockInfo(ctx sdk.Context, info types.L1BlockInfo) error { //nolint:gocritic
infoBytes, err := info.Marshal()
if err != nil {
return types.WrapError(err, "marshal L1 block info")
}
if err = k.storeService.OpenKVStore(ctx).Set([]byte(types.L1BlockInfoKey), infoBytes); err != nil {
return types.WrapError(err, "set latest L1 block info")
}
return nil
}

// processL1AttributesTx processes the L1 Attributes tx and returns the L1 block info.
func (k *Keeper) processL1AttributesTx(ctx sdk.Context, txBytes []byte) (*types.L1BlockInfo, error) { //nolint:gocritic // hugeParam
var tx ethtypes.Transaction
Expand Down Expand Up @@ -132,7 +117,7 @@ func (k *Keeper) processL1UserDepositTxs(

params, err := k.GetParams(ctx)
if err != nil {
return nil, types.WrapError(types.ErrInitiateFeeWithdrawal, "failed to get params: %v", err)
return nil, types.WrapError(types.ErrParams, "failed to get params: %v", err)
}

// Convert the L1CrossDomainMessenger address to its L2 aliased address
Expand Down
37 changes: 37 additions & 0 deletions x/rollup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/polymerdao/monomer/x/rollup/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var _ types.QueryServer = (*Keeper)(nil)

// L1BlockInfo implements the Query/L1BlockInfo gRPC method
func (k *Keeper) L1BlockInfo(ctx context.Context, req *types.QueryL1BlockInfoRequest) (*types.QueryL1BlockInfoResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
l1BlockInfo, err := k.GetL1BlockInfo(sdk.UnwrapSDKContext(ctx))
if err != nil {
return nil, fmt.Errorf("get L1 block info: %w", err)
}
return &types.QueryL1BlockInfoResponse{L1BlockInfo: *l1BlockInfo}, nil
}

// Params implements the Query/Params gRPC method
func (k *Keeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
params, err := k.GetParams(sdk.UnwrapSDKContext(ctx))
if err != nil {
return nil, fmt.Errorf("get params: %w", err)
}
return &types.QueryParamsResponse{Params: *params}, nil
}
30 changes: 30 additions & 0 deletions x/rollup/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/polymerdao/monomer/x/rollup/types"
)

func (s *KeeperTestSuite) TestParamsQuery() {
params := types.DefaultParams()
err := s.rollupKeeper.SetParams(sdk.UnwrapSDKContext(s.ctx), &params)
s.Require().NoError(err)

response, err := s.rollupKeeper.Params(s.ctx, &types.QueryParamsRequest{})
s.Require().NoError(err)
s.Require().Equal(&types.QueryParamsResponse{Params: params}, response)
}

func (s *KeeperTestSuite) TestL1BlockInfoQuery() {
l1BlockInfo := types.L1BlockInfo{
Number: 1,
Time: 1,
}

err := s.rollupKeeper.SetL1BlockInfo(sdk.UnwrapSDKContext(s.ctx), l1BlockInfo)
s.Require().NoError(err)

response, err := s.rollupKeeper.L1BlockInfo(s.ctx, &types.QueryL1BlockInfoRequest{})
s.Require().NoError(err)
s.Require().Equal(&types.QueryL1BlockInfoResponse{L1BlockInfo: l1BlockInfo}, response)
}
15 changes: 15 additions & 0 deletions x/rollup/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ func (k *Keeper) GetL1BlockInfo(ctx sdk.Context) (*types.L1BlockInfo, error) { /
return &l1BlockInfo, nil
}

// SetL1BlockInfo sets the derived L1 block info in the rollup store.
//
// Persisted data conforms to optimism specs on L1 attributes:
// https://github.com/ethereum-optimism/optimism/blob/develop/specs/deposits.md#l1-attributes-predeployed-contract
func (k *Keeper) SetL1BlockInfo(ctx sdk.Context, info types.L1BlockInfo) error { //nolint:gocritic
infoBytes, err := info.Marshal()
if err != nil {
return types.WrapError(err, "marshal L1 block info")
}
if err = k.storeService.OpenKVStore(ctx).Set([]byte(types.L1BlockInfoKey), infoBytes); err != nil {
return types.WrapError(err, "set latest L1 block info")
}
return nil
}

func (k *Keeper) GetParams(ctx sdk.Context) (*types.Params, error) { //nolint:gocritic // hugeParam
paramsBz, err := k.storeService.OpenKVStore(ctx).Get([]byte(types.ParamsKey))
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions x/rollup/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (s *KeeperTestSuite) SetupTest() {
s.setup()
}

func (s *KeeperTestSuite) SetupSubTest() {
s.setup()
}

func (s *KeeperTestSuite) setup() {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
s.ctx = testutil.DefaultContextWithDB(
s.T(),
Expand Down
2 changes: 1 addition & 1 deletion x/rollup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (k *Keeper) ApplyL1Txs(goCtx context.Context, msg *types.MsgApplyL1Txs) (*t
}

// save L1 block info to AppState
if err = k.setL1BlockInfo(ctx, *l1blockInfo); err != nil {
if err = k.SetL1BlockInfo(ctx, *l1blockInfo); err != nil {
return nil, types.WrapError(types.ErrL1BlockInfo, "save error: %v", err)
}

Expand Down
5 changes: 3 additions & 2 deletions x/rollup/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ func (am AppModule) Name() string {
// QuerierRoute returns the rollup module's query routing key.
func (AppModule) QuerierRoute() string { return types.QuerierRoute }

// RegisterServices registers a GRPC query service to respond to the
// module-specific GRPC queries.
// RegisterServices registers a Msg service to respond to module-specific messages and a GRPC query service to respond
// to module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// RegisterInvariants registers the rollup module's invariants.
Expand Down
3 changes: 2 additions & 1 deletion x/rollup/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (
ErrMintETH = registerErr("failed to mint ETH")
ErrBurnETH = registerErr("failed to burn ETH")
ErrInvalidSender = registerErr("invalid sender address")
ErrL1BlockInfo = registerErr("L1 block info")
ErrL1BlockInfo = registerErr("l1 block info")
ErrParams = registerErr("params")
ErrProcessL1UserDepositTxs = registerErr("failed to process L1 user deposit txs")
ErrProcessL1SystemDepositTx = registerErr("failed to process L1 system deposit tx")
ErrInitiateFeeWithdrawal = registerErr("failed to initiate fee withdrawal")
Expand Down
Loading

0 comments on commit eef648c

Please sign in to comment.