Skip to content

Commit

Permalink
client: startup improvements and minor fixes
Browse files Browse the repository at this point in the history
* client/{core,webserver}: loud and clear startup messages

* client/core: fix resumed trades possibly mis-setting coinslocked
  • Loading branch information
chappjc committed Oct 27, 2020
1 parent 8a69e99 commit a01e403
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
35 changes: 27 additions & 8 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 6 additions & 3 deletions client/core/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}
Expand Down
3 changes: 2 additions & 1 deletion client/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit a01e403

Please sign in to comment.