poll_get_balance no longer fails intermittently for zero balance accounts

While polling for a non-zero balance, it's not uncommon for one of the
get_balance requests to fail with EWOULDBLOCK.  Previously when a get_balance
request failure occurred on the last iteration of the polling loop,
poll_get_balance returned an error even though the N-1 iterations may have
successfully retrieved a balance of 0.
This commit is contained in:
Michael Vines 2018-07-26 21:27:44 -07:00 committed by Grimes
parent f11aa4a57b
commit 308d8c254d
1 changed files with 13 additions and 4 deletions

View File

@ -196,11 +196,15 @@ impl ThinClient {
} }
pub fn poll_get_balance(&mut self, pubkey: &PublicKey) -> io::Result<i64> { pub fn poll_get_balance(&mut self, pubkey: &PublicKey) -> io::Result<i64> {
let mut balance; let mut balance_result;
let mut balance_value = -1;
let now = Instant::now(); let now = Instant::now();
loop { loop {
balance = self.get_balance(pubkey); balance_result = self.get_balance(pubkey);
if balance.is_ok() && *balance.as_ref().unwrap() != 0 || now.elapsed().as_secs() > 1 { if balance_result.is_ok() {
balance_value = *balance_result.as_ref().unwrap();
}
if balance_value > 0 || now.elapsed().as_secs() > 1 {
break; break;
} }
sleep(Duration::from_millis(100)); sleep(Duration::from_millis(100));
@ -214,7 +218,12 @@ impl ThinClient {
) )
.to_owned(), .to_owned(),
); );
balance if balance_value >= 0 {
Ok(balance_value)
} else {
assert!(balance_result.is_err());
balance_result
}
} }
/// Poll the server to confirm a transaction. /// Poll the server to confirm a transaction.