/
update_price.go
276 lines (247 loc) · 8.9 KB
/
update_price.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package keeper
import (
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/constants"
"sort"
"time"
gometrics "github.com/armon/go-metrics"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
pricefeedmetrics "github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/metrics"
"github.com/dydxprotocol/v4-chain/protocol/lib/metrics"
"github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)
// GetValidMarketPriceUpdates returns a msg containing a list of "valid" price updates that should
// be included in a block. A "valid" price update means:
// 1) All values used to compute the valid price must exist and be valid:
// - the index price exists and is nonzero.
// - the smoothed price exists and is nonzero.
// 2) The smoothed price and the index price must be on the same side compared to the oracle price.
// 3) The proposed price is either the index price or the smoothed price, depending on which is closer to the
// oracle price.
// 4) The proposed price meets the minimum price change ppm requirement.
// Note: the list of market price updates can be empty if there are no "valid" index prices, smoothed prices, and/or
// proposed prices for any market.
func (k Keeper) GetValidMarketPriceUpdates(
ctx sdk.Context,
) *types.MsgUpdateMarketPrices {
defer telemetry.ModuleMeasureSince(
types.ModuleName,
time.Now(),
metrics.GetValidMarketPriceUpdates,
metrics.Latency,
)
// 1. Get all markets from state.
allMarketParamPrices, err := k.GetAllMarketParamPrices(ctx)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("error getting all market param prices: %v", err))
// If there is an error, return an empty list of price updates. We don't want to introduce
// liveness issues due to an error in market state.
}
allMarketParams := make([]types.MarketParam, len(allMarketParamPrices))
for i, marketParamPrice := range allMarketParamPrices {
allMarketParams[i] = marketParamPrice.Param
}
// 2. Get all index prices from in-memory cache.
allIndexPrices := k.indexPriceCache.GetValidMedianPrices(allMarketParams, k.timeProvider.Now())
// 3. Collect all "valid" price updates.
updates := make([]*types.MsgUpdateMarketPrices_MarketPrice, 0, len(allMarketParamPrices))
for _, marketParamPrice := range allMarketParamPrices {
marketId := marketParamPrice.Param.Id
indexPrice, indexPriceExists := allIndexPrices[marketId]
marketMetricsLabel := pricefeedmetrics.GetLabelForMarketId(marketId)
// Skip proposal logic in the event of invalid inputs, which is only likely to occur around network genesis.
if !indexPriceExists {
metrics.IncrCountMetricWithLabels(types.ModuleName, metrics.IndexPriceDoesNotExist, marketMetricsLabel)
// Conditionally escalate log level to error 20s after genesis/restart. We expect that it may take a few
// seconds for the index price to populate after network genesis or a network restart.
logMethod := k.Logger(ctx).Error
if k.IsRecentlyAvailable(ctx, marketId) {
logMethod = k.Logger(ctx).Info
}
logMethod(
"Index price for market does not exist",
constants.MarketIdLogKey,
marketId,
)
continue
}
// Index prices of 0 are unexpected. In this scenario, we skip the proposal logic for the market and report an
// error.
if indexPrice == 0 {
metrics.IncrCountMetricWithLabels(types.ModuleName, metrics.IndexPriceIsZero, marketMetricsLabel)
k.Logger(ctx).Error(
"Unexpected error: index price for market is zero",
constants.MarketIdLogKey,
marketId,
)
continue
}
historicalSmoothedPrices := k.marketToSmoothedPrices.GetHistoricalSmoothedPrices(marketId)
// We generally expect to have a smoothed price history for each market, except during the first few blocks
// after network genesis or a network restart. In this scenario, we use the index price as the smoothed price.
if len(historicalSmoothedPrices) == 0 {
// Conditionally escalate log level to error 20s after genesis/restart. We expect that there will be a delay
// in populating historical smoothed prices after network genesis or a network restart, because they
// depend on present index prices.
logMethod := k.Logger(ctx).Error
if k.IsRecentlyAvailable(ctx, marketId) {
logMethod = k.Logger(ctx).Info
}
logMethod(fmt.Sprintf("Smoothed price for market (%v) does not exist", marketId))
historicalSmoothedPrices = []uint64{indexPrice}
}
smoothedPrice := historicalSmoothedPrices[0]
proposalPrice := getProposalPrice(smoothedPrice, indexPrice, marketParamPrice.Price.Price)
shouldPropose, reasons := shouldProposePrice(
proposalPrice,
marketParamPrice,
indexPrice,
historicalSmoothedPrices,
)
// If the index price would have updated, track how the proposal price changes the update
// decision / amount.
if isAboveRequiredMinPriceChange(marketParamPrice, indexPrice) {
logPriceUpdateBehavior(
k.Logger(ctx),
marketParamPrice,
proposalPrice,
indexPrice,
marketMetricsLabel,
shouldPropose,
reasons,
)
}
if shouldPropose {
// Add as a "valid" price update.
updates = append(
updates,
&types.MsgUpdateMarketPrices_MarketPrice{
MarketId: marketId,
Price: proposalPrice,
},
)
}
}
// 4. Sort price updates by market id in ascending order.
sort.Slice(updates, func(i, j int) bool { return updates[i].MarketId < updates[j].MarketId })
return &types.MsgUpdateMarketPrices{
MarketPriceUpdates: updates,
}
}
func logPriceUpdateBehavior(
logger log.Logger,
marketParamPrice types.MarketParamPrice,
proposalPrice uint64,
indexPrice uint64,
marketMetricsLabel gometrics.Label,
shouldPropose bool,
reasons []proposeCancellationReason,
) {
loggingVerb := "proposed"
if !shouldPropose {
loggingVerb = "not proposed"
// Convert reasons map to a slice of metrics labels and include market label.
labels := make([]gometrics.Label, 0, len(reasons)+1)
labels = append(labels, marketMetricsLabel)
for _, reason := range reasons {
labels = append(labels, metrics.GetLabelForBoolValue(reason.Reason, reason.Value))
}
metrics.IncrCountMetricWithLabels(
types.ModuleName,
metrics.ProposedPriceChangesPriceUpdateDecision,
labels...,
)
}
logger.Info(fmt.Sprintf(
"Proposal price (%v) %v for market (%v), index price (%v), oracle price (%v), min price change (%v)",
proposalPrice,
loggingVerb,
marketParamPrice.Param.Id,
indexPrice,
marketParamPrice.Price.Price,
getMinPriceChangeAmountForMarket(marketParamPrice),
))
}
type proposeCancellationReason struct {
Reason string
Value bool
}
// shouldProposePrice determines if a price should be proposed for a market. It returns the result as well as a list of
// possible reasons why the price should not be proposed that can be used to create metrics labels.
// All of the logic for determining if a price should be proposed was consolidated here to prevent inconsistencies
// between proposal behavior and logging/metrics.
func shouldProposePrice(
proposalPrice uint64,
marketParamPrice types.MarketParamPrice,
indexPrice uint64,
historicalSmoothedPrices []uint64,
) (
shouldPropose bool,
reasons []proposeCancellationReason,
) {
reasons = make([]proposeCancellationReason, 0, 4)
shouldPropose = true
// If any smoothed price crosses the old price compared to the index price, do not update.
reasons = append(
reasons,
proposeCancellationReason{
Reason: metrics.RecentSmoothedPriceCrossesOraclePrice,
Value: false,
},
)
for _, smoothedPrice := range historicalSmoothedPrices {
if isCrossingOldPrice(PriceTuple{
OldPrice: marketParamPrice.Price.Price,
IndexPrice: indexPrice,
NewPrice: smoothedPrice,
}) {
shouldPropose = false
reasons[len(reasons)-1].Value = true
break
}
}
// If the proposal price crosses the old price compared to the index price, do not update.
reasons = append(
reasons,
proposeCancellationReason{
Reason: metrics.ProposedPriceCrossesOraclePrice,
},
)
if isCrossingOldPrice(PriceTuple{
OldPrice: marketParamPrice.Price.Price,
IndexPrice: indexPrice,
NewPrice: proposalPrice,
}) {
shouldPropose = false
reasons[len(reasons)-1].Value = true
}
// If any smoothed price does not meet the min price change, do not update.
reasons = append(
reasons,
proposeCancellationReason{
Reason: metrics.RecentSmoothedPriceDoesNotMeetMinPriceChange,
Value: false,
},
)
for _, smoothedPrice := range historicalSmoothedPrices {
if !isAboveRequiredMinPriceChange(marketParamPrice, smoothedPrice) {
shouldPropose = false
reasons[len(reasons)-1].Value = true
break
}
}
// If the proposal price does not meet the min price change, do not update.
reasons = append(
reasons,
proposeCancellationReason{
Reason: metrics.ProposedPriceDoesNotMeetMinPriceChange,
},
)
if !isAboveRequiredMinPriceChange(marketParamPrice, proposalPrice) {
shouldPropose = false
reasons[len(reasons)-1].Value = true
}
return shouldPropose, reasons
}