Skip to content

Commit

Permalink
Add authority depinject and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
natebeauregard committed Nov 12, 2024
1 parent 42a9885 commit 5f3f935
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 23 deletions.
71 changes: 62 additions & 9 deletions gen/rollup/module/v1/module.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions proto/rollup/module/v1/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ message Module {
option (cosmos.app.v1alpha1.module) = {
go_import: "github.com/polymerdao/monomer/x/rollup"
};

// authority defines the custom module authority. If not set, defaults to the governance module.
string authority = 1;
}
18 changes: 16 additions & 2 deletions x/rollup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ import (
"google.golang.org/grpc/status"
)

type Querier struct {
Keeper
}

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

func NewQuerier(keeper *Keeper) Querier {
return Querier{Keeper: *keeper}
}

// L1BlockInfo implements the Query/L1BlockInfo gRPC method
func (k *Keeper) L1BlockInfo(ctx context.Context, req *types.QueryL1BlockInfoRequest) (*types.QueryL1BlockInfoResponse, error) {
func (k Keeper) L1BlockInfo( //nolint:gocritic // hugeParam
ctx context.Context,
req *types.QueryL1BlockInfoRequest,
) (*types.QueryL1BlockInfoResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand All @@ -25,7 +36,10 @@ func (k *Keeper) L1BlockInfo(ctx context.Context, req *types.QueryL1BlockInfoReq
}

// Params implements the Query/Params gRPC method
func (k *Keeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (k Keeper) Params( //nolint:gocritic // hugeParam
ctx context.Context,
req *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
Expand Down
12 changes: 10 additions & 2 deletions x/rollup/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
protov1 "github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand All @@ -29,6 +31,7 @@ import (
type ModuleInputs struct {
depinject.In

Config *modulev1.Module
Codec codec.Codec
StoreService store.KVStoreService
BankKeeper bankkeeper.Keeper
Expand Down Expand Up @@ -57,8 +60,13 @@ func ProvideCustomGetSigner() signing.CustomGetSigner {
}

func ProvideModule(in ModuleInputs) ModuleOutputs { //nolint:gocritic // hugeParam
// TODO: figure out correct depinject for authority
k := keeper.NewKeeper(in.Codec, in.StoreService, sdk.AccAddress{}, in.BankKeeper, in.AccountKeeper)
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
if in.Config.Authority != "" {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}

k := keeper.NewKeeper(in.Codec, in.StoreService, authority, in.BankKeeper, in.AccountKeeper)
return ModuleOutputs{
Keeper: k,
Module: NewAppModule(in.Codec, k),
Expand Down
45 changes: 35 additions & 10 deletions x/rollup/tests/integration/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const initialFeeCollectorBalance = 1_000_000

func TestRollup(t *testing.T) {
integrationApp, feeCollectorAddr := setupIntegrationApp(t)
queryClient := banktypes.NewQueryClient(integrationApp.QueryHelper())
bankQueryClient := banktypes.NewQueryClient(integrationApp.QueryHelper())
rollupQueryClient := rolluptypes.NewQueryClient(integrationApp.QueryHelper())

erc20tokenAddr := common.HexToAddress("0xabcdef123456")
erc20userAddr := common.HexToAddress("0x123456abcdef")
Expand All @@ -61,15 +62,15 @@ func TestRollup(t *testing.T) {
require.NoError(t, err)

// query the mint address ETH balance and assert it's zero
require.Equal(t, math.ZeroInt(), queryETHBalance(t, queryClient, mintAddr, integrationApp))
require.Equal(t, math.ZeroInt(), queryETHBalance(t, bankQueryClient, mintAddr, integrationApp))

// query the recipient address ETH balance and assert it's zero
require.Equal(t, math.ZeroInt(), queryETHBalance(t, queryClient, userCosmosAddr, integrationApp))
require.Equal(t, math.ZeroInt(), queryETHBalance(t, bankQueryClient, userCosmosAddr, integrationApp))

// query the user's ERC20 balance and assert it's zero
erc20userCosmosAddr, err := monomer.CosmosETHAddress(erc20userAddr).Encode(sdk.GetConfig().GetBech32AccountAddrPrefix())
require.NoError(t, err)
require.Equal(t, math.ZeroInt(), queryERC20Balance(t, queryClient, erc20userCosmosAddr, erc20tokenAddr, integrationApp))
require.Equal(t, math.ZeroInt(), queryERC20Balance(t, bankQueryClient, erc20userCosmosAddr, erc20tokenAddr, integrationApp))

// send an invalid MsgApplyL1Txs and assert error
_, err = integrationApp.RunMsg(&rolluptypes.MsgApplyL1Txs{
Expand All @@ -84,13 +85,13 @@ func TestRollup(t *testing.T) {
require.NoError(t, err)

// query the mint address ETH balance and assert it's equal to the mint amount minus the transfer amount
require.Equal(t, new(big.Int).Sub(mintAmount, transferAmount), queryETHBalance(t, queryClient, mintAddr, integrationApp).BigInt())
require.Equal(t, new(big.Int).Sub(mintAmount, transferAmount), queryETHBalance(t, bankQueryClient, mintAddr, integrationApp).BigInt())

// query the recipient address ETH balance and assert it's equal to the transfer amount
require.Equal(t, transferAmount, queryETHBalance(t, queryClient, userCosmosAddr, integrationApp).BigInt())
require.Equal(t, transferAmount, queryETHBalance(t, bankQueryClient, userCosmosAddr, integrationApp).BigInt())

// query the user's ERC20 balance and assert it's equal to the deposit amount
require.Equal(t, erc20depositAmount, queryERC20Balance(t, queryClient, erc20userCosmosAddr, erc20tokenAddr, integrationApp).BigInt())
require.Equal(t, erc20depositAmount, queryERC20Balance(t, bankQueryClient, erc20userCosmosAddr, erc20tokenAddr, integrationApp).BigInt())

// try to withdraw more than deposited and assert error
_, err = integrationApp.RunMsg(&rolluptypes.MsgInitiateWithdrawal{
Expand All @@ -113,10 +114,10 @@ func TestRollup(t *testing.T) {
require.NoError(t, err)

// query the recipient address ETH balance and assert it's zero
require.Equal(t, math.ZeroInt(), queryETHBalance(t, queryClient, userCosmosAddr, integrationApp))
require.Equal(t, math.ZeroInt(), queryETHBalance(t, bankQueryClient, userCosmosAddr, integrationApp))

// query the fee collector's ETH balance and assert it's equal to the initial mint amount
require.Equal(t, math.NewInt(initialFeeCollectorBalance), queryETHBalance(t, queryClient, feeCollectorAddr, integrationApp))
require.Equal(t, math.NewInt(initialFeeCollectorBalance), queryETHBalance(t, bankQueryClient, feeCollectorAddr, integrationApp))

// send a successful MsgInitiateFeeWithdrawal
_, err = integrationApp.RunMsg(&rolluptypes.MsgInitiateFeeWithdrawal{
Expand All @@ -125,7 +126,30 @@ func TestRollup(t *testing.T) {
require.NoError(t, err)

// query the fee collector's ETH balance and assert it's zero
require.Equal(t, math.ZeroInt(), queryETHBalance(t, queryClient, feeCollectorAddr, integrationApp))
require.Equal(t, math.ZeroInt(), queryETHBalance(t, bankQueryClient, feeCollectorAddr, integrationApp))

// query the current x/rollup params and assert they're the default params
paramsResp, err := rollupQueryClient.Params(integrationApp.Context(), &rolluptypes.QueryParamsRequest{})
require.NoError(t, err)
require.Equal(t, rolluptypes.DefaultParams(), paramsResp.Params)

// send a successful MsgUpdateParams
updatedParams := rolluptypes.Params{
L1FeeRecipient: common.HexToAddress("0x123456abcdef").String(),
L1CrossDomainMessenger: common.HexToAddress("0xabcdef123456").String(),
MinFeeWithdrawalAmount: 5,
FeeWithdrawalGasLimit: 5,
}
_, err = integrationApp.RunMsg(&rolluptypes.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: updatedParams,
})
require.NoError(t, err)

// query the updated x/rollup params and assert they're the updated params
updatedParamsResp, err := rollupQueryClient.Params(integrationApp.Context(), &rolluptypes.QueryParamsRequest{})
require.NoError(t, err)
require.Equal(t, updatedParams, updatedParamsResp.Params)
}

func setupIntegrationApp(t *testing.T) (*integration.App, string) {
Expand Down Expand Up @@ -187,6 +211,7 @@ func setupIntegrationApp(t *testing.T) (*integration.App, string) {
},
)
rolluptypes.RegisterMsgServer(integrationApp.MsgServiceRouter(), rollupKeeper)
rolluptypes.RegisterQueryServer(integrationApp.QueryHelper(), rollupkeeper.NewQuerier(rollupKeeper))
banktypes.RegisterQueryServer(integrationApp.QueryHelper(), bankkeeper.NewQuerier(&bankKeeper))

return integrationApp, accountKeeper.GetModuleAddress(authtypes.FeeCollectorName).String()
Expand Down

0 comments on commit 5f3f935

Please sign in to comment.