From c6dd61b0f537e8fe03eefcb646fb1cb6c6531397 Mon Sep 17 00:00:00 2001 From: Sai Kumar Date: Tue, 14 Nov 2023 15:50:43 +0530 Subject: [PATCH] move the new migration of uibc params to keeper --- app/upgrades.go | 11 ++-------- proto/umee/uibc/v1/genesis.proto | 4 ++-- util/store/store.go | 8 +++++++ x/uibc/genesis.pb.go | 4 ++-- x/uibc/params.go | 10 ++++++++- x/uibc/quota/keeper/migrations.go | 17 +++++++++++++++ x/uibc/quota/keeper/quota.go | 35 ++++++++++--------------------- 7 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 x/uibc/quota/keeper/migrations.go diff --git a/app/upgrades.go b/app/upgrades.go index a4b37d79fc..b80a9215be 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -132,16 +132,9 @@ func (app *UmeeApp) registerUpgrade6_2(upgradeInfo upgradetypes.Plan) { // uibc migrations uIBCKeeper := app.UIbcQuotaKeeperB.Keeper(&ctx) // migrating outflow - oldTotalOutflow := uIBCKeeper.GetOldTotalOutflow() - uIBCKeeper.SetTotalOutflowSum(oldTotalOutflow) + uIBCKeeper.MigrateTotalOutflowSum() // uibc params - uibcParams := uIBCKeeper.GetParams() - uibcParams.TotalQuota = sdk.NewDec(1_600_000) - uibcParams.TokenQuota = sdk.NewDec(900_000) - uibcParams.InflowOutflowQuotaBase = sdk.NewDec(1_000_000) - uibcParams.InflowOutflowQuotaRate = sdk.MustNewDecFromStr("0.25") - - err = uIBCKeeper.SetParams(uibcParams) + err = uIBCKeeper.SetParams(uibc.DefaultParams()) return fromVM, err }, ) diff --git a/proto/umee/uibc/v1/genesis.proto b/proto/umee/uibc/v1/genesis.proto index 942c48b5a0..84df483b26 100644 --- a/proto/umee/uibc/v1/genesis.proto +++ b/proto/umee/uibc/v1/genesis.proto @@ -30,12 +30,12 @@ message GenesisState { (gogoproto.jsontag) = "quota_duration,omitempty", (gogoproto.moretags) = "yaml:\"quota_expires\"" ]; - // inflows defines inflow amount per denom denoms + // inflows tracks IBC inflow transfers (in USD) for each denom during quota period. repeated cosmos.base.v1beta1.DecCoin inflows = 5 [ (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins", (gogoproto.nullable) = false ]; - // total_inflow_sum defines the total inflow sum of ibc-transfer in USD. + // total_inflow_sum defines tracks total sum of IBC inflow transfers (in USD) during quota period. string total_inflow_sum = 6 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", diff --git a/util/store/store.go b/util/store/store.go index 1fd9f8af32..ed8ff69ea2 100644 --- a/util/store/store.go +++ b/util/store/store.go @@ -15,6 +15,7 @@ import ( "time" sdkmath "cosmossdk.io/math" + db "github.com/cometbft/cometbft-db" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -209,3 +210,10 @@ func GetInteger[T Integer](store sdk.KVStore, key []byte) (T, bool) { } panic("not possible: all types must be covered above") } + +func DeleteByIterator(store sdk.KVStore, iter db.Iterator) { + defer iter.Close() + for ; iter.Valid(); iter.Next() { + store.Delete(iter.Key()) + } +} diff --git a/x/uibc/genesis.pb.go b/x/uibc/genesis.pb.go index acf0468b89..3b373b4f1d 100644 --- a/x/uibc/genesis.pb.go +++ b/x/uibc/genesis.pb.go @@ -38,9 +38,9 @@ type GenesisState struct { TotalOutflowSum github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=total_outflow_sum,json=totalOutflowSum,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"total_outflow_sum"` // quota_expires defines quota expire time (as unix timestamp) for ibc-transfer denom. QuotaExpires time.Time `protobuf:"bytes,4,opt,name=quota_expires,json=quotaExpires,proto3,stdtime" json:"quota_duration,omitempty" yaml:"quota_expires"` - // inflows defines inflow amount per denom denoms + // inflows tracks IBC inflow transfers (in USD) during quota period. Inflows github_com_cosmos_cosmos_sdk_types.DecCoins `protobuf:"bytes,5,rep,name=inflows,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.DecCoins" json:"inflows"` - // total_inflow_sum defines the total inflow sum of ibc-transfer in USD. + // total_inflow_sum defines tracks total sum of IBC inflow transfers (in USD) during quota period. TotalInflowSum github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,6,opt,name=total_inflow_sum,json=totalInflowSum,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"total_inflow_sum"` } diff --git a/x/uibc/params.go b/x/uibc/params.go index f381c89a00..4a6b756cce 100644 --- a/x/uibc/params.go +++ b/x/uibc/params.go @@ -36,7 +36,7 @@ func (p Params) Validate() error { if err := validateQuota(p.InflowOutflowQuotaBase, "total inflow outflow quota base"); err != nil { return err } - if err := validateQuota(p.InflowOutflowQuotaRate, "total inflow outflow quota rate"); err != nil { + if err := validateQuotaRate(p.InflowOutflowQuotaRate, "total inflow outflow quota rate"); err != nil { return err } if err := validateQuota(p.InflowOutflowQuotaTokenBase, "total inflow outflow quota token base"); err != nil { @@ -76,6 +76,14 @@ func validateQuota(q sdk.Dec, typ string) error { return nil } +func validateQuotaRate(q sdk.Dec, typ string) error { + if q.LT(sdk.ZeroDec()) || q.GT(sdk.NewDec(2)) { + return fmt.Errorf("%s must be between 0 and 2: %s", typ, q) + } + + return validateQuota(q, typ) +} + // IBCTransferEnabled returns true if the ibc-transfer is enabled for both inflow and outflow." func (status IBCTransferStatus) IBCTransferEnabled() bool { return status != IBCTransferStatus_IBC_TRANSFER_STATUS_TRANSFERS_PAUSED diff --git a/x/uibc/quota/keeper/migrations.go b/x/uibc/quota/keeper/migrations.go new file mode 100644 index 0000000000..ef2b866ea3 --- /dev/null +++ b/x/uibc/quota/keeper/migrations.go @@ -0,0 +1,17 @@ +package keeper + +import sdk "github.com/cosmos/cosmos-sdk/types" + +// GetOldTotalOutflow returns the total outflow of ibc-transfer amount. +// Note: only use for v6.2 migration from v6.1.0 +func (k Keeper) GetOldTotalOutflow() sdk.Dec { + bz := k.store.Get(keyTotalOutflows) + return sdk.MustNewDecFromStr(string(bz)) +} + +// MigrateTotalOutflowSum migrate the old total outflow type to new one +// Note: only use for v6.2 migration from v6.1.0 +func (k Keeper) MigrateTotalOutflowSum() { + oldTotalOutflow := k.GetOldTotalOutflow() + k.SetTotalOutflowSum(oldTotalOutflow) +} diff --git a/x/uibc/quota/keeper/quota.go b/x/uibc/quota/keeper/quota.go index f001aece7a..327fb794d8 100644 --- a/x/uibc/quota/keeper/quota.go +++ b/x/uibc/quota/keeper/quota.go @@ -22,7 +22,7 @@ var ten = sdk.MustNewDecFromStr("10") // GetAllOutflows returns sum of outflows of all tokens in USD value. func (k Keeper) GetAllOutflows() (sdk.DecCoins, error) { var outflows sdk.DecCoins - iter := func(key, val []byte) error { + cb := func(key, val []byte) error { o := sdk.DecCoin{Denom: DenomFromKey(key, keyPrefixDenomOutflows)} if err := o.Amount.Unmarshal(val); err != nil { return err @@ -31,7 +31,7 @@ func (k Keeper) GetAllOutflows() (sdk.DecCoins, error) { return nil } - err := store.Iterate(k.store, keyPrefixDenomOutflows, iter) + err := store.Iterate(k.store, keyPrefixDenomOutflows, cb) return outflows, err } @@ -71,7 +71,7 @@ func (k Keeper) SetTokenOutflow(outflow sdk.DecCoin) { // GetAllInflows returns inflows of all registered tokens in USD value. func (k Keeper) GetAllInflows() (sdk.DecCoins, error) { var inflows sdk.DecCoins - iter := func(key, val []byte) error { + cb := func(key, val []byte) error { o := sdk.DecCoin{Denom: DenomFromKey(key, keyPrefixDenomInflows)} if err := o.Amount.Unmarshal(val); err != nil { return err @@ -79,7 +79,7 @@ func (k Keeper) GetAllInflows() (sdk.DecCoins, error) { inflows = append(inflows, o) return nil } - err := store.Iterate(k.store, keyPrefixDenomInflows, iter) + err := store.Iterate(k.store, keyPrefixDenomInflows, cb) return inflows, err } @@ -137,21 +137,15 @@ func (k Keeper) ResetAllQuotas() error { zero := sdk.NewDec(0) // outflows k.SetTotalOutflowSum(zero) - store := k.PrefixStore(keyPrefixDenomOutflows) - iter := sdk.KVStorePrefixIterator(store, nil) - defer iter.Close() - for ; iter.Valid(); iter.Next() { - store.Delete(iter.Key()) - } + ps := k.PrefixStore(keyPrefixDenomOutflows) + iter := sdk.KVStorePrefixIterator(ps, nil) + store.DeleteByIterator(ps, iter) // inflows k.SetTotalInflow(zero) - store = k.PrefixStore(keyPrefixDenomInflows) - iter = sdk.KVStorePrefixIterator(store, nil) - defer iter.Close() - for ; iter.Valid(); iter.Next() { - store.Delete(iter.Key()) - } + ps = k.PrefixStore(keyPrefixDenomInflows) + iter = sdk.KVStorePrefixIterator(ps, nil) + store.DeleteByIterator(ps, iter) return nil } @@ -273,7 +267,7 @@ func (k Keeper) RecordIBCInflow(ctx sdk.Context, } } - // get the exchange price (eg: UMEE) in USD from oracle using SYMBOL Denom eg: `UMEE` (uumee) + // get the exchange price (eg: UMEE) in USD from oracle using SYMBOL Denom eg: `UMEE` exchangeRate, err := k.oracle.Price(*k.ctx, strings.ToUpper(ts.SymbolDenom)) if err != nil { return channeltypes.NewErrorAcknowledgement(err) @@ -290,10 +284,3 @@ func (k Keeper) RecordIBCInflow(ctx sdk.Context, return nil } - -// GetOldTotalOutflow returns the total outflow of ibc-transfer amount. -// Note: only using for migration -func (k Keeper) GetOldTotalOutflow() sdk.Dec { - bz := k.store.Get(keyTotalOutflows) - return sdk.MustNewDecFromStr(string(bz)) -}