From 890736b1aeeae7ba641f0c77ead5a6f4ccc2e52a Mon Sep 17 00:00:00 2001 From: ze97286 Date: Wed, 27 Sep 2023 21:07:58 +0100 Subject: [PATCH 1/2] chore: migration support for positions in market activity tracker --- CHANGELOG.md | 1 + core/execution/common/market_activity_tracker.go | 15 +++++++++++++++ core/execution/future/market.go | 7 +++++++ 3 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47a50142280..64be257bf8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,7 @@ - [9564](https://github.com/vegaprotocol/vega/issues/9564) - Fix error message for too many staking tiers. - [8421](https://github.com/vegaprotocol/vega/issues/8421) - Markets that spend too long in opening auction should be cancelled. - [9575](https://github.com/vegaprotocol/vega/issues/9575) - Expand SLA statistics by required liquidity and notional volumes. +- [9590](https://github.com/vegaprotocol/vega/issues/9590) - Restore positions for market activity tracker on migration from old version. ### 🐛 Fixes diff --git a/core/execution/common/market_activity_tracker.go b/core/execution/common/market_activity_tracker.go index 03c372e21f5..ca899bd3ead 100644 --- a/core/execution/common/market_activity_tracker.go +++ b/core/execution/common/market_activity_tracker.go @@ -124,6 +124,16 @@ func (mat *MarketActivityTracker) OnMinEpochsInTeamForRewardEligibilityUpdated(_ return nil } +// NeedsInitialisation is a heuristc migration - if there is no time weighted position data when restoring from snapshot, we will restore +// positions from the market. This will only happen on the one time migration from a version preceding the new metrics. If we're already on a +// new version, either there are no timeweighted positions and no positions or there are time weighted positions and they will not be restored. +func (mat *MarketActivityTracker) NeedsInitialisation(asset, market string) bool { + if tracker, ok := mat.getMarketTracker(asset, market); ok { + return len(tracker.twPosition) == 0 + } + return false +} + // GetProposer returns the proposer of the market or empty string if the market doesn't exist. func (mat *MarketActivityTracker) GetProposer(market string) string { for _, markets := range mat.assetToMarketTrackers { @@ -451,6 +461,11 @@ func (mat *MarketActivityTracker) getMarketTracker(asset, market string) (*marke return tracker, true } +// RestorePosition restores a position as if it were acquired at the beginning of the epoch. This is purely for migration from an old version. +func (mat *MarketActivityTracker) RestorePosition(asset, party, market string, pos int64, price *num.Uint, positionFactor num.Decimal) { + mat.RecordPosition(asset, party, market, pos, price, positionFactor, mat.epochStartTime) +} + // RecordPosition passes the position of the party in the asset/market to the market tracker to be recorded. func (mat *MarketActivityTracker) RecordPosition(asset, party, market string, pos int64, price *num.Uint, positionFactor num.Decimal, time time.Time) { if tracker, ok := mat.getMarketTracker(asset, market); ok { diff --git a/core/execution/future/market.go b/core/execution/future/market.go index 52eb46ef4f4..993ed51ca6d 100644 --- a/core/execution/future/market.go +++ b/core/execution/future/market.go @@ -834,6 +834,13 @@ func (m *Market) PostRestore(ctx context.Context) error { m.log.Panic("failed to get bonus distribution account", logging.Error(err)) } + // if loading from an old snapshot we're restoring positions using the position engine + if m.marketActivityTracker.NeedsInitialisation(m.settlementAsset, m.mkt.ID) { + for _, mp := range m.position.Positions() { + m.marketActivityTracker.RestorePosition(m.settlementAsset, mp.Party(), m.mkt.ID, mp.Size(), mp.Price(), m.positionFactor) + } + } + return nil } From 8cf18ae12760a5d492d8dcf9053baca08cd73d3b Mon Sep 17 00:00:00 2001 From: ze97286 Date: Wed, 27 Sep 2023 21:52:38 +0100 Subject: [PATCH 2/2] chore: do not restore 0 positions --- core/execution/future/market.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/execution/future/market.go b/core/execution/future/market.go index 993ed51ca6d..ca72fc79076 100644 --- a/core/execution/future/market.go +++ b/core/execution/future/market.go @@ -837,7 +837,9 @@ func (m *Market) PostRestore(ctx context.Context) error { // if loading from an old snapshot we're restoring positions using the position engine if m.marketActivityTracker.NeedsInitialisation(m.settlementAsset, m.mkt.ID) { for _, mp := range m.position.Positions() { - m.marketActivityTracker.RestorePosition(m.settlementAsset, mp.Party(), m.mkt.ID, mp.Size(), mp.Price(), m.positionFactor) + if mp.Size() != 0 { + m.marketActivityTracker.RestorePosition(m.settlementAsset, mp.Party(), m.mkt.ID, mp.Size(), mp.Price(), m.positionFactor) + } } }