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

Remove using an iterator from updating TWAP records #7266

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#7203](https://github.com/osmosis-labs/osmosis/pull/7203) Make a maximum number of pools of 100 billion.
* [#7259](https://github.com/osmosis-labs/osmosis/pull/7259) Lower gas and CPU overhead of chargeTakerFee (in every swap)
* [#7258](https://github.com/osmosis-labs/osmosis/pull/7258) Remove an iterator call in CL swaps and spot price calls.
* [#7266](https://github.com/osmosis-labs/osmosis/pull/7266) Remove an iterator call in updating TWAP records

### Bug Fixes

Expand Down
6 changes: 3 additions & 3 deletions x/twap/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func (k Keeper) EndBlock(ctx sdk.Context) {
// - the number of records does not match expected relative to the
// number of denoms in the pool.
func (k Keeper) updateRecords(ctx sdk.Context, poolId uint64) error {
// Will only err if pool doesn't have most recent entry set
records, err := k.GetAllMostRecentRecordsForPool(ctx, poolId)
denoms, err := k.poolmanagerKeeper.RouteGetPoolDenoms(ctx, poolId)
if err != nil {
return err
}

denoms, err := k.poolmanagerKeeper.RouteGetPoolDenoms(ctx, poolId)
// Will only err if pool doesn't have most recent entry set
records, err := k.GetAllMostRecentRecordsForPoolWithDenoms(ctx, poolId, denoms)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions x/twap/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ func (k Keeper) GetAllMostRecentRecordsForPool(ctx sdk.Context, poolId uint64) (
return types.GetAllMostRecentTwapsForPool(store, poolId)
}

// GetAllMostRecentRecordsForPool returns all most recent twap records
// (in state representation) for the provided pool id.
func (k Keeper) GetAllMostRecentRecordsForPoolWithDenoms(ctx sdk.Context, poolId uint64, denoms []string) ([]types.TwapRecord, error) {
Copy link
Member

@mattverse mattverse Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have unit test for this method?

store := ctx.KVStore(k.storeKey)
// if length != 2, use iterator
Copy link
Member

@mattverse mattverse Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 🌮

if len(denoms) != 2 {
return types.GetAllMostRecentTwapsForPool(store, poolId)
}
// else, directly fetch the key.
asset0Denom, asset1Denom, err := types.LexicographicalOrderDenoms(denoms[0], denoms[1])
if err != nil {
return []types.TwapRecord{}, err
}
record, err := types.GetMostRecentTwapForPool(store, poolId, asset0Denom, asset1Denom)
return []types.TwapRecord{record}, err
}

// getAllHistoricalTimeIndexedTWAPs returns all historical TWAPs indexed by time.
func (k Keeper) GetAllHistoricalTimeIndexedTWAPs(ctx sdk.Context) ([]types.TwapRecord, error) {
return osmoutils.GatherValuesFromStorePrefix(ctx.KVStore(k.storeKey), []byte(types.HistoricalTWAPTimeIndexPrefix), types.ParseTwapFromBz)
Expand Down
6 changes: 6 additions & 0 deletions x/twap/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func GetAllMostRecentTwapsForPool(store sdk.KVStore, poolId uint64) ([]TwapRecor
return osmoutils.GatherValuesFromStore(store, []byte(startPrefix), []byte(endPrefix), ParseTwapFromBz)
}

func GetMostRecentTwapForPool(store sdk.KVStore, poolId uint64, denom1, denom2 string) (TwapRecord, error) {
key := FormatMostRecentTWAPKey(poolId, denom1, denom2)
bz := store.Get(key)
return ParseTwapFromBz(bz)
}
Comment on lines +95 to +99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would it be better to have this within keeper since it touches store?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having this here makes sense given prev code structure (e.g GetAllMostRecentTwapsForPool also lives in types/keys.go)


func ParseTwapFromBz(bz []byte) (twap TwapRecord, err error) {
if len(bz) == 0 {
return TwapRecord{}, errors.New("twap not found")
Expand Down