From ae2dd69247e00ddce762e07c0a2054d0a5ab48d5 Mon Sep 17 00:00:00 2001 From: ze97286 Date: Thu, 23 May 2024 17:29:57 +0100 Subject: [PATCH] fix: temp fix for price range cache serialisation --- core/monitor/price/snapshot.go | 40 +++++++++++++-------- core/monitor/price/snapshot_test.go | 54 +++++++++++++++++++++++++++++ core/types/snapshot_nodes.go | 4 +++ 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/core/monitor/price/snapshot.go b/core/monitor/price/snapshot.go index 208e2a8477..5bd2658161 100644 --- a/core/monitor/price/snapshot.go +++ b/core/monitor/price/snapshot.go @@ -161,34 +161,44 @@ func newPriceRangeCacheFromSlice(prs []*types.PriceRangeCache) map[*bound]priceR return priceRangesCache } -// SerialisePriceranges expored for testing. -func (e *Engine) SerialisePriceRanges() []*types.PriceRangeCache { - prc := make([]*types.PriceRangeCache, 0, len(e.priceRangesCache)) - for bound, priceRange := range e.priceRangesCache { - prc = append(prc, &types.PriceRangeCache{ - Bound: internalBoundToPriceBoundType(bound), - Range: &types.PriceRange{ - Min: priceRange.MinPrice.Original(), - Max: priceRange.MaxPrice.Original(), - Ref: priceRange.ReferencePrice, - }, - }) - } - +func SortPriceRangeCache(prc []*types.PriceRangeCache) { sort.SliceStable(prc, func(i, j int) bool { if prc[i].Bound.Active != prc[j].Bound.Active { return prc[i].Bound.Active } + if prc[i].Bound.Equal(prc[j].Bound) { + if prc[i].Range.Max.Equal(prc[j].Range.Max) { + if prc[i].Range.Min.Equal(prc[j].Range.Min) { + return prc[i].Range.Ref.LessThan(prc[j].Range.Ref) + } + return prc[i].Range.Min.LessThan(prc[j].Range.Min) + } + return prc[i].Range.Max.LessThan(prc[j].Range.Max) + } if prc[i].Bound.UpFactor.Equal(prc[j].Bound.UpFactor) { if prc[i].Bound.DownFactor.Equal(prc[j].Bound.DownFactor) { return prc[i].Bound.Trigger.Horizon < prc[j].Bound.Trigger.Horizon } return prc[j].Bound.DownFactor.LessThan(prc[i].Bound.DownFactor) } - return prc[j].Bound.UpFactor.GreaterThan(prc[i].Bound.UpFactor) }) +} +// SerialisePriceranges expored for testing. +func (e *Engine) SerialisePriceRanges() []*types.PriceRangeCache { + prc := make([]*types.PriceRangeCache, 0, len(e.priceRangesCache)) + for bound, priceRange := range e.priceRangesCache { + prc = append(prc, &types.PriceRangeCache{ + Bound: internalBoundToPriceBoundType(bound), + Range: &types.PriceRange{ + Min: priceRange.MinPrice.Original(), + Max: priceRange.MaxPrice.Original(), + Ref: priceRange.ReferencePrice, + }, + }) + } + SortPriceRangeCache(prc) return prc } diff --git a/core/monitor/price/snapshot_test.go b/core/monitor/price/snapshot_test.go index 88ee609c74..8711cd5adc 100644 --- a/core/monitor/price/snapshot_test.go +++ b/core/monitor/price/snapshot_test.go @@ -319,3 +319,57 @@ func TestSerialiseBoundsDeterministically(t *testing.T) { } } } + +func TestSortPriceRangeCache(t *testing.T) { + for i := 0; i < 100; i++ { + m := map[int]*types.PriceRangeCache{ + 1: { + Bound: &types.PriceBound{ + Active: true, + UpFactor: num.DecimalE(), + DownFactor: num.DecimalFromFloat(0.5), + Trigger: &types.PriceMonitoringTrigger{ + Horizon: 1, + HorizonDec: num.DecimalFromFloat(2), + Probability: num.DecimalFromFloat(0.5), + AuctionExtension: 1, + }, + }, + Range: &types.PriceRange{ + Min: num.DecimalFromFloat(0.1), + Max: num.DecimalFromFloat(0.3), + Ref: num.DecimalFromFloat(0.5), + }, + }, 2: { + Bound: &types.PriceBound{ + Active: true, + UpFactor: num.DecimalE(), + DownFactor: num.DecimalFromFloat(0.5), + Trigger: &types.PriceMonitoringTrigger{ + Horizon: 1, + HorizonDec: num.DecimalFromFloat(2), + Probability: num.DecimalFromFloat(0.5), + AuctionExtension: 1, + }, + }, + Range: &types.PriceRange{ + Min: num.DecimalFromFloat(0.5), + Max: num.DecimalFromFloat(0.3), + Ref: num.DecimalFromFloat(0.1), + }, + }, + } + prc := []*types.PriceRangeCache{} + for _, v := range m { + prc = append(prc, v) + } + + price.SortPriceRangeCache(prc) + require.Equal(t, "0.1", prc[0].Range.Min.String()) + require.Equal(t, "0.3", prc[0].Range.Max.String()) + require.Equal(t, "0.5", prc[0].Range.Ref.String()) + require.Equal(t, "0.5", prc[1].Range.Min.String()) + require.Equal(t, "0.3", prc[1].Range.Max.String()) + require.Equal(t, "0.1", prc[1].Range.Ref.String()) + } +} diff --git a/core/types/snapshot_nodes.go b/core/types/snapshot_nodes.go index bb7504fb6b..7b13faee55 100644 --- a/core/types/snapshot_nodes.go +++ b/core/types/snapshot_nodes.go @@ -524,6 +524,10 @@ type PriceBound struct { Trigger *PriceMonitoringTrigger } +func (pb *PriceBound) Equal(other *PriceBound) bool { + return pb.Active == other.Active && pb.UpFactor.Equal(other.UpFactor) && pb.DownFactor.Equal(other.DownFactor) && pb.Trigger.String() == other.Trigger.String() +} + type PriceRangeCache struct { Bound *PriceBound Range *PriceRange