mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-25 15:55:18 +00:00
614d4e40fe
* Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
252 lines
8.5 KiB
Go
252 lines
8.5 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/cometbft/cometbft/libs/log"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
|
|
|
"github.com/kava-labs/kava/x/bep3/types"
|
|
)
|
|
|
|
// Keeper of the bep3 store
|
|
type Keeper struct {
|
|
key storetypes.StoreKey
|
|
cdc codec.Codec
|
|
paramSubspace paramtypes.Subspace
|
|
bankKeeper types.BankKeeper
|
|
accountKeeper types.AccountKeeper
|
|
Maccs map[string]bool
|
|
}
|
|
|
|
// NewKeeper creates a bep3 keeper
|
|
func NewKeeper(cdc codec.Codec, key storetypes.StoreKey, sk types.BankKeeper, ak types.AccountKeeper,
|
|
paramstore paramtypes.Subspace, maccs map[string]bool,
|
|
) Keeper {
|
|
if !paramstore.HasKeyTable() {
|
|
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
|
|
}
|
|
|
|
keeper := Keeper{
|
|
key: key,
|
|
cdc: cdc,
|
|
paramSubspace: paramstore,
|
|
bankKeeper: sk,
|
|
accountKeeper: ak,
|
|
Maccs: maccs,
|
|
}
|
|
return keeper
|
|
}
|
|
|
|
// Logger returns a module-specific logger.
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
|
}
|
|
|
|
// ------------------------------------------
|
|
// Atomic Swaps
|
|
// ------------------------------------------
|
|
|
|
// SetAtomicSwap puts the AtomicSwap into the store, and updates any indexes.
|
|
func (k Keeper) SetAtomicSwap(ctx sdk.Context, atomicSwap types.AtomicSwap) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
|
|
bz := k.cdc.MustMarshal(&atomicSwap)
|
|
store.Set(atomicSwap.GetSwapID(), bz)
|
|
}
|
|
|
|
// GetAtomicSwap gets an AtomicSwap from the store.
|
|
func (k Keeper) GetAtomicSwap(ctx sdk.Context, swapID []byte) (types.AtomicSwap, bool) {
|
|
var atomicSwap types.AtomicSwap
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
|
|
bz := store.Get(swapID)
|
|
if bz == nil {
|
|
return atomicSwap, false
|
|
}
|
|
|
|
k.cdc.MustUnmarshal(bz, &atomicSwap)
|
|
return atomicSwap, true
|
|
}
|
|
|
|
// RemoveAtomicSwap removes an AtomicSwap from the AtomicSwapKeyPrefix.
|
|
func (k Keeper) RemoveAtomicSwap(ctx sdk.Context, swapID []byte) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
|
|
store.Delete(swapID)
|
|
}
|
|
|
|
// IterateAtomicSwaps provides an iterator over all stored AtomicSwaps.
|
|
// For each AtomicSwap, cb will be called. If cb returns true, the iterator will close and stop.
|
|
func (k Keeper) IterateAtomicSwaps(ctx sdk.Context, cb func(atomicSwap types.AtomicSwap) (stop bool)) {
|
|
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.AtomicSwapKeyPrefix)
|
|
|
|
defer iterator.Close()
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
var atomicSwap types.AtomicSwap
|
|
k.cdc.MustUnmarshal(iterator.Value(), &atomicSwap)
|
|
|
|
if cb(atomicSwap) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetAllAtomicSwaps returns all AtomicSwaps from the store
|
|
func (k Keeper) GetAllAtomicSwaps(ctx sdk.Context) (atomicSwaps types.AtomicSwaps) {
|
|
k.IterateAtomicSwaps(ctx, func(atomicSwap types.AtomicSwap) bool {
|
|
atomicSwaps = append(atomicSwaps, atomicSwap)
|
|
return false
|
|
})
|
|
return
|
|
}
|
|
|
|
// ------------------------------------------
|
|
// Atomic Swap Block Index
|
|
// ------------------------------------------
|
|
|
|
// InsertIntoByBlockIndex adds a swap ID and expiration time into the byBlock index.
|
|
func (k Keeper) InsertIntoByBlockIndex(ctx sdk.Context, atomicSwap types.AtomicSwap) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
|
|
store.Set(types.GetAtomicSwapByHeightKey(atomicSwap.ExpireHeight, atomicSwap.GetSwapID()), atomicSwap.GetSwapID())
|
|
}
|
|
|
|
// RemoveFromByBlockIndex removes an AtomicSwap from the byBlock index.
|
|
func (k Keeper) RemoveFromByBlockIndex(ctx sdk.Context, atomicSwap types.AtomicSwap) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
|
|
store.Delete(types.GetAtomicSwapByHeightKey(atomicSwap.ExpireHeight, atomicSwap.GetSwapID()))
|
|
}
|
|
|
|
// IterateAtomicSwapsByBlock provides an iterator over AtomicSwaps ordered by AtomicSwap expiration block
|
|
// For each AtomicSwap cb will be called. If cb returns true the iterator will close and stop.
|
|
func (k Keeper) IterateAtomicSwapsByBlock(ctx sdk.Context, inclusiveCutoffTime uint64, cb func(swapID []byte) (stop bool)) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapByBlockPrefix)
|
|
iterator := store.Iterator(
|
|
nil, // start at the very start of the prefix store
|
|
sdk.PrefixEndBytes(sdk.Uint64ToBigEndian(inclusiveCutoffTime)), // end of range
|
|
)
|
|
|
|
defer iterator.Close()
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
id := iterator.Value()
|
|
|
|
if cb(id) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------
|
|
// Atomic Swap Longterm Storage Index
|
|
// ------------------------------------------
|
|
|
|
// InsertIntoLongtermStorage adds a swap ID and deletion time into the longterm storage index.
|
|
// Completed swaps are stored for 1 week.
|
|
func (k Keeper) InsertIntoLongtermStorage(ctx sdk.Context, atomicSwap types.AtomicSwap) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapLongtermStoragePrefix)
|
|
deletionHeight := uint64(atomicSwap.ClosedBlock) + types.DefaultLongtermStorageDuration
|
|
store.Set(types.GetAtomicSwapByHeightKey(deletionHeight, atomicSwap.GetSwapID()), atomicSwap.GetSwapID())
|
|
}
|
|
|
|
// RemoveFromLongtermStorage removes a swap from the into the longterm storage index
|
|
func (k Keeper) RemoveFromLongtermStorage(ctx sdk.Context, atomicSwap types.AtomicSwap) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapLongtermStoragePrefix)
|
|
deletionHeight := uint64(atomicSwap.ClosedBlock) + types.DefaultLongtermStorageDuration
|
|
store.Delete(types.GetAtomicSwapByHeightKey(deletionHeight, atomicSwap.GetSwapID()))
|
|
}
|
|
|
|
// IterateAtomicSwapsLongtermStorage provides an iterator over AtomicSwaps ordered by deletion height.
|
|
// For each AtomicSwap cb will be called. If cb returns true the iterator will close and stop.
|
|
func (k Keeper) IterateAtomicSwapsLongtermStorage(ctx sdk.Context, inclusiveCutoffTime uint64,
|
|
cb func(swapID []byte) (stop bool),
|
|
) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AtomicSwapLongtermStoragePrefix)
|
|
iterator := store.Iterator(
|
|
nil, // start at the very start of the prefix store
|
|
sdk.PrefixEndBytes(sdk.Uint64ToBigEndian(inclusiveCutoffTime)), // end of range
|
|
)
|
|
|
|
defer iterator.Close()
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
id := iterator.Value()
|
|
|
|
if cb(id) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------
|
|
// Asset Supplies
|
|
// ------------------------------------------
|
|
|
|
// GetAssetSupply gets an asset's current supply from the store.
|
|
func (k Keeper) GetAssetSupply(ctx sdk.Context, denom string) (types.AssetSupply, bool) {
|
|
var assetSupply types.AssetSupply
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AssetSupplyPrefix)
|
|
bz := store.Get([]byte(denom))
|
|
if bz == nil {
|
|
return types.AssetSupply{}, false
|
|
}
|
|
k.cdc.MustUnmarshal(bz, &assetSupply)
|
|
return assetSupply, true
|
|
}
|
|
|
|
// SetAssetSupply updates an asset's supply
|
|
func (k Keeper) SetAssetSupply(ctx sdk.Context, supply types.AssetSupply, denom string) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.AssetSupplyPrefix)
|
|
store.Set([]byte(denom), k.cdc.MustMarshal(&supply))
|
|
}
|
|
|
|
// IterateAssetSupplies provides an iterator over all stored AssetSupplies.
|
|
func (k Keeper) IterateAssetSupplies(ctx sdk.Context, cb func(supply types.AssetSupply) (stop bool)) {
|
|
iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.AssetSupplyPrefix)
|
|
|
|
defer iterator.Close()
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
var supply types.AssetSupply
|
|
k.cdc.MustUnmarshal(iterator.Value(), &supply)
|
|
|
|
if cb(supply) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// GetAllAssetSupplies returns all asset supplies from the store
|
|
func (k Keeper) GetAllAssetSupplies(ctx sdk.Context) (supplies types.AssetSupplies) {
|
|
k.IterateAssetSupplies(ctx, func(supply types.AssetSupply) bool {
|
|
supplies = append(supplies, supply)
|
|
return false
|
|
})
|
|
return
|
|
}
|
|
|
|
// GetPreviousBlockTime get the blocktime for the previous block
|
|
func (k Keeper) GetPreviousBlockTime(ctx sdk.Context) (blockTime time.Time, found bool) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousBlockTimeKey)
|
|
b := store.Get(types.PreviousBlockTimeKey)
|
|
if b == nil {
|
|
return time.Time{}, false
|
|
}
|
|
if err := blockTime.UnmarshalBinary(b); err != nil {
|
|
panic(err)
|
|
}
|
|
return blockTime, true
|
|
}
|
|
|
|
// SetPreviousBlockTime set the time of the previous block
|
|
func (k Keeper) SetPreviousBlockTime(ctx sdk.Context, blockTime time.Time) {
|
|
store := prefix.NewStore(ctx.KVStore(k.key), types.PreviousBlockTimeKey)
|
|
b, err := blockTime.MarshalBinary()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
store.Set(types.PreviousBlockTimeKey, b)
|
|
}
|