2019-10-03 17:16:09 +03:00
|
|
|
// Copyright 2019 The Grin Developers
|
2019-02-13 18:05:19 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! Client functions, implementations of the NodeClient trait
|
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
use crate::api::{self, LocatedTxKernel, OutputListing, OutputPrintable};
|
|
|
|
use crate::core::core::{Transaction, TxKernel};
|
|
|
|
use crate::libwallet::{NodeClient, NodeVersionInfo};
|
2020-02-24 18:20:14 +03:00
|
|
|
use futures::stream::FuturesUnordered;
|
|
|
|
use futures::TryStreamExt;
|
2019-02-13 18:05:19 +03:00
|
|
|
use std::collections::HashMap;
|
2020-01-17 14:27:47 +03:00
|
|
|
use std::env;
|
2020-02-14 12:57:39 +03:00
|
|
|
use tokio::runtime::Builder;
|
2019-02-13 18:05:19 +03:00
|
|
|
|
2019-10-14 22:24:09 +03:00
|
|
|
use crate::client_utils::Client;
|
2019-02-13 18:05:19 +03:00
|
|
|
use crate::libwallet;
|
|
|
|
use crate::util::secp::pedersen;
|
2019-09-24 11:56:10 +03:00
|
|
|
use crate::util::{self, to_hex};
|
2019-02-13 18:05:19 +03:00
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
use super::resp_types::*;
|
|
|
|
use crate::client_utils::json_rpc::*;
|
|
|
|
|
|
|
|
const ENDPOINT: &str = "/v2/foreign";
|
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct HTTPNodeClient {
|
|
|
|
node_url: String,
|
|
|
|
node_api_secret: Option<String>,
|
2019-06-27 12:41:05 +03:00
|
|
|
node_version_info: Option<NodeVersionInfo>,
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HTTPNodeClient {
|
|
|
|
/// Create a new client that will communicate with the given grin node
|
|
|
|
pub fn new(node_url: &str, node_api_secret: Option<String>) -> HTTPNodeClient {
|
|
|
|
HTTPNodeClient {
|
|
|
|
node_url: node_url.to_owned(),
|
|
|
|
node_api_secret: node_api_secret,
|
2019-06-27 12:41:05 +03:00
|
|
|
node_version_info: None,
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 10:33:23 +03:00
|
|
|
|
|
|
|
/// Allow returning the chain height without needing a wallet instantiated
|
2019-11-05 00:10:05 +03:00
|
|
|
pub fn chain_height(&self) -> Result<(u64, String), libwallet::Error> {
|
|
|
|
self.get_chain_tip()
|
2019-05-31 10:33:23 +03:00
|
|
|
}
|
2020-02-14 12:16:43 +03:00
|
|
|
|
|
|
|
fn send_json_request<D: serde::de::DeserializeOwned>(
|
|
|
|
&self,
|
|
|
|
method: &str,
|
|
|
|
params: &serde_json::Value,
|
|
|
|
) -> Result<D, libwallet::Error> {
|
|
|
|
let url = format!("{}{}", self.node_url(), ENDPOINT);
|
|
|
|
let client = Client::new();
|
|
|
|
let req = build_request(method, params);
|
|
|
|
let res = client.post::<Request, Response>(url.as_str(), self.node_api_secret(), &req);
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Err(e) => {
|
|
|
|
let report = format!("Error calling {}: {}", method, e);
|
|
|
|
error!("{}", report);
|
|
|
|
Err(libwallet::ErrorKind::ClientCallback(report).into())
|
|
|
|
}
|
|
|
|
Ok(inner) => match inner.clone().into_result() {
|
|
|
|
Ok(r) => Ok(r),
|
|
|
|
Err(e) => {
|
|
|
|
error!("{:?}", inner);
|
|
|
|
let report = format!("Unable to parse response for {}: {}", method, e);
|
|
|
|
error!("{}", report);
|
|
|
|
Err(libwallet::ErrorKind::ClientCallback(report).into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeClient for HTTPNodeClient {
|
|
|
|
fn node_url(&self) -> &str {
|
|
|
|
&self.node_url
|
|
|
|
}
|
|
|
|
fn node_api_secret(&self) -> Option<String> {
|
|
|
|
self.node_api_secret.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_node_url(&mut self, node_url: &str) {
|
|
|
|
self.node_url = node_url.to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_node_api_secret(&mut self, node_api_secret: Option<String>) {
|
|
|
|
self.node_api_secret = node_api_secret;
|
|
|
|
}
|
|
|
|
|
2019-06-27 12:41:05 +03:00
|
|
|
fn get_version_info(&mut self) -> Option<NodeVersionInfo> {
|
|
|
|
if let Some(v) = self.node_version_info.as_ref() {
|
|
|
|
return Some(v.clone());
|
|
|
|
}
|
2020-02-14 12:16:43 +03:00
|
|
|
let retval = match self
|
|
|
|
.send_json_request::<GetVersionResp>("get_version", &serde_json::Value::Null)
|
|
|
|
{
|
|
|
|
Ok(n) => NodeVersionInfo {
|
|
|
|
node_version: n.node_version,
|
|
|
|
block_header_version: n.block_header_version,
|
|
|
|
verified: Some(true),
|
|
|
|
},
|
2019-10-14 22:24:09 +03:00
|
|
|
Err(e) => {
|
|
|
|
// If node isn't available, allow offline functions
|
|
|
|
// unfortunately have to parse string due to error structure
|
|
|
|
let err_string = format!("{}", e);
|
|
|
|
if err_string.contains("404") {
|
|
|
|
return Some(NodeVersionInfo {
|
|
|
|
node_version: "1.0.0".into(),
|
|
|
|
block_header_version: 1,
|
|
|
|
verified: Some(false),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
error!("Unable to contact Node to get version info: {}", e);
|
|
|
|
return None;
|
2019-06-27 12:41:05 +03:00
|
|
|
}
|
2019-10-14 22:24:09 +03:00
|
|
|
}
|
|
|
|
};
|
2019-06-27 12:41:05 +03:00
|
|
|
self.node_version_info = Some(retval.clone());
|
|
|
|
Some(retval)
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
/// Posts a transaction to a grin node
|
2020-02-14 12:16:43 +03:00
|
|
|
fn post_tx(&self, tx: &Transaction, fluff: bool) -> Result<(), libwallet::Error> {
|
|
|
|
let params = json!([tx, fluff]);
|
|
|
|
self.send_json_request::<serde_json::Value>("push_transaction", ¶ms)?;
|
2019-02-13 18:05:19 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the chain tip from a given node
|
2019-11-05 00:10:05 +03:00
|
|
|
fn get_chain_tip(&self) -> Result<(u64, String), libwallet::Error> {
|
2020-02-14 12:16:43 +03:00
|
|
|
let result = self.send_json_request::<GetTipResp>("get_tip", &serde_json::Value::Null)?;
|
|
|
|
Ok((result.height, result.last_block_pushed))
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
|
2019-09-24 11:56:10 +03:00
|
|
|
/// Get kernel implementation
|
|
|
|
fn get_kernel(
|
|
|
|
&mut self,
|
|
|
|
excess: &pedersen::Commitment,
|
|
|
|
min_height: Option<u64>,
|
|
|
|
max_height: Option<u64>,
|
|
|
|
) -> Result<Option<(TxKernel, u64, u64)>, libwallet::Error> {
|
2020-02-14 12:16:43 +03:00
|
|
|
let method = "get_kernel";
|
|
|
|
let params = json!([to_hex(excess.0.to_vec()), min_height, max_height]);
|
|
|
|
// have to handle this manually since the error needs to be parsed
|
|
|
|
let url = format!("{}{}", self.node_url(), ENDPOINT);
|
|
|
|
let client = Client::new();
|
|
|
|
let req = build_request(method, ¶ms);
|
|
|
|
let res = client.post::<Request, Response>(url.as_str(), self.node_api_secret(), &req);
|
2019-09-24 11:56:10 +03:00
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
match res {
|
|
|
|
Err(e) => {
|
|
|
|
let report = format!("Error calling {}: {}", method, e);
|
|
|
|
error!("{}", report);
|
|
|
|
Err(libwallet::ErrorKind::ClientCallback(report).into())
|
2019-09-24 11:56:10 +03:00
|
|
|
}
|
2020-02-14 12:16:43 +03:00
|
|
|
Ok(inner) => match inner.clone().into_result::<LocatedTxKernel>() {
|
|
|
|
Ok(r) => Ok(Some((r.tx_kernel, r.height, r.mmr_index))),
|
|
|
|
Err(e) => {
|
|
|
|
let contents = format!("{:?}", inner);
|
|
|
|
if contents.contains("NotFound") {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
let report = format!("Unable to parse response for {}: {}", method, e);
|
|
|
|
error!("{}", report);
|
|
|
|
Err(libwallet::ErrorKind::ClientCallback(report).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-09-24 11:56:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:05:19 +03:00
|
|
|
/// Retrieve outputs from node
|
|
|
|
fn get_outputs_from_node(
|
|
|
|
&self,
|
|
|
|
wallet_outputs: Vec<pedersen::Commitment>,
|
|
|
|
) -> Result<HashMap<pedersen::Commitment, (String, u64, u64)>, libwallet::Error> {
|
2020-02-14 12:16:43 +03:00
|
|
|
// build a map of api outputs by commit so we can look them up efficiently
|
|
|
|
let mut api_outputs: HashMap<pedersen::Commitment, (String, u64, u64)> = HashMap::new();
|
|
|
|
|
|
|
|
if wallet_outputs.is_empty() {
|
|
|
|
return Ok(api_outputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// build vec of commits for inclusion in query
|
2019-02-13 18:05:19 +03:00
|
|
|
let query_params: Vec<String> = wallet_outputs
|
|
|
|
.iter()
|
2020-02-14 12:16:43 +03:00
|
|
|
.map(|commit| format!("{}", util::to_hex(commit.as_ref().to_vec())))
|
2019-02-13 18:05:19 +03:00
|
|
|
.collect();
|
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
// going to leave this here even though we're moving
|
|
|
|
// to the json RPC api to keep the functionality of
|
|
|
|
// parallelizing larger requests. Will raise default
|
|
|
|
// from 200 to 500, however
|
|
|
|
let chunk_default = 500;
|
2020-01-17 14:27:47 +03:00
|
|
|
let chunk_size = match env::var("GRIN_OUTPUT_QUERY_SIZE") {
|
|
|
|
Ok(s) => match s.parse::<usize>() {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
"Unable to parse GRIN_OUTPUT_QUERY_SIZE, defaulting to {}",
|
|
|
|
chunk_default
|
|
|
|
);
|
|
|
|
error!("Reason: {}", e);
|
|
|
|
chunk_default
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => chunk_default,
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!("Output query chunk size is: {}", chunk_size);
|
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
let url = format!("{}{}", self.node_url(), ENDPOINT);
|
|
|
|
|
2020-02-24 18:20:14 +03:00
|
|
|
let task = async move {
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
let params: Vec<_> = query_params
|
|
|
|
.chunks(chunk_size)
|
|
|
|
.map(|c| json!([c, null, null, false, false]))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut reqs = Vec::with_capacity(params.len());
|
|
|
|
for p in ¶ms {
|
|
|
|
reqs.push(build_request("get_outputs", p));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut tasks = Vec::with_capacity(params.len());
|
|
|
|
for req in &reqs {
|
|
|
|
tasks.push(client.post_async::<Request, Response>(
|
|
|
|
url.as_str(),
|
|
|
|
req,
|
|
|
|
self.node_api_secret(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let task: FuturesUnordered<_> = tasks.into_iter().collect();
|
|
|
|
task.try_collect().await
|
|
|
|
};
|
2019-02-13 18:05:19 +03:00
|
|
|
|
2020-02-24 18:20:14 +03:00
|
|
|
let mut rt = Builder::new()
|
|
|
|
.threaded_scheduler()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
let res: Result<Vec<Response>, _> = rt.block_on(task);
|
2020-02-14 12:57:39 +03:00
|
|
|
let results: Vec<OutputPrintable> = match res {
|
2020-02-14 12:16:43 +03:00
|
|
|
Ok(resps) => {
|
|
|
|
let mut results = vec![];
|
|
|
|
for r in resps {
|
|
|
|
match r.into_result::<Vec<OutputPrintable>>() {
|
|
|
|
Ok(mut r) => results.append(&mut r),
|
|
|
|
Err(e) => {
|
|
|
|
let report = format!("Unable to parse response for get_outputs: {}", e);
|
|
|
|
error!("{}", report);
|
|
|
|
return Err(libwallet::ErrorKind::ClientCallback(report).into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
results
|
|
|
|
}
|
2019-02-13 18:05:19 +03:00
|
|
|
Err(e) => {
|
|
|
|
let report = format!("Getting outputs by id: {}", e);
|
|
|
|
error!("Outputs by id failed: {}", e);
|
|
|
|
return Err(libwallet::ErrorKind::ClientCallback(report).into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
for out in results.iter() {
|
|
|
|
let height = match out.block_height {
|
|
|
|
Some(h) => h,
|
|
|
|
None => {
|
|
|
|
let msg = format!("Missing block height for output {:?}", out.commit);
|
|
|
|
return Err(libwallet::ErrorKind::ClientCallback(msg).into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
api_outputs.insert(
|
|
|
|
out.commit,
|
|
|
|
(util::to_hex(out.commit.0.to_vec()), height, out.mmr_index),
|
|
|
|
);
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
|
|
|
Ok(api_outputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_outputs_by_pmmr_index(
|
|
|
|
&self,
|
2019-11-05 00:10:05 +03:00
|
|
|
start_index: u64,
|
|
|
|
end_index: Option<u64>,
|
2019-02-13 18:05:19 +03:00
|
|
|
max_outputs: u64,
|
|
|
|
) -> Result<
|
|
|
|
(
|
|
|
|
u64,
|
|
|
|
u64,
|
|
|
|
Vec<(pedersen::Commitment, pedersen::RangeProof, bool, u64, u64)>,
|
|
|
|
),
|
|
|
|
libwallet::Error,
|
|
|
|
> {
|
|
|
|
let mut api_outputs: Vec<(pedersen::Commitment, pedersen::RangeProof, bool, u64, u64)> =
|
|
|
|
Vec::new();
|
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
let params = json!([start_index, end_index, max_outputs, Some(true)]);
|
|
|
|
let res = self.send_json_request::<OutputListing>("get_unspent_outputs", ¶ms)?;
|
|
|
|
for out in res.outputs {
|
|
|
|
let is_coinbase = match out.output_type {
|
|
|
|
api::OutputType::Coinbase => true,
|
|
|
|
api::OutputType::Transaction => false,
|
|
|
|
};
|
|
|
|
let range_proof = match out.range_proof() {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
let msg = format!(
|
|
|
|
"Unexpected error in returned output (missing range proof): {:?}. {:?}, {}",
|
|
|
|
out.commit, out, e
|
|
|
|
);
|
|
|
|
error!("{}", msg);
|
|
|
|
return Err(libwallet::ErrorKind::ClientCallback(msg).into());
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
2020-02-14 12:16:43 +03:00
|
|
|
};
|
|
|
|
let block_height = match out.block_height {
|
|
|
|
Some(h) => h,
|
|
|
|
None => {
|
|
|
|
let msg = format!(
|
|
|
|
"Unexpected error in returned output (missing block height): {:?}. {:?}",
|
|
|
|
out.commit, out
|
|
|
|
);
|
|
|
|
error!("{}", msg);
|
|
|
|
return Err(libwallet::ErrorKind::ClientCallback(msg).into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
api_outputs.push((
|
|
|
|
out.commit,
|
|
|
|
range_proof,
|
|
|
|
is_coinbase,
|
|
|
|
block_height,
|
|
|
|
out.mmr_index,
|
|
|
|
));
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
2020-02-14 12:16:43 +03:00
|
|
|
Ok((res.highest_index, res.last_retrieved_index, api_outputs))
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|
2019-11-05 00:10:05 +03:00
|
|
|
|
|
|
|
fn height_range_to_pmmr_indices(
|
|
|
|
&self,
|
|
|
|
start_height: u64,
|
|
|
|
end_height: Option<u64>,
|
|
|
|
) -> Result<(u64, u64), libwallet::Error> {
|
2020-02-14 12:16:43 +03:00
|
|
|
let params = json!([start_height, end_height]);
|
|
|
|
let res = self.send_json_request::<OutputListing>("get_pmmr_indices", ¶ms)?;
|
2019-11-05 00:10:05 +03:00
|
|
|
|
2020-02-14 12:16:43 +03:00
|
|
|
Ok((res.last_retrieved_index, res.highest_index))
|
2019-11-05 00:10:05 +03:00
|
|
|
}
|
2019-02-13 18:05:19 +03:00
|
|
|
}
|