cosmos-sdk/x/epoching/keeper/keeper.go
Robert Zaremba 479485f95d
style: lint go and markdown (#10060)
## Description

+ fixing `x/bank/migrations/v44.migrateDenomMetadata` - we could potentially put a wrong data in a new key if the old keys have variable length.
+ linting the code

Putting in the same PR because i found the issue when running a linter.

Depends on: #10112

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-10-30 13:43:04 +00:00

193 lines
5.1 KiB
Go

package keeper
import (
"time"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
db "github.com/tendermint/tm-db"
)
const (
DefaultEpochActionID = 1
DefaultEpochNumber = 0
)
var (
NextEpochActionID = []byte{0x11}
EpochNumberID = []byte{0x12}
EpochActionQueuePrefix = []byte{0x13} // prefix for the epoch
)
// Keeper of the store
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
// Used to calculate the estimated next epoch time.
// This is local to every node
// TODO: remove in favor of consensus param when its added
commitTimeout time.Duration
}
// NewKeeper creates a epoch queue manager
func NewKeeper(cdc codec.BinaryCodec, key storetypes.StoreKey, commitTimeout time.Duration) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
commitTimeout: commitTimeout,
}
}
// GetNewActionID returns ID to be used for next epoch
func (k Keeper) GetNewActionID(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(NextEpochActionID)
if bz == nil {
// return default action ID to 1
return DefaultEpochActionID
}
id := sdk.BigEndianToUint64(bz)
// increment next action ID
store.Set(NextEpochActionID, sdk.Uint64ToBigEndian(id+1))
return id
}
// ActionStoreKey returns action store key from ID
func ActionStoreKey(epochNumber int64, actionID uint64) []byte {
return append(EpochActionQueuePrefix, byte(epochNumber), byte(actionID))
}
// QueueMsgForEpoch save the actions that need to be executed on next epoch
func (k Keeper) QueueMsgForEpoch(ctx sdk.Context, epochNumber int64, msg sdk.Msg) {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.MarshalInterface(msg)
if err != nil {
panic(err)
}
actionID := k.GetNewActionID(ctx)
store.Set(ActionStoreKey(epochNumber, actionID), bz)
}
// RestoreEpochAction restore the actions that need to be executed on next epoch
func (k Keeper) RestoreEpochAction(ctx sdk.Context, epochNumber int64, action *codectypes.Any) {
store := ctx.KVStore(k.storeKey)
// reference from TestMarshalAny(t *testing.T)
bz, err := k.cdc.MarshalInterface(action)
if err != nil {
panic(err)
}
actionID := k.GetNewActionID(ctx)
store.Set(ActionStoreKey(epochNumber, actionID), bz)
}
// GetEpochMsg gets a msg by ID
func (k Keeper) GetEpochMsg(ctx sdk.Context, epochNumber int64, actionID uint64) sdk.Msg {
store := ctx.KVStore(k.storeKey)
bz := store.Get(ActionStoreKey(epochNumber, actionID))
if bz == nil {
return nil
}
var action sdk.Msg
k.cdc.UnmarshalInterface(bz, &action)
return action
}
// GetEpochActions get all actions
func (k Keeper) GetEpochActions(ctx sdk.Context) []sdk.Msg {
actions := []sdk.Msg{}
iterator := k.GetEpochActionsIterator(ctx)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var action sdk.Msg
bz := iterator.Value()
k.cdc.UnmarshalInterface(bz, &action)
actions = append(actions, action)
}
return actions
}
// GetEpochActionsIterator returns iterator for EpochActions
func (k Keeper) GetEpochActionsIterator(ctx sdk.Context) db.Iterator {
return sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), EpochActionQueuePrefix)
}
// DequeueEpochActions dequeue all the actions store on epoch
func (k Keeper) DequeueEpochActions(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, EpochActionQueuePrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
store.Delete(key)
}
}
// DeleteByKey delete item by key
func (k Keeper) DeleteByKey(ctx sdk.Context, key []byte) {
store := ctx.KVStore(k.storeKey)
store.Delete(key)
}
// GetEpochActionByIterator get action by iterator
func (k Keeper) GetEpochActionByIterator(iterator db.Iterator) sdk.Msg {
bz := iterator.Value()
var action sdk.Msg
k.cdc.UnmarshalInterface(bz, &action)
return action
}
// SetEpochNumber set epoch number
func (k Keeper) SetEpochNumber(ctx sdk.Context, epochNumber int64) {
store := ctx.KVStore(k.storeKey)
store.Set(EpochNumberID, sdk.Uint64ToBigEndian(uint64(epochNumber)))
}
// GetEpochNumber fetches epoch number
func (k Keeper) GetEpochNumber(ctx sdk.Context) int64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(EpochNumberID)
if bz == nil {
return DefaultEpochNumber
}
return int64(sdk.BigEndianToUint64(bz))
}
// IncreaseEpochNumber increases epoch number
func (k Keeper) IncreaseEpochNumber(ctx sdk.Context) {
epochNumber := k.GetEpochNumber(ctx)
k.SetEpochNumber(ctx, epochNumber+1)
}
// GetNextEpochHeight returns next epoch block height
func (k Keeper) GetNextEpochHeight(ctx sdk.Context, epochInterval int64) int64 {
currentHeight := ctx.BlockHeight()
return currentHeight + (epochInterval - currentHeight%epochInterval)
}
// GetNextEpochTime returns estimated next epoch time
func (k Keeper) GetNextEpochTime(ctx sdk.Context, epochInterval int64) time.Time {
currentTime := ctx.BlockTime()
currentHeight := ctx.BlockHeight()
return currentTime.Add(k.commitTimeout * time.Duration(k.GetNextEpochHeight(ctx, epochInterval)-currentHeight))
}