Skip to content

Commit

Permalink
[oracle] Move store operations outside of iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
udpatil committed Feb 27, 2023
1 parent 69c6655 commit 8e7c6c6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,19 +386,23 @@ func (k Keeper) AddPriceSnapshot(ctx sdk.Context, snapshot types.PriceSnapshot)
k.SetPriceSnapshot(ctx, snapshot)

var lastOutOfRangeSnapshotTimestamp int64 = -1
timestampsToDelete := []int64{}
// we need to evict old snapshots (except for one that is out of range)
k.IteratePriceSnapshots(ctx, func(snapshot types.PriceSnapshot) (stop bool) {
if snapshot.SnapshotTimestamp+lookbackDuration >= ctx.BlockTime().Unix() {
return true
}
// delete the previous out of range snapshot
if lastOutOfRangeSnapshotTimestamp >= 0 {
k.DeletePriceSnapshot(ctx, lastOutOfRangeSnapshotTimestamp)
timestampsToDelete = append(timestampsToDelete, lastOutOfRangeSnapshotTimestamp)
}
// update last out of range snapshot
lastOutOfRangeSnapshotTimestamp = snapshot.SnapshotTimestamp
return false
})
for _, ts := range timestampsToDelete {
k.DeletePriceSnapshot(ctx, ts)
}
}

func (k Keeper) IteratePriceSnapshots(ctx sdk.Context, handler func(snapshot types.PriceSnapshot) (stop bool)) {
Expand Down

0 comments on commit 8e7c6c6

Please sign in to comment.