Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple monitor module #274

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ import (
"github.com/babylonchain/babylon/x/epoching"
epochingkeeper "github.com/babylonchain/babylon/x/epoching/keeper"
epochingtypes "github.com/babylonchain/babylon/x/epoching/types"
"github.com/babylonchain/babylon/x/monitor"
monitorkeeper "github.com/babylonchain/babylon/x/monitor/keeper"
monitortypes "github.com/babylonchain/babylon/x/monitor/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand Down Expand Up @@ -162,6 +165,7 @@ var (
btclightclient.AppModuleBasic{},
btccheckpoint.AppModuleBasic{},
checkpointing.AppModuleBasic{},
monitor.AppModuleBasic{},

// IBC-related
ibc.AppModuleBasic{},
Expand Down Expand Up @@ -225,6 +229,7 @@ type BabylonApp struct {
BTCLightClientKeeper btclightclientkeeper.Keeper
BtcCheckpointKeeper btccheckpointkeeper.Keeper
CheckpointingKeeper checkpointingkeeper.Keeper
MonitorKeeper monitorkeeper.Keeper

// IBC-related modules
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
Expand Down Expand Up @@ -287,6 +292,7 @@ func NewBabylonApp(
btclightclienttypes.StoreKey,
btccheckpointtypes.StoreKey,
checkpointingtypes.StoreKey,
monitortypes.StoreKey,
// IBC-related modules
ibchost.StoreKey,
ibctransfertypes.StoreKey,
Expand Down Expand Up @@ -467,14 +473,6 @@ func NewBabylonApp(
// No more routes can be added
app.IBCKeeper.SetRouter(ibcRouter)

// add msgServiceRouter so that the epoching module can forward unwrapped messages to the staking module
epochingKeeper.SetMsgServiceRouter(app.BaseApp.MsgServiceRouter())
// make ZoneConcierge to subscribe to the epoching's hooks
epochingKeeper.SetHooks(
epochingtypes.NewMultiEpochingHooks(app.ZoneConciergeKeeper.Hooks()),
)
app.EpochingKeeper = epochingKeeper

btclightclientKeeper := *btclightclientkeeper.NewKeeper(
appCodec,
keys[btclightclienttypes.StoreKey],
Expand All @@ -483,6 +481,22 @@ func NewBabylonApp(
btcConfig,
)

app.MonitorKeeper = monitorkeeper.NewKeeper(
appCodec,
keys[monitortypes.StoreKey],
keys[monitortypes.StoreKey],
app.GetSubspace(monitortypes.ModuleName),
&btclightclientKeeper,
)

// add msgServiceRouter so that the epoching module can forward unwrapped messages to the staking module
epochingKeeper.SetMsgServiceRouter(app.BaseApp.MsgServiceRouter())
// make ZoneConcierge to subscribe to the epoching's hooks
epochingKeeper.SetHooks(
epochingtypes.NewMultiEpochingHooks(app.ZoneConciergeKeeper.Hooks(), app.MonitorKeeper.Hooks()),
)
app.EpochingKeeper = epochingKeeper

checkpointingKeeper :=
checkpointingkeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -559,6 +573,7 @@ func NewBabylonApp(
btclightclient.NewAppModule(appCodec, app.BTCLightClientKeeper, app.AccountKeeper, app.BankKeeper),
btccheckpoint.NewAppModule(appCodec, app.BtcCheckpointKeeper, app.AccountKeeper, app.BankKeeper),
checkpointing.NewAppModule(appCodec, app.CheckpointingKeeper, app.AccountKeeper, app.BankKeeper),
monitor.NewAppModule(appCodec, app.MonitorKeeper, app.AccountKeeper, app.BankKeeper),
// IBC-related modules
ibc.NewAppModule(app.IBCKeeper),
transferModule,
Expand All @@ -581,6 +596,7 @@ func NewBabylonApp(
btclightclienttypes.ModuleName,
btccheckpointtypes.ModuleName,
checkpointingtypes.ModuleName,
monitortypes.ModuleName,
// IBC-related modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
Expand All @@ -603,6 +619,7 @@ func NewBabylonApp(
btclightclienttypes.ModuleName,
btccheckpointtypes.ModuleName,
checkpointingtypes.ModuleName,
monitortypes.ModuleName,
// IBC-related modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
Expand All @@ -627,6 +644,7 @@ func NewBabylonApp(
btclightclienttypes.ModuleName,
btccheckpointtypes.ModuleName,
checkpointingtypes.ModuleName,
monitortypes.ModuleName,
// IBC-related modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
Expand Down Expand Up @@ -666,6 +684,7 @@ func NewBabylonApp(
btclightclient.NewAppModule(appCodec, app.BTCLightClientKeeper, app.AccountKeeper, app.BankKeeper),
btccheckpoint.NewAppModule(appCodec, app.BtcCheckpointKeeper, app.AccountKeeper, app.BankKeeper),
checkpointing.NewAppModule(appCodec, app.CheckpointingKeeper, app.AccountKeeper, app.BankKeeper),
monitor.NewAppModule(appCodec, app.MonitorKeeper, app.AccountKeeper, app.BankKeeper),
// IBC-related modules
ibc.NewAppModule(app.IBCKeeper),
transferModule,
Expand Down Expand Up @@ -898,6 +917,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(btclightclienttypes.ModuleName)
paramsKeeper.Subspace(btccheckpointtypes.ModuleName)
paramsKeeper.Subspace(checkpointingtypes.ModuleName)
paramsKeeper.Subspace(monitortypes.ModuleName)
// IBC-related modules
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
Expand Down
10 changes: 10 additions & 0 deletions proto/babylon/monitor/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package babylon.monitor.v1;

import "gogoproto/gogo.proto";
import "babylon/monitor/params.proto";

option go_package = "github.com/babylonchain/babylon/x/monitor/types";

// GenesisState defines the monitor module's genesis state.
message GenesisState { Params params = 1 [ (gogoproto.nullable) = false ]; }
11 changes: 11 additions & 0 deletions proto/babylon/monitor/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package babylon.monitor.v1;

import "gogoproto/gogo.proto";

option go_package = "github.com/babylonchain/babylon/x/monitor/types";

// Params defines the parameters for the module.
message Params {
option (gogoproto.equal) = true;
}
40 changes: 40 additions & 0 deletions proto/babylon/monitor/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";
package babylon.monitor.v1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "babylon/monitor/params.proto";

option go_package = "github.com/babylonchain/babylon/x/monitor/types";

// Query defines the gRPC querier service.
service Query {
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/babylon/monitor/v1/params";
}

// FinishedEpochBtcHeight btc light client height at provided epoch finish
rpc FinishedEpochBtcHeight(QueryFinishedEpochBtcHeightRequest) returns (QueryFinishedEpochBtcHeightResponse) {
option (google.api.http).get = "/babylon/monitor/v1/{epoch_num}";
}
}

// 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 of this module.
Params params = 1 [ (gogoproto.nullable) = false ];
}

message QueryFinishedEpochBtcHeightRequest {
uint64 epoch_num = 1;
}

message QueryFinishedEpochBtcHeightResponse {
// height of btc ligh client when epoch ended
uint64 btc_light_client_height = 1;
}
23 changes: 23 additions & 0 deletions test/e2e/configurer/chain/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/babylonchain/babylon/test/e2e/util"
blc "github.com/babylonchain/babylon/x/btclightclient/types"
ct "github.com/babylonchain/babylon/x/checkpointing/types"
etypes "github.com/babylonchain/babylon/x/epoching/types"
mtypes "github.com/babylonchain/babylon/x/monitor/types"
zctypes "github.com/babylonchain/babylon/x/zoneconcierge/types"
)

Expand Down Expand Up @@ -217,3 +219,24 @@ func (n *NodeConfig) QueryCheckpointChainInfo(chainId string) (*zctypes.ChainInf
}
return infoResponse.ChainInfo, nil
}

func (n *NodeConfig) QueryCurrentEpoch() (uint64, error) {
bz, err := n.QueryGRPCGateway("/babylon/epoching/v1/current_epoch")
require.NoError(n.t, err)
var epochResponse etypes.QueryCurrentEpochResponse
if err := util.Cdc.UnmarshalJSON(bz, &epochResponse); err != nil {
return 0, err
}
return epochResponse.CurrentEpoch, nil
}

func (n *NodeConfig) QueryLightClientHeighEpochEnd(epoch uint64) (uint64, error) {
monitorPath := fmt.Sprintf("/babylon/monitor/v1/%d", epoch)
bz, err := n.QueryGRPCGateway(monitorPath)
require.NoError(n.t, err)
var mResponse mtypes.QueryFinishedEpochBtcHeightResponse
if err := util.Cdc.UnmarshalJSON(bz, &mResponse); err != nil {
return 0, err
}
return mResponse.BtcLightClientHeight, nil
}
13 changes: 13 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package e2e

import (
"fmt"

"github.com/babylonchain/babylon/test/e2e/initialization"
ct "github.com/babylonchain/babylon/x/checkpointing/types"
)
Expand Down Expand Up @@ -44,6 +46,17 @@ func (s *IntegrationTestSuite) TestIbcCheckpointing() {
s.Equal(fininfo.FinalizedChainInfo.ChainId, initialization.ChainBID)
s.Equal(fininfo.EpochInfo.EpochNumber, uint64(2))

currEpoch, err := nonValidatorNode.QueryCurrentEpoch()
s.NoError(err)

heightAtFinishedEpoch, err := nonValidatorNode.QueryLightClientHeighEpochEnd(currEpoch - 1)
s.NoError(err)

if heightAtFinishedEpoch == 0 {
// we can only assert, that btc lc height is larger than 0.
s.FailNow(fmt.Sprintf("Light client height should be > 0 on epoch %d", currEpoch-1))
}

chainB := s.configurer.GetChainConfig(1)
_, err = chainB.GetDefaultNode()
s.NoError(err)
Expand Down
4 changes: 4 additions & 0 deletions x/btclightclient/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,7 @@ func (k Keeper) IsAncestor(ctx sdk.Context, parentHashBytes *bbn.BTCHeaderHashBy
// Return whether the last element of the ancestry is equal to the parent
return ancestry[len(ancestry)-1].Eq(parentHeader), nil
}

func (k Keeper) GetTipInfo(ctx sdk.Context) *types.BTCHeaderInfo {
return k.headersState(ctx).GetTip()
}
25 changes: 25 additions & 0 deletions x/monitor/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"

"github.com/babylonchain/babylon/x/monitor/types"
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string) *cobra.Command {
// Group monitor queries under a subcommand
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

return cmd
}
34 changes: 34 additions & 0 deletions x/monitor/client/cli/query_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cli

import (
"context"

"github.com/babylonchain/babylon/x/monitor/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
)

func CmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Short: "shows the parameters of the module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
24 changes: 24 additions & 0 deletions x/monitor/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"

"github.com/babylonchain/babylon/x/monitor/types"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

return cmd
}
21 changes: 21 additions & 0 deletions x/monitor/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package monitor

import (
"github.com/babylonchain/babylon/x/monitor/keeper"
"github.com/babylonchain/babylon/x/monitor/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)

return genesis
}
33 changes: 33 additions & 0 deletions x/monitor/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package monitor_test

import (
"testing"

"github.com/babylonchain/babylon/x/monitor"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

simapp "github.com/babylonchain/babylon/app"
"github.com/babylonchain/babylon/x/monitor/types"
)

func TestExportGenesis(t *testing.T) {
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

app.MonitorKeeper.SetParams(ctx, types.DefaultParams())
genesisState := monitor.ExportGenesis(ctx, app.MonitorKeeper)
require.Equal(t, genesisState.Params, types.DefaultParams())
}

func TestInitGenesis(t *testing.T) {
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

genesisState := types.GenesisState{
Params: types.Params{},
}

monitor.InitGenesis(ctx, app.MonitorKeeper, genesisState)
require.Equal(t, app.MonitorKeeper.GetParams(ctx), genesisState.Params)
}
Loading