Skip to content

Commit

Permalink
chore: savepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kioqq committed Dec 12, 2023
1 parent 6e3b515 commit d04d0f8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 65 deletions.
18 changes: 4 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,8 +1146,10 @@ func (app *Haqq) setupUpgradeHandlers() {
v164.CreateUpgradeHandler(
app.mm,
app.configurator,
// app.GetKey(coinomicstypes.StoreKey),
// app.GetKey(paramstypes.StoreKey),
app.GetKey(coinomicstypes.StoreKey),
app.GetKey(paramstypes.StoreKey),
app.DistrKeeper,
app.CoinomicsKeeper,
// app.GetSubspace(coinomicstypes.ModuleName)
),
)
Expand All @@ -1166,23 +1168,11 @@ func (app *Haqq) setupUpgradeHandlers() {

var storeUpgrades *storetypes.StoreUpgrades

// if upgradeInfo.Name == v160.UpgradeName {
// storeUpgrades = &storetypes.StoreUpgrades{
// Added: []string{
// icahosttypes.SubModuleName,
// },
// }
// }

switch upgradeInfo.Name {
case v160.UpgradeName:
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{icahosttypes.SubModuleName},
}
// case v164.UpgradeName:
// storeUpgrades = &storetypes.StoreUpgrades{
// Added: []string{coinomicstypes.ModuleName},
// }
default:
// no-op
}
Expand Down
96 changes: 47 additions & 49 deletions app/upgrades/v1.6.4/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
package v164

import (
// storetypes "github.com/cosmos/cosmos-sdk/store/types"
"strings"

"cosmossdk.io/math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

// paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
// coinomicstypes "github.com/haqq-network/haqq/x/coinomics/types"
"github.com/haqq-network/haqq/utils"
coinomicskeeper "github.com/haqq-network/haqq/x/coinomics/keeper"
coinomicstypes "github.com/haqq-network/haqq/x/coinomics/types"
)

const ModuleName = "coinomics"

// CreateUpgradeHandler creates an SDK upgrade handler for v1.6.4
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
// storeKey storetypes.StoreKey,
// paramsStoreKey storetypes.StoreKey,
// paramsSubspace paramtypes.Subspace,
storeKey storetypes.StoreKey,
paramsStoreKey storetypes.StoreKey,
dk distrkeeper.Keeper,
ck coinomicskeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger()

// logger.Info("start clean coinomics store")

// // Используйте переданный ключ хранилища
// store := ctx.KVStore(storeKey)
MigrateStore(ctx, storeKey, paramsStoreKey, dk, ck)

// iterator := sdk.KVStorePrefixIterator(store, nil)

// for ; iterator.Valid(); iterator.Next() {
// println("coinomics store key for delete")
// println(string(iterator.Key()))

// store.Delete(iterator.Key())
// }

// iterator.Close() // Не забудьте закрыть итератор
// logger.Info("cleared coinomics store")
return mm.RunMigrations(ctx, configurator, vm)
}
}

// logger.Info("start cleaning params for module")
// MigrateStore migrates the x/coinomics module state
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey,
paramsStoreKey storetypes.StoreKey, dk distrkeeper.Keeper,
ck coinomicskeeper.Keeper) {
// clean module state
store := ctx.KVStore(storeKey)
iterator := sdk.KVStorePrefixIterator(store, nil)

// keyTable := coinomicstypes.ParamKeyTable()
// paramsSubspace = paramsSubspace.WithKeyTable(keyTable)
for ; iterator.Valid(); iterator.Next() {
println("coinomics store key for delete")
println(string(iterator.Key()))

// paramsSubspace.SetParamSet(ctx, &coinomicstypes.Params{})
store.Delete(iterator.Key())
}

// if paramsSubspace.HasKeyTable() {
// clean params
paramsStore := ctx.KVStore(paramsStoreKey)
paramsIterator := sdk.KVStorePrefixIterator(paramsStore, nil)

// paramsSubspace.WithKeyTable(paramtypes.NewKeyTable())
// // paramsSubspace.IterateKeys(ctx, func(key []byte) bool {
// // println(string(key))
for ; paramsIterator.Valid(); paramsIterator.Next() {
if strings.Contains(string(paramsIterator.Key()), "coinomics/") {
println("coinomics store key/value")

// // paramsSubspace.Update(ctx, key, nil)
// // return false
// // })
// }
println(string(paramsIterator.Key()))
println(string(paramsIterator.Value()))

// if !ps.HasKeyTable() {
// ps = ps.WithKeyTable(types.ParamKeyTable())
// }
paramsStore.Delete(paramsIterator.Key())
}
}

// paramsSubspace.Set(ctx, []byte(ModuleName), nil) // Получаем пространство параметров для модуля
// iter := storeParams.Iterator(nil, nil) // Создаем итератор для всех ключей
// defer iter.Close()
// reset coinomics params
defaultParams := coinomicstypes.DefaultParams()

// for ; iter.Valid(); iter.Next() {
// storeParams.Delete(iter.Key()) // Удаляем каждый ключ
// }
if utils.IsMainNetwork(ctx.ChainID()) {
defaultParams.EnableCoinomics = false
}

logger.Info("start default sdk migration for v1.6.4")
maxSupply := sdk.Coin{Denom: "aISLM", Amount: math.NewIntWithDecimal(100_000_000_000, 18)} // 100bn ISLM

return mm.RunMigrations(ctx, configurator, vm)
}
ck.SetMaxSupply(ctx, maxSupply)
ck.SetParams(ctx, defaultParams)
}
20 changes: 19 additions & 1 deletion x/coinomics/keeper/inflation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"log"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
Expand All @@ -13,7 +15,7 @@ func (k Keeper) MintAndAllocate(ctx sdk.Context) error {
currentBlockTS, _ := sdk.NewDecFromStr(math.NewInt(ctx.BlockTime().UnixMilli()).String())

// Skip minting for the first block after activation, waiting for previous block timestamp to be set
if k.GetPrevBlockTS(ctx) == sdk.ZeroInt() {
if k.GetPrevBlockTS(ctx).Equal(sdk.ZeroInt()) {
k.SetPrevBlockTS(ctx, currentBlockTS.RoundInt())
return nil
}
Expand All @@ -39,16 +41,32 @@ func (k Keeper) MintAndAllocate(ctx sdk.Context) error {
// totalBonded * rewardCoefficient * ((currentBlockTS - prevBlockTS) / yearInMillis)
blockMint := totalBonded.Mul(rewardCoefficient).Mul((currentBlockTS.Sub(prevBlockTS)).Quo(yearInMillis))

log.Printf("totalBonded: %s", totalBonded.String())
log.Printf("rewardCoefficient: %s", rewardCoefficient.String())
log.Printf("currentBlockTS: %s", currentBlockTS.String())
log.Printf("prevBlockTS: %s", prevBlockTS.String())
log.Printf("yearInMillis: %s", yearInMillis.String())
log.Printf("blockMint #1: %s", blockMint.String())

bankTotalSupply, _ := sdk.NewDecFromStr(k.bankKeeper.GetSupply(ctx, params.MintDenom).Amount.String())
maxSupply, _ := sdk.NewDecFromStr(k.GetMaxSupply(ctx).Amount.String())

log.Printf("bankTotalSupply: %s", bankTotalSupply.String())
log.Printf("maxSupply: %s", maxSupply.String())

// Ensure minting does not exceed the maximum supply
if bankTotalSupply.Add(blockMint).GT(maxSupply) {
blockMint = maxSupply.Sub(bankTotalSupply)
params.EnableCoinomics = false
k.SetParams(ctx, params)
}

if blockMint.IsNegative() {
return nil
}

log.Printf("blockMint #2: %s", blockMint.String())

// Mint and allocate the calculated coin amount
totalMintOnBlockCoin := sdk.NewCoin(params.MintDenom, blockMint.RoundInt())
if err := k.MintCoins(ctx, totalMintOnBlockCoin); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/coinomics/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var ParamsKey = []byte{0x00}
// constants
const (
// module name
ModuleName = "coinomics_v2"
ModuleName = "coinomics"

// StoreKey to be used when creating the KVStore
StoreKey = ModuleName
Expand Down

0 comments on commit d04d0f8

Please sign in to comment.