Skip to content

Commit b3c0e9d

Browse files
committed
accounts/usbwallet: two phase Ledger refreshes to avoid Windows bug
1 parent 470b793 commit b3c0e9d

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

accounts/usbwallet/ledger.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -234,32 +234,11 @@ func (hub *LedgerHub) rescan() {
234234
hub.lock.Lock()
235235
defer hub.lock.Unlock()
236236

237-
// Iterate over all attached devices and fetch those seemingly Ledger
238-
present := make(map[uint16]bool)
239-
devices, _ := hub.ctx.ListDevices(func(desc *usb.Descriptor) bool {
240-
// Discard all devices not advertizing as Ledger
241-
ledger := false
242-
for _, id := range ledgerDeviceIDs {
243-
if desc.Vendor == id.Vendor && desc.Product == id.Product {
244-
ledger = true
245-
}
246-
}
247-
if !ledger {
248-
return false
249-
}
250-
// If we have a Ledger, mark as still present, or open as new
251-
id := uint16(desc.Bus)<<8 + uint16(desc.Address)
252-
if _, known := hub.wallets[id]; known {
253-
// Track it's presence, but don't open again
254-
present[id] = true
255-
return false
256-
}
257-
// New Ledger device, open it for communication
258-
return true
259-
})
260-
// Drop any tracker wallet which disconnected
237+
// Iterate over all connected Ledger devices and do a heartbeat test
261238
for id, wallet := range hub.wallets {
262-
if !present[id] {
239+
// If the device doesn't respond (io error on Windows, no device on Linux), drop
240+
if err := wallet.resolveVersion(); err == usb.ERROR_IO || err == usb.ERROR_NO_DEVICE {
241+
// Wallet disconnected or at least in a useless state
263242
if wallet.address == (common.Address{}) {
264243
glog.V(logger.Info).Infof("ledger wallet [%03d.%03d] disconnected", wallet.device.Bus, wallet.device.Address)
265244
} else {
@@ -279,6 +258,26 @@ func (hub *LedgerHub) rescan() {
279258
wallet.device.Close()
280259
}
281260
}
261+
// Iterate over all attached devices and fetch those seemingly Ledger
262+
devices, _ := hub.ctx.ListDevices(func(desc *usb.Descriptor) bool {
263+
// Discard all devices not advertizing as Ledger
264+
ledger := false
265+
for _, id := range ledgerDeviceIDs {
266+
if desc.Vendor == id.Vendor && desc.Product == id.Product {
267+
ledger = true
268+
}
269+
}
270+
if !ledger {
271+
return false
272+
}
273+
// If we have an already known Ledger, skip opening it
274+
id := uint16(desc.Bus)<<8 + uint16(desc.Address)
275+
if _, known := hub.wallets[id]; known {
276+
return false
277+
}
278+
// New Ledger device, open it for communication
279+
return true
280+
})
282281
// Start tracking all wallets which newly appeared
283282
var err error
284283
for _, device := range devices {

0 commit comments

Comments
 (0)