Skip to content

Commit

Permalink
fix: temp fix for price range cache serialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 committed May 23, 2024
1 parent 7ecac47 commit ae2dd69
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
40 changes: 25 additions & 15 deletions core/monitor/price/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
54 changes: 54 additions & 0 deletions core/monitor/price/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
4 changes: 4 additions & 0 deletions core/types/snapshot_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ae2dd69

Please sign in to comment.