Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
70 lines
2.3 KiB
Go
70 lines
2.3 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/epochs/types"
|
|
)
|
|
|
|
// GetEpochInfo returns epoch info by identifier.
|
|
func (k *Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInfo, error) {
|
|
return k.EpochInfo.Get(ctx, identifier)
|
|
}
|
|
|
|
// AddEpochInfo adds a new epoch info. Will return an error if the epoch fails validation,
|
|
// or re-uses an existing identifier.
|
|
// This method also sets the start time if left unset, and sets the epoch start height.
|
|
func (k *Keeper) AddEpochInfo(ctx sdk.Context, epoch types.EpochInfo) error {
|
|
err := epoch.Validate()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Check if identifier already exists
|
|
isExist, err := k.EpochInfo.Has(ctx, epoch.Identifier)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if isExist {
|
|
return fmt.Errorf("epoch with identifier %s already exists", epoch.Identifier)
|
|
}
|
|
|
|
// Initialize empty and default epoch values
|
|
if epoch.StartTime.IsZero() {
|
|
epoch.StartTime = ctx.BlockTime()
|
|
}
|
|
if epoch.CurrentEpochStartHeight == 0 && !epoch.StartTime.After(ctx.BlockTime()) {
|
|
epoch.CurrentEpochStartHeight = ctx.BlockHeight()
|
|
}
|
|
return k.EpochInfo.Set(ctx, epoch.Identifier, epoch)
|
|
}
|
|
|
|
// AllEpochInfos iterate through epochs to return all epochs info.
|
|
func (k *Keeper) AllEpochInfos(ctx sdk.Context) ([]types.EpochInfo, error) {
|
|
var epochs []types.EpochInfo
|
|
err := k.EpochInfo.Walk(
|
|
ctx,
|
|
nil,
|
|
func(key string, value types.EpochInfo) (stop bool, err error) {
|
|
epochs = append(epochs, value)
|
|
return false, nil
|
|
},
|
|
)
|
|
return epochs, err
|
|
}
|
|
|
|
// NumBlocksSinceEpochStart returns the number of blocks since the epoch started.
|
|
// if the epoch started on block N, then calling this during block N (after BeforeEpochStart)
|
|
// would return 0.
|
|
// Calling it any point in block N+1 (assuming the epoch doesn't increment) would return 1.
|
|
func (k *Keeper) NumBlocksSinceEpochStart(ctx sdk.Context, identifier string) (int64, error) {
|
|
epoch, err := k.EpochInfo.Get(ctx, identifier)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("epoch with identifier %s not found", identifier)
|
|
}
|
|
if ctx.BlockTime().Before(epoch.StartTime) {
|
|
return 0, fmt.Errorf("epoch with identifier %s has not started yet: start time: %s", identifier, epoch.StartTime)
|
|
}
|
|
|
|
return ctx.BlockHeight() - epoch.CurrentEpochStartHeight, nil
|
|
}
|