/
core.go
616 lines (559 loc) · 20.4 KB
/
core.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// +build !js
// package core contains everything needed to configure and run a 0x Mesh node.
package core
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/0xProject/0x-mesh/db"
"github.com/0xProject/0x-mesh/ethereum"
"github.com/0xProject/0x-mesh/ethereum/blockwatch"
"github.com/0xProject/0x-mesh/expirationwatch"
"github.com/0xProject/0x-mesh/keys"
"github.com/0xProject/0x-mesh/loghooks"
"github.com/0xProject/0x-mesh/meshdb"
"github.com/0xProject/0x-mesh/p2p"
"github.com/0xProject/0x-mesh/rpc"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/0xProject/0x-mesh/zeroex/orderwatch"
"github.com/albrow/stringset"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/event"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/google/uuid"
p2pcrypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
peerstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
log "github.com/sirupsen/logrus"
"github.com/xeipuuv/gojsonschema"
)
const (
blockWatcherRetentionLimit = 20
ethereumRPCRequestTimeout = 30 * time.Second
ethWatcherPollingInterval = 1 * time.Minute
peerConnectTimeout = 60 * time.Second
checkNewAddrInterval = 20 * time.Second
expirationPollingInterval = 50 * time.Millisecond
)
// Config is a set of configuration options for 0x Mesh.
type Config struct {
// Verbosity is the logging verbosity: 0=panic, 1=fatal, 2=error, 3=warn, 4=info, 5=debug 6=trace
Verbosity int `envvar:"VERBOSITY" default:"2"`
// DataDir is the directory to use for persisting all data, including the
// database and private key files.
DataDir string `envvar:"DATA_DIR" default:"0x_mesh"`
// P2PListenPort is the port on which to listen for new peer connections.
P2PListenPort int `envvar:"P2P_LISTEN_PORT"`
// EthereumRPCURL is the URL of an Etheruem node which supports the JSON RPC
// API.
EthereumRPCURL string `envvar:"ETHEREUM_RPC_URL" json:"-"`
// EthereumNetworkID is the network ID to use when communicating with
// Ethereum.
EthereumNetworkID int `envvar:"ETHEREUM_NETWORK_ID"`
// UseBootstrapList is whether to use the predetermined list of peers to
// bootstrap the DHT and peer discovery.
UseBootstrapList bool `envvar:"USE_BOOTSTRAP_LIST" default:"true"`
// OrderExpirationBuffer is the amount of time before the order's stipulated expiration time
// that you'd want it pruned from the Mesh node.
OrderExpirationBuffer time.Duration `envvar:"ORDER_EXPIRATION_BUFFER" default:"10s"`
// BlockPollingInterval is the polling interval to wait before checking for a new Ethereum block
// that might contain transactions that impact the fillability of orders stored by Mesh. Different
// networks have different block producing intervals: POW networks are typically slower (e.g., Mainnet)
// and POA networks faster (e.g., Kovan) so one should adjust the polling interval accordingly.
BlockPollingInterval time.Duration `envvar:"BLOCK_POLLING_INTERVAL" default:"5s"`
// EthereumRPCMaxContentLength is the maximum request Content-Length accepted by the backing Ethereum RPC
// endpoint used by Mesh. Geth & Infura both limit a request's content length to 1024 * 512 Bytes. Parity
// and Alchemy have much higher limits. When batch validating 0x orders, we will fit as many orders into a
// request without crossing the max content length. The default value is appropriate for operators using Geth
// or Infura. If using Alchemy or Parity, feel free to double the default max in order to reduce the
// number of RPC calls made by Mesh.
EthereumRPCMaxContentLength int `envvar:"ETHEREUM_RPC_MAX_CONTENT_LENGTH" default:"524288"`
}
type snapshotInfo struct {
Snapshot *db.Snapshot
ExpirationTimestamp time.Time
}
type App struct {
config Config
peerID peer.ID
privKey p2pcrypto.PrivKey
db *meshdb.MeshDB
node *p2p.Node
networkID int
blockWatcher *blockwatch.Watcher
orderWatcher *orderwatch.Watcher
ethWatcher *ethereum.ETHWatcher
orderValidator *zeroex.OrderValidator
orderJSONSchema *gojsonschema.Schema
meshMessageJSONSchema *gojsonschema.Schema
snapshotExpirationWatcher *expirationwatch.Watcher
muIdToSnapshotInfo sync.Mutex
idToSnapshotInfo map[string]snapshotInfo
}
func New(config Config) (*App, error) {
// Configure logger
// TODO(albrow): Don't use global variables for log settings.
log.SetLevel(log.Level(config.Verbosity))
log.AddHook(loghooks.NewKeySuffixHook())
// Load private key and add peer ID hook.
privKeyPath := filepath.Join(config.DataDir, "keys", "privkey")
privKey, err := initPrivateKey(privKeyPath)
if err != nil {
return nil, err
}
peerID, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return nil, err
}
log.AddHook(loghooks.NewPeerIDHook(peerID))
if config.EthereumRPCMaxContentLength < maxOrderSizeInBytes {
return nil, fmt.Errorf("Cannot set `EthereumRPCMaxContentLength` to be less then maxOrderSizeInBytes: %d", maxOrderSizeInBytes)
}
config = unquoteConfig(config)
// Initialize db
databasePath := filepath.Join(config.DataDir, "db")
meshDB, err := meshdb.New(databasePath)
if err != nil {
return nil, err
}
// Check if the DB has been previously intialized with a different networkId
if err = initNetworkID(config.EthereumNetworkID, meshDB); err != nil {
return nil, err
}
// Initialize the ETH client, which will be used by various watchers.
ethClient, err := ethclient.Dial(config.EthereumRPCURL)
if err != nil {
return nil, err
}
// Initialize block watcher (but don't start it yet).
blockWatcherClient, err := blockwatch.NewRpcClient(config.EthereumRPCURL, ethereumRPCRequestTimeout)
if err != nil {
return nil, err
}
topics := orderwatch.GetRelevantTopics()
blockWatcherConfig := blockwatch.Config{
MeshDB: meshDB,
PollingInterval: config.BlockPollingInterval,
StartBlockDepth: ethrpc.LatestBlockNumber,
BlockRetentionLimit: blockWatcherRetentionLimit,
WithLogs: true,
Topics: topics,
Client: blockWatcherClient,
}
blockWatcher := blockwatch.New(blockWatcherConfig)
// Initialize the order validator
orderValidator, err := zeroex.NewOrderValidator(ethClient, config.EthereumNetworkID, config.EthereumRPCMaxContentLength)
if err != nil {
return nil, err
}
// Initialize order watcher (but don't start it yet).
orderWatcher, err := orderwatch.New(meshDB, blockWatcher, orderValidator, config.EthereumNetworkID, config.OrderExpirationBuffer)
if err != nil {
return nil, err
}
// Initialize the ETH balance watcher (but don't start it yet).
ethWatcher, err := ethereum.NewETHWatcher(ethWatcherPollingInterval, ethClient, config.EthereumNetworkID)
if err != nil {
return nil, err
}
// TODO(albrow): Call Add for all existing makers/signers in the database.
snapshotExpirationWatcher := expirationwatch.New(0 * time.Second)
orderJSONSchema, err := setupOrderSchemaValidator()
if err != nil {
return nil, err
}
meshMessageJSONSchema, err := setupMeshMessageSchemaValidator()
if err != nil {
return nil, err
}
app := &App{
config: config,
privKey: privKey,
peerID: peerID,
db: meshDB,
networkID: config.EthereumNetworkID,
blockWatcher: blockWatcher,
orderWatcher: orderWatcher,
ethWatcher: ethWatcher,
orderValidator: orderValidator,
orderJSONSchema: orderJSONSchema,
meshMessageJSONSchema: meshMessageJSONSchema,
snapshotExpirationWatcher: snapshotExpirationWatcher,
idToSnapshotInfo: map[string]snapshotInfo{},
}
log.WithFields(map[string]interface{}{
"config": config,
"version": "development",
}).Info("finished initializing core.App")
return app, nil
}
// unquoteConfig removes quotes (if needed) from each string field in config.
func unquoteConfig(config Config) Config {
if unquotedEthereumRPCURL, err := strconv.Unquote(config.EthereumRPCURL); err == nil {
config.EthereumRPCURL = unquotedEthereumRPCURL
}
if unquotedDataDir, err := strconv.Unquote(config.DataDir); err == nil {
config.DataDir = unquotedDataDir
}
return config
}
func getPubSubTopic(networkID int) string {
return fmt.Sprintf("/0x-orders/network/%d/version/1", networkID)
}
func getRendezvous(networkID int) string {
return fmt.Sprintf("/0x-mesh/network/%d/version/1", networkID)
}
func initPrivateKey(path string) (p2pcrypto.PrivKey, error) {
privKey, err := keys.GetPrivateKeyFromPath(path)
if err == nil {
return privKey, nil
} else if os.IsNotExist(err) {
// If the private key doesn't exist, generate one.
log.Info("No private key found. Generating a new one.")
return keys.GenerateAndSavePrivateKey(path)
}
// For any other type of error, return it.
return nil, err
}
func initNetworkID(networkID int, meshDB *meshdb.MeshDB) error {
metadata, err := meshDB.GetMetadata()
if err != nil {
if _, ok := err.(db.NotFoundError); ok {
// No stored metadata found (first startup)
metadata = &meshdb.Metadata{EthereumNetworkID: networkID}
if err := meshDB.SaveMetadata(metadata); err != nil {
return err
}
return nil
}
return err
}
// on subsequent startups, verify we are on the same network
if metadata.EthereumNetworkID != networkID {
err := fmt.Errorf("expected networkID to be %d but got %d", metadata.EthereumNetworkID, networkID)
log.WithError(err).Error("Mesh previously started on different Ethereum network; switch networks or remove DB")
return err
}
return nil
}
func (app *App) Start(ctx context.Context) error {
// Create a child context so that we can preemptively cancel if there is an
// error.
innerCtx, cancel := context.WithCancel(ctx)
defer cancel()
// Below, we will start several independent goroutines. We use separate
// channels to communicate errors and a waitgroup to wait for all goroutines
// to exit.
wg := &sync.WaitGroup{}
// Close the database when the context is canceled.
wg.Add(1)
go func() {
defer wg.Done()
<-innerCtx.Done()
app.db.Close()
}()
// Set up and start the snapshot expiration watcher.
wg.Add(1)
go func() {
defer wg.Done()
for expiredSnapshots := range app.snapshotExpirationWatcher.ExpiredItems() {
for _, expiredSnapshot := range expiredSnapshots {
app.muIdToSnapshotInfo.Lock()
delete(app.idToSnapshotInfo, expiredSnapshot.ID)
app.muIdToSnapshotInfo.Unlock()
}
}
}()
snapshotExpirationWatcherErrChan := make(chan error, 1)
wg.Add(1)
go func() {
defer wg.Done()
snapshotExpirationWatcherErrChan <- app.snapshotExpirationWatcher.Watch(innerCtx, expirationPollingInterval)
}()
// Start the order watcher.
orderWatcherErrChan := make(chan error, 1)
wg.Add(1)
go func() {
defer wg.Done()
log.Info("starting order watcher")
orderWatcherErrChan <- app.orderWatcher.Watch(innerCtx)
}()
// Start the ETH balance watcher.
// TODO(fabio): Subscribe to the ETH balance updates and update them in the DB
// for future use by the order storing algorithm.
ethWatcherErrChan := make(chan error, 1)
wg.Add(1)
go func() {
defer wg.Done()
log.Info("starting ETH balance watcher")
ethWatcherErrChan <- app.ethWatcher.Watch(innerCtx)
}()
// Backfill block events if needed. This is a blocking call so we won't
// continue set up until its finished.
if err := app.blockWatcher.BackfillEventsIfNeeded(innerCtx); err != nil {
return err
}
// Start the block watcher.
blockWatcherErrChan := make(chan error, 1)
wg.Add(1)
go func() {
defer wg.Done()
log.Info("starting block watcher")
blockWatcherErrChan <- app.blockWatcher.Watch(innerCtx)
}()
// Initialize the p2p node.
nodeConfig := p2p.Config{
Topic: getPubSubTopic(app.config.EthereumNetworkID),
ListenPort: app.config.P2PListenPort,
Insecure: false,
PrivateKey: app.privKey,
MessageHandler: app,
RendezvousString: getRendezvous(app.config.EthereumNetworkID),
UseBootstrapList: app.config.UseBootstrapList,
}
var err error
app.node, err = p2p.New(innerCtx, nodeConfig)
if err != nil {
return err
}
// Start the p2p node.
p2pErrChan := make(chan error, 1)
wg.Add(1)
go func() {
defer wg.Done()
addrs := app.node.Multiaddrs()
log.WithFields(map[string]interface{}{
"addresses": addrs,
}).Info("starting p2p node")
wg.Add(1)
go func() {
defer wg.Done()
app.periodicallyCheckForNewAddrs(innerCtx, addrs)
}()
p2pErrChan <- app.node.Start()
}()
// If any error channel returns a non-nil error, we cancel the inner context
// and return the error. Note that this means we only return the first error
// that occurs.
select {
case err := <-p2pErrChan:
if err != nil {
log.WithError(err).Error("p2p node exited with error")
cancel()
return err
}
case err := <-orderWatcherErrChan:
if err != nil {
log.WithError(err).Error("order watcher exited with error")
cancel()
return err
}
case err := <-blockWatcherErrChan:
log.WithError(err).Error("block watcher exited with error")
if err != nil {
cancel()
return err
}
case err := <-ethWatcherErrChan:
log.WithError(err).Error("eth watcher exited with error")
if err != nil {
cancel()
return err
}
case err := <-snapshotExpirationWatcherErrChan:
log.WithError(err).Error("snapshot expiration watcher exited with error")
if err != nil {
cancel()
return err
}
}
// Wait for all goroutines to exit. If we reached here it means we are done
// and there are no errors.
wg.Wait()
return nil
}
func (app *App) periodicallyCheckForNewAddrs(ctx context.Context, startingAddrs []ma.Multiaddr) {
// TODO(albrow): There might be a more efficient way to do this if we have access to
// an event bus. See: https://github.com/libp2p/go-libp2p/issues/467
seenAddrs := stringset.New()
for _, addr := range startingAddrs {
seenAddrs.Add(addr.String())
}
ticker := time.NewTicker(checkNewAddrInterval)
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
newAddrs := app.node.Multiaddrs()
for _, addr := range newAddrs {
if !seenAddrs.Contains(addr.String()) {
log.WithFields(map[string]interface{}{
"address": addr,
}).Info("found new listen address")
seenAddrs.Add(addr.String())
}
}
}
}
}
// ErrSnapshotNotFound is the error returned when a snapshot not found with a particular id
type ErrSnapshotNotFound struct {
id string
}
func (e ErrSnapshotNotFound) Error() string {
return fmt.Sprintf("No snapshot found with id: %s. To create a new snapshot, send a request with an empty snapshotID", e.id)
}
// GetOrders retrieves paginated orders from the Mesh DB at a specific snapshot in time. Passing an empty
// string as `snapshotID` creates a new snapshot and returns the first set of results. To fetch all orders,
// continue to make requests supplying the `snapshotID` returned from the first request. After 1 minute of not
// received further requests referencing a specific snapshot, the snapshot expires and can no longer be used.
func (app *App) GetOrders(page, perPage int, snapshotID string) (*rpc.GetOrdersResponse, error) {
ordersInfos := []*zeroex.AcceptedOrderInfo{}
if perPage <= 0 {
return &rpc.GetOrdersResponse{
OrdersInfos: ordersInfos,
SnapshotID: snapshotID,
}, nil
}
var snapshot *db.Snapshot
if snapshotID == "" {
// Create a new snapshot
snapshotID = uuid.New().String()
var err error
snapshot, err = app.db.Orders.GetSnapshot()
if err != nil {
return nil, err
}
expirationTimestamp := time.Now().Add(1 * time.Minute)
app.snapshotExpirationWatcher.Add(expirationTimestamp, snapshotID)
app.muIdToSnapshotInfo.Lock()
app.idToSnapshotInfo[snapshotID] = snapshotInfo{
Snapshot: snapshot,
ExpirationTimestamp: expirationTimestamp,
}
app.muIdToSnapshotInfo.Unlock()
} else {
// Try and find an existing snapshot
app.muIdToSnapshotInfo.Lock()
info, ok := app.idToSnapshotInfo[snapshotID]
if !ok {
app.muIdToSnapshotInfo.Unlock()
return nil, ErrSnapshotNotFound{id: snapshotID}
}
snapshot = info.Snapshot
// Reset the snapshot's expiry
app.snapshotExpirationWatcher.Remove(info.ExpirationTimestamp, snapshotID)
expirationTimestamp := time.Now().Add(1 * time.Minute)
app.snapshotExpirationWatcher.Add(expirationTimestamp, snapshotID)
app.idToSnapshotInfo[snapshotID] = snapshotInfo{
Snapshot: snapshot,
ExpirationTimestamp: expirationTimestamp,
}
app.muIdToSnapshotInfo.Unlock()
}
notRemovedFilter := app.db.Orders.IsRemovedIndex.ValueFilter([]byte{0})
var selectedOrders []*meshdb.Order
err := snapshot.NewQuery(notRemovedFilter).Offset(page * perPage).Max(perPage).Run(&selectedOrders)
if err != nil {
return nil, err
}
for _, order := range selectedOrders {
ordersInfos = append(ordersInfos, &zeroex.AcceptedOrderInfo{
OrderHash: order.Hash,
SignedOrder: order.SignedOrder,
FillableTakerAssetAmount: order.FillableTakerAssetAmount,
})
}
getOrdersResponse := &rpc.GetOrdersResponse{
SnapshotID: snapshotID,
OrdersInfos: ordersInfos,
}
return getOrdersResponse, nil
}
// AddOrders can be used to add orders to Mesh. It validates the given orders
// and if they are valid, will store and eventually broadcast the orders to peers.
func (app *App) AddOrders(signedOrdersRaw []*json.RawMessage) (*zeroex.ValidationResults, error) {
allValidationResults := &zeroex.ValidationResults{
Accepted: []*zeroex.AcceptedOrderInfo{},
Rejected: []*zeroex.RejectedOrderInfo{},
}
schemaValidOrders := []*zeroex.SignedOrder{}
for _, signedOrderRaw := range signedOrdersRaw {
signedOrderBytes := []byte(*signedOrderRaw)
result, err := app.schemaValidateOrder(signedOrderBytes)
if err != nil {
signedOrder := &zeroex.SignedOrder{}
if err := signedOrder.UnmarshalJSON(signedOrderBytes); err != nil {
signedOrder = nil
}
log.WithField("signedOrderRaw", string(signedOrderBytes)).Info("Unexpected error while attempting to validate signedOrderJSON against schema")
allValidationResults.Rejected = append(allValidationResults.Rejected, &zeroex.RejectedOrderInfo{
SignedOrder: signedOrder,
Kind: MeshValidation,
Status: zeroex.RejectedOrderStatus{
Code: ROInvalidSchemaCode,
Message: "order did not pass JSON-schema validation: Malformed JSON or empty payload",
},
})
continue
}
if !result.Valid() {
log.WithField("signedOrderRaw", string(signedOrderBytes)).Info("Order failed schema validation")
status := zeroex.RejectedOrderStatus{
Code: ROInvalidSchemaCode,
Message: fmt.Sprintf("order did not pass JSON-schema validation: %s", result.Errors()),
}
signedOrder := &zeroex.SignedOrder{}
if err := signedOrder.UnmarshalJSON(signedOrderBytes); err != nil {
signedOrder = nil
}
allValidationResults.Rejected = append(allValidationResults.Rejected, &zeroex.RejectedOrderInfo{
SignedOrder: signedOrder,
Kind: MeshValidation,
Status: status,
})
continue
}
signedOrder := &zeroex.SignedOrder{}
if err := signedOrder.UnmarshalJSON(signedOrderBytes); err != nil {
// This error should never happen since the signedOrder already passed the JSON schema validation above
log.WithField("signedOrderRaw", string(signedOrderBytes)).Error("Failed to unmarshal SignedOrder")
return nil, err
}
schemaValidOrders = append(schemaValidOrders, signedOrder)
}
validationResults, err := app.validateOrders(schemaValidOrders)
if err != nil {
return nil, err
}
for _, orderInfo := range validationResults.Accepted {
allValidationResults.Accepted = append(allValidationResults.Accepted, orderInfo)
}
for _, orderInfo := range validationResults.Rejected {
allValidationResults.Rejected = append(allValidationResults.Rejected, orderInfo)
}
for _, acceptedOrderInfo := range allValidationResults.Accepted {
err = app.orderWatcher.Add(acceptedOrderInfo)
if err != nil {
return nil, err
}
}
return allValidationResults, nil
}
// AddPeer can be used to manually connect to a new peer.
func (app *App) AddPeer(peerInfo peerstore.PeerInfo) error {
return app.node.Connect(peerInfo, peerConnectTimeout)
}
// SubscribeToOrderEvents let's one subscribe to order events emitted by the OrderWatcher
func (app *App) SubscribeToOrderEvents(sink chan<- []*zeroex.OrderEvent) event.Subscription {
subscription := app.orderWatcher.Subscribe(sink)
return subscription
}