From 8e7c6c63d44419e4ba1e962bea5011a4a2e047d6 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Mon, 27 Feb 2023 17:49:33 -0600 Subject: [PATCH] [oracle] Move store operations outside of iterator --- x/oracle/keeper/keeper.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x/oracle/keeper/keeper.go b/x/oracle/keeper/keeper.go index c1ba6e7067..ca5835a7a9 100755 --- a/x/oracle/keeper/keeper.go +++ b/x/oracle/keeper/keeper.go @@ -386,6 +386,7 @@ 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() { @@ -393,12 +394,15 @@ func (k Keeper) AddPriceSnapshot(ctx sdk.Context, snapshot types.PriceSnapshot) } // 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)) {