Skip to content

Commit

Permalink
Merge pull request #9591 from vegaprotocol/fix-mat
Browse files Browse the repository at this point in the history
chore: migration support for positions in market activity tracker
  • Loading branch information
jeremyletang authored Sep 27, 2023
2 parents 2b4cf98 + 8cf18ae commit 16004c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions core/execution/common/market_activity_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,15 @@ 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() {
if mp.Size() != 0 {
m.marketActivityTracker.RestorePosition(m.settlementAsset, mp.Party(), m.mkt.ID, mp.Size(), mp.Price(), m.positionFactor)
}
}
}

return nil
}

Expand Down

0 comments on commit 16004c4

Please sign in to comment.