From a01e403d09c491765d71ac34fc2d60b7898c3596 Mon Sep 17 00:00:00 2001 From: Jonathan Chappelow Date: Sun, 25 Oct 2020 12:17:41 -0500 Subject: [PATCH] client: startup improvements and minor fixes * client/{core,webserver}: loud and clear startup messages * client/core: fix resumed trades possibly mis-setting coinslocked --- client/core/core.go | 35 +++++++++++++++++++++++++++-------- client/core/trade.go | 9 ++++++--- client/webserver/webserver.go | 3 ++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/client/core/core.go b/client/core/core.go index 1240c6ee51..72ead19714 100644 --- a/client/core/core.go +++ b/client/core/core.go @@ -2784,7 +2784,7 @@ func (c *Core) AssetBalance(assetID uint32) (*WalletBalance, error) { func (c *Core) initialize() { accts, err := c.db.Accounts() if err != nil { - c.log.Errorf("Error retrieve accounts from database: %v", err) + c.log.Errorf("Error retrieving accounts from database: %v", err) // panic? } var wg sync.WaitGroup for _, acct := range accts { @@ -2793,7 +2793,7 @@ func (c *Core) initialize() { defer wg.Done() host, err := addrHost(acct.Host) if err != nil { - c.log.Errorf("skipping loading of %s due to address parse error: %w", acct.Host, err) + c.log.Errorf("skipping loading of %s due to address parse error: %v", acct.Host, err) return } dc, err := c.connectDEX(acct) @@ -2844,6 +2844,12 @@ func (c *Core) initialize() { wg.Wait() c.connMtx.RLock() c.log.Infof("Successfully connected to %d out of %d DEX servers", len(c.conns), len(accts)) + for dexName, dc := range c.conns { + activeOrders, _ := c.dbOrders(dc) // non-nil error will load 0 orders, and any subsequent db error will cause a shutdown on dex auth or sooner + if n := len(activeOrders); n > 0 { + c.log.Warnf("\n\n\t **** IMPORTANT: You have %d active orders on %s. LOGIN immediately! **** \n", n, dexName) + } + } c.connMtx.RUnlock() c.refreshUser() } @@ -2930,11 +2936,7 @@ func (c *Core) reFee(dcrWallet *xcWallet, dc *dexConnection) { c.verifyRegistrationFee(dcrWallet.AssetID, dc, acctInfo.FeeCoin, confs) } -// dbTrackers prepares trackedTrades based on active orders and matches in the -// database. Since dbTrackers runs before sign in when wallets are not connected -// or unlocked, wallets and coins are not added to the returned trackers. Use -// resumeTrades with the app Crypter to prepare wallets and coins. -func (c *Core) dbTrackers(dc *dexConnection) (map[order.OrderID]*trackedTrade, error) { +func (c *Core) dbOrders(dc *dexConnection) ([]*db.MetaOrder, error) { // Prepare active orders, according to the DB. dbOrders, err := c.db.ActiveDEXOrders(dc.acct.host) if err != nil { @@ -2969,6 +2971,20 @@ func (c *Core) dbTrackers(dc *dexConnection) (map[order.OrderID]*trackedTrade, e dbOrders = append(dbOrders, dbOrder) } + return dbOrders, nil +} + +// dbTrackers prepares trackedTrades based on active orders and matches in the +// database. Since dbTrackers may run before sign in when wallets are not +// connected or unlocked, wallets and coins are not added to the returned +// trackers. Use resumeTrades with the app Crypter to prepare wallets and coins. +func (c *Core) dbTrackers(dc *dexConnection) (map[order.OrderID]*trackedTrade, error) { + // Prepare active orders, according to the DB. + dbOrders, err := c.dbOrders(dc) + if err != nil { + return nil, err + } + trackers := make(map[order.OrderID]*trackedTrade, len(dbOrders)) for _, dbOrder := range dbOrders { ord := dbOrder.Order @@ -3026,7 +3042,7 @@ func (c *Core) dbTrackers(dc *dexConnection) (map[order.OrderID]*trackedTrade, e copy(pimg[:], metaCancel.MetaData.Proof.Preimage) err = tracker.cancelTrade(co, pimg) if err != nil { - c.log.Errorf("error setting cancel order info %s: %w", co.ID(), err) + c.log.Errorf("Error setting cancel order info %s: %v", co.ID(), err) } // TODO: The trackedTrade.cancel.matches is not being repopulated on // startup. The consequences are that the Filled value will not include @@ -3202,6 +3218,9 @@ func (c *Core) resumeTrades(dc *dexConnection, trackers []*trackedTrade) assetMa notifyErr("Order coin error", "Source coins retrieval error for %s %s: %v", unbip(wallets.fromAsset.ID), tracker.token(), err) continue } + // NOTE: change and changeLocked are not set even if the funding + // coins were loaded from the DB's ChangeCoin. + tracker.coinsLocked = true tracker.coins = mapifyCoins(coins) } diff --git a/client/core/trade.go b/client/core/trade.go index 365a4206d4..a7601524f0 100644 --- a/client/core/trade.go +++ b/client/core/trade.go @@ -127,8 +127,8 @@ func newTrackedTrade(dbOrder *db.MetaOrder, preImg order.Preimage, dc *dexConnec wallets: wallets, preImg: preImg, mktID: marketName(ord.Base(), ord.Quote()), - coins: mapifyCoins(coins), - coinsLocked: true, + coins: mapifyCoins(coins), // must not be nil even if empty + coinsLocked: len(coins) > 0, lockTimeTaker: lockTimeTaker, lockTimeMaker: lockTimeMaker, matches: make(map[order.MatchID]*matchTracker), @@ -186,7 +186,10 @@ func (t *trackedTrade) coreOrderInternal() *Order { // BUY order. // lockedAmount should be called with the mtx >= RLocked. func (t *trackedTrade) lockedAmount() (locked uint64) { - if t.coinsLocked { // implies no swap has been sent + if t.coinsLocked { + // This implies either no swap has been sent, or the trade has been + // resumed on restart after a swap that produced locked change (partial + // fill and still booked) since restarting loads into coins/coinsLocked. for _, coin := range t.coins { locked += coin.Value() } diff --git a/client/webserver/webserver.go b/client/webserver/webserver.go index 2c6b95a731..67db7e6942 100644 --- a/client/webserver/webserver.go +++ b/client/webserver/webserver.go @@ -316,7 +316,8 @@ func (s *WebServer) Connect(ctx context.Context) (*sync.WaitGroup, error) { s.readNotifications(ctx) }() - log.Infof("Web server listening on http://%s", s.addr) + log.Infof("Web server listening on %s", s.addr) + fmt.Printf("\n\t**** OPEN IN YOUR BROWSER TO TRADE ---> http://%s ****\n\n", s.addr) return &wg, nil }