Reduce number of unwwaps in api crate (#2681)

* Reduce number of unwwaps in api crate

* Format use section
This commit is contained in:
hashmap 2019-03-18 19:34:35 +01:00 committed by GitHub
parent 2b218f2dc3
commit 7fad5b040f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 165 additions and 123 deletions

View file

@ -13,19 +13,25 @@
// limitations under the License. // limitations under the License.
use crate::router::{Handler, HandlerObj, ResponseFuture}; use crate::router::{Handler, HandlerObj, ResponseFuture};
use crate::web::response;
use futures::future::ok; use futures::future::ok;
use hyper::header::{HeaderValue, AUTHORIZATION, WWW_AUTHENTICATE}; use hyper::header::{HeaderValue, AUTHORIZATION, WWW_AUTHENTICATE};
use hyper::{Body, Request, Response, StatusCode}; use hyper::{Body, Request, Response, StatusCode};
use ring::constant_time::verify_slices_are_equal; use ring::constant_time::verify_slices_are_equal;
lazy_static! {
pub static ref GRIN_BASIC_REALM: HeaderValue =
HeaderValue::from_str("Basic realm=GrinAPI").unwrap();
}
// Basic Authentication Middleware // Basic Authentication Middleware
pub struct BasicAuthMiddleware { pub struct BasicAuthMiddleware {
api_basic_auth: String, api_basic_auth: String,
basic_realm: String, basic_realm: &'static HeaderValue,
} }
impl BasicAuthMiddleware { impl BasicAuthMiddleware {
pub fn new(api_basic_auth: String, basic_realm: String) -> BasicAuthMiddleware { pub fn new(api_basic_auth: String, basic_realm: &'static HeaderValue) -> BasicAuthMiddleware {
BasicAuthMiddleware { BasicAuthMiddleware {
api_basic_auth, api_basic_auth,
basic_realm, basic_realm,
@ -39,8 +45,12 @@ impl Handler for BasicAuthMiddleware {
req: Request<Body>, req: Request<Body>,
mut handlers: Box<dyn Iterator<Item = HandlerObj>>, mut handlers: Box<dyn Iterator<Item = HandlerObj>>,
) -> ResponseFuture { ) -> ResponseFuture {
let next_handler = match handlers.next() {
Some(h) => h,
None => return response(StatusCode::INTERNAL_SERVER_ERROR, "no handler found"),
};
if req.method().as_str() == "OPTIONS" { if req.method().as_str() == "OPTIONS" {
return handlers.next().unwrap().call(req, handlers); return next_handler.call(req, handlers);
} }
if req.headers().contains_key(AUTHORIZATION) if req.headers().contains_key(AUTHORIZATION)
&& verify_slices_are_equal( && verify_slices_are_equal(
@ -49,7 +59,7 @@ impl Handler for BasicAuthMiddleware {
) )
.is_ok() .is_ok()
{ {
handlers.next().unwrap().call(req, handlers) next_handler.call(req, handlers)
} else { } else {
// Unauthorized 401 // Unauthorized 401
unauthorized_response(&self.basic_realm) unauthorized_response(&self.basic_realm)
@ -57,13 +67,10 @@ impl Handler for BasicAuthMiddleware {
} }
} }
fn unauthorized_response(basic_realm: &str) -> ResponseFuture { fn unauthorized_response(basic_realm: &HeaderValue) -> ResponseFuture {
let response = Response::builder() let response = Response::builder()
.status(StatusCode::UNAUTHORIZED) .status(StatusCode::UNAUTHORIZED)
.header( .header(WWW_AUTHENTICATE, basic_realm)
WWW_AUTHENTICATE,
HeaderValue::from_str(basic_realm).unwrap(),
)
.body(Body::empty()) .body(Body::empty())
.unwrap(); .unwrap();
Box::new(ok(response)) Box::new(ok(response))

View file

@ -136,9 +136,8 @@ fn build_request<'a>(
.into() .into()
})?; })?;
let mut builder = Request::builder(); let mut builder = Request::builder();
if api_secret.is_some() { if let Some(api_secret) = api_secret {
let basic_auth = let basic_auth = format!("Basic {}", to_base64(&format!("grin:{}", api_secret)));
"Basic ".to_string() + &to_base64(&("grin:".to_string() + &api_secret.unwrap()));
builder.header(AUTHORIZATION, basic_auth); builder.header(AUTHORIZATION, basic_auth);
} }
@ -224,6 +223,7 @@ fn send_request_async(req: Request<Body>) -> Box<dyn Future<Item = String, Error
fn send_request(req: Request<Body>) -> Result<String, Error> { fn send_request(req: Request<Body>) -> Result<String, Error> {
let task = send_request_async(req); let task = send_request_async(req);
let mut rt = Runtime::new().unwrap(); let mut rt =
Runtime::new().context(ErrorKind::Internal("can't create Tokio runtime".to_owned()))?;
Ok(rt.block_on(task)?) Ok(rt.block_on(task)?)
} }

View file

@ -20,39 +20,26 @@ mod server_api;
mod transactions_api; mod transactions_api;
mod utils; mod utils;
use crate::router::{Router, RouterError};
// Server
use self::server_api::IndexHandler;
use self::server_api::StatusHandler;
// Blocks
use self::blocks_api::BlockHandler; use self::blocks_api::BlockHandler;
use self::blocks_api::HeaderHandler; use self::blocks_api::HeaderHandler;
// TX Set
use self::transactions_api::TxHashSetHandler;
// Chain
use self::chain_api::ChainCompactHandler; use self::chain_api::ChainCompactHandler;
use self::chain_api::ChainHandler; use self::chain_api::ChainHandler;
use self::chain_api::ChainValidationHandler; use self::chain_api::ChainValidationHandler;
use self::chain_api::OutputHandler; use self::chain_api::OutputHandler;
// Pool Handlers
use self::pool_api::PoolInfoHandler;
use self::pool_api::PoolPushHandler;
// Peers
use self::peers_api::PeerHandler; use self::peers_api::PeerHandler;
use self::peers_api::PeersAllHandler; use self::peers_api::PeersAllHandler;
use self::peers_api::PeersConnectedHandler; use self::peers_api::PeersConnectedHandler;
use self::pool_api::PoolInfoHandler;
use crate::auth::BasicAuthMiddleware; use self::pool_api::PoolPushHandler;
use self::server_api::IndexHandler;
use self::server_api::StatusHandler;
use self::transactions_api::TxHashSetHandler;
use crate::auth::{BasicAuthMiddleware, GRIN_BASIC_REALM};
use crate::chain; use crate::chain;
use crate::p2p; use crate::p2p;
use crate::pool; use crate::pool;
use crate::rest::*; use crate::rest::*;
use crate::router::{Router, RouterError};
use crate::util; use crate::util;
use crate::util::RwLock; use crate::util::RwLock;
use std::net::SocketAddr; use std::net::SocketAddr;
@ -76,11 +63,10 @@ pub fn start_rest_apis(
) -> bool { ) -> bool {
let mut apis = ApiServer::new(); let mut apis = ApiServer::new();
let mut router = build_router(chain, tx_pool, peers).expect("unable to build API router"); let mut router = build_router(chain, tx_pool, peers).expect("unable to build API router");
if api_secret.is_some() { if let Some(api_secret) = api_secret {
let api_basic_auth = let api_basic_auth = format!("Basic {}", util::to_base64(&format!("grin:{}", api_secret)));
"Basic ".to_string() + &util::to_base64(&("grin:".to_string() + &api_secret.unwrap())); let basic_auth_middleware =
let basic_realm = "Basic realm=GrinAPI".to_string(); Arc::new(BasicAuthMiddleware::new(api_basic_auth, &GRIN_BASIC_REALM));
let basic_auth_middleware = Arc::new(BasicAuthMiddleware::new(api_basic_auth, basic_realm));
router.add_middleware(basic_auth_middleware); router.add_middleware(basic_auth_middleware);
} }

View file

@ -41,7 +41,7 @@ impl HeaderHandler {
return Ok(h); return Ok(h);
} }
if let Ok(height) = input.parse() { if let Ok(height) = input.parse() {
match w(&self.chain).get_header_by_height(height) { match w(&self.chain)?.get_header_by_height(height) {
Ok(header) => return Ok(BlockHeaderPrintable::from_header(&header)), Ok(header) => return Ok(BlockHeaderPrintable::from_header(&header)),
Err(_) => return Err(ErrorKind::NotFound)?, Err(_) => return Err(ErrorKind::NotFound)?,
} }
@ -50,7 +50,7 @@ impl HeaderHandler {
let vec = util::from_hex(input) let vec = util::from_hex(input)
.map_err(|e| ErrorKind::Argument(format!("invalid input: {}", e)))?; .map_err(|e| ErrorKind::Argument(format!("invalid input: {}", e)))?;
let h = Hash::from_vec(&vec); let h = Hash::from_vec(&vec);
let header = w(&self.chain) let header = w(&self.chain)?
.get_block_header(&h) .get_block_header(&h)
.context(ErrorKind::NotFound)?; .context(ErrorKind::NotFound)?;
Ok(BlockHeaderPrintable::from_header(&header)) Ok(BlockHeaderPrintable::from_header(&header))
@ -58,7 +58,7 @@ impl HeaderHandler {
fn get_header_for_output(&self, commit_id: String) -> Result<BlockHeaderPrintable, Error> { fn get_header_for_output(&self, commit_id: String) -> Result<BlockHeaderPrintable, Error> {
let oid = get_output(&self.chain, &commit_id)?.1; let oid = get_output(&self.chain, &commit_id)?.1;
match w(&self.chain).get_header_for_output(&oid) { match w(&self.chain)?.get_header_for_output(&oid) {
Ok(header) => Ok(BlockHeaderPrintable::from_header(&header)), Ok(header) => Ok(BlockHeaderPrintable::from_header(&header)),
Err(_) => Err(ErrorKind::NotFound)?, Err(_) => Err(ErrorKind::NotFound)?,
} }
@ -85,22 +85,23 @@ pub struct BlockHandler {
impl BlockHandler { impl BlockHandler {
fn get_block(&self, h: &Hash) -> Result<BlockPrintable, Error> { fn get_block(&self, h: &Hash) -> Result<BlockPrintable, Error> {
let block = w(&self.chain).get_block(h).context(ErrorKind::NotFound)?; let chain = w(&self.chain)?;
Ok(BlockPrintable::from_block(&block, w(&self.chain), false)) let block = chain.get_block(h).context(ErrorKind::NotFound)?;
BlockPrintable::from_block(&block, chain, false)
.map_err(|_| ErrorKind::Internal("chain error".to_owned()).into())
} }
fn get_compact_block(&self, h: &Hash) -> Result<CompactBlockPrintable, Error> { fn get_compact_block(&self, h: &Hash) -> Result<CompactBlockPrintable, Error> {
let block = w(&self.chain).get_block(h).context(ErrorKind::NotFound)?; let chain = w(&self.chain)?;
Ok(CompactBlockPrintable::from_compact_block( let block = chain.get_block(h).context(ErrorKind::NotFound)?;
&block.into(), CompactBlockPrintable::from_compact_block(&block.into(), chain)
w(&self.chain), .map_err(|_| ErrorKind::Internal("chain error".to_owned()).into())
))
} }
// Try to decode the string as a height or a hash. // Try to decode the string as a height or a hash.
fn parse_input(&self, input: String) -> Result<Hash, Error> { fn parse_input(&self, input: String) -> Result<Hash, Error> {
if let Ok(height) = input.parse() { if let Ok(height) = input.parse() {
match w(&self.chain).get_header_by_height(height) { match w(&self.chain)?.get_header_by_height(height) {
Ok(header) => return Ok(header.hash()), Ok(header) => return Ok(header.hash()),
Err(_) => return Err(ErrorKind::NotFound)?, Err(_) => return Err(ErrorKind::NotFound)?,
} }

View file

@ -21,6 +21,7 @@ use crate::types::*;
use crate::util; use crate::util;
use crate::util::secp::pedersen::Commitment; use crate::util::secp::pedersen::Commitment;
use crate::web::*; use crate::web::*;
use failure::ResultExt;
use hyper::{Body, Request, StatusCode}; use hyper::{Body, Request, StatusCode};
use std::sync::Weak; use std::sync::Weak;
@ -32,7 +33,7 @@ pub struct ChainHandler {
impl ChainHandler { impl ChainHandler {
fn get_tip(&self) -> Result<Tip, Error> { fn get_tip(&self) -> Result<Tip, Error> {
let head = w(&self.chain) let head = w(&self.chain)?
.head() .head()
.map_err(|e| ErrorKind::Internal(format!("can't get head: {}", e)))?; .map_err(|e| ErrorKind::Internal(format!("can't get head: {}", e)))?;
Ok(Tip::from_tip(head)) Ok(Tip::from_tip(head))
@ -53,7 +54,7 @@ pub struct ChainValidationHandler {
impl Handler for ChainValidationHandler { impl Handler for ChainValidationHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture { fn get(&self, _req: Request<Body>) -> ResponseFuture {
match w(&self.chain).validate(true) { match w_fut!(&self.chain).validate(true) {
Ok(_) => response(StatusCode::OK, "{}"), Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response( Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
@ -72,7 +73,7 @@ pub struct ChainCompactHandler {
impl Handler for ChainCompactHandler { impl Handler for ChainCompactHandler {
fn post(&self, _req: Request<Body>) -> ResponseFuture { fn post(&self, _req: Request<Body>) -> ResponseFuture {
match w(&self.chain).compact() { match w_fut!(&self.chain).compact() {
Ok(_) => response(StatusCode::OK, "{}"), Ok(_) => response(StatusCode::OK, "{}"),
Err(e) => response( Err(e) => response(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
@ -118,13 +119,14 @@ impl OutputHandler {
commitments: Vec<Commitment>, commitments: Vec<Commitment>,
include_proof: bool, include_proof: bool,
) -> Result<BlockOutputs, Error> { ) -> Result<BlockOutputs, Error> {
let header = w(&self.chain) let header = w(&self.chain)?
.get_header_by_height(block_height) .get_header_by_height(block_height)
.map_err(|_| ErrorKind::NotFound)?; .map_err(|_| ErrorKind::NotFound)?;
// TODO - possible to compact away blocks we care about // TODO - possible to compact away blocks we care about
// in the period between accepting the block and refreshing the wallet // in the period between accepting the block and refreshing the wallet
let block = w(&self.chain) let chain = w(&self.chain)?;
let block = chain
.get_block(&header.hash()) .get_block(&header.hash())
.map_err(|_| ErrorKind::NotFound)?; .map_err(|_| ErrorKind::NotFound)?;
let outputs = block let outputs = block
@ -132,9 +134,10 @@ impl OutputHandler {
.iter() .iter()
.filter(|output| commitments.is_empty() || commitments.contains(&output.commit)) .filter(|output| commitments.is_empty() || commitments.contains(&output.commit))
.map(|output| { .map(|output| {
OutputPrintable::from_output(output, w(&self.chain), Some(&header), include_proof) OutputPrintable::from_output(output, chain.clone(), Some(&header), include_proof)
}) })
.collect(); .collect::<Result<Vec<_>, _>>()
.context(ErrorKind::Internal("cain error".to_owned()))?;
Ok(BlockOutputs { Ok(BlockOutputs {
header: BlockHeaderInfo::from_header(&header), header: BlockHeaderInfo::from_header(&header),

View file

@ -26,7 +26,7 @@ pub struct PeersAllHandler {
impl Handler for PeersAllHandler { impl Handler for PeersAllHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture { fn get(&self, _req: Request<Body>) -> ResponseFuture {
let peers = &w(&self.peers).all_peers(); let peers = &w_fut!(&self.peers).all_peers();
json_response_pretty(&peers) json_response_pretty(&peers)
} }
} }
@ -37,7 +37,7 @@ pub struct PeersConnectedHandler {
impl Handler for PeersConnectedHandler { impl Handler for PeersConnectedHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture { fn get(&self, _req: Request<Body>) -> ResponseFuture {
let peers: Vec<PeerInfoDisplay> = w(&self.peers) let peers: Vec<PeerInfoDisplay> = w_fut!(&self.peers)
.connected_peers() .connected_peers()
.iter() .iter()
.map(|p| p.info.clone().into()) .map(|p| p.info.clone().into())
@ -73,7 +73,7 @@ impl Handler for PeerHandler {
); );
} }
match w(&self.peers).get_peer(peer_addr) { match w_fut!(&self.peers).get_peer(peer_addr) {
Ok(peer) => json_response(&peer), Ok(peer) => json_response(&peer),
Err(_) => response(StatusCode::NOT_FOUND, "peer not found"), Err(_) => response(StatusCode::NOT_FOUND, "peer not found"),
} }
@ -101,8 +101,8 @@ impl Handler for PeerHandler {
}; };
match command { match command {
"ban" => w(&self.peers).ban_peer(addr, ReasonForBan::ManualBan), "ban" => w_fut!(&self.peers).ban_peer(addr, ReasonForBan::ManualBan),
"unban" => w(&self.peers).unban_peer(addr), "unban" => w_fut!(&self.peers).unban_peer(addr),
_ => return response(StatusCode::BAD_REQUEST, "invalid command"), _ => return response(StatusCode::BAD_REQUEST, "invalid command"),
}; };

View file

@ -24,7 +24,7 @@ use crate::util;
use crate::util::RwLock; use crate::util::RwLock;
use crate::web::*; use crate::web::*;
use failure::ResultExt; use failure::ResultExt;
use futures::future::ok; use futures::future::{err, ok};
use futures::Future; use futures::Future;
use hyper::{Body, Request, StatusCode}; use hyper::{Body, Request, StatusCode};
use std::sync::Weak; use std::sync::Weak;
@ -37,7 +37,7 @@ pub struct PoolInfoHandler {
impl Handler for PoolInfoHandler { impl Handler for PoolInfoHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture { fn get(&self, _req: Request<Body>) -> ResponseFuture {
let pool_arc = w(&self.tx_pool); let pool_arc = w_fut!(&self.tx_pool);
let pool = pool_arc.read(); let pool = pool_arc.read();
json_response(&PoolInfo { json_response(&PoolInfo {
@ -63,7 +63,11 @@ impl PoolPushHandler {
let params = QueryParams::from(req.uri().query()); let params = QueryParams::from(req.uri().query());
let fluff = params.get("fluff").is_some(); let fluff = params.get("fluff").is_some();
let pool_arc = w(&self.tx_pool).clone(); let pool_arc = match w(&self.tx_pool) {
//w(&self.tx_pool).clone();
Ok(p) => p,
Err(e) => return Box::new(err(e)),
};
Box::new( Box::new(
parse_body(req) parse_body(req)

View file

@ -45,12 +45,12 @@ pub struct StatusHandler {
impl StatusHandler { impl StatusHandler {
fn get_status(&self) -> Result<Status, Error> { fn get_status(&self) -> Result<Status, Error> {
let head = w(&self.chain) let head = w(&self.chain)?
.head() .head()
.map_err(|e| ErrorKind::Internal(format!("can't get head: {}", e)))?; .map_err(|e| ErrorKind::Internal(format!("can't get head: {}", e)))?;
Ok(Status::from_tip_and_peers( Ok(Status::from_tip_and_peers(
head, head,
w(&self.peers).peer_count(), w(&self.peers)?.peer_count(),
)) ))
} }
} }

View file

@ -45,23 +45,26 @@ pub struct TxHashSetHandler {
impl TxHashSetHandler { impl TxHashSetHandler {
// gets roots // gets roots
fn get_roots(&self) -> TxHashSet { fn get_roots(&self) -> Result<TxHashSet, Error> {
TxHashSet::from_head(w(&self.chain)) Ok(TxHashSet::from_head(w(&self.chain)?))
} }
// gets last n outputs inserted in to the tree // gets last n outputs inserted in to the tree
fn get_last_n_output(&self, distance: u64) -> Vec<TxHashSetNode> { fn get_last_n_output(&self, distance: u64) -> Result<Vec<TxHashSetNode>, Error> {
TxHashSetNode::get_last_n_output(w(&self.chain), distance) Ok(TxHashSetNode::get_last_n_output(w(&self.chain)?, distance))
} }
// gets last n outputs inserted in to the tree // gets last n outputs inserted in to the tree
fn get_last_n_rangeproof(&self, distance: u64) -> Vec<TxHashSetNode> { fn get_last_n_rangeproof(&self, distance: u64) -> Result<Vec<TxHashSetNode>, Error> {
TxHashSetNode::get_last_n_rangeproof(w(&self.chain), distance) Ok(TxHashSetNode::get_last_n_rangeproof(
w(&self.chain)?,
distance,
))
} }
// gets last n outputs inserted in to the tree // gets last n outputs inserted in to the tree
fn get_last_n_kernel(&self, distance: u64) -> Vec<TxHashSetNode> { fn get_last_n_kernel(&self, distance: u64) -> Result<Vec<TxHashSetNode>, Error> {
TxHashSetNode::get_last_n_kernel(w(&self.chain), distance) Ok(TxHashSetNode::get_last_n_kernel(w(&self.chain)?, distance))
} }
// allows traversal of utxo set // allows traversal of utxo set
@ -70,18 +73,21 @@ impl TxHashSetHandler {
if max > 1000 { if max > 1000 {
max = 1000; max = 1000;
} }
let outputs = w(&self.chain) let chain = w(&self.chain)?;
let outputs = chain
.unspent_outputs_by_insertion_index(start_index, max) .unspent_outputs_by_insertion_index(start_index, max)
.context(ErrorKind::NotFound)?; .context(ErrorKind::NotFound)?;
Ok(OutputListing { let out = OutputListing {
last_retrieved_index: outputs.0, last_retrieved_index: outputs.0,
highest_index: outputs.1, highest_index: outputs.1,
outputs: outputs outputs: outputs
.2 .2
.iter() .iter()
.map(|x| OutputPrintable::from_output(x, w(&self.chain), None, true)) .map(|x| OutputPrintable::from_output(x, chain.clone(), None, true))
.collect(), .collect::<Result<Vec<_>, _>>()
}) .context(ErrorKind::Internal("cain error".to_owned()))?,
};
Ok(out)
} }
// return a dummy output with merkle proof for position filled out // return a dummy output with merkle proof for position filled out
@ -92,10 +98,9 @@ impl TxHashSetHandler {
id id
)))?; )))?;
let commit = Commitment::from_vec(c); let commit = Commitment::from_vec(c);
let output_pos = w(&self.chain) let chain = w(&self.chain)?;
.get_output_pos(&commit) let output_pos = chain.get_output_pos(&commit).context(ErrorKind::NotFound)?;
.context(ErrorKind::NotFound)?; let merkle_proof = chain::Chain::get_merkle_proof_for_pos(&chain, commit)
let merkle_proof = chain::Chain::get_merkle_proof_for_pos(&w(&self.chain), commit)
.map_err(|_| ErrorKind::NotFound)?; .map_err(|_| ErrorKind::NotFound)?;
Ok(OutputPrintable { Ok(OutputPrintable {
output_type: OutputType::Coinbase, output_type: OutputType::Coinbase,
@ -120,10 +125,10 @@ impl Handler for TxHashSetHandler {
let id = parse_param_no_err!(params, "id", "".to_owned()); let id = parse_param_no_err!(params, "id", "".to_owned());
match right_path_element!(req) { match right_path_element!(req) {
"roots" => json_response_pretty(&self.get_roots()), "roots" => result_to_response(self.get_roots()),
"lastoutputs" => json_response_pretty(&self.get_last_n_output(last_n)), "lastoutputs" => result_to_response(self.get_last_n_output(last_n)),
"lastrangeproofs" => json_response_pretty(&self.get_last_n_rangeproof(last_n)), "lastrangeproofs" => result_to_response(self.get_last_n_rangeproof(last_n)),
"lastkernels" => json_response_pretty(&self.get_last_n_kernel(last_n)), "lastkernels" => result_to_response(self.get_last_n_kernel(last_n)),
"outputs" => result_to_response(self.outputs(start_index, max)), "outputs" => result_to_response(self.outputs(start_index, max)),
"merkleproof" => result_to_response(self.get_merkle_proof_for_output(&id)), "merkleproof" => result_to_response(self.get_merkle_proof_for_output(&id)),
_ => response(StatusCode::BAD_REQUEST, ""), _ => response(StatusCode::BAD_REQUEST, ""),

View file

@ -24,8 +24,9 @@ use std::sync::{Arc, Weak};
// All handlers use `Weak` references instead of `Arc` to avoid cycles that // All handlers use `Weak` references instead of `Arc` to avoid cycles that
// can never be destroyed. These 2 functions are simple helpers to reduce the // can never be destroyed. These 2 functions are simple helpers to reduce the
// boilerplate of dealing with `Weak`. // boilerplate of dealing with `Weak`.
pub fn w<T>(weak: &Weak<T>) -> Arc<T> { pub fn w<T>(weak: &Weak<T>) -> Result<Arc<T>, Error> {
weak.upgrade().unwrap() weak.upgrade()
.ok_or_else(|| ErrorKind::Internal("failed to upgrade weak refernce".to_owned()).into())
} }
/// Retrieves an output from the chain given a commit id (a tiny bit iteratively) /// Retrieves an output from the chain given a commit id (a tiny bit iteratively)
@ -48,14 +49,16 @@ pub fn get_output(
OutputIdentifier::new(OutputFeatures::Coinbase, &commit), OutputIdentifier::new(OutputFeatures::Coinbase, &commit),
]; ];
for x in outputs.iter().filter(|x| w(chain).is_unspent(x).is_ok()) { let chain = w(chain)?;
let block_height = w(chain)
for x in outputs.iter().filter(|x| chain.is_unspent(x).is_ok()) {
let block_height = chain
.get_header_for_output(&x) .get_header_for_output(&x)
.context(ErrorKind::Internal( .context(ErrorKind::Internal(
"Can't get header for output".to_owned(), "Can't get header for output".to_owned(),
))? ))?
.height; .height;
let output_pos = w(chain).get_output_pos(&x.commit).unwrap_or(0); let output_pos = chain.get_output_pos(&x.commit).unwrap_or(0);
return Ok((Output::new(&commit, block_height, output_pos), x.clone())); return Ok((Output::new(&commit, block_height, output_pos), x.clone()));
} }
Err(ErrorKind::NotFound)? Err(ErrorKind::NotFound)?

View file

@ -39,7 +39,7 @@ mod rest;
mod router; mod router;
mod types; mod types;
pub use crate::auth::BasicAuthMiddleware; pub use crate::auth::{BasicAuthMiddleware, GRIN_BASIC_REALM};
pub use crate::handlers::start_rest_apis; pub use crate::handlers::start_rest_apis;
pub use crate::rest::*; pub use crate::rest::*;
pub use crate::router::*; pub use crate::router::*;

View file

@ -19,11 +19,12 @@
//! register them on a ApiServer. //! register them on a ApiServer.
use crate::router::{Handler, HandlerObj, ResponseFuture, Router}; use crate::router::{Handler, HandlerObj, ResponseFuture, Router};
use crate::web::response;
use failure::{Backtrace, Context, Fail, ResultExt}; use failure::{Backtrace, Context, Fail, ResultExt};
use futures::sync::oneshot; use futures::sync::oneshot;
use futures::Stream; use futures::Stream;
use hyper::rt::Future; use hyper::rt::Future;
use hyper::{rt, Body, Request, Server}; use hyper::{rt, Body, Request, Server, StatusCode};
use rustls; use rustls;
use rustls::internal::pemfile; use rustls::internal::pemfile;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
@ -264,6 +265,9 @@ impl Handler for LoggingMiddleware {
mut handlers: Box<dyn Iterator<Item = HandlerObj>>, mut handlers: Box<dyn Iterator<Item = HandlerObj>>,
) -> ResponseFuture { ) -> ResponseFuture {
debug!("REST call: {} {}", req.method(), req.uri().path()); debug!("REST call: {} {}", req.method(), req.uri().path());
handlers.next().unwrap().call(req, handlers) match handlers.next() {
Some(handler) => handler.call(req, handlers),
None => response(StatusCode::INTERNAL_SERVER_ERROR, "no handler found"),
}
} }
} }

View file

@ -222,7 +222,9 @@ impl<'de> serde::de::Visitor<'de> for PrintableCommitmentVisitor {
E: serde::de::Error, E: serde::de::Error,
{ {
Ok(PrintableCommitment { Ok(PrintableCommitment {
commit: pedersen::Commitment::from_vec(util::from_hex(String::from(v)).unwrap()), commit: pedersen::Commitment::from_vec(
util::from_hex(String::from(v)).map_err(serde::de::Error::custom)?,
),
}) })
} }
} }
@ -255,7 +257,7 @@ impl OutputPrintable {
chain: Arc<chain::Chain>, chain: Arc<chain::Chain>,
block_header: Option<&core::BlockHeader>, block_header: Option<&core::BlockHeader>,
include_proof: bool, include_proof: bool,
) -> OutputPrintable { ) -> Result<OutputPrintable, chain::Error> {
let output_type = if output.is_coinbase() { let output_type = if output.is_coinbase() {
OutputType::Coinbase OutputType::Coinbase
} else { } else {
@ -266,7 +268,7 @@ impl OutputPrintable {
let spent = chain.is_unspent(&out_id).is_err(); let spent = chain.is_unspent(&out_id).is_err();
let block_height = match spent { let block_height = match spent {
true => None, true => None,
false => Some(chain.get_header_for_output(&out_id).unwrap().height), false => Some(chain.get_header_for_output(&out_id)?.height),
}; };
let proof = if include_proof { let proof = if include_proof {
@ -280,13 +282,15 @@ impl OutputPrintable {
// We require the rewind() to be stable even after the PMMR is pruned and // We require the rewind() to be stable even after the PMMR is pruned and
// compacted so we can still recreate the necessary proof. // compacted so we can still recreate the necessary proof.
let mut merkle_proof = None; let mut merkle_proof = None;
if output.is_coinbase() && !spent && block_header.is_some() { if output.is_coinbase() && !spent {
merkle_proof = chain.get_merkle_proof(&out_id, &block_header.unwrap()).ok() if let Some(block_header) = block_header {
merkle_proof = chain.get_merkle_proof(&out_id, &block_header).ok();
}
}; };
let output_pos = chain.get_output_pos(&output.commit).unwrap_or(0); let output_pos = chain.get_output_pos(&output.commit).unwrap_or(0);
OutputPrintable { Ok(OutputPrintable {
output_type, output_type,
commit: output.commit, commit: output.commit,
spent, spent,
@ -295,7 +299,7 @@ impl OutputPrintable {
block_height, block_height,
merkle_proof, merkle_proof,
mmr_index: output_pos, mmr_index: output_pos,
} })
} }
pub fn commit(&self) -> Result<pedersen::Commitment, ser::Error> { pub fn commit(&self) -> Result<pedersen::Commitment, ser::Error> {
@ -303,12 +307,13 @@ impl OutputPrintable {
} }
pub fn range_proof(&self) -> Result<pedersen::RangeProof, ser::Error> { pub fn range_proof(&self) -> Result<pedersen::RangeProof, ser::Error> {
let proof_str = self let proof_str = match self.proof.clone() {
.proof Some(p) => p,
.clone() None => return Err(ser::Error::HexError(format!("output range_proof missing"))),
.ok_or_else(|| ser::Error::HexError(format!("output range_proof missing"))) };
.unwrap();
let p_vec = util::from_hex(proof_str).unwrap(); let p_vec = util::from_hex(proof_str)
.map_err(|_| ser::Error::HexError(format!("invalud output range_proof")))?;
let mut p_bytes = [0; util::secp::constants::MAX_PROOF_SIZE]; let mut p_bytes = [0; util::secp::constants::MAX_PROOF_SIZE];
for i in 0..p_bytes.len() { for i in 0..p_bytes.len() {
p_bytes[i] = p_vec[i]; p_bytes[i] = p_vec[i];
@ -428,6 +433,15 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable {
} }
} }
if output_type.is_none()
|| commit.is_none()
|| spent.is_none()
|| proof_hash.is_none()
|| mmr_index.is_none()
{
return Err(serde::de::Error::custom("invalid output"));
}
Ok(OutputPrintable { Ok(OutputPrintable {
output_type: output_type.unwrap(), output_type: output_type.unwrap(),
commit: commit.unwrap(), commit: commit.unwrap(),
@ -570,7 +584,7 @@ impl BlockPrintable {
block: &core::Block, block: &core::Block,
chain: Arc<chain::Chain>, chain: Arc<chain::Chain>,
include_proof: bool, include_proof: bool,
) -> BlockPrintable { ) -> Result<BlockPrintable, chain::Error> {
let inputs = block let inputs = block
.inputs() .inputs()
.iter() .iter()
@ -587,18 +601,19 @@ impl BlockPrintable {
include_proof, include_proof,
) )
}) })
.collect(); .collect::<Result<Vec<_>, _>>()?;
let kernels = block let kernels = block
.kernels() .kernels()
.iter() .iter()
.map(|kernel| TxKernelPrintable::from_txkernel(kernel)) .map(|kernel| TxKernelPrintable::from_txkernel(kernel))
.collect(); .collect();
BlockPrintable { Ok(BlockPrintable {
header: BlockHeaderPrintable::from_header(&block.header), header: BlockHeaderPrintable::from_header(&block.header),
inputs: inputs, inputs: inputs,
outputs: outputs, outputs: outputs,
kernels: kernels, kernels: kernels,
} })
} }
} }
@ -620,24 +635,24 @@ impl CompactBlockPrintable {
pub fn from_compact_block( pub fn from_compact_block(
cb: &core::CompactBlock, cb: &core::CompactBlock,
chain: Arc<chain::Chain>, chain: Arc<chain::Chain>,
) -> CompactBlockPrintable { ) -> Result<CompactBlockPrintable, chain::Error> {
let block = chain.get_block(&cb.hash()).unwrap(); let block = chain.get_block(&cb.hash())?;
let out_full = cb let out_full = cb
.out_full() .out_full()
.iter() .iter()
.map(|x| OutputPrintable::from_output(x, chain.clone(), Some(&block.header), false)) .map(|x| OutputPrintable::from_output(x, chain.clone(), Some(&block.header), false))
.collect(); .collect::<Result<Vec<_>, _>>()?;
let kern_full = cb let kern_full = cb
.kern_full() .kern_full()
.iter() .iter()
.map(|x| TxKernelPrintable::from_txkernel(x)) .map(|x| TxKernelPrintable::from_txkernel(x))
.collect(); .collect();
CompactBlockPrintable { Ok(CompactBlockPrintable {
header: BlockHeaderPrintable::from_header(&cb.header), header: BlockHeaderPrintable::from_header(&cb.header),
out_full, out_full,
kern_full, kern_full,
kern_ids: cb.kern_ids().iter().map(|x| x.to_hex()).collect(), kern_ids: cb.kern_ids().iter().map(|x| x.to_hex()).collect(),
} })
} }
} }

View file

@ -180,3 +180,12 @@ macro_rules! parse_param_no_err(
} }
} }
)); ));
#[macro_export]
macro_rules! w_fut(
($p: expr) =>(
match w($p) {
Ok(p) => p,
Err(_) => return response(StatusCode::INTERNAL_SERVER_ERROR, "weak reference upgrade failed" ),
}
));

View file

@ -2,7 +2,7 @@ use grin_api as api;
use grin_util as util; use grin_util as util;
use crate::api::*; use crate::api::*;
use hyper::{Body, Request}; use hyper::{Body, Request, StatusCode};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::sync::Arc; use std::sync::Arc;
@ -43,7 +43,10 @@ impl Handler for CounterMiddleware {
mut handlers: Box<dyn Iterator<Item = HandlerObj>>, mut handlers: Box<dyn Iterator<Item = HandlerObj>>,
) -> ResponseFuture { ) -> ResponseFuture {
self.counter.fetch_add(1, Ordering::SeqCst); self.counter.fetch_add(1, Ordering::SeqCst);
handlers.next().unwrap().call(req, handlers) match handlers.next() {
Some(h) => h.call(req, handlers),
None => return response(StatusCode::INTERNAL_SERVER_ERROR, "no handler found"),
}
} }
} }

View file

@ -16,7 +16,9 @@
//! invocations) as needed. //! invocations) as needed.
//! Still experimental //! Still experimental
use crate::adapters::{FileWalletCommAdapter, HTTPWalletCommAdapter, KeybaseWalletCommAdapter}; use crate::adapters::{FileWalletCommAdapter, HTTPWalletCommAdapter, KeybaseWalletCommAdapter};
use crate::api::{ApiServer, BasicAuthMiddleware, Handler, ResponseFuture, Router, TLSConfig}; use crate::api::{
ApiServer, BasicAuthMiddleware, Handler, ResponseFuture, Router, TLSConfig, GRIN_BASIC_REALM,
};
use crate::core::core; use crate::core::core;
use crate::core::core::Transaction; use crate::core::core::Transaction;
use crate::keychain::Keychain; use crate::keychain::Keychain;
@ -89,8 +91,8 @@ where
if api_secret.is_some() { if api_secret.is_some() {
let api_basic_auth = let api_basic_auth =
"Basic ".to_string() + &to_base64(&("grin:".to_string() + &api_secret.unwrap())); "Basic ".to_string() + &to_base64(&("grin:".to_string() + &api_secret.unwrap()));
let basic_realm = "Basic realm=GrinOwnerAPI".to_string(); let basic_auth_middleware =
let basic_auth_middleware = Arc::new(BasicAuthMiddleware::new(api_basic_auth, basic_realm)); Arc::new(BasicAuthMiddleware::new(api_basic_auth, &GRIN_BASIC_REALM));
router.add_middleware(basic_auth_middleware); router.add_middleware(basic_auth_middleware);
} }
router router

View file

@ -75,7 +75,7 @@ fn get_outputs_by_pmmr_index_local(
outputs: outputs outputs: outputs
.2 .2
.iter() .iter()
.map(|x| api::OutputPrintable::from_output(x, chain.clone(), None, true)) .map(|x| api::OutputPrintable::from_output(x, chain.clone(), None, true).unwrap())
.collect(), .collect(),
} }
} }