Skip to content

Commit

Permalink
added verbose tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stackman27 committed Aug 8, 2023
1 parent 21a7552 commit 4ea33fa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 46 deletions.
3 changes: 1 addition & 2 deletions app/upgrades/v17/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func FlipTwapSpotPriceRecords(ctx sdk.Context, pools []poolmanagertypes.PoolI, k
keepers.TwapKeeper.DeleteHistoricalRecord(ctx, oldRecord)
}

// check that the twap record exists
clPoolTwapRecords, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, pool.GetId())
if err != nil {
return err
Expand All @@ -174,7 +173,7 @@ func FlipTwapSpotPriceRecords(ctx sdk.Context, pools []poolmanagertypes.PoolI, k
twapRecord.P0LastSpotPrice, twapRecord.P1LastSpotPrice = oldRecord.P1LastSpotPrice, oldRecord.P0LastSpotPrice

keepers.TwapKeeper.StoreNewRecord(ctx, twapRecord)
keepers.TwapKeeper.DeleteOldRecord(ctx, oldRecord)
keepers.TwapKeeper.DeleteMostRecentRecord(ctx, oldRecord)
}
}

Expand Down
91 changes: 80 additions & 11 deletions app/upgrades/v17/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,49 @@ func (suite *UpgradeTestSuite) TestUpgrade() {
}

existingPool := suite.PrepareConcentratedPoolWithCoins("ibc/1480B8FD20AD5FCAE81EA87584D269547DD4D436843C1D20F15E00EB64743EF4", "uosmo")
existingPool2 := suite.PrepareConcentratedPoolWithCoins("akash", "uosmo")

// create few TWAP records for the pools
t1 := dummyTwapRecord(existingPool.GetId(), time.Now().Add(-time.Hour*24), "ibc/1480B8FD20AD5FCAE81EA87584D269547DD4D436843C1D20F15E00EB64743EF4", "uosmo", sdk.NewDec(10),
sdk.OneDec().MulInt64(10*10),
sdk.OneDec().MulInt64(3),
sdk.ZeroDec())

t2 := dummyTwapRecord(existingPool.GetId(), time.Now().Add(-time.Hour*24), "ibc/1480B8FD20AD5FCAE81EA87584D269547DD4D436843C1D20F15E00EB64743EF4", "uosmo", sdk.NewDec(30),
sdk.OneDec().MulInt64(10*10+10),
sdk.OneDec().MulInt64(5),
sdk.ZeroDec())

t3 := dummyTwapRecord(existingPool.GetId(), time.Now().Add(-time.Hour*24), "ibc/1480B8FD20AD5FCAE81EA87584D269547DD4D436843C1D20F15E00EB64743EF4", "uosmo", sdk.NewDec(20),
sdk.OneDec().MulInt64(10*10+10*5),
sdk.OneDec().MulInt64(10),
sdk.ZeroDec())

t4 := dummyTwapRecord(existingPool2.GetId(), time.Now().Add(-time.Hour*24), "akash", "uosmo", sdk.NewDec(10),
sdk.OneDec().MulInt64(10*10*10),
sdk.OneDec().MulInt64(5),
sdk.ZeroDec())

t5 := dummyTwapRecord(existingPool2.GetId(), time.Now().Add(-time.Hour*24), "akash", "uosmo", sdk.NewDec(20),
sdk.OneDec().MulInt64(10),
sdk.OneDec().MulInt64(2),
sdk.ZeroDec())

suite.App.TwapKeeper.StoreNewRecord(suite.Ctx, t1)
suite.App.TwapKeeper.StoreNewRecord(suite.Ctx, t2)
suite.App.TwapKeeper.StoreNewRecord(suite.Ctx, t3)
suite.App.TwapKeeper.StoreNewRecord(suite.Ctx, t4)
suite.App.TwapKeeper.StoreNewRecord(suite.Ctx, t5)

clPoolTwapRecordPreUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, existingPool.GetId())
clPool2TwapRecordPreUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, existingPool2.GetId())
suite.Require().NoError(err)

return expectedCoinsUsedInUpgradeHandler, lastPoolID, clPoolTwapRecordPreUpgrade
return expectedCoinsUsedInUpgradeHandler, lastPoolID, clPool2TwapRecordPreUpgrade

},
func(ctx sdk.Context, keepers *keepers.AppKeepers, expectedCoinsUsedInUpgradeHandler sdk.Coins, lastPoolID uint64, clPoolTwapRecordPreUpgrade []types.TwapRecord) {
lastPoolID = clPoolTwapRecordPreUpgrade[0].GetPoolId()
lastPoolIdMinusOne := lastPoolID - 1
stakingParams := suite.App.StakingKeeper.GetParams(suite.Ctx)
stakingParams.BondDenom = "uosmo"
suite.App.StakingKeeper.SetParams(suite.Ctx, stakingParams)
Expand All @@ -168,7 +194,19 @@ func (suite *UpgradeTestSuite) TestUpgrade() {
communityPoolAddress := suite.App.AccountKeeper.GetModuleAddress(distrtypes.ModuleName)
communityPoolBalancePre := suite.App.BankKeeper.GetAllBalances(suite.Ctx, communityPoolAddress)

clPoolTwapRecordHistoricalPoolIndexPreUpgrade, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, lastPoolID)
clPool1TwapRecordPreUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, lastPoolIdMinusOne)
suite.Require().NoError(err)

clPool1TwapRecordHistoricalPoolIndexPreUpgrade, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, lastPoolIdMinusOne)
suite.Require().NoError(err)

clPool2TwapRecordPreUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, lastPoolID)
suite.Require().NoError(err)

clPool2TwapRecordHistoricalPoolIndexPreUpgrade, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, lastPoolID)
suite.Require().NoError(err)

clPoolsTwapRecordHistoricalTimeIndexPreUpgrade, err := keepers.TwapKeeper.GetAllHistoricalTimeIndexedTWAPs(ctx)
suite.Require().NoError(err)

// Run upgrade handler.
Expand All @@ -177,20 +215,51 @@ func (suite *UpgradeTestSuite) TestUpgrade() {
suite.App.BeginBlocker(suite.Ctx, abci.RequestBeginBlock{})
})

clPoolTwapRecordPostUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, lastPoolID)
clPool1TwapRecordPostUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, lastPoolIdMinusOne)
suite.Require().NoError(err)

clPool1TwapRecordHistoricalPoolIndexPostUpgrade, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, lastPoolIdMinusOne)
suite.Require().NoError(err)

clPool2TwapRecordPostUpgrade, err := keepers.TwapKeeper.GetAllMostRecentRecordsForPool(ctx, lastPoolID)
suite.Require().NoError(err)

clPool2TwapRecordHistoricalPoolIndexPostUpgrade, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, lastPoolID)
suite.Require().NoError(err)

clPoolTwapRecordHistoricalPoolIndexPostUpgrade, err := keepers.TwapKeeper.GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx, lastPoolID)
clPoolsTwapRecordHistoricalTimeIndexPostUpgrade, err := keepers.TwapKeeper.GetAllHistoricalTimeIndexedTWAPs(ctx)
suite.Require().NoError(err)

for i := range clPoolTwapRecordHistoricalPoolIndexPostUpgrade {
suite.Require().Equal(clPoolTwapRecordHistoricalPoolIndexPreUpgrade[i].Asset0Denom, clPoolTwapRecordHistoricalPoolIndexPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPoolTwapRecordHistoricalPoolIndexPreUpgrade[i].Asset1Denom, clPoolTwapRecordHistoricalPoolIndexPostUpgrade[i].Asset0Denom)
suite.Require().NotEmpty(clPool1TwapRecordPostUpgrade, "Most recent TWAP records should not be empty after upgrade.")
suite.Require().NotEmpty(clPool1TwapRecordHistoricalPoolIndexPostUpgrade, "Historical Pool Index TWAP record should not be empty after upgrade")
suite.Require().NotEmpty(clPool2TwapRecordPostUpgrade, "Most recent TWAP records should not be empty after upgrade.")
suite.Require().NotEmpty(clPool2TwapRecordHistoricalPoolIndexPostUpgrade, "Historical Pool Index TWAP record should not be empty after upgrade")

for i := range clPool1TwapRecordPostUpgrade {
suite.Require().Equal(clPool1TwapRecordPreUpgrade[i].Asset0Denom, clPool1TwapRecordPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPool1TwapRecordPreUpgrade[i].Asset1Denom, clPool1TwapRecordPostUpgrade[i].Asset0Denom)
}

for i := range clPoolTwapRecordPostUpgrade {
suite.Require().Equal(clPoolTwapRecordPreUpgrade[i].Asset0Denom, clPoolTwapRecordPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPoolTwapRecordPreUpgrade[i].Asset1Denom, clPoolTwapRecordPostUpgrade[i].Asset0Denom)
for i := range clPool1TwapRecordHistoricalPoolIndexPostUpgrade {
suite.Require().Equal(clPool1TwapRecordHistoricalPoolIndexPreUpgrade[i].Asset0Denom, clPool1TwapRecordHistoricalPoolIndexPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPool1TwapRecordHistoricalPoolIndexPreUpgrade[i].Asset1Denom, clPool1TwapRecordHistoricalPoolIndexPostUpgrade[i].Asset0Denom)
}

for i := range clPool2TwapRecordPostUpgrade {
suite.Require().Equal(clPool2TwapRecordPreUpgrade[i].Asset0Denom, clPool2TwapRecordPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPool2TwapRecordPreUpgrade[i].Asset1Denom, clPool2TwapRecordPostUpgrade[i].Asset0Denom)
}

for i := range clPool2TwapRecordHistoricalPoolIndexPostUpgrade {
suite.Require().Equal(clPool2TwapRecordHistoricalPoolIndexPreUpgrade[i].Asset0Denom, clPool2TwapRecordHistoricalPoolIndexPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPool2TwapRecordHistoricalPoolIndexPreUpgrade[i].Asset1Denom, clPool2TwapRecordHistoricalPoolIndexPostUpgrade[i].Asset0Denom)
}

for i := range clPoolsTwapRecordHistoricalTimeIndexPostUpgrade {
if (clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].PoolId == lastPoolID) || (clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].PoolId == lastPoolIdMinusOne) {
suite.Require().Equal(clPoolsTwapRecordHistoricalTimeIndexPreUpgrade[i].Asset0Denom, clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].Asset1Denom)
suite.Require().Equal(clPoolsTwapRecordHistoricalTimeIndexPreUpgrade[i].Asset1Denom, clPoolsTwapRecordHistoricalTimeIndexPostUpgrade[i].Asset0Denom)
}
}

// Retrieve the community pool balance (and the feePool balance) after the upgrade
Expand Down
7 changes: 4 additions & 3 deletions x/twap/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (k Keeper) GetAllHistoricalPoolIndexedTWAPs(ctx sdk.Context) ([]types.TwapR

// GetAllHistoricalPoolIndexedTWAPsForPoolId returns HistoricalTwapRecord for a pool give poolId.
func (k Keeper) GetAllHistoricalPoolIndexedTWAPsForPoolId(ctx sdk.Context, poolId uint64) ([]types.TwapRecord, error) {
return osmoutils.GatherValuesFromStorePrefixWithKeyParser(ctx.KVStore(k.storeKey), types.FormatKeyPoolTwapRecords(poolId), types.ParseTwapHistoricalPoolIndexedRecordFromBz)
return osmoutils.GatherValuesFromStorePrefix(ctx.KVStore(k.storeKey), types.FormatKeyPoolTwapRecords(poolId), types.ParseTwapFromBz)
}

// StoreNewRecord stores a record, in both the most recent record store and historical stores.
Expand All @@ -174,8 +174,9 @@ func (k Keeper) StoreNewRecord(ctx sdk.Context, twap types.TwapRecord) {
k.StoreHistoricalTWAP(ctx, twap)
}

// DeleteOldRecord deletes a record, in most recent record store
func (k Keeper) DeleteOldRecord(ctx sdk.Context, twap types.TwapRecord) {
// DeleteMostRecentRecord deletes a given record in most recent record store.
// Note that if there are entries in historical indexes for this record, they are not deleted by this method.
func (k Keeper) DeleteMostRecentRecord(ctx sdk.Context, twap types.TwapRecord) {
store := ctx.KVStore(k.storeKey)
key := types.FormatMostRecentTWAPKey(twap.PoolId, twap.Asset0Denom, twap.Asset1Denom)
store.Delete(key)
Expand Down
31 changes: 1 addition & 30 deletions x/twap/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package types

import (
"errors"
fmt "fmt"
"strconv"
"strings"
"fmt"
time "time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -98,30 +96,3 @@ func ParseTwapFromBz(bz []byte) (twap TwapRecord, err error) {
}
return twap, err
}

// ParseTwapHistoricalPoolIndexedRecordFromBz parses through an existing Twap Record by key and returns Twap Record.
func ParseTwapHistoricalPoolIndexedRecordFromBz(key []byte, value []byte) (TwapRecord, error) {
if len(key) == 0 {
return TwapRecord{}, fmt.Errorf("Invalid twap record key")
}
if len(value) == 0 {
return TwapRecord{}, fmt.Errorf("Invalid twap record value")
}

keyStr := string(key)
twapRecordKeyComponent := strings.Split(keyStr, KeySeparator)

poolId, err := strconv.ParseUint(twapRecordKeyComponent[1], 10, 64)
if err != nil {
return TwapRecord{}, fmt.Errorf("cannot parse poolId key")
}

twapRecord, err := ParseTwapFromBz(value)
if err != nil {
return TwapRecord{}, fmt.Errorf("cannot parse TwapRecord value")
}

twapRecord.PoolId = poolId

return twapRecord, nil
}

0 comments on commit 4ea33fa

Please sign in to comment.