-
Notifications
You must be signed in to change notification settings - Fork 90
/
client.go
338 lines (298 loc) · 12.5 KB
/
client.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package client
import (
"context"
"errors"
"fmt"
"sync"
"time"
appflags "github.com/dydxprotocol/v4-chain/protocol/app/flags"
daemontypes "github.com/dydxprotocol/v4-chain/protocol/daemons/types"
sdklog "cosmossdk.io/log"
"github.com/dydxprotocol/v4-chain/protocol/daemons/flags"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/api"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/constants"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/handler"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/price_fetcher"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/client/types"
libtime "github.com/dydxprotocol/v4-chain/protocol/lib/time"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
"github.com/cometbft/cometbft/libs/log"
)
// Client encapsulates the logic for executing and cleanly stopping all subtasks associated with the
// pricefeed client daemon. Access to the client's internal state is synchronized.
// The pricefeed daemon is a job that periodically queries external exchanges and transmits
// price data to the pricefeed service, which is then used by the application to compute index
// prices for proposing and validating oracle price updates on the blockchain.
// Note: price fetchers manage their own subtasks by blocking on their completion on every subtask run.
// When the price fetcher is stopped, it will wait for all of its own subtasks to complete before returning.
type Client struct {
// daemonStartup tracks whether the daemon has finished startup. The daemon
// cannot be stopped until all persistent daemon subtasks have been launched within `Start`.
daemonStartup sync.WaitGroup
// runningSubtasksWaitGroup tracks the number of running subtasks on the daemon.
// This is used to block the daemon from stopping until all running processes have completed.
runningSubtasksWaitGroup sync.WaitGroup
// tickers tracks the list of tickers that are used to execute subtasks that repeat periodically on the daemon.
// Access to tickers is implicitly synchronized by the daemonStartup WaitGroup.
tickers []*time.Ticker
// stops tracks the list of channels that are used to send a stop signal to subtasks on the daemon.
// Access to stops is implicitly synchronized by the daemonStartup WaitGroup.
stops []chan bool
// Ensure stop only executes one time.
stopDaemon sync.Once
}
func newClient() *Client {
client := &Client{
tickers: []*time.Ticker{},
stops: []chan bool{},
}
// Set the client's daemonStartup state to indicate that the daemon has not finished starting up.
client.daemonStartup.Add(1)
return client
}
// newTickerWithStop creates a new ticker and a channel for iteratively looping through a subtask with a stop signal
// for any subtask kicked off by the client. The ticker and channel are tracked in order to properly clean up and send
// all needed stop signals when the daemon is stopped.
// Note: this method is not synchronized. It is expected to be called from the client's `StartNewClient` method before
// `client.CompleteStartup`.
func (c *Client) newTickerWithStop(intervalMs int) (*time.Ticker, <-chan bool) {
ticker := time.NewTicker(time.Duration(intervalMs) * time.Millisecond)
c.tickers = append(c.tickers, ticker)
stop := make(chan bool)
c.stops = append(c.stops, stop)
return ticker, stop
}
// Stop stops the daemon and all running subtasks. This method is synchronized by the daemonStartup WaitGroup.
func (c *Client) Stop() {
c.stopDaemon.Do(func() {
c.daemonStartup.Wait()
// Send a signal to all tickers and stop channels to stop all running subtasks managed by the client.
for _, stop := range c.stops {
close(stop)
}
for _, ticker := range c.tickers {
ticker.Stop()
}
c.runningSubtasksWaitGroup.Wait()
})
}
// start begins a job that:
// A) periodically queries prices from external data sources and saves the retrieved prices in an
// in-memory datastore
// B) periodically sends the most recent prices to a gRPC server
// C) periodically queries the prices module for the latest market/exchange configuration and then updates
// the shared, in-memory datastore with the latest configuration.
// The exchangeIdToStartupConfig map dictates which exchanges the pricefeed client queries against.
// For all exchanges included in this map, the pricefeed client expects an exchangeQueryDetails and an
// initialExchangeMarketConfig object to be defined in the parameter maps. To initialize an exchange with
// zero markets, pass in an initialExchangeMarketConfig object with an empty map of market tickers for that
// exchange.
// Implementation:
// 1. Establish connections to gRPC servers.
// 2. Validate daemon configuration.
// 3. Initialize synchronized, in-memory shared daemon configuration.
// 4. Start PriceEncoder and PriceFetcher per exchange. Each price fetcher adds itself to the shared
// daemon config.
// 5. Start MarketUpdater subtask to periodically update the market configs.
// 6. Start PriceUpdater to begin broadcasting prices.
func (c *Client) start(ctx context.Context,
daemonFlags flags.DaemonFlags,
appFlags appflags.Flags,
logger log.Logger,
grpcClient daemontypes.GrpcClient,
exchangeIdToStartupConfig map[types.ExchangeId]*types.ExchangeStartupConfig,
exchangeIdToExchangeDetails map[types.ExchangeId]types.ExchangeQueryDetails,
subTaskRunner SubTaskRunner,
) (err error) {
// 1. Establish connections to gRPC servers.
queryConn, err := grpcClient.NewTcpConnection(ctx, appFlags.GrpcAddress)
if err != nil {
logger.Error("Failed to establish gRPC connection to Cosmos gRPC query services", "error", err)
return err
}
// Defer closing gRPC connection until job completes.
defer func() {
if connErr := grpcClient.CloseConnection(queryConn); connErr != nil {
err = connErr
}
}()
daemonConn, err := grpcClient.NewGrpcConnection(ctx, daemonFlags.Shared.SocketAddress)
if err != nil {
logger.Error("Failed to establish gRPC connection to socket address", "error", err)
return err
}
// Defer closing gRPC connection until job completes.
defer func() {
if connErr := grpcClient.CloseConnection(daemonConn); connErr != nil {
err = connErr
}
}()
pricesQueryClient := pricestypes.NewQueryClient(queryConn)
// 2. Validate daemon configuration.
if err := validateDaemonConfiguration(
exchangeIdToStartupConfig,
exchangeIdToExchangeDetails,
); err != nil {
return err
}
// Let the canonical list of exchange feeds be the keys of the map of exchange feed ids to startup configs.
canonicalExchangeIds := make([]types.ExchangeId, 0, len(exchangeIdToStartupConfig))
for exchangeId := range exchangeIdToStartupConfig {
canonicalExchangeIds = append(canonicalExchangeIds, exchangeId)
}
// 3. Initialize synchronized, in-memory shared daemon configuration.
priceFeedMutableMarketConfigs := types.NewPriceFeedMutableMarketConfigs(
canonicalExchangeIds,
)
exchangeToMarketPrices, err := types.NewExchangeToMarketPrices(canonicalExchangeIds)
if err != nil {
return err
}
// 4. Start PriceEncoder and PriceFetcher per exchange.
timeProvider := &libtime.TimeProviderImpl{}
for _exchangeId := range exchangeIdToStartupConfig {
// Assign these within the loop to avoid unexpected values being passed to the goroutines.
exchangeId := _exchangeId
exchangeConfig := exchangeIdToStartupConfig[exchangeId]
// Expect an ExchangeQueryDetails to exist for each supported exchange feed id.
exchangeDetails, exists := exchangeIdToExchangeDetails[exchangeId]
if !exists {
return fmt.Errorf("no exchange details exists for exchangeId: %v", exchangeId)
}
// Instantiate shared buffered channel to be written to by the price fetcher and read from
// by the price encoder.
bCh := make(chan *price_fetcher.PriceFetcherSubtaskResponse, constants.FixedBufferSize)
c.runningSubtasksWaitGroup.Add(1)
go func() {
defer c.runningSubtasksWaitGroup.Done()
subTaskRunner.StartPriceEncoder(
exchangeId,
priceFeedMutableMarketConfigs,
exchangeToMarketPrices,
logger,
bCh,
)
}()
ticker, stop := c.newTickerWithStop(int(exchangeConfig.IntervalMs))
c.runningSubtasksWaitGroup.Add(1)
go func() {
defer c.runningSubtasksWaitGroup.Done()
subTaskRunner.StartPriceFetcher(
ticker,
stop,
priceFeedMutableMarketConfigs,
*exchangeConfig,
exchangeDetails,
&handler.ExchangeQueryHandlerImpl{TimeProvider: timeProvider},
logger,
bCh,
)
}()
}
// 5. Start MarketUpdater go routine to periodically update the market configs.
marketParamUpdaterTicker, marketParamUpdaterStop := c.newTickerWithStop(constants.MarketUpdateIntervalMs)
c.runningSubtasksWaitGroup.Add(1)
go func() {
defer c.runningSubtasksWaitGroup.Done()
subTaskRunner.StartMarketParamUpdater(
ctx,
marketParamUpdaterTicker,
marketParamUpdaterStop,
priceFeedMutableMarketConfigs,
pricesQueryClient,
logger,
)
}()
// 6. Start PriceUpdater to begin broadcasting prices.
// `StartPriceUpdater` does not run in a go-routine since it is used to block indefinitely
// until the pricefeed daemon ends.
// The price updater will read from an in-memory cache and send updates over gRPC for the
// server to read.
priceUpdaterTicker, priceUpdaterStop := c.newTickerWithStop(int(daemonFlags.Price.LoopDelayMs))
// Now that all persistent subtasks have been started and all tickers and stop channels are created,
// signal that the startup process is complete. This needs to be called before entering the
// price updater loop, which loops indefinitely until the daemon is stopped.
c.daemonStartup.Done()
pricefeedClient := api.NewPriceFeedServiceClient(daemonConn)
subTaskRunner.StartPriceUpdater(
ctx,
priceUpdaterTicker,
priceUpdaterStop,
exchangeToMarketPrices,
pricefeedClient,
logger,
)
return nil
}
// StartNewClient initializes and starts a new pricefeed daemon as a subtask of the calling process.
// The pricefeed daemon is a job that periodically queries external exchanges and transmits
// price data to the pricefeed service, which is then used by the application to compute index
// prices for proposing and validating oracle price updates on the blockchain.
// Note: the daemon will panic if it fails to start up.
func StartNewClient(
ctx context.Context,
daemonFlags flags.DaemonFlags,
appFlags appflags.Flags,
logger log.Logger,
grpcClient daemontypes.GrpcClient,
exchangeIdToStartupConfig map[types.ExchangeId]*types.ExchangeStartupConfig,
exchangeIdToExchangeDetails map[types.ExchangeId]types.ExchangeQueryDetails,
subTaskRunner SubTaskRunner,
) (client *Client) {
// Log the daemon flags.
logger.Info(
"Starting pricefeed daemon with flags",
"PriceFlags", daemonFlags.Price,
)
client = newClient()
client.runningSubtasksWaitGroup.Add(1)
go func() {
defer client.runningSubtasksWaitGroup.Done()
err := client.start(
ctx,
daemonFlags,
appFlags,
logger.With(sdklog.ModuleKey, constants.PricefeedDaemonModuleName),
grpcClient,
exchangeIdToStartupConfig,
exchangeIdToExchangeDetails,
subTaskRunner,
)
if err != nil {
logger.Error("Error initializing pricefeed daemon: %w", err.Error())
panic(err)
}
}()
return client
}
// validateDaemonConfiguration validates the daemon configuration.
// The list of exchanges used as keys for the exchangeIdToStartupConfig defines the exchanges used
// by the daemon.
// The daemon configuration is valid iff:
// 1) The exchangeIdToExchangeDetails map has an entry for each exchange.
// 2) The static exchange names map has an entry for each exchange, and each name is unique.
func validateDaemonConfiguration(
exchangeIdToStartupConfig map[types.ExchangeId]*types.ExchangeStartupConfig,
exchangeIdToExchangeDetails map[types.ExchangeId]types.ExchangeQueryDetails,
) (
err error,
) {
// Loop through all exchanges, which are defined by the exchangeIdToStartupConfig map,
// and validate all ids are unique and have a corresponding ExchangeQueryDetails.
exchangeIds := make(map[string]struct{}, len(exchangeIdToStartupConfig))
for exchangeId := range exchangeIdToStartupConfig {
if _, exists := exchangeIds[exchangeId]; exists {
return fmt.Errorf("duplicate exchange id '%v' found for exchangeIds", exchangeId)
}
exchangeIds[exchangeId] = struct{}{}
// Expect an ExchangeQueryDetails to exist for each supported exchange feed id.
if _, exists := exchangeIdToExchangeDetails[exchangeId]; !exists {
return fmt.Errorf("no exchange details exists for exchangeId: %v", exchangeId)
}
}
// Validate that there is at least 1 exchange.
if len(exchangeIds) == 0 {
return errors.New("exchangeIds must not be empty")
}
return nil
}