Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stakeibc re-org part 1 (keepers) #1112

Merged
merged 11 commits into from
Mar 1, 2024
18 changes: 0 additions & 18 deletions x/stakeibc/keeper/deposit_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,6 @@ import (
"github.com/Stride-Labs/stride/v18/x/stakeibc/types"
)

// Create a new deposit record for each host zone for the given epoch
func (k Keeper) CreateDepositRecordsForEpoch(ctx sdk.Context, epochNumber uint64) {
k.Logger(ctx).Info(fmt.Sprintf("Creating Deposit Records for Epoch %d", epochNumber))

for _, hostZone := range k.GetAllActiveHostZone(ctx) {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Creating Deposit Record"))

depositRecord := recordstypes.DepositRecord{
Amount: sdkmath.ZeroInt(),
Denom: hostZone.HostDenom,
HostZoneId: hostZone.ChainId,
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: epochNumber,
}
k.RecordsKeeper.AppendDepositRecord(ctx, depositRecord)
}
}

// Iterate each deposit record marked TRANSFER_QUEUE and IBC transfer tokens from the Stride controller account to the delegation ICAs on each host zone
func (k Keeper) TransferExistingDepositsToHostZones(ctx sdk.Context, epochNumber uint64, depositRecords []recordstypes.DepositRecord) {
k.Logger(ctx).Info("Transfering deposit records...")
Expand Down
83 changes: 0 additions & 83 deletions x/stakeibc/keeper/deposit_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,89 +207,6 @@ func (s *KeeperTestSuite) SetupDepositRecords() DepositRecordsTestCase {
}
}

func (s *KeeperTestSuite) TestCreateDepositRecordsForEpoch_Successful() {
// Set host zones
hostZones := []stakeibctypes.HostZone{
{
ChainId: "HOST1",
HostDenom: "denom1",
},
{
ChainId: "HOST2",
HostDenom: "denom2",
},
{
ChainId: "HOST3",
HostDenom: "denom3",
},
}
for _, hostZone := range hostZones {
s.App.StakeibcKeeper.SetHostZone(s.Ctx, hostZone)
}

// Create depoist records for two epochs
s.App.StakeibcKeeper.CreateDepositRecordsForEpoch(s.Ctx, 1)
s.App.StakeibcKeeper.CreateDepositRecordsForEpoch(s.Ctx, 2)

expectedDepositRecords := []recordstypes.DepositRecord{
// Epoch 1
{
Id: 0,
Amount: sdkmath.ZeroInt(),
Denom: "denom1",
HostZoneId: "HOST1",
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: 1,
},
{
Id: 1,
Amount: sdkmath.ZeroInt(),
Denom: "denom2",
HostZoneId: "HOST2",
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: 1,
},
{
Id: 2,
Amount: sdkmath.ZeroInt(),
Denom: "denom3",
HostZoneId: "HOST3",
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: 1,
},
// Epoch 2
{
Id: 3,
Amount: sdkmath.ZeroInt(),
Denom: "denom1",
HostZoneId: "HOST1",
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: 2,
},
{
Id: 4,
Amount: sdkmath.ZeroInt(),
Denom: "denom2",
HostZoneId: "HOST2",
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: 2,
},
{
Id: 5,
Amount: sdkmath.ZeroInt(),
Denom: "denom3",
HostZoneId: "HOST3",
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: 2,
},
}

// Confirm deposit records
actualDepositRecords := s.App.RecordsKeeper.GetAllDepositRecord(s.Ctx)
s.Require().Equal(len(expectedDepositRecords), len(actualDepositRecords), "number of deposit records")
s.Require().Equal(expectedDepositRecords, actualDepositRecords, "deposit records")
}

// Helper function to check the state after transferring deposit records
// This assumes the last X transfers failed
func (s *KeeperTestSuite) CheckStateAfterTransferringDepositRecords(tc DepositRecordsTestCase, numTransfersFailed int) {
Expand Down
120 changes: 0 additions & 120 deletions x/stakeibc/keeper/epoch_elapsed_shares_test.go

This file was deleted.

80 changes: 80 additions & 0 deletions x/stakeibc/keeper/epoch_tracker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package keeper

import (
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cast"

epochstypes "github.com/Stride-Labs/stride/v18/x/epochs/types"
"github.com/Stride-Labs/stride/v18/x/stakeibc/types"
)

Expand Down Expand Up @@ -60,3 +66,77 @@ func (k Keeper) GetAllEpochTracker(ctx sdk.Context) (list []types.EpochTracker)

return
}

// Update the epoch information in the stakeibc epoch tracker
func (k Keeper) UpdateEpochTracker(ctx sdk.Context, epochInfo epochstypes.EpochInfo) (epochNumber uint64, err error) {
epochNumber, err = cast.ToUint64E(epochInfo.CurrentEpoch)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not convert epoch number to uint64: %v", err))
return 0, err
}
epochDurationNano, err := cast.ToUint64E(epochInfo.Duration.Nanoseconds())
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not convert epoch duration to uint64: %v", err))
return 0, err
}
nextEpochStartTime, err := cast.ToUint64E(epochInfo.CurrentEpochStartTime.Add(epochInfo.Duration).UnixNano())
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Could not convert epoch duration to uint64: %v", err))
return 0, err
}
epochTracker := types.EpochTracker{
EpochIdentifier: epochInfo.Identifier,
EpochNumber: epochNumber,
Duration: epochDurationNano,
NextEpochStartTime: nextEpochStartTime,
}
k.SetEpochTracker(ctx, epochTracker)

return epochNumber, nil
}

// helper to get what share of the curr epoch we're through
func (k Keeper) GetStrideEpochElapsedShare(ctx sdk.Context) (sdk.Dec, error) {
// Get the current stride epoch
epochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH)
if !found {
errMsg := fmt.Sprintf("Failed to get epoch tracker for %s", epochstypes.STRIDE_EPOCH)
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), errorsmod.Wrapf(sdkerrors.ErrNotFound, errMsg)
}

// Get epoch start time, end time, and duration
epochDuration, err := cast.ToInt64E(epochTracker.Duration)
if err != nil {
errMsg := fmt.Sprintf("unable to convert epoch duration to int64, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), errorsmod.Wrapf(types.ErrIntCast, errMsg)
}
epochEndTime, err := cast.ToInt64E(epochTracker.NextEpochStartTime)
if err != nil {
errMsg := fmt.Sprintf("unable to convert next epoch start time to int64, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), errorsmod.Wrapf(types.ErrIntCast, errMsg)
}
epochStartTime := epochEndTime - epochDuration

// Confirm the current block time is inside the current epoch's start and end times
currBlockTime := ctx.BlockTime().UnixNano()
if currBlockTime < epochStartTime || currBlockTime > epochEndTime {
errMsg := fmt.Sprintf("current block time %d is not within current epoch (ending at %d)", currBlockTime, epochTracker.NextEpochStartTime)
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), errorsmod.Wrapf(types.ErrInvalidEpoch, errMsg)
}

// Get elapsed share
elapsedTime := currBlockTime - epochStartTime
elapsedShare := sdk.NewDec(elapsedTime).Quo(sdk.NewDec(epochDuration))
if elapsedShare.LT(sdk.ZeroDec()) || elapsedShare.GT(sdk.OneDec()) {
errMsg := fmt.Sprintf("elapsed share (%s) for epoch is not between 0 and 1", elapsedShare)
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), errorsmod.Wrapf(types.ErrInvalidEpoch, errMsg)
}

k.Logger(ctx).Info(fmt.Sprintf("Epoch elapsed share: %v (Block Time: %d, Epoch End Time: %d)", elapsedShare, currBlockTime, epochEndTime))
return elapsedShare, nil
}
Loading
Loading