Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet2019 committed Dec 18, 2023
1 parent a54dfa7 commit e2393f3
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 142 deletions.
12 changes: 6 additions & 6 deletions proto/pstake/ratesync/v1beta1/ratesync.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ message HostChain{
uint64 i_d = 1;
string chain_i_d = 2; //not really required, just easier readability
string connection_i_d = 3;
pstake.liquidstakeibc.v1beta1.ICAAccount icaAccount = 4 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
pstake.liquidstakeibc.v1beta1.ICAAccount i_c_a_account = 4 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
Feature features = 5 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}
message Feature{
Expand All @@ -35,6 +35,11 @@ enum InstantiationState{
INSTANTIATION_COMPLETED = 2;
}

enum FeatureType {
LIQUID_STAKE_IBC = 0;
LIQUID_STAKE = 1;
}

message LiquidStake{
FeatureType feature_type = 1;

Expand All @@ -48,7 +53,6 @@ message LiquidStake{
repeated string denoms = 5;

bool enabled = 6;

}

// aim to keep this smaller than 256 MaxCharLen in ICA memo.
Expand All @@ -57,7 +61,3 @@ message ICAMemo {
uint64 host_chain_i_d = 2;
}

enum FeatureType {
LIQUID_STAKE_IBC = 0;
LIQUID_STAKE = 1;
}
4 changes: 3 additions & 1 deletion x/liquidstakeibc/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (k *Keeper) OnChanOpenAck(
// get host chain
hc, found := k.GetHostChain(ctx, chainID)
if !found {
return fmt.Errorf("host chain with id %s is not registered", chainID)
// probably for non chain ica stack.
k.Logger(ctx).Info(fmt.Sprintf("liquidstakeibc host chain with id %s is not registered", chainID))
return nil
}

switch {
Expand Down
6 changes: 3 additions & 3 deletions x/ratesync/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) {

func (k *Keeper) DoRecreateICA(ctx sdk.Context, hc types.HostChain) {
// return early if any of the accounts is currently being recreated
if hc.IcaAccount.ChannelState == liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATING {
if hc.ICAAccount.ChannelState == liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATING {
return
}

// if the channel is closed, and it is not being recreated, recreate it
portID := types.MustICAPortIDFromOwner(hc.IcaAccount.Owner)
portID := types.MustICAPortIDFromOwner(hc.ICAAccount.Owner)
_, isActive := k.icaControllerKeeper.GetOpenActiveChannel(ctx, hc.ConnectionID, portID)
if !isActive {
if err := k.icaControllerKeeper.RegisterInterchainAccount(ctx, hc.ConnectionID, portID, ""); err != nil {
k.Logger(ctx).Error("error recreating %s ratesync ica: %w", hc.ChainID, err)
} else {
k.Logger(ctx).Info("Recreating ratesync ICA.", "chain", hc.ChainID)

hc.IcaAccount.ChannelState = liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATING
hc.ICAAccount.ChannelState = liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATING
k.SetHostChain(ctx, hc)
}
}
Expand Down
37 changes: 16 additions & 21 deletions x/ratesync/keeper/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// IncrementHostChainID increments and returns a unique ID for an unbonding operation
func (k Keeper) IncrementHostChainID(ctx sdk.Context) (hostChainID uint64) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.HostChainIDKey())
bz := store.Get(types.HostChainIDKeyPrefix)
if bz != nil {
hostChainID = binary.BigEndian.Uint64(bz)
}
Expand All @@ -23,14 +23,14 @@ func (k Keeper) IncrementHostChainID(ctx sdk.Context) (hostChainID uint64) {
bz = make([]byte, 8)
binary.BigEndian.PutUint64(bz, hostChainID)

store.Set(types.HostChainIDKey(), bz)
store.Set(types.HostChainIDKeyPrefix, bz)

return hostChainID
}

// SetHostChain set a specific chain in the store from its index
func (k Keeper) SetHostChain(ctx sdk.Context, chain types.HostChain) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKeyPrefix))
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.HostChainKeyPrefix)
b := k.cdc.MustMarshal(&chain)
store.Set(types.HostChainKey(
chain.ID,
Expand All @@ -43,7 +43,7 @@ func (k Keeper) GetHostChain(
id uint64,

) (val types.HostChain, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKeyPrefix))
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.HostChainKeyPrefix)

b := store.Get(types.HostChainKey(
id,
Expand All @@ -60,47 +60,42 @@ func (k Keeper) GetHostChain(
func (k Keeper) GetHostChainsByChainID(
ctx sdk.Context,
chainID string,
) (vals []types.HostChain) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
) []types.HostChain {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.HostChainKeyPrefix)
iterator := sdk.KVStorePrefixIterator(store, nil)

defer iterator.Close()

var vals []types.HostChain
for ; iterator.Valid(); iterator.Next() {
var val types.HostChain
k.cdc.MustUnmarshal(iterator.Value(), &val)
if val.ChainID == chainID {
vals = append(vals, val)
}
}

return vals
}

// RemoveHostChain removes a chain from the store
func (k Keeper) RemoveHostChain(
ctx sdk.Context,
id uint64,

) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKeyPrefix))
store.Delete(types.HostChainKey(
id,
))
func (k Keeper) RemoveHostChain(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.HostChainKeyPrefix)
store.Delete(types.HostChainKey(id))
}

// GetAllHostChain returns all chain
func (k Keeper) GetAllHostChain(ctx sdk.Context) (list []types.HostChain) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.HostChainKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
func (k Keeper) GetAllHostChain(ctx sdk.Context) []types.HostChain {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.HostChainKeyPrefix)
iterator := sdk.KVStorePrefixIterator(store, nil)

defer iterator.Close()

var list []types.HostChain
for ; iterator.Valid(); iterator.Next() {
var val types.HostChain
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
return list
}
4 changes: 2 additions & 2 deletions x/ratesync/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (k Keeper) PostCValueUpdate(ctx sdk.Context, mintDenom, hostDenom string, c
hcs := k.GetAllHostChain(ctx)
for _, hc := range hcs {
if hc.Features.LiquidStakeIBC.Enabled {
err := k.ExecuteLiquidStakeRateTx(ctx, hc.Features.LiquidStakeIBC, mintDenom, hostDenom, cValue, hc.ID, hc.ConnectionID, hc.IcaAccount)
err := k.ExecuteLiquidStakeRateTx(ctx, hc.Features.LiquidStakeIBC, mintDenom, hostDenom, cValue, hc.ID, hc.ConnectionID, hc.ICAAccount)
if err != nil {
k.Logger(ctx).Error("cannot ExecuteLiquidStakeRateTx for host chain ",
"id", hc.ID,
Expand Down Expand Up @@ -69,7 +69,7 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumb
for _, hc := range hcs {
if hc.Features.LiquidStake.Enabled && epochIdentifier == types.LiquidStakeEpoch {
// Add liquidstakekeeper and do stuff
err := k.ExecuteLiquidStakeRateTx(ctx, hc.Features.LiquidStakeIBC, liquidBondDenom, bondDenom, nas.MintRate, hc.ID, hc.ConnectionID, hc.IcaAccount)
err := k.ExecuteLiquidStakeRateTx(ctx, hc.Features.LiquidStakeIBC, liquidBondDenom, bondDenom, nas.MintRate, hc.ID, hc.ConnectionID, hc.ICAAccount)
if err != nil {
k.Logger(ctx).Error("cannot ExecuteLiquidStakeRateTx for host chain ",
"id", hc.ID,
Expand Down
12 changes: 6 additions & 6 deletions x/ratesync/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func (k *Keeper) OnChanOpenAck(
// get host chain
hc, found := k.GetHostChain(ctx, id)
if !found {
k.Logger(ctx).Info(fmt.Sprintf("host chain with id %s is not registered", id))
k.Logger(ctx).Info(fmt.Sprintf("host chain with id %v is not registered", id))
return nil
}

switch {
case portOwner == hc.IcaAccount.Owner:
hc.IcaAccount.Address = address
hc.IcaAccount.ChannelState = liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATED
case portOwner == hc.ICAAccount.Owner:
hc.ICAAccount.Address = address
hc.ICAAccount.ChannelState = liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATED
default:
k.Logger(ctx).Error("Unrecognised ICA account type for the module", "port-id:", portID, "chain-id", chainID)
return nil
Expand Down Expand Up @@ -111,7 +111,7 @@ func (k *Keeper) OnAcknowledgementPacket(
// get host chain
hc, found := k.GetHostChain(ctx, id)
if !found {
return errorsmod.Wrapf(sdkerrors.ErrNotFound, "host chain with id %s is not present", id)
return errorsmod.Wrapf(sdkerrors.ErrNotFound, "host chain with id %v is not present", id)
}

var ack channeltypes.Acknowledgement
Expand Down Expand Up @@ -187,7 +187,7 @@ func (k *Keeper) OnTimeoutPacket(
// get host chain
hc, found := k.GetHostChain(ctx, id)
if !found {
return errorsmod.Wrapf(sdkerrors.ErrNotFound, "host chain with id %s is not present", id)
return errorsmod.Wrapf(sdkerrors.ErrNotFound, "host chain with id %v is not present", id)
}
var icaPacket icatypes.InterchainAccountPacketData
if err := icatypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &icaPacket); err != nil {
Expand Down
40 changes: 20 additions & 20 deletions x/ratesync/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func (k msgServer) CreateHostChain(goCtx context.Context, msg *types.MsgCreateHo
id := k.IncrementHostChainID(ctx)
msg.HostChain.ID = id

if msg.HostChain.IcaAccount.Owner == "" {
msg.HostChain.IcaAccount.Owner = types.DefaultPortOwner(id)
if msg.HostChain.ICAAccount.Owner == "" {
msg.HostChain.ICAAccount.Owner = types.DefaultPortOwner(id)
} // else handled in msg.ValidateBasic()
// register ratesyn ICA
if msg.HostChain.IcaAccount.ChannelState == liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATING {
err = k.icaControllerKeeper.RegisterInterchainAccount(ctx, msg.HostChain.ConnectionID, msg.HostChain.IcaAccount.Owner, "")
if msg.HostChain.ICAAccount.ChannelState == liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATING {
err = k.icaControllerKeeper.RegisterInterchainAccount(ctx, msg.HostChain.ConnectionID, msg.HostChain.ICAAccount.Owner, "")
if err != nil {
return nil, errorsmod.Wrapf(
types.ErrRegisterFailed,
"error registering %s ratesync ica with owner: %s, err:%s",
chainID, msg.HostChain.IcaAccount.Owner,
chainID, msg.HostChain.ICAAccount.Owner,
err.Error(),
)
}
Expand Down Expand Up @@ -108,15 +108,15 @@ func (k msgServer) UpdateHostChain(goCtx context.Context, msg *types.MsgUpdateHo
"connectionID mismatch got %s, found %s", msg.HostChain.ConnectionID, oldHC.ConnectionID)
}

if oldHC.IcaAccount.ChannelState != liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATED {
if oldHC.ICAAccount.ChannelState != liquidstakeibctypes.ICAAccount_ICA_CHANNEL_CREATED {
return nil, errorsmod.Wrapf(types.ErrInvalid, "invalid ICAAccount state, should already be active")
}
if msg.HostChain.IcaAccount.ChannelState != oldHC.IcaAccount.ChannelState ||
msg.HostChain.IcaAccount.Address != oldHC.IcaAccount.Address ||
msg.HostChain.IcaAccount.Owner != oldHC.IcaAccount.Owner ||
!msg.HostChain.IcaAccount.Balance.IsEqual(oldHC.IcaAccount.Balance) {
if msg.HostChain.ICAAccount.ChannelState != oldHC.ICAAccount.ChannelState ||
msg.HostChain.ICAAccount.Address != oldHC.ICAAccount.Address ||
msg.HostChain.ICAAccount.Owner != oldHC.ICAAccount.Owner ||
!msg.HostChain.ICAAccount.Balance.IsEqual(oldHC.ICAAccount.Balance) {
return nil, errorsmod.Wrapf(types.ErrInvalid, "invalid ICAAccount, ICA account cannot be updated, "+
"ICAAccount mismatch got %s, found %s", msg.HostChain.IcaAccount, oldHC.IcaAccount)
"ICAAccount mismatch got %s, found %s", msg.HostChain.ICAAccount, oldHC.ICAAccount)
}

updateStr := ""
Expand Down Expand Up @@ -151,16 +151,16 @@ func (k msgServer) UpdateHostChain(goCtx context.Context, msg *types.MsgUpdateHo

// generate contract msg{msg}
contractMsg := types.InstantiateLiquidStakeRateContract{
Admin: oldHC.IcaAccount.Address,
Admin: oldHC.ICAAccount.Address,
}
contractMsgBz, err := json.Marshal(contractMsg)
if err != nil {
return nil, errorsmod.Wrapf(err, "unable to marshal InstantiateLiquidStakeRateContract")
}

msg := &wasmtypes.MsgInstantiateContract{
Sender: oldHC.IcaAccount.Address,
Admin: oldHC.IcaAccount.Address,
Sender: oldHC.ICAAccount.Address,
Admin: oldHC.ICAAccount.Address,
CodeID: oldHC.Features.LiquidStakeIBC.CodeID,
Label: fmt.Sprintf("PSTAKE ratesync, ID-%v", oldHC.ID),
Msg: contractMsgBz,
Expand All @@ -174,7 +174,7 @@ func (k msgServer) UpdateHostChain(goCtx context.Context, msg *types.MsgUpdateHo
if err != nil {
return nil, err
}
_, err = k.GenerateAndExecuteICATx(ctx, oldHC.ConnectionID, oldHC.IcaAccount.Owner, []proto.Message{msg}, string(memobz))
_, err = k.GenerateAndExecuteICATx(ctx, oldHC.ConnectionID, oldHC.ICAAccount.Owner, []proto.Message{msg}, string(memobz))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -202,16 +202,16 @@ func (k msgServer) UpdateHostChain(goCtx context.Context, msg *types.MsgUpdateHo

// generate contract msg{msg}
contractMsg := types.InstantiateLiquidStakeRateContract{
Admin: oldHC.IcaAccount.Address,
Admin: oldHC.ICAAccount.Address,
}
contractMsgBz, err := json.Marshal(contractMsg)
if err != nil {
return nil, errorsmod.Wrapf(err, "unable to marshal InstantiateLiquidStakeRateContract")
}

msg := &wasmtypes.MsgInstantiateContract{
Sender: oldHC.IcaAccount.Address,
Admin: oldHC.IcaAccount.Address,
Sender: oldHC.ICAAccount.Address,
Admin: oldHC.ICAAccount.Address,
CodeID: oldHC.Features.LiquidStake.CodeID,
Label: fmt.Sprintf("PSTAKE ratesync, ID-%v", oldHC.ID),
Msg: contractMsgBz,
Expand All @@ -225,7 +225,7 @@ func (k msgServer) UpdateHostChain(goCtx context.Context, msg *types.MsgUpdateHo
if err != nil {
return nil, err
}
_, err = k.GenerateAndExecuteICATx(ctx, oldHC.ConnectionID, oldHC.IcaAccount.Owner, []proto.Message{msg}, string(memobz))
_, err = k.GenerateAndExecuteICATx(ctx, oldHC.ConnectionID, oldHC.ICAAccount.Owner, []proto.Message{msg}, string(memobz))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (k msgServer) DeleteHostChain(goCtx context.Context, msg *types.MsgDeleteHo
}

// check pending packets, do not allow to delete if packets are pending.
portID := types.MustICAPortIDFromOwner(hc.IcaAccount.Owner)
portID := types.MustICAPortIDFromOwner(hc.ICAAccount.Owner)
channelID, ok := k.icaControllerKeeper.GetOpenActiveChannel(ctx, hc.ChainID, portID)
if !ok {
return nil, errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "PortID: %s, connectionID: %s", portID, hc.ConnectionID)
Expand Down
4 changes: 2 additions & 2 deletions x/ratesync/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// GetParams gets the parameters.
func (k *Keeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyPrefix(types.ParamsKeyPrefix))
bz := store.Get(types.ParamsKeyPrefix)
if bz == nil {
return params
}
Expand All @@ -22,5 +22,5 @@ func (k *Keeper) GetParams(ctx sdk.Context) (params types.Params) {
func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) {
store := ctx.KVStore(k.storeKey)
bytes := k.cdc.MustMarshal(&params)
store.Set(types.KeyPrefix(types.ParamsKeyPrefix), bytes)
store.Set(types.ParamsKeyPrefix, bytes)
}
2 changes: 1 addition & 1 deletion x/ratesync/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (k Keeper) AllHostChains(goCtx context.Context, req *types.QueryAllHostChai
ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
chainStore := prefix.NewStore(store, types.KeyPrefix(types.HostChainKeyPrefix))
chainStore := prefix.NewStore(store, types.HostChainKeyPrefix)

pageRes, err := query.Paginate(chainStore, req.Pagination, func(key []byte, value []byte) error {
var chain types.HostChain
Expand Down
Loading

0 comments on commit e2393f3

Please sign in to comment.