Skip to content

Commit

Permalink
Merge pull request #8838 from vegaprotocol/snapshot-fix
Browse files Browse the repository at this point in the history
fix: skip parent markets that are no more
  • Loading branch information
jeremyletang committed Jul 20, 2023
2 parents a0323e3 + d4121a0 commit c7c94d3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- [8853](https://github.com/vegaprotocol/vega/issues/8853) - Liquidity provision amendment bug fixes
- [8862](https://github.com/vegaprotocol/vega/issues/8862) - Fix settlement via governance
- [8772](https://github.com/vegaprotocol/vega/issues/8772) - Checkpoint panic on successor markets.
- [8837](https://github.com/vegaprotocol/vega/issues/8837) - Remove successor entries from snapshot if they will be removed next tick.


## 0.72.1
Expand Down
3 changes: 3 additions & 0 deletions core/execution/engine_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ func (e *Engine) serialise() (snapshot []byte, providers []types.StateProvider,
})
successors := make([]*types.Successors, 0, len(e.successors))
for pid, ids := range e.successors {
if _, ok := e.GetMarket(pid, true); !ok {
continue
}
successors = append(successors, &types.Successors{
ParentMarket: pid,
SuccessorMarkets: ids,
Expand Down
70 changes: 70 additions & 0 deletions core/execution/engine_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,73 @@ func TestValidSettledMarketSnapshot(t *testing.T) {
_, ok := engine2.GetMarket(marketConfig.ID, true)
require.True(t, ok)
}

func TestSuccessorMapSnapshot(t *testing.T) {
ctx := vgcontext.WithTraceID(context.Background(), hex.EncodeToString([]byte("0deadbeef")))
engine := getMockedEngine(t)
engine.collateral.EXPECT().AssetExists(gomock.Any()).AnyTimes().Return(true)
engine.collateral.EXPECT().CreateMarketAccounts(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
engine.collateral.EXPECT().GetMarketLiquidityFeeAccount(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Account{Balance: num.UintZero()}, nil)
engine.collateral.EXPECT().GetInsurancePoolBalance(gomock.Any(), gomock.Any()).AnyTimes().Return(num.UintZero(), true)
engine.collateral.EXPECT().FinalSettlement(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, nil)
engine.collateral.EXPECT().ClearMarket(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, nil)
engine.timeSvc.EXPECT().GetTimeNow().AnyTimes()
engine.broker.EXPECT().Send(gomock.Any()).AnyTimes()
engine.broker.EXPECT().SendBatch(gomock.Any()).AnyTimes()
// engine.oracle.EXPECT().Subscribe(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
engine.statevar.EXPECT().RegisterStateVariable(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
engine.statevar.EXPECT().UnregisterStateVariable(gomock.Any(), gomock.Any()).AnyTimes()
engine.statevar.EXPECT().NewEvent(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
engine.epoch.EXPECT().NotifyOnEpoch(gomock.Any(), gomock.Any()).AnyTimes()
engine.asset.EXPECT().Get(gomock.Any()).AnyTimes().DoAndReturn(func(a string) (*assets.Asset, error) {
as := NewAssetStub(a, 0)
return as, nil
})
// create a market
marketConfig := getMarketConfig()
// ensure CP state doesn't get invalidated the moment the market is settled
engine.OnSuccessorMarketTimeWindowUpdate(ctx, time.Hour)
// now let's set up the settlement and trading terminated callbacks
engine.oracle.EXPECT().Subscribe(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(func(_ context.Context, s spec.Spec, cb spec.OnMatchedData) (spec.SubscriptionID, spec.Unsubscriber, error) {
return spec.SubscriptionID(0), func(_ context.Context, _ spec.SubscriptionID) {}, nil
})
defer engine.ctrl.Finish()
assert.NotNil(t, engine)

err := engine.SubmitMarket(ctx, marketConfig, "", time.Now())
assert.NoError(t, err)
// now let's settle the market by:
// 1. Create successor
successor := marketConfig.DeepClone()
successor.ID = "successor-id"
successor.ParentMarketID = marketConfig.ID
successor.InsurancePoolFraction = num.DecimalFromFloat(1)
successor.State = types.MarketStateProposed
successor.TradingMode = types.MarketTradingModeNoTrading
// submit the successor market
engine.SubmitMarket(ctx, successor, "", time.Now())
engine.OnTick(ctx, time.Now())
// 2. cancel the parent market (before leaving opening auction)
engine.RejectMarket(ctx, marketConfig.ID)
engine.OnTick(ctx, time.Now())

// 3. Check the successor map in the snapshot

keys := engine.Keys()
require.Equal(t, 1, len(keys))
key := keys[0]

// Take the snapshot and hash
b, _, err := engine.GetState(key)
assert.NoError(t, err)
assert.NotEmpty(t, b)

snap := &snapshot.Payload{}
err = proto.Unmarshal(b, snap)
assert.NoError(t, err)

// Check the hashes are the same
execMkts := snap.GetExecutionMarkets()
require.NotNil(t, execMkts)
require.Empty(t, execMkts.Successors)
}

0 comments on commit c7c94d3

Please sign in to comment.