Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Jan 22, 2024
1 parent 5ed5054 commit d9f528e
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 64 deletions.
16 changes: 13 additions & 3 deletions app/monitored/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/register/app.go → app/registry/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ import (

const (
AccountAddressPrefix = "cosmos"
Name = "registe"
Name = "registry"
)

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions bin/run_test_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
2 changes: 1 addition & 1 deletion cmd/registryd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
sdk "github.com/cosmos/cosmos-sdk/types"

app "healthcheck/app/register"
app "healthcheck/app/registry"
)

func initSDKConfig() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/registryd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/registryd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
6 changes: 3 additions & 3 deletions x/healthcheck/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)

Expand Down
14 changes: 7 additions & 7 deletions x/healthcheck/keeper/monitored_chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -42,15 +42,15 @@ 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,
))
}

// 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()
Expand All @@ -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))
Expand All @@ -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,
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion x/healthcheck/keeper/query_monitored_chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions x/healthcheck/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/healthcheck/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 9 additions & 23 deletions x/healthcheck/types/key_monitored_chains.go
Original file line number Diff line number Diff line change
@@ -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)...)
}
1 change: 1 addition & 0 deletions x/healthcheck/types/messages_monitored_chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewMsgCreateMonitoredChain(

) *MsgCreateMonitoredChain {
return &MsgCreateMonitoredChain{
Creator: creator,
ChainId: chainId,
ConnectionId: connectionId,
}
Expand Down
2 changes: 1 addition & 1 deletion x/monitored/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/monitored/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down
10 changes: 3 additions & 7 deletions x/monitored/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
)

const (
DefaultTimeoutInterval uint64 = 2
DefaultUpdateInterval uint64 = 6
DefaultTimeoutInterval uint64 = 20
DefaultUpdateInterval uint64 = 10
DefaultPacketSendInterval uint64 = 5
)

type IBCModule struct {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion x/monitored/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d9f528e

Please sign in to comment.