Skip to content

Commit

Permalink
HTTPNodeClient can with no urls
Browse files Browse the repository at this point in the history
  • Loading branch information
bayk committed Sep 20, 2024
1 parent cd110b3 commit 77a36d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
36 changes: 20 additions & 16 deletions impls/src/node_clients/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl HTTPNodeClient {
params: &serde_json::Value,
counter: i32,
) -> Result<D, libwallet::Error> {
let url = format!("{}{}", self.node_url(), ENDPOINT);
let url = format!("{}{}", self.node_url()?, ENDPOINT);
let req = build_request(method, params);
let res = self
.client
Expand Down Expand Up @@ -181,7 +181,7 @@ impl HTTPNodeClient {
counter: i32,
) -> Result<Vec<crate::grin_p2p::types::PeerInfoDisplayLegacy>, libwallet::Error> {
// There is no v2 API with connected peers. Keep using v1 for that
let addr = self.node_url();
let addr = self.node_url()?;
let url = format!("{}/v1/peers/connected", addr);

let res = self
Expand Down Expand Up @@ -217,7 +217,7 @@ impl HTTPNodeClient {
let method = "get_kernel";
let params = json!([excess.0.as_ref().to_hex(), min_height, max_height]);
// have to handle this manually since the error needs to be parsed
let url = format!("{}{}", self.node_url(), ENDPOINT);
let url = format!("{}{}", self.node_url()?, ENDPOINT);
let req = build_request(method, &params);
let res = self
.client
Expand Down Expand Up @@ -291,7 +291,7 @@ impl HTTPNodeClient {

trace!("Output query chunk size is: {}", chunk_size);

let url = format!("{}{}", self.node_url(), ENDPOINT);
let url = format!("{}{}", self.node_url()?, ENDPOINT);
let api_secret = self.node_api_secret();
let cl = self.client.clone();
let task = async move {
Expand Down Expand Up @@ -372,14 +372,20 @@ impl HTTPNodeClient {

impl NodeClient for HTTPNodeClient {
fn increase_index(&self) {
let index =
(self.current_node_index.load(Ordering::Relaxed) + 1) % self.node_url_list.len();
self.current_node_index.store(index, Ordering::Relaxed);
if !self.node_url_list.is_empty() {
let index =
(self.current_node_index.load(Ordering::Relaxed) + 1) % self.node_url_list.len();
self.current_node_index.store(index, Ordering::Relaxed);
}
}
fn node_url(&self) -> &str {
fn node_url(&self) -> Result<String, libwallet::Error> {
if self.node_url_list.is_empty() {
return Err(libwallet::Error::NodeUrlIsEmpty);
}
let index = self.current_node_index.load(Ordering::Relaxed) % self.node_url_list.len();
&self.node_url_list.get(index).unwrap()
Ok(self.node_url_list[index].clone())
}

fn node_api_secret(&self) -> &Option<String> {
&self.node_api_secret
}
Expand All @@ -392,14 +398,12 @@ impl NodeClient for HTTPNodeClient {
self.node_api_secret = node_api_secret;
}
fn set_node_index(&mut self, node_index: u8) {
self.current_node_index.store(
node_index as usize % self.node_url_list.len(),
Ordering::Relaxed,
);
self.current_node_index
.store(node_index as usize, Ordering::Relaxed);
}
fn get_node_index(&self) -> u8 {
let index = self.current_node_index.load(Ordering::Relaxed);
(index % self.node_url_list.len()) as u8
index as u8
}

fn reset_cache(&self) {
Expand Down Expand Up @@ -432,7 +436,7 @@ impl NodeClient for HTTPNodeClient {
});
} else {
error!(
"Unable to contact Node to get version info: {}, {}",
"Unable to contact Node to get version info: {:?}, {}",
self.node_url(), e
);
return None;
Expand Down Expand Up @@ -652,7 +656,7 @@ impl NodeClient for HTTPNodeClient {
}
Err(e) => {
let report = format!(
"get_blocks_by_height: error calling api 'get_block' at {}. Error: {}",
"get_blocks_by_height: error calling api 'get_block' at {:?}. Error: {}",
self.node_url(),
e
);
Expand Down
20 changes: 10 additions & 10 deletions impls/src/test_framework/testclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ impl LocalWalletClient {

impl NodeClient for LocalWalletClient {
fn increase_index(&self) {}
fn node_url(&self) -> &str {
"node"
fn node_url(&self) -> Result<String, libwallet::Error> {
Err(libwallet::Error::NodeUrlIsEmpty)
}
fn node_api_secret(&self) -> &Option<String> {
&None
Expand All @@ -495,7 +495,7 @@ impl NodeClient for LocalWalletClient {
fn post_tx(&self, tx: &Transaction, _fluff: bool) -> Result<(), libwallet::Error> {
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "post_tx".to_owned(),
body: serde_json::to_string(tx).unwrap(),
};
Expand All @@ -514,7 +514,7 @@ impl NodeClient for LocalWalletClient {
fn get_chain_tip(&self) -> Result<(u64, String, u64), libwallet::Error> {
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "get_chain_tip".to_owned(),
body: "".to_owned(),
};
Expand All @@ -538,7 +538,7 @@ impl NodeClient for LocalWalletClient {
fn get_header_info(&self, height: u64) -> Result<HeaderInfo, libwallet::Error> {
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "get_header_info".to_owned(),
body: format!("{}", height),
};
Expand Down Expand Up @@ -595,7 +595,7 @@ impl NodeClient for LocalWalletClient {
let query_str = query_params.join(",");
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "get_outputs_from_node".to_owned(),
body: query_str,
};
Expand Down Expand Up @@ -638,7 +638,7 @@ impl NodeClient for LocalWalletClient {

let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "get_kernel".to_owned(),
body: query,
};
Expand Down Expand Up @@ -683,7 +683,7 @@ impl NodeClient for LocalWalletClient {
};
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "get_outputs_by_pmmr_index".to_owned(),
body: query_str,
};
Expand Down Expand Up @@ -733,7 +733,7 @@ impl NodeClient for LocalWalletClient {
};
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "height_range_to_pmmr_indices".to_owned(),
body: query_str,
};
Expand Down Expand Up @@ -761,7 +761,7 @@ impl NodeClient for LocalWalletClient {
) -> Result<Vec<api::BlockPrintable>, libwallet::Error> {
let m = WalletProxyMessage {
sender_id: self.id.clone(),
dest: self.node_url().to_owned(),
dest: self.node_url()?,
method: "get_blocks_by_height".to_owned(),
body: format!("{},{}", start_height, end_height),
};
Expand Down
4 changes: 4 additions & 0 deletions libwallet/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ pub enum Error {
#[error("Node not ready or not available")]
NodeNotReady,

/// Node api url is not set.
#[error("node_url is empty. Please update your config.")]
NodeUrlIsEmpty,

/// Error originating from hyper.
#[error("Hyper error, {0}")]
Hyper(String),
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ pub trait NodeClient: Send + Sync + Clone {
fn increase_index(&self);

/// Return the URL of the check node
fn node_url(&self) -> &str;
fn node_url(&self) -> Result<String, Error>;

/// Set the node URL
fn set_node_url(&mut self, node_url: Vec<String>);
Expand Down

0 comments on commit 77a36d0

Please sign in to comment.