Skip to content

Commit

Permalink
move the new migration of uibc params to keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Nov 14, 2023
1 parent 1842dc0 commit c6dd61b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 38 deletions.
11 changes: 2 additions & 9 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
Expand Down
4 changes: 2 additions & 2 deletions proto/umee/uibc/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions util/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
}
}
4 changes: 2 additions & 2 deletions x/uibc/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion x/uibc/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions x/uibc/quota/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -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)
}
35 changes: 11 additions & 24 deletions x/uibc/quota/keeper/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -71,15 +71,15 @@ 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
}
inflows = append(inflows, o)
return nil
}
err := store.Iterate(k.store, keyPrefixDenomInflows, iter)
err := store.Iterate(k.store, keyPrefixDenomInflows, cb)
return inflows, err
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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))
}

0 comments on commit c6dd61b

Please sign in to comment.