Skip to content

Commit

Permalink
client/core: omit TopicWalletPeersRestored on first report
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Jul 22, 2022
1 parent a3dac7e commit f020984
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
10 changes: 6 additions & 4 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,7 @@ func (c *Core) loadWallet(dbWallet *db.Wallet) (*xcWallet, error) {
},
encPass: dbWallet.EncryptedPW,
address: dbWallet.Address,
peerCount: -1, // no count yet
dbID: dbWallet.ID(),
walletType: dbWallet.Type,
broadcasting: new(uint32),
Expand Down Expand Up @@ -4414,7 +4415,7 @@ func (c *Core) prepareTrackedTrade(dc *dexConnection, form *TradeForm, crypter e
}
w.mtx.RLock()
defer w.mtx.RUnlock()
if w.peerCount == 0 {
if w.peerCount < 1 {
return fmt.Errorf("%s wallet has no network peers (check your network or firewall)",
unbip(w.AssetID))
}
Expand Down Expand Up @@ -6966,14 +6967,15 @@ func (c *Core) peerChange(w *xcWallet, numPeers uint32, err error) {

w.mtx.Lock()
wasDisconnected := w.peerCount == 0
w.peerCount = numPeers
w.peerCount = int32(numPeers)
if numPeers == 0 {
w.synced = false
}
w.mtx.Unlock()

// When we get peers after having none, start waiting for re-sync, otherwise
// leave synced alone.
// leave synced alone. This excludes the unknown state (-1) prior to the
// initial peer count report.
if wasDisconnected && numPeers > 0 {
subject, details := c.formatDetails(TopicWalletPeersRestored, w.Info().Name)
c.notify(newWalletConfigNote(TopicWalletPeersRestored, subject, details,
Expand All @@ -6983,7 +6985,7 @@ func (c *Core) peerChange(w *xcWallet, numPeers uint32, err error) {

// Send a WalletStateNote in case Synced or anything else has changed.
if atomic.LoadUint32(w.broadcasting) == 1 {
if (numPeers == 0 || err != nil) && !wasDisconnected {
if (numPeers == 0 || err != nil) && !wasDisconnected { // was connected or initial report
if err != nil {
subject, details := c.formatDetails(TopicWalletCommsWarning,
w.Info().Name, err.Error())
Expand Down
4 changes: 2 additions & 2 deletions client/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2737,8 +2737,8 @@ func TestTrade(t *testing.T) {
t.Fatalf("no error for closing BTC wallet with active orders")
}

// We want to set peerCount to 0, but we'll do this the hard way to ensure
// the peerChange handler works as intended.
// We want to set peerCount to 0 (from 1), but we'll do this the hard way to
// ensure the peerChange handler works as intended.
// dcrWallet.mtx.Lock()
// dcrWallet.peerCount = 0
// dcrWallet.mtx.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion client/core/locale_ntfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var enUS = map[Topic]*translation{
subject: "Wallet unlock error",
template: "Connected to wallet to complete registration at %s, but failed to unlock: %v",
},
// [asset name]
// [asset name, error message]
TopicWalletCommsWarning: {
subject: "Wallet connection issue",
template: "Unable to communicate with %v wallet! Reason: %q",
Expand Down
12 changes: 8 additions & 4 deletions client/core/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ type xcWallet struct {
balance *WalletBalance
pw encode.PassBytes
address string
peerCount uint32
peerCount int32 // -1 means no count yet
monitored uint32 // startWalletSyncMonitor goroutines monitoring sync status
hookedUp bool
synced bool
syncProgress float32

// When wallets are being reconfigured and especially when the wallet type
// or host is being changed, we want to supress "walletstate" notes to
// or host is being changed, we want to suppress "walletstate" notes to
// prevent subscribers from prematurely adopting the new WalletState before
// the new wallet is fully validated and added to the Core wallets map.
// Walletstate notes during reconfiguration can come from the sync loop or
// WalletState notes during reconfiguration can come from the sync loop or
// from the PeersChange callback.
broadcasting *uint32
}
Expand Down Expand Up @@ -173,6 +173,10 @@ func (w *xcWallet) locallyUnlocked() bool {

// state returns the current WalletState.
func (w *xcWallet) state() *WalletState {
var peerCount uint32
if w.peerCount > 0 { // -1 initially
peerCount = uint32(w.peerCount)
}
w.mtx.RLock()
defer w.mtx.RUnlock()
winfo := w.Info()
Expand All @@ -186,7 +190,7 @@ func (w *xcWallet) state() *WalletState {
Address: w.address,
Units: winfo.UnitInfo.AtomicUnit,
Encrypted: len(w.encPass) > 0,
PeerCount: w.peerCount,
PeerCount: peerCount,
Synced: w.synced,
SyncProgress: w.syncProgress,
WalletType: w.walletType,
Expand Down

0 comments on commit f020984

Please sign in to comment.