From d9f528eceff225b8aecd4e5288ad9dceb5aed41a Mon Sep 17 00:00:00 2001 From: stana-ethernal Date: Mon, 22 Jan 2024 11:03:57 +0100 Subject: [PATCH] minor fixes --- app/monitored/app.go | 16 ++++++++-- app/{register => registry}/app.go | 2 +- app/{register => registry}/encoding.go | 0 app/{register => registry}/export.go | 0 app/{register => registry}/genesis.go | 0 bin/run_test_environment.sh | 4 +-- cmd/registryd/cmd/config.go | 2 +- cmd/registryd/cmd/root.go | 2 +- cmd/registryd/main.go | 2 +- x/healthcheck/keeper/keeper.go | 6 ++-- x/healthcheck/keeper/monitored_chains.go | 14 ++++---- .../keeper/query_monitored_chains.go | 2 +- x/healthcheck/module.go | 22 +++++++------ x/healthcheck/module_ibc.go | 2 +- x/healthcheck/types/key_monitored_chains.go | 32 ++++++------------- .../types/messages_monitored_chains.go | 1 + x/monitored/keeper/keeper.go | 2 +- x/monitored/module.go | 2 +- x/monitored/module_ibc.go | 10 ++---- x/monitored/types/keys.go | 2 +- 20 files changed, 59 insertions(+), 64 deletions(-) rename app/{register => registry}/app.go (99%) rename app/{register => registry}/encoding.go (100%) rename app/{register => registry}/export.go (100%) rename app/{register => registry}/genesis.go (100%) diff --git a/app/monitored/app.go b/app/monitored/app.go index 87db85b..ee4a4eb 100644 --- a/app/monitored/app.go +++ b/app/monitored/app.go @@ -239,9 +239,8 @@ type App struct { ScopedTransferKeeper capabilitykeeper.ScopedKeeper ScopedICAHostKeeper capabilitykeeper.ScopedKeeper - ScopedHealthcheckKeeper capabilitykeeper.ScopedKeeper - ScopedMonitoredKeeper capabilitykeeper.ScopedKeeper - MonitoredKeeper monitoredmodulekeeper.Keeper + ScopedMonitoredKeeper capabilitykeeper.ScopedKeeper + MonitoredKeeper monitoredmodulekeeper.Keeper // this line is used by starport scaffolding # stargate/app/keeperDeclaration // mm is the module manager @@ -498,6 +497,17 @@ func New( govConfig, ) + scopedMonitoredKeeper := app.CapabilityKeeper.ScopeToModule(monitoredmoduletypes.ModuleName) + app.ScopedMonitoredKeeper = scopedMonitoredKeeper + app.MonitoredKeeper = *monitoredmodulekeeper.NewKeeper( + appCodec, + keys[monitoredmoduletypes.StoreKey], + keys[monitoredmoduletypes.MemStoreKey], + app.GetSubspace(monitoredmoduletypes.ModuleName), + app.IBCKeeper.ChannelKeeper, + &app.IBCKeeper.PortKeeper, + scopedMonitoredKeeper, + ) monitoredModule := monitoredmodule.NewAppModule(appCodec, app.MonitoredKeeper, app.AccountKeeper, app.BankKeeper) monitoredIBCModule := monitoredmodule.NewIBCModule(app.MonitoredKeeper) diff --git a/app/register/app.go b/app/registry/app.go similarity index 99% rename from app/register/app.go rename to app/registry/app.go index ca628cc..af5d612 100644 --- a/app/register/app.go +++ b/app/registry/app.go @@ -115,7 +115,7 @@ import ( const ( AccountAddressPrefix = "cosmos" - Name = "registe" + Name = "registry" ) // this line is used by starport scaffolding # stargate/wasm/app/enabledProposals diff --git a/app/register/encoding.go b/app/registry/encoding.go similarity index 100% rename from app/register/encoding.go rename to app/registry/encoding.go diff --git a/app/register/export.go b/app/registry/export.go similarity index 100% rename from app/register/export.go rename to app/registry/export.go diff --git a/app/register/genesis.go b/app/registry/genesis.go similarity index 100% rename from app/register/genesis.go rename to app/registry/genesis.go diff --git a/bin/run_test_environment.sh b/bin/run_test_environment.sh index f8f3e13..2ef235b 100644 --- a/bin/run_test_environment.sh +++ b/bin/run_test_environment.sh @@ -196,9 +196,9 @@ sleep 1 hermes create connection --a-chain $MONITORED_CHAIN_ID --b-chain $REGISTRY_CHAIN_ID sleep 1 -./$REGISTRY_BINARY tx healthcheck create-chain $MONITORED_CHAIN_ID connection-0 --from $VALIDATOR -y +./$REGISTRY_BINARY tx healthcheck create-monitored-chains $MONITORED_CHAIN_ID connection-0 --from $VALIDATOR -y -hermes create channel --a-chain $MONITORED_CHAIN_ID --a-connection connection-0 --a-port monitored --b-port healthcheck --channel-version 1 --order ordered +hermes create channel --a-chain $MONITORED_CHAIN_ID --a-connection connection-0 --a-port monitored --b-port healthcheck --channel-version 1 sleep 1 hermes start &> ~/.hermes/logs & \ No newline at end of file diff --git a/cmd/registryd/cmd/config.go b/cmd/registryd/cmd/config.go index 2c15ebb..fb21779 100644 --- a/cmd/registryd/cmd/config.go +++ b/cmd/registryd/cmd/config.go @@ -3,7 +3,7 @@ package cmd import ( sdk "github.com/cosmos/cosmos-sdk/types" - app "healthcheck/app/register" + app "healthcheck/app/registry" ) func initSDKConfig() { diff --git a/cmd/registryd/cmd/root.go b/cmd/registryd/cmd/root.go index 6106177..97652a5 100644 --- a/cmd/registryd/cmd/root.go +++ b/cmd/registryd/cmd/root.go @@ -37,7 +37,7 @@ import ( // this line is used by starport scaffolding # root/moduleImport appparams "healthcheck/app/params" - app "healthcheck/app/register" + app "healthcheck/app/registry" ) // NewRootCmd creates a new root command for a Cosmos SDK application diff --git a/cmd/registryd/main.go b/cmd/registryd/main.go index fe3e6a9..4811ccb 100644 --- a/cmd/registryd/main.go +++ b/cmd/registryd/main.go @@ -6,7 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/server" svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" - app "healthcheck/app/register" + app "healthcheck/app/registry" "healthcheck/cmd/registryd/cmd" ) diff --git a/x/healthcheck/keeper/keeper.go b/x/healthcheck/keeper/keeper.go index bc59d36..d125977 100644 --- a/x/healthcheck/keeper/keeper.go +++ b/x/healthcheck/keeper/keeper.go @@ -145,7 +145,7 @@ func (k Keeper) GetClientChainIdFromConnection(ctx sdk.Context, connectionID str return chainID, nil } -func (k Keeper) OnRecvHealthcheckPacket(ctx sdk.Context, channelID string, packetData commonTypes.HealthcheckPacketData) exported.Acknowledgement { +func (k Keeper) OnRecvHealthcheckPacket(ctx sdk.Context, channelID string, packetData commonTypes.HealthcheckUpdateData) exported.Acknowledgement { chainID, err := k.GetClientChainIdFromChannel(ctx, channelID) if err != nil { panic(fmt.Errorf("cannot get client id from chainnel: %s", chainID)) @@ -156,8 +156,8 @@ func (k Keeper) OnRecvHealthcheckPacket(ctx sdk.Context, channelID string, packe panic(fmt.Errorf("monitored chain not registered: %s", chainID)) } - chain.Status.Block = packetData.GetHealtcheckUpdate().Block - chain.Status.Timestamp = packetData.GetHealtcheckUpdate().Timestamp + chain.Status.Block = packetData.Block + chain.Status.Timestamp = packetData.Timestamp chain.Status.RegistryBlockHeight = uint64(ctx.BlockHeader().Height) chain.Status.Status = string(types.Active) diff --git a/x/healthcheck/keeper/monitored_chains.go b/x/healthcheck/keeper/monitored_chains.go index 6c50310..5933e90 100644 --- a/x/healthcheck/keeper/monitored_chains.go +++ b/x/healthcheck/keeper/monitored_chains.go @@ -10,7 +10,7 @@ import ( // SetMonitoredChain set a specific MonitoredChain in the store from its index func (k Keeper) SetMonitoredChain(ctx sdk.Context, MonitoredChain types.MonitoredChain) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MonitoredChainKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.MonitoredChainKeyPrefix))) b := k.cdc.MustMarshal(&MonitoredChain) store.Set(types.MonitoredChainKey( MonitoredChain.ChainId, @@ -23,7 +23,7 @@ func (k Keeper) GetMonitoredChain( chainId string, ) (val types.MonitoredChain, found bool) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MonitoredChainKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.MonitoredChainKeyPrefix))) b := store.Get(types.MonitoredChainKey( chainId, @@ -42,7 +42,7 @@ func (k Keeper) RemoveMonitoredChain( chainId string, ) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MonitoredChainKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.MonitoredChainKeyPrefix))) store.Delete(types.MonitoredChainKey( chainId, )) @@ -50,7 +50,7 @@ func (k Keeper) RemoveMonitoredChain( // GetAllMonitoredChain returns all MonitoredChain func (k Keeper) GetAllMonitoredChain(ctx sdk.Context) (list []types.MonitoredChain) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MonitoredChainKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.MonitoredChainKeyPrefix))) iterator := sdk.KVStorePrefixIterator(store, []byte{}) defer iterator.Close() @@ -66,7 +66,7 @@ func (k Keeper) GetAllMonitoredChain(ctx sdk.Context) (list []types.MonitoredCha // SetChainToChannelMap sets the mapping from a monitored chainID to the channel ID for that monitored chain. func (k Keeper) SetChainToChannelMap(ctx sdk.Context, chainID, channelID string) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainToChannelKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.ChainToChannelKeyPrefix))) store.Set(types.ChainToChannelKey( chainID, ), []byte(channelID)) @@ -76,7 +76,7 @@ func (k Keeper) SetChainToChannelMap(ctx sdk.Context, chainID, channelID string) // GetChainToChannelMap gets the channelID for the given moniotred chainID func (k Keeper) GetChainToChannelMap(ctx sdk.Context, chainID string) (string, bool) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainToChannelKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.ChainToChannelKeyPrefix))) bz := store.Get(types.ChainToChannelKey( chainID, @@ -90,7 +90,7 @@ func (k Keeper) GetChainToChannelMap(ctx sdk.Context, chainID string) (string, b // RemoveChanFromChainToChannelMap removes the channelID for the given moniotred chainID func (k Keeper) RemoveChanFromChainToChannelMap(ctx sdk.Context, chainID string) { - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ChainToChannelKeyPrefix)) + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(string(types.ChainToChannelKeyPrefix))) store.Delete(types.ChainToChannelKey(chainID)) } diff --git a/x/healthcheck/keeper/query_monitored_chains.go b/x/healthcheck/keeper/query_monitored_chains.go index e7e8361..f08842f 100644 --- a/x/healthcheck/keeper/query_monitored_chains.go +++ b/x/healthcheck/keeper/query_monitored_chains.go @@ -21,7 +21,7 @@ func (k Keeper) MonitoredChainAll(goCtx context.Context, req *types.QueryAllMoni ctx := sdk.UnwrapSDKContext(goCtx) store := ctx.KVStore(k.storeKey) - MonitoredChainStore := prefix.NewStore(store, types.KeyPrefix(types.MonitoredChainKeyPrefix)) + MonitoredChainStore := prefix.NewStore(store, types.KeyPrefix(string(types.MonitoredChainKeyPrefix))) pageRes, err := query.Paginate(MonitoredChainStore, req.Pagination, func(key []byte, value []byte) error { var MonitoredChain types.MonitoredChain diff --git a/x/healthcheck/module.go b/x/healthcheck/module.go index 443dee1..78a6721 100644 --- a/x/healthcheck/module.go +++ b/x/healthcheck/module.go @@ -163,19 +163,21 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val curentBlock := uint64(ctx.BlockHeader().Height) for _, chain := range chains { - if am.keeper.IsMonitoredChanOpen(ctx, chain.ChainId) { + if chain.Status != nil { lastCheckinBlock := chain.Status.RegistryBlockHeight - if curentBlock > lastCheckinBlock+chain.UpdateInterval { - if curentBlock > lastCheckinBlock+chain.TimeoutInterval+chain.UpdateInterval { - channelID, ok := am.keeper.GetChainToChannelMap(ctx, chain.ChainId) - if ok { - am.keeper.CloseChannel(ctx, channelID) - am.keeper.RemoveChanFromChainToChannelMap(ctx, channelID) + if am.keeper.IsMonitoredChanOpen(ctx, chain.ChainId) && lastCheckinBlock > 0 { + if curentBlock > lastCheckinBlock+chain.UpdateInterval { + if curentBlock > lastCheckinBlock+chain.TimeoutInterval+chain.UpdateInterval { + channelID, ok := am.keeper.GetChainToChannelMap(ctx, chain.ChainId) + if ok { + am.keeper.CloseChannel(ctx, channelID) + am.keeper.RemoveChanFromChainToChannelMap(ctx, channelID) + } } - } - chain.Status.Status = string(types.Inactive) - am.keeper.SetMonitoredChain(ctx, chain) + chain.Status.Status = string(types.Inactive) + am.keeper.SetMonitoredChain(ctx, chain) + } } } } diff --git a/x/healthcheck/module_ibc.go b/x/healthcheck/module_ibc.go index 84c9205..7710d78 100644 --- a/x/healthcheck/module_ibc.go +++ b/x/healthcheck/module_ibc.go @@ -141,7 +141,7 @@ func (im IBCModule) OnRecvPacket( // this line is used by starport scaffolding # oracle/packet/module/recv - var packetData commonTypes.HealthcheckPacketData + var packetData commonTypes.HealthcheckUpdateData if err := types.ModuleCdc.UnmarshalJSON(modulePacket.GetData(), &packetData); err != nil { return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error())) } else { diff --git a/x/healthcheck/types/key_monitored_chains.go b/x/healthcheck/types/key_monitored_chains.go index b25a346..0287e3f 100644 --- a/x/healthcheck/types/key_monitored_chains.go +++ b/x/healthcheck/types/key_monitored_chains.go @@ -1,38 +1,24 @@ package types -import "encoding/binary" +import ( + "encoding/binary" +) var _ binary.ByteOrder const ( // MonitoredChainKeyPrefix is the prefix to retrieve all MonitoredChain - MonitoredChainKeyPrefix = "MonitoredChain/value/" + MonitoredChainKeyPrefix byte = iota // ChainToChannelKeyPrefix is the prefix to retrieve channel by monitored chain id - ChainToChannelKeyPrefix = "ChainToChannel/value/" + ChainToChannelKeyPrefix ) // MonitoredChainKey returns the store key to retrieve a MonitoredChain from the index fields -func MonitoredChainKey( - chainId string, -) []byte { - var key []byte - - chainIdBytes := []byte(chainId) - key = append(key, chainIdBytes...) - key = append(key, []byte("/")...) - - return key +func MonitoredChainKey(chainId string) []byte { + return append([]byte{MonitoredChainKeyPrefix}, []byte(chainId)...) } // ChainToChannelKey returns the store key to retrieve a channel of the MonitoredChain from the index fields -func ChainToChannelKey( - chainId string, -) []byte { - var key []byte - - chainIdBytes := []byte(chainId) - key = append(key, chainIdBytes...) - key = append(key, []byte("/")...) - - return key +func ChainToChannelKey(chainId string) []byte { + return append([]byte{ChainToChannelKeyPrefix}, []byte(chainId)...) } diff --git a/x/healthcheck/types/messages_monitored_chains.go b/x/healthcheck/types/messages_monitored_chains.go index d220fa4..c4cfb8a 100644 --- a/x/healthcheck/types/messages_monitored_chains.go +++ b/x/healthcheck/types/messages_monitored_chains.go @@ -20,6 +20,7 @@ func NewMsgCreateMonitoredChain( ) *MsgCreateMonitoredChain { return &MsgCreateMonitoredChain{ + Creator: creator, ChainId: chainId, ConnectionId: connectionId, } diff --git a/x/monitored/keeper/keeper.go b/x/monitored/keeper/keeper.go index 28f47a7..4d362bd 100644 --- a/x/monitored/keeper/keeper.go +++ b/x/monitored/keeper/keeper.go @@ -90,7 +90,7 @@ func (k Keeper) GetLastCheckin(ctx sdk.Context) (uint64, bool) { if bz == nil { return 0, false } - return sdk.BigEndianToUint64(bz), false + return sdk.BigEndianToUint64(bz), true } // SetLastCheckin sets the the last checkin info diff --git a/x/monitored/module.go b/x/monitored/module.go index 5441b81..7b0c883 100644 --- a/x/monitored/module.go +++ b/x/monitored/module.go @@ -169,7 +169,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, block abci.RequestEndBlock) []abci // if update interval is not reached, dont send packets lastChekingBlock, ok := am.keeper.GetLastCheckin(ctx) - if ok && lastChekingBlock > uint64(block.Height)-uint64(DefaultUpdateInterval) { + if ok && lastChekingBlock > uint64(block.Height)-uint64(DefaultPacketSendInterval) { return []abci.ValidatorUpdate{} } diff --git a/x/monitored/module_ibc.go b/x/monitored/module_ibc.go index 10b5c05..2d486b9 100644 --- a/x/monitored/module_ibc.go +++ b/x/monitored/module_ibc.go @@ -18,8 +18,9 @@ import ( ) const ( - DefaultTimeoutInterval uint64 = 2 - DefaultUpdateInterval uint64 = 6 + DefaultTimeoutInterval uint64 = 20 + DefaultUpdateInterval uint64 = 10 + DefaultPacketSendInterval uint64 = 5 ) type IBCModule struct { @@ -170,11 +171,6 @@ func (im IBCModule) OnAcknowledgementPacket( // this line is used by starport scaffolding # oracle/packet/module/ack - var modulePacketData commonTypes.HealthcheckPacketData - if err := modulePacketData.Unmarshal(modulePacket.GetData()); err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()) - } - var eventType string ctx.EventManager().EmitEvent( diff --git a/x/monitored/types/keys.go b/x/monitored/types/keys.go index 53fdaeb..2c97ffc 100644 --- a/x/monitored/types/keys.go +++ b/x/monitored/types/keys.go @@ -20,7 +20,7 @@ var ( // HealthcheckChannelKey defines the key to store the channel for communication with the healthcheck chain HealthcheckChannelKey = KeyPrefix("health-check-channel") // LAstCheckinKey defines the key to store the last checkin infromation in the store - LastCheckinKey = KeyPrefix("health-check-channel") + LastCheckinKey = KeyPrefix("last-checkin") ) func KeyPrefix(p string) []byte {