From 308d8c254d440862675171c27036ff4359c71422 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Thu, 26 Jul 2018 21:27:44 -0700 Subject: [PATCH] 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. --- src/thin_client.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/thin_client.rs b/src/thin_client.rs index 5c2f294b39..131764759c 100644 --- a/src/thin_client.rs +++ b/src/thin_client.rs @@ -196,11 +196,15 @@ impl ThinClient { } pub fn poll_get_balance(&mut self, pubkey: &PublicKey) -> io::Result { - let mut balance; + let mut balance_result; + let mut balance_value = -1; let now = Instant::now(); loop { - balance = self.get_balance(pubkey); - if balance.is_ok() && *balance.as_ref().unwrap() != 0 || now.elapsed().as_secs() > 1 { + balance_result = self.get_balance(pubkey); + if balance_result.is_ok() { + balance_value = *balance_result.as_ref().unwrap(); + } + if balance_value > 0 || now.elapsed().as_secs() > 1 { break; } sleep(Duration::from_millis(100)); @@ -214,7 +218,12 @@ impl ThinClient { ) .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.