From 414a30224ecb329221a4b37aed28e9ffb5f99b49 Mon Sep 17 00:00:00 2001 From: hashmap Date: Mon, 6 Aug 2018 21:48:05 +0200 Subject: [PATCH] Fix get outputs call in wallet (#1319) * Fix get outputs call in wallet. It generates an invalid url if there are 1000+ outputs. * Also switched to async http client for performance reasons * Couple unrelated cleanups * fixup! Fix get outputs call in wallet --- api/src/router.rs | 2 -- wallet/src/client.rs | 31 ++++++++++++++++++++---------- wallet/src/libwallet/controller.rs | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/api/src/router.rs b/api/src/router.rs index 49231d942..3d2c4ebf1 100644 --- a/api/src/router.rs +++ b/api/src/router.rs @@ -5,7 +5,6 @@ use hyper::{Body, Method, Request, Response, StatusCode}; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use std::sync::Arc; -use util::LOGGER; lazy_static! { static ref WILDCARD_HASH: u64 = calculate_hash(&"*"); @@ -141,7 +140,6 @@ impl Router { for key in keys { node_id = self.find(node_id, key).ok_or(RouterError::RouteNotFound)?; if self.node(node_id).key == *WILDCARD_STOP_HASH { - debug!(LOGGER, "ROUTER stop card"); break; } } diff --git a/wallet/src/client.rs b/wallet/src/client.rs index e1b178c33..e030c1f6e 100644 --- a/wallet/src/client.rs +++ b/wallet/src/client.rs @@ -16,8 +16,11 @@ //! specific to the FileWallet use failure::ResultExt; +use futures::{stream, Stream}; + use libwallet::types::*; use std::collections::HashMap; +use tokio::runtime::Runtime; use api; use error::{Error, ErrorKind}; @@ -129,19 +132,27 @@ impl WalletClient for HTTPWalletClient { // build a map of api outputs by commit so we can look them up efficiently let mut api_outputs: HashMap = HashMap::new(); + let mut tasks = Vec::new(); - for query_chunk in query_params.chunks(1000) { + for query_chunk in query_params.chunks(500) { let url = format!("{}/v1/chain/outputs/byids?{}", addr, query_chunk.join("&"),); + tasks.push(api::client::get_async::>(url.as_str())); + } - match api::client::get::>(url.as_str()) { - Ok(outputs) => for out in outputs { - api_outputs.insert(out.commit.commit(), util::to_hex(out.commit.to_vec())); - }, - Err(_) => { - // if we got anything other than 200 back from server, don't attempt to refresh - // the wallet data after - return Err(libwallet::ErrorKind::ClientCallback("Error from server"))?; - } + let task = stream::futures_unordered(tasks).collect(); + + let mut rt = Runtime::new().unwrap(); + let results = match rt.block_on(task) { + Ok(outputs) => outputs, + Err(e) => { + error!(LOGGER, "Outputs by id failed: {}", e); + return Err(libwallet::ErrorKind::ClientCallback("Error from server"))?; + } + }; + + for res in results { + for out in res { + api_outputs.insert(out.commit.commit(), util::to_hex(out.commit.to_vec())); } } Ok(api_outputs) diff --git a/wallet/src/libwallet/controller.rs b/wallet/src/libwallet/controller.rs index 3d77e82ec..93de71a18 100644 --- a/wallet/src/libwallet/controller.rs +++ b/wallet/src/libwallet/controller.rs @@ -32,7 +32,7 @@ use keychain::Keychain; use libtx::slate::Slate; use libwallet::api::{APIForeign, APIOwner}; use libwallet::types::{ - BlockFees, CbData, OutputData, SendTXArgs, TxLogEntry, WalletBackend, WalletClient, WalletInfo, + CbData, OutputData, SendTXArgs, TxLogEntry, WalletBackend, WalletClient, WalletInfo, }; use libwallet::{Error, ErrorKind}; use url::form_urlencoded;