From 3282cb85ae76292e8e11b3bc32eb57c7e0337641 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Tue, 15 Jan 2019 16:24:09 -0800 Subject: [PATCH] Refactor request_and_confirm_airdrop() to use send_and_confirm_tx() --- wallet/src/wallet.rs | 83 +++++++------------------------------------- wallet/tests/pay.rs | 6 ++-- 2 files changed, 15 insertions(+), 74 deletions(-) diff --git a/wallet/src/wallet.rs b/wallet/src/wallet.rs index c66fe20b8a..dcb340df55 100644 --- a/wallet/src/wallet.rs +++ b/wallet/src/wallet.rs @@ -351,8 +351,7 @@ pub fn process_command(config: &WalletConfig) -> Result Result Result Result<(), Box> { - let mut last_id = get_last_id(rpc_client)?; - let mut send_retries = 3; - loop { - let tx = request_airdrop_transaction(drone_addr, id, tokens, last_id)?; - let mut status_retries = 4; - let signature_str = send_tx(rpc_client, &tx)?; - let status = loop { - let status = confirm_tx(rpc_client, &signature_str)?; - if status == RpcSignatureStatus::SignatureNotFound { - status_retries -= 1; - if status_retries == 0 { - break status; - } - } else { - break status; - } - if cfg!(not(test)) { - sleep(Duration::from_secs(1)); - } - }; - match status { - RpcSignatureStatus::AccountInUse => { - // Fetch a new last_id to prevent the retry from getting rejected as a - // DuplicateSignature - let mut next_last_id_retries = 3; - loop { - let next_last_id = get_last_id(rpc_client)?; - if next_last_id != last_id { - last_id = next_last_id; - break; - } - if next_last_id_retries == 0 { - Err(WalletError::RpcRequestError( - "Unable to fetch next last_id".to_string(), - ))?; - } - next_last_id_retries -= 1; - if cfg!(not(test)) { - sleep(Duration::from_secs(1)); - } - } - send_retries -= 1; - if send_retries == 0 { - Err(WalletError::RpcRequestError(format!( - "AccountInUse after 3 retries: {:?}", - tx.account_keys[0] - )))? - } - } - RpcSignatureStatus::Confirmed => { - return Ok(()); - } - _ => { - Err(WalletError::RpcRequestError(format!( - "Transaction {:?} failed: {:?}", - signature_str, status - )))?; - } - } - } + let last_id = get_last_id(rpc_client)?; + let mut tx = request_airdrop_transaction(drone_addr, &signer.pubkey(), tokens, last_id)?; + send_and_confirm_tx(rpc_client, &mut tx, signer)?; + Ok(()) } #[cfg(test)] @@ -1483,17 +1424,17 @@ mod tests { fn test_request_and_confirm_airdrop() { let rpc_client = RpcClient::new("succeeds".to_string()); let drone_addr = socketaddr!(0, 0); - let id = Keypair::new().pubkey(); + let keypair = Keypair::new(); let tokens = 50; assert_eq!( - request_and_confirm_airdrop(&rpc_client, &drone_addr, &id, tokens).unwrap(), + request_and_confirm_airdrop(&rpc_client, &drone_addr, &keypair, tokens).unwrap(), () ); let rpc_client = RpcClient::new("account_in_use".to_string()); - assert!(request_and_confirm_airdrop(&rpc_client, &drone_addr, &id, tokens).is_err()); + assert!(request_and_confirm_airdrop(&rpc_client, &drone_addr, &keypair, tokens).is_err()); let tokens = 0; - assert!(request_and_confirm_airdrop(&rpc_client, &drone_addr, &id, tokens).is_err()); + assert!(request_and_confirm_airdrop(&rpc_client, &drone_addr, &keypair, tokens).is_err()); } } diff --git a/wallet/tests/pay.rs b/wallet/tests/pay.rs index 7977865c25..3923c550cf 100644 --- a/wallet/tests/pay.rs +++ b/wallet/tests/pay.rs @@ -83,7 +83,7 @@ fn test_wallet_timestamp_tx() { assert_ne!(config_payer.id.pubkey(), config_witness.id.pubkey()); - request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id.pubkey(), 50).unwrap(); + request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id, 50).unwrap(); let params = json!([format!("{}", config_payer.id.pubkey())]); check_balance(50, &rpc_client, params); @@ -184,7 +184,7 @@ fn test_wallet_witness_tx() { assert_ne!(config_payer.id.pubkey(), config_witness.id.pubkey()); - request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id.pubkey(), 50).unwrap(); + request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id, 50).unwrap(); // Make transaction (from config_payer to bob_pubkey) requiring witness signature from config_witness config_payer.command = WalletCommand::Pay( @@ -281,7 +281,7 @@ fn test_wallet_cancel_tx() { assert_ne!(config_payer.id.pubkey(), config_witness.id.pubkey()); - request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id.pubkey(), 50).unwrap(); + request_and_confirm_airdrop(&rpc_client, &drone_addr, &config_payer.id, 50).unwrap(); // Make transaction (from config_payer to bob_pubkey) requiring witness signature from config_witness config_payer.command = WalletCommand::Pay(