Skip to content

Commit

Permalink
Merge branch 'develop' into 10750
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 authored Feb 26, 2024
2 parents c09c6f8 + 1291262 commit 7ae7c34
Show file tree
Hide file tree
Showing 57 changed files with 882 additions and 1,864 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.envrc
.idea/
.vscode/**/*
!.vscode/launch.json
.vscode/launch.json

# Exclude binaries
build/
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [10748](https://github.com/vegaprotocol/vega/issues/10748) - Ensure apply fees cannot fail.
- [10752](https://github.com/vegaprotocol/vega/issues/10752) - Handle amend in place correctly for failure in isolated margin check.
- [10753](https://github.com/vegaprotocol/vega/issues/10753) - Handle the case that a submitted order is `FoK` in isolated margin to not double discount it from position.
- [10136](https://github.com/vegaprotocol/vega/issues/10136) - Assure opening auction uncrossing price gets registered in the perps engine.

## 0.74.3

Expand Down
12 changes: 0 additions & 12 deletions core/datasource/spec/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (

"code.vegaprotocol.io/vega/core/datasource"
"code.vegaprotocol.io/vega/core/datasource/common"
ethcallcommon "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common"
"code.vegaprotocol.io/vega/core/events"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/logging"
vegapb "code.vegaprotocol.io/vega/protos/vega"
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
Expand Down Expand Up @@ -200,16 +198,6 @@ func (e *Engine) sendNewSpecSubscription(ctx context.Context, update updatedSubs
proto.Spec.CreatedAt = update.specActivatedAt.UnixNano()
proto.Spec.Status = vegapb.DataSourceSpec_STATUS_ACTIVE

if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.14") {
def := update.spec.GetDefinition()
switch def.DataSourceType.(type) {
case ethcallcommon.Spec:
// save to re-send after upgrade
// we have to do this because the introduction of the source-chain-id
// changes the hash of the proto struct, which is used in generation of the ID
e.broker.Stage(events.NewOracleSpecEvent(ctx, &vegapb.OracleSpec{ExternalDataSourceSpec: proto}))
}
}
e.broker.Send(events.NewOracleSpecEvent(ctx, &vegapb.OracleSpec{ExternalDataSourceSpec: proto}))
}

Expand Down
79 changes: 0 additions & 79 deletions core/evtforward/forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
snp "code.vegaprotocol.io/vega/core/snapshot"
"code.vegaprotocol.io/vega/core/stats"
"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/proto"
vgtest "code.vegaprotocol.io/vega/libs/test"
Expand Down Expand Up @@ -218,84 +217,6 @@ func getTestChainEvent(txid string) *commandspb.ChainEvent {
}
}

func TestMigrationTo74(t *testing.T) {
evtFwd := getTestEvtFwd(t)
ctx := vgcontext.WithSnapshotInfo(context.Background(), "v0.73.14", true)
evtFwd.top.EXPECT().AllNodeIDs().AnyTimes().Return(testAllPubKeys)

hashes := []string{
crypto.RandomHash(),
crypto.RandomHash(),
crypto.RandomHash(),
crypto.RandomHash(),
}
// old payload, with couple of key
p := types.Payload{
Data: &types.PayloadEventForwarder{
Keys: hashes,
},
}
_, err := evtFwd.LoadState(ctx, &p)
assert.NoError(t, err)

// now get state, and make sure it's all fine, we have move time.
state, _, err := evtFwd.GetState((&types.PayloadEventForwarder{}).Key())
require.Nil(t, err)

// restore the state
var pl snapshot.Payload
proto.Unmarshal(state, &pl)
newP := types.PayloadFromProto(&pl).Data.(*types.PayloadEventForwarder)
assert.Len(t, newP.Buckets, 1)
assert.EqualValues(t, newP.Buckets[0].Hashes, hashes)

// move time forward, should clear the buckets
now := time.Now()
evtFwd.OnTick(context.Background(), now)

state, _, err = evtFwd.GetState((&types.PayloadEventForwarder{}).Key())
require.Nil(t, err)

proto.Unmarshal(state, &pl)
newP2 := types.PayloadFromProto(&pl).Data.(*types.PayloadEventForwarder)
assert.Len(t, newP2.Buckets, 0) // no acks left

// now create new ones

newHashes := []string{
crypto.RandomHash(),
crypto.RandomHash(),
crypto.RandomHash(),
}
evtFwd.time.EXPECT().GetTimeNow().Times(2).Return(now)
evtFwd.Ack(getTestChainEvent(newHashes[0]))
evtFwd.Ack(getTestChainEvent(newHashes[1]))

// increase time
now2 := now.Add(1 * time.Second)
evtFwd.OnTick(context.Background(), now2)

evtFwd.time.EXPECT().GetTimeNow().Times(1).Return(now2)
evtFwd.Ack(getTestChainEvent(newHashes[2]))

expects := map[int64][]string{
now.Unix(): {newHashes[0], newHashes[1]},
now2.Unix(): {newHashes[2]},
}

// get the state and we should have 2 buckets
state, _, err = evtFwd.GetState((&types.PayloadEventForwarder{}).Key())
require.Nil(t, err)

proto.Unmarshal(state, &pl)
newP3 := types.PayloadFromProto(&pl).Data.(*types.PayloadEventForwarder)
assert.Len(t, newP3.Buckets, 2) // 2 buckets

for _, v := range newP3.Buckets {
assert.Len(t, v.Hashes, len(expects[v.Ts]))
}
}

func TestSnapshotRoundTripViaEngine(t *testing.T) {
eventForwarder1 := getTestEvtFwd(t)

Expand Down
15 changes: 1 addition & 14 deletions core/evtforward/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"

"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/proto"
snapshotpb "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"

Expand Down Expand Up @@ -94,24 +93,12 @@ func (f *Forwarder) LoadState(ctx context.Context, p *types.Payload) ([]types.St
return nil, types.ErrUnknownSnapshotType
}

func (f *Forwarder) restore(ctx context.Context, p *types.PayloadEventForwarder) {
func (f *Forwarder) restore(_ context.Context, p *types.PayloadEventForwarder) {
f.ackedEvts = &ackedEvents{
timeService: f.timeService,
events: treeset.NewWith(ackedEvtBucketComparator),
}

// upgrading from 73.12, we need to load previous snapshot format
if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.14") {
// add at 0 time, so it's always way in the past.
bucket := &ackedEvtBucket{
ts: 0,
hashes: []string{},
}
bucket.hashes = append(bucket.hashes, p.Keys...)
f.ackedEvts.events.Add(bucket)
return
}

for _, v := range p.Buckets {
f.ackedEvts.AddAt(v.Ts, v.Hashes...)
}
Expand Down
4 changes: 2 additions & 2 deletions core/execution/common/mark_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ func (mpc *CompositePriceCalculator) GetConfig() *types.CompositePriceConfigurat

// CalculateMarkPrice is called at the end of each mark price calculation interval and calculates the mark price
// using the mark price type methodology.
func (mpc *CompositePriceCalculator) CalculateMarkPrice(t int64, ob *matching.CachedOrderBook, markPriceFrequency time.Duration, initialScalingFactor, slippageFactor, shortRiskFactor, longRiskFactor num.Decimal) *num.Uint {
func (mpc *CompositePriceCalculator) CalculateMarkPrice(t int64, ob *matching.CachedOrderBook, markPriceFrequency time.Duration, initialScalingFactor, slippageFactor, shortRiskFactor, longRiskFactor num.Decimal, leavingAuction bool) *num.Uint {
if mpc.config.CompositePriceType == types.CompositePriceTypeByLastTrade {
// if there are no trades, the mark price remains what it was before.
if len(mpc.trades) > 0 {
mpc.price = mpc.trades[len(mpc.trades)-1].Price
mpc.price = mpc.trades[len(mpc.trades)-1].Price.Clone()
}
mpc.trades = []*types.Trade{}
return mpc.price
Expand Down
7 changes: 6 additions & 1 deletion core/execution/common/mark_price_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ func CalculateTimeWeightedAverageBookPrice(timeToPrice map[int64]*num.Uint, t in
}
var timeWeight num.Decimal
if totalDuration.IsZero() {
timeWeight = num.DecimalZero()
if len(keys) == 1 {
// if there's just one observation it should get all the weight
timeWeight = num.DecimalOne()
} else {
timeWeight = num.DecimalZero()
}
} else {
timeWeight = num.DecimalFromInt64(duration).Div(totalDuration)
}
Expand Down
50 changes: 0 additions & 50 deletions core/execution/engine_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import (
"time"

"code.vegaprotocol.io/vega/core/assets"
ethcallcommon "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common"
"code.vegaprotocol.io/vega/core/execution/common"
"code.vegaprotocol.io/vega/core/execution/future"
"code.vegaprotocol.io/vega/core/execution/spot"
"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/proto"
"code.vegaprotocol.io/vega/logging"
)
Expand Down Expand Up @@ -165,10 +163,6 @@ func (e *Engine) restoreSpotMarket(ctx context.Context, em *types.ExecSpotMarket

func (e *Engine) restoreMarket(ctx context.Context, em *types.ExecMarket) (*future.Market, error) {
marketConfig := em.Market
// ensure the default chain ID is set, can be removed after protocol upgrade
if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.14") {
e.ensureChainIDSet(marketConfig)
}

if len(marketConfig.ID) == 0 {
return nil, ErrNoMarketID
Expand Down Expand Up @@ -250,50 +244,6 @@ func (e *Engine) restoreMarket(ctx context.Context, em *types.ExecMarket) (*futu
return mkt, nil
}

func (e *Engine) ensureChainIDSet(marketConfig *types.Market) {
if perp := marketConfig.GetPerps(); perp != nil {
if perp.Perps.DataSourceSpecForSettlementData != nil && perp.Perps.DataSourceSpecForSettlementData.Data != nil {
switch ct := perp.Perps.DataSourceSpecForSettlementData.Data.DataSourceType.(type) {
case ethcallcommon.Spec:
if ct.SourceChainID == 0 {
ct.SourceChainID = e.npv.chainID
perp.Perps.DataSourceSpecForSettlementData.Data.DataSourceType = ct
}
}
}
if perp.Perps.DataSourceSpecForSettlementSchedule != nil && perp.Perps.DataSourceSpecForSettlementSchedule.Data != nil {
switch ct := perp.Perps.DataSourceSpecForSettlementSchedule.Data.DataSourceType.(type) {
case ethcallcommon.Spec:
if ct.SourceChainID == 0 {
ct.SourceChainID = e.npv.chainID
perp.Perps.DataSourceSpecForSettlementSchedule.Data.DataSourceType = ct
}
}
}
return
}
if future := marketConfig.GetFuture(); future != nil {
if future.Future.DataSourceSpecForSettlementData != nil && future.Future.DataSourceSpecForSettlementData.Data != nil {
switch ft := future.Future.DataSourceSpecForSettlementData.Data.DataSourceType.(type) {
case ethcallcommon.Spec:
if ft.SourceChainID == 0 {
ft.SourceChainID = e.npv.chainID
future.Future.DataSourceSpecForSettlementData.Data.DataSourceType = ft
}
}
}
if future.Future.DataSourceSpecForTradingTermination != nil && future.Future.DataSourceSpecForTradingTermination.Data != nil {
switch ft := future.Future.DataSourceSpecForTradingTermination.Data.DataSourceType.(type) {
case ethcallcommon.Spec:
if ft.SourceChainID == 0 {
ft.SourceChainID = e.npv.chainID
future.Future.DataSourceSpecForTradingTermination.Data.DataSourceType = ft
}
}
}
}
}

func (e *Engine) restoreMarketsStates(ctx context.Context, ems []*types.ExecMarket) ([]types.StateProvider, error) {
e.futureMarkets = map[string]*future.Market{}

Expand Down
25 changes: 15 additions & 10 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ func (m *Market) BlockEnd(ctx context.Context) {
t.UnixNano(),
m.matching,
m.mtmDelta,
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, m.mkt.LinearSlippageFactor, m.risk.GetRiskFactors().Short, m.risk.GetRiskFactors().Long)
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, m.mkt.LinearSlippageFactor, m.risk.GetRiskFactors().Short, m.risk.GetRiskFactors().Long, false)
m.nextInternalCompositePriceCalc = t.Add(m.internalCompositePriceFrequency)
if (prevInternalCompositePrice == nil || !m.internalCompositePriceCalculator.GetPrice().EQ(prevInternalCompositePrice) || m.settlement.HasTraded()) &&
!m.getCurrentInternalCompositePrice().IsZero() {
Expand All @@ -1068,7 +1068,7 @@ func (m *Market) BlockEnd(ctx context.Context) {
t.UnixNano(),
m.matching,
m.mtmDelta,
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, m.mkt.LinearSlippageFactor, m.risk.GetRiskFactors().Short, m.risk.GetRiskFactors().Long)
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, m.mkt.LinearSlippageFactor, m.risk.GetRiskFactors().Short, m.risk.GetRiskFactors().Long, false)
// if we don't have an alternative configuration (and schedule) for the mark price the we push the mark price to the perp as a new datapoint
// on the standard mark price
if m.internalCompositePriceCalculator == nil && m.perp &&
Expand Down Expand Up @@ -1570,30 +1570,33 @@ func (m *Market) leaveAuction(ctx context.Context, now time.Time) {
updatedOrders = append(
updatedOrders, uncrossedOrder.PassiveOrdersAffected...)
}

t := m.timeService.GetTimeNow().UnixNano()
m.markPriceCalculator.CalculateBookMarkPriceAtTimeT(m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, m.mkt.LinearSlippageFactor, m.risk.GetRiskFactors().Short, m.risk.GetRiskFactors().Long, t, m.matching)
m.markPriceCalculator.CalculateMarkPrice(
m.timeService.GetTimeNow().UnixNano(),
t,
m.matching,
m.mtmDelta,
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin,
m.mkt.LinearSlippageFactor,
m.risk.GetRiskFactors().Short,
m.risk.GetRiskFactors().Long)
m.risk.GetRiskFactors().Long,
true)

if wasOpeningAuction && (m.getCurrentMarkPrice().IsZero()) {
m.markPriceCalculator.OverridePrice(m.lastTradedPrice)
}

if m.perp {
if m.internalCompositePriceCalculator != nil {
m.internalCompositePriceCalculator.CalculateBookMarkPriceAtTimeT(m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin, m.mkt.LinearSlippageFactor, m.risk.GetRiskFactors().Short, m.risk.GetRiskFactors().Long, t, m.matching)
m.internalCompositePriceCalculator.CalculateMarkPrice(
m.timeService.GetTimeNow().UnixNano(),
t,
m.matching,
m.internalCompositePriceFrequency,
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin,
m.mkt.LinearSlippageFactor,
m.risk.GetRiskFactors().Short,
m.risk.GetRiskFactors().Long)
m.risk.GetRiskFactors().Long,
true)

if wasOpeningAuction && (m.getCurrentInternalCompositePrice().IsZero()) {
m.internalCompositePriceCalculator.OverridePrice(m.lastTradedPrice)
Expand Down Expand Up @@ -4347,7 +4350,8 @@ func (m *Market) terminateMarket(ctx context.Context, finalState types.MarketSta
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin,
m.mkt.LinearSlippageFactor,
m.risk.GetRiskFactors().Short,
m.risk.GetRiskFactors().Long)
m.risk.GetRiskFactors().Long,
false)

if m.internalCompositePriceCalculator != nil {
m.internalCompositePriceCalculator.CalculateMarkPrice(
Expand All @@ -4357,7 +4361,8 @@ func (m *Market) terminateMarket(ctx context.Context, finalState types.MarketSta
m.tradableInstrument.MarginCalculator.ScalingFactors.InitialMargin,
m.mkt.LinearSlippageFactor,
m.risk.GetRiskFactors().Short,
m.risk.GetRiskFactors().Long)
m.risk.GetRiskFactors().Long,
false)
}

if m.perp {
Expand Down
Loading

0 comments on commit 7ae7c34

Please sign in to comment.