-
Notifications
You must be signed in to change notification settings - Fork 1.7k
refactor (hardware wallet) : reduce the number of threads #9644
Conversation
hw/src/ledger.rs
Outdated
pub fn new(hidapi: Arc<Mutex<hidapi::HidApi>>, exiting: Arc<AtomicBool>) -> Result<Arc<Self>, libusb::Error> { | ||
let manager = Arc::new(Self { | ||
usb: hidapi, | ||
pub fn new(usb: Arc<Mutex<hidapi::HidApi>>) -> Arc<Self> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't make sense that this function returns Result anymore because it can't fail
anymore
hw/src/ledger.rs
Outdated
.ok(); | ||
|
||
Ok(manager) | ||
}) | ||
} | ||
|
||
// Transport Protocol: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix formatting!!!
@niklasad1 what's the status of this? Do you want feedback from @dvdplm @debris ? |
@5chdn Need grab to hardware wallets and test this first. I will change the label when it is good for reviewing hopefully later today |
013abd3
to
aacb057
Compare
334c6fb
to
c26a70f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
c26a70f
to
79ba32d
Compare
👍 needs a 2nd review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits, but overall looks good.
hw/src/lib.rs
Outdated
/// Hardware wallet event handler | ||
/// | ||
/// Note, that this run to completion and race-conditions can't occur but this can | ||
/// therefore starve other events for being process with a spinlock or similar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// therefore starve other events for being process with a spinlock or similar | |
/// stop other events from being processed with a spinlock or similar. |
(but I'm not sure what you mean by "with spinlock or similar" tbh)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spinlock is a bit blurry in this context, should probably say spinning instead.
I mean that if the thread is in an infinite loop or looping for a condition it will prevent other events from being processed. I can rephrase it but not really introduced in this PR
hw/src/lib.rs
Outdated
impl libusb::Hotplug for EventHandler { | ||
fn device_arrived(&mut self, device: libusb::Device) { | ||
// Upgrade reference to an Arc | ||
if let (Some(ledger), Some(trezor)) = (self.ledger.upgrade(), self.trezor.upgrade()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: is it impossible to have one upgrade call return Some
and the other None
? Wouldn't that cause the whole block to be skipped? In other words, are both upgrade()
calls going to return Some
if one of them does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, because the HardwareWalletManager keeps an Arc
for both Ledger
and Trezor
for the entire lifetime of Parity unless it is disabled by the cli arg --no-hardware-wallet
! upgrade()will always succeed as long the
HardwareWalletManageris enabled (the strong count will be at least 1). So, either both
Trezorand
Ledger` is enabled or none is enabled.
However, I admit it is a bit sloppy. Theoretically, interleaving "could" happen if one of the hw-wallets
has been dropped and the other has not been dropped but this is matter in granularity in terms of microseconds and I don't worry too much about this.
I can change it if you like? :P
hw/src/lib.rs
Outdated
|
||
fn device_left(&mut self, device: libusb::Device) { | ||
// Upgrade reference to an Arc | ||
if let (Some(ledger), Some(trezor)) = (self.ledger.upgrade(), self.trezor.upgrade()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question here.
09649d6
to
fa2324a
Compare
fa2324a
to
f099f3d
Compare
Please resolve conflicts :) |
This changes parity to use one thread instead of two to subscribe USB events via the hardware-wallets.
Co-Authored-By: niklasad1 <[email protected]>
Co-Authored-By: niklasad1 <[email protected]>
Co-Authored-By: niklasad1 <[email protected]>
* Clarify bad explaination of `run-to-completion` * Replace magic numbers with constants
f099f3d
to
0329802
Compare
Attempt to close #9622 along with fixing some minor formatting things!
It introduces some boiler-plate code though such as:
Tested successfully both on
Trezor
andLedger
on LinuxProof that number of threads have been reduced: