mirror of
https://github.com/Qortal/piratewallet-light-cli.git
synced 2025-02-01 03:12:15 +00:00
Improve error handling
This commit is contained in:
parent
0d874c2dec
commit
16dab37a2e
@ -44,7 +44,7 @@ pub const DEFAULT_SERVER: &str = "https://lightd-test.zecwallet.co:443";
|
|||||||
pub const WALLET_NAME: &str = "zeclite.wallet.dat";
|
pub const WALLET_NAME: &str = "zeclite.wallet.dat";
|
||||||
pub const LOGFILE_NAME: &str = "zeclite.debug.log";
|
pub const LOGFILE_NAME: &str = "zeclite.debug.log";
|
||||||
|
|
||||||
/// A Secure (https) grpc destination. If insecure=true, then it will not use TLS (eg. for local testing)
|
/// A Secure (https) grpc destination.
|
||||||
struct Dst {
|
struct Dst {
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
host: String,
|
host: String,
|
||||||
@ -112,7 +112,6 @@ impl tower_service::Service<()> for Dst {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
macro_rules! make_grpc_client {
|
macro_rules! make_grpc_client {
|
||||||
($protocol:expr, $host:expr, $port:expr) => {{
|
($protocol:expr, $host:expr, $port:expr) => {{
|
||||||
let uri: http::Uri = format!("{}://{}", $protocol, $host).parse().unwrap();
|
let uri: http::Uri = format!("{}://{}", $protocol, $host).parse().unwrap();
|
||||||
@ -128,9 +127,7 @@ macro_rules! make_grpc_client {
|
|||||||
|
|
||||||
make_client
|
make_client
|
||||||
.make_service(())
|
.make_service(())
|
||||||
.map_err(|e| {
|
.map_err(|e| { format!("HTTP/2 connection failed; err={:?}", e) })
|
||||||
eprintln!("HTTP/2 connection failed; err={:?}", e);
|
|
||||||
})
|
|
||||||
.and_then(move |conn| {
|
.and_then(move |conn| {
|
||||||
let conn = tower_request_modifier::Builder::new()
|
let conn = tower_request_modifier::Builder::new()
|
||||||
.set_origin(uri)
|
.set_origin(uri)
|
||||||
@ -140,12 +137,13 @@ macro_rules! make_grpc_client {
|
|||||||
CompactTxStreamer::new(conn)
|
CompactTxStreamer::new(conn)
|
||||||
// Wait until the client is ready...
|
// Wait until the client is ready...
|
||||||
.ready()
|
.ready()
|
||||||
.map_err(|e| eprintln!("client closed: {:?}", e))
|
.map_err(|e| { format!("client closed: {:?}", e) })
|
||||||
})
|
})
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct LightClientConfig {
|
pub struct LightClientConfig {
|
||||||
pub server : http::Uri,
|
pub server : http::Uri,
|
||||||
@ -222,7 +220,14 @@ impl LightClientConfig {
|
|||||||
|
|
||||||
pub fn get_server_or_default(server: Option<String>) -> http::Uri {
|
pub fn get_server_or_default(server: Option<String>) -> http::Uri {
|
||||||
match server {
|
match server {
|
||||||
Some(s) => if s.starts_with("http") {s} else { "http://".to_string() + &s}
|
Some(s) => {
|
||||||
|
let mut s = if s.starts_with("http") {s} else { "http://".to_string() + &s};
|
||||||
|
let uri: http::Uri = s.parse().unwrap();
|
||||||
|
if uri.port_part().is_none() {
|
||||||
|
s = s + ":443";
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
None => DEFAULT_SERVER.to_string()
|
None => DEFAULT_SERVER.to_string()
|
||||||
}.parse().unwrap()
|
}.parse().unwrap()
|
||||||
}
|
}
|
||||||
@ -454,31 +459,22 @@ impl LightClient {
|
|||||||
self.config.server.clone()
|
self.config.server.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_info(uri: http::Uri) -> LightdInfo {
|
pub fn get_info(uri: http::Uri) -> Result<LightdInfo, String> {
|
||||||
use std::cell::RefCell;
|
|
||||||
|
|
||||||
let info = Arc::new(RefCell::<LightdInfo>::default());
|
|
||||||
let info_inner = info.clone();
|
|
||||||
let runner = make_grpc_client!(uri.scheme_str().unwrap(), uri.host().unwrap(), uri.port_part().unwrap())
|
let runner = make_grpc_client!(uri.scheme_str().unwrap(), uri.host().unwrap(), uri.port_part().unwrap())
|
||||||
.and_then(move |mut client| {
|
.and_then(move |mut client| {
|
||||||
client.get_lightd_info(Request::new(Empty{}))
|
client.get_lightd_info(Request::new(Empty{}))
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
println!("ERR = {:?}", e);
|
format!("ERR = {:?}", e)
|
||||||
})
|
})
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
info_inner.replace(response.into_inner());
|
Ok(response.into_inner())
|
||||||
|
|
||||||
Ok(())
|
|
||||||
})
|
})
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
println!("ERR = {:?}", e);
|
format!("ERR = {:?}", e)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner).unwrap();
|
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner)
|
||||||
let ans = info.borrow().clone();
|
|
||||||
|
|
||||||
ans
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn do_info(uri: http::Uri) -> String {
|
pub fn do_info(uri: http::Uri) -> String {
|
||||||
@ -851,7 +847,10 @@ impl LightClient {
|
|||||||
);
|
);
|
||||||
|
|
||||||
match rawtx {
|
match rawtx {
|
||||||
Some(txbytes) => self.broadcast_raw_tx(txbytes),
|
Some(txbytes) => match self.broadcast_raw_tx(txbytes) {
|
||||||
|
Ok(k) => k,
|
||||||
|
Err(e) => e,
|
||||||
|
},
|
||||||
None => format!("No Tx to broadcast")
|
None => format!("No Tx to broadcast")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -860,7 +859,6 @@ impl LightClient {
|
|||||||
// GRPC code
|
// GRPC code
|
||||||
// ==============
|
// ==============
|
||||||
|
|
||||||
|
|
||||||
pub fn fetch_blocks<F : 'static + std::marker::Send>(&self, start_height: u64, end_height: u64, c: F)
|
pub fn fetch_blocks<F : 'static + std::marker::Send>(&self, start_height: u64, end_height: u64, c: F)
|
||||||
where F : Fn(&[u8]) {
|
where F : Fn(&[u8]) {
|
||||||
let uri = self.get_server_uri();
|
let uri = self.get_server_uri();
|
||||||
@ -873,7 +871,7 @@ impl LightClient {
|
|||||||
client
|
client
|
||||||
.get_block_range(br)
|
.get_block_range(br)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
eprintln!("RouteChat request failed; err={:?}", e);
|
format!("RouteChat request failed; err={:?}", e)
|
||||||
})
|
})
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
let inbound = response.into_inner();
|
let inbound = response.into_inner();
|
||||||
@ -886,11 +884,17 @@ impl LightClient {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|e| eprintln!("gRPC inbound stream error: {:?}", e))
|
.map_err(|e| format!("gRPC inbound stream error: {:?}", e))
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner).unwrap();
|
match tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner) {
|
||||||
|
Ok(_) => {}, // The result is processed in callbacks, so nothing to do here
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error while executing fetch_blocks: {}", e);
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_transparent_txids<F : 'static + std::marker::Send>(&self, address: String,
|
pub fn fetch_transparent_txids<F : 'static + std::marker::Send>(&self, address: String,
|
||||||
@ -907,7 +911,7 @@ impl LightClient {
|
|||||||
client
|
client
|
||||||
.get_address_txids(br)
|
.get_address_txids(br)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
eprintln!("RouteChat request failed; err={:?}", e);
|
format!("RouteChat request failed; err={:?}", e)
|
||||||
})
|
})
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
let inbound = response.into_inner();
|
let inbound = response.into_inner();
|
||||||
@ -917,11 +921,17 @@ impl LightClient {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|e| eprintln!("gRPC inbound stream error: {:?}", e))
|
.map_err(|e| format!("gRPC inbound stream error: {:?}", e))
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner).unwrap();
|
match tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner) {
|
||||||
|
Ok(_) => {}, // The result is processed in callbacks, so nothing to do here
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error while executing fetch_transparent_txids: {}", e);
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_full_tx<F : 'static + std::marker::Send>(&self, txid: TxId, c: F)
|
pub fn fetch_full_tx<F : 'static + std::marker::Send>(&self, txid: TxId, c: F)
|
||||||
@ -932,53 +942,45 @@ impl LightClient {
|
|||||||
let txfilter = TxFilter { block: None, index: 0, hash: txid.0.to_vec() };
|
let txfilter = TxFilter { block: None, index: 0, hash: txid.0.to_vec() };
|
||||||
client.get_transaction(Request::new(txfilter))
|
client.get_transaction(Request::new(txfilter))
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
eprintln!("RouteChat request failed; err={:?}", e);
|
format!("RouteChat request failed; err={:?}", e)
|
||||||
})
|
})
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
//let tx = Transaction::read(&response.into_inner().data[..]).unwrap();
|
|
||||||
c(&response.into_inner().data);
|
c(&response.into_inner().data);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|e| {
|
.map_err(|e| { format!("ERR = {:?}", e) })
|
||||||
println!("ERR = {:?}", e);
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner).unwrap();
|
match tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner) {
|
||||||
|
Ok(_) => {}, // The result is processed in callbacks, so nothing to do here
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error while executing fetch_full_tx: {}", e);
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn broadcast_raw_tx(&self, tx_bytes: Box<[u8]>) -> String {
|
pub fn broadcast_raw_tx(&self, tx_bytes: Box<[u8]>) -> Result<String, String> {
|
||||||
use std::cell::RefCell;
|
|
||||||
|
|
||||||
let infostr = Arc::new(RefCell::<String>::default());
|
|
||||||
let infostrinner = infostr.clone();
|
|
||||||
|
|
||||||
let uri = self.get_server_uri();
|
let uri = self.get_server_uri();
|
||||||
let runner = make_grpc_client!(uri.scheme_str().unwrap(), uri.host().unwrap(), uri.port_part().unwrap())
|
let runner = make_grpc_client!(uri.scheme_str().unwrap(), uri.host().unwrap(), uri.port_part().unwrap())
|
||||||
.and_then(move |mut client| {
|
.and_then(move |mut client| {
|
||||||
client.send_transaction(Request::new(RawTransaction {data: tx_bytes.to_vec(), height: 0}))
|
client.send_transaction(Request::new(RawTransaction {data: tx_bytes.to_vec(), height: 0}))
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
println!("ERR = {:?}", e);
|
format!("ERR = {:?}", e)
|
||||||
})
|
})
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
let sendresponse = response.into_inner();
|
let sendresponse = response.into_inner();
|
||||||
if sendresponse.error_code == 0 {
|
if sendresponse.error_code == 0 {
|
||||||
infostrinner.replace(format!("Successfully broadcast Tx: {}", sendresponse.error_message));
|
Ok(format!("Successfully broadcast Tx: {}", sendresponse.error_message))
|
||||||
} else {
|
} else {
|
||||||
infostrinner.replace(format!("Error: {:?}", sendresponse));
|
Err(format!("Error: {:?}", sendresponse))
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
.map_err(|e| {
|
|
||||||
println!("ERR = {:?}", e);
|
|
||||||
})
|
})
|
||||||
|
.map_err(|e| { format!("ERR = {:?}", e) })
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner).unwrap();
|
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner)
|
||||||
|
|
||||||
let ans = infostr.borrow().clone();
|
|
||||||
ans
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_latest_block<F : 'static + std::marker::Send>(&self, mut c : F)
|
pub fn fetch_latest_block<F : 'static + std::marker::Send>(&self, mut c : F)
|
||||||
@ -987,19 +989,21 @@ impl LightClient {
|
|||||||
let runner = make_grpc_client!(uri.scheme_str().unwrap(), uri.host().unwrap(), uri.port_part().unwrap())
|
let runner = make_grpc_client!(uri.scheme_str().unwrap(), uri.host().unwrap(), uri.port_part().unwrap())
|
||||||
.and_then(|mut client| {
|
.and_then(|mut client| {
|
||||||
client.get_latest_block(Request::new(ChainSpec {}))
|
client.get_latest_block(Request::new(ChainSpec {}))
|
||||||
.map_err(|e| {
|
.map_err(|e| { format!("ERR = {:?}", e) })
|
||||||
println!("ERR = {:?}", e);
|
|
||||||
})
|
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
c(response.into_inner());
|
c(response.into_inner());
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.map_err(|e| {
|
.map_err(|e| { format!("ERR = {:?}", e) })
|
||||||
println!("ERR = {:?}", e);
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner).unwrap();
|
match tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner) {
|
||||||
|
Ok(_) => {}, // The result is processed in callbacks, so nothing to do here
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error while executing fetch_latest_block: {}", e);
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
14
src/main.rs
14
src/main.rs
@ -47,8 +47,20 @@ pub fn main() {
|
|||||||
|
|
||||||
let server = LightClientConfig::get_server_or_default(maybe_server);
|
let server = LightClientConfig::get_server_or_default(maybe_server);
|
||||||
|
|
||||||
|
// Test to make sure the server has all of scheme, host and port
|
||||||
|
if server.scheme_str().is_none() || server.host().is_none() || server.port_part().is_none() {
|
||||||
|
eprintln!("Please provide the --server parameter as [scheme]://[host]:[port].\nYou provided: {}", server);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Do a getinfo first, before opening the wallet
|
// Do a getinfo first, before opening the wallet
|
||||||
let info = LightClient::get_info(server.clone());
|
let info = match LightClient::get_info(server.clone()) {
|
||||||
|
Ok(ld) => ld,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error:\n{}\nCouldn't get server info, quitting!", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Create a Light Client Config
|
// Create a Light Client Config
|
||||||
let config = lightclient::LightClientConfig {
|
let config = lightclient::LightClientConfig {
|
||||||
|
Loading…
Reference in New Issue
Block a user