Skip to content

Commit

Permalink
grin v5.3 compared 'api' with resulting branch, updated some formatti…
Browse files Browse the repository at this point in the history
…ng, comments
  • Loading branch information
bayk committed Jun 24, 2024
1 parent a640448 commit 89a28e0
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Copyright 2018 The Grin Developers
# Copyright 2021 The Grin Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion api/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
7 changes: 3 additions & 4 deletions api/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -196,7 +196,6 @@ fn build_request_ex(
.map_err::<Error, _>(|e| Error::Argument(format!("Invalid url {}, {}", url, e)))?;

let mut builder = Request::builder();

if basic_auth_key.is_some() && api_secret.is_some() {
let basic_auth = format!(
"Basic {}",
Expand Down Expand Up @@ -272,12 +271,12 @@ where

async fn send_request_async(req: Request<Body>, timeout: TimeOut) -> Result<String, Error> {
let https = hyper_rustls::HttpsConnector::new();
let mut connector = TimeoutConnector::new(https);
let (connect, read, write) = (
Some(timeout.connect),
Some(timeout.read),
Some(timeout.write),
);
let mut connector = TimeoutConnector::new(https);
connector.set_connect_timeout(connect);
connector.set_read_timeout(read);
connector.set_write_timeout(write);
Expand Down Expand Up @@ -310,6 +309,6 @@ pub fn send_request(req: Request<Body>, timeout: TimeOut) -> Result<String, Erro
.basic_scheduler()
.enable_all()
.build()
.map_err(|e| Error::Internal(format!("can't create Tokio runtime, {}", e)))?;
.map_err(|e| Error::RequestError(format!("can't create Tokio runtime, {}", e)))?;
rt.block_on(send_request_async(req, timeout))
}
21 changes: 8 additions & 13 deletions api/src/foreign.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -384,21 +384,16 @@ where
let pool_handler = PoolHandler {
tx_pool: self.tx_pool.clone(),
};
match pool_handler.push_transaction(tx, fluff) {
Ok(_) => Ok(()),
Err(e) => {
warn!(
"Unable to push transaction {} into the pool, {}",
tx_hash, e
);
Err(e)
}
}
pool_handler.push_transaction(tx, fluff).map_err(|e| {
warn!(
"Unable to push transaction {} into the pool, {}",
tx_hash, e
);
e
})
}

/// Get TOR address on this node. Return none if TOR is not running.
pub fn get_libp2p_peers(&self) -> Result<Libp2pPeers, Error> {
//get_server_onion_address()
let libp2p_peers: Vec<String> = libp2p_connection::get_libp2p_connections()
.iter()
.map(|peer| peer.to_string())
Expand Down
3 changes: 1 addition & 2 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,6 @@ use crate::types::{
BlockHeaderPrintable, BlockListing, BlockPrintable, LocatedTxKernel, OutputListing,
OutputPrintable, Tip, Version,
};

use crate::{util, Libp2pMessages, Libp2pPeers};

/// Public definition used to generate Node jsonrpc api.
Expand Down
20 changes: 10 additions & 10 deletions api/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -88,8 +88,7 @@ where
B: BlockChain + 'static,
P: PoolAdapter + 'static,
{
// Manually build router when getting rid of v1
//let mut router = Router::new();
// Adding legacy owner v1 API
let mut router = build_router(
chain.clone(),
tx_pool.clone(),
Expand All @@ -113,7 +112,6 @@ where
"Basic {}",
to_base64(&format!("{}:{}", basic_auth_key, api_secret))
);

let basic_auth_middleware = Arc::new(BasicAuthMiddleware::new(
api_basic_auth,
&MWC_BASIC_REALM,
Expand All @@ -122,12 +120,12 @@ where
router.add_middleware(basic_auth_middleware);
}

let api_handler_v2 = OwnerAPIHandlerV2::new(
let api_handler = OwnerAPIHandlerV2::new(
Arc::downgrade(&chain),
Arc::downgrade(&peers),
Arc::downgrade(&sync_state),
);
router.add_route("/v2/owner", Arc::new(api_handler_v2))?;
router.add_route("/v2/owner", Arc::new(api_handler))?;

let stratum_handler_v2 = StratumAPIHandlerV2::new(stratum_ip_pool);
router.add_route("/v2/stratum", Arc::new(stratum_handler_v2))?;
Expand All @@ -138,7 +136,6 @@ where
"Basic {}",
to_base64(&format!("{}:{}", basic_auth_key, api_secret))
);

let basic_auth_middleware = Arc::new(BasicAuthURIMiddleware::new(
api_basic_auth,
&MWC_FOREIGN_BASIC_REALM,
Expand All @@ -147,13 +144,13 @@ where
router.add_middleware(basic_auth_middleware);
}

let api_handler_v2 = ForeignAPIHandlerV2::new(
let api_handler = ForeignAPIHandlerV2::new(
Arc::downgrade(&peers),
Arc::downgrade(&chain),
Arc::downgrade(&tx_pool),
Arc::downgrade(&sync_state),
);
router.add_route("/v2/foreign", Arc::new(api_handler_v2))?;
router.add_route("/v2/foreign", Arc::new(api_handler))?;

let mut apis = ApiServer::new();
warn!("Starting HTTP Node APIs server at {}.", addr);
Expand All @@ -180,7 +177,10 @@ where
Ok(_) => Ok(()),
Err(e) => {
error!("HTTP API server failed to start. Err: {}", e);
Err(Error::Internal(format!("HTTP API server failed to start, {}", e)).into())
Err(Error::Internal(format!(
"HTTP API server failed to start, {}",
e
)))
}
}
}
Expand Down
40 changes: 21 additions & 19 deletions api/src/handlers/blocks_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +48,7 @@ impl HeaderHandler {
return Err(Error::NotFound(format!(
"Header for height {}, {}",
height, e
)))?
)))
}
}
}
Expand All @@ -66,15 +66,18 @@ impl HeaderHandler {
let oid = match get_output(&self.chain, &commit_id)? {
Some((_, o)) => o,
None => {
return Err(Error::NotFound(format!("Commit id {} not found", commit_id)).into())
return Err(Error::NotFound(format!(
"Commit id {} not found",
commit_id
)))
}
};
match w(&self.chain)?.get_header_for_output(oid.commitment()) {
Ok(header) => Ok(BlockHeaderPrintable::from_header(&header)),
Err(e) => Err(Error::NotFound(format!(
"Header for output {}, {}",
commit_id, e
)))?,
))),
}
}

Expand All @@ -83,7 +86,7 @@ impl HeaderHandler {
let header = chain
.get_block_header(h)
.map_err(|e| Error::NotFound(format!("Block header for hash {}, {}", h, e)))?;
return Ok(BlockHeaderPrintable::from_header(&header));
Ok(BlockHeaderPrintable::from_header(&header))
}

// Try to get hash from height, hash or output commit
Expand All @@ -100,7 +103,7 @@ impl HeaderHandler {
return Err(Error::NotFound(format!(
"Header for height {}, {}",
height, e
)))?
)))
}
}
}
Expand All @@ -110,22 +113,22 @@ impl HeaderHandler {
if let Some(commit) = commit {
let oid = match get_output_v2(&self.chain, &commit, false, false)? {
Some((_, o)) => o,
None => return Err(Error::NotFound(format!("Output {} not found", commit)).into()),
None => return Err(Error::NotFound(format!("Output {} not found", commit))),
};
match w(&self.chain)?.get_header_for_output(oid.commitment()) {
Ok(header) => return Ok(header.hash()),
Err(e) => {
return Err(Error::NotFound(format!(
"Header for output {:?}, {}",
oid, e
)))?
)))
}
}
}
return Err(Error::Argument(format!(
Err(Error::Argument(format!(
"not a valid hash {:?}, height {:?} or output commit {:?}",
hash, height, commit
)))?;
)))
}
}

Expand Down Expand Up @@ -162,7 +165,7 @@ impl BlockHandler {
.get_block(h)
.map_err(|e| Error::NotFound(format!("Block for hash {}, {}", h, e)))?;
BlockPrintable::from_block(&block, &chain, include_proof, include_merkle_proof).map_err(
|e| Error::Internal(format!("chain error, broken block for hash {}. {}", h, e)).into(),
|e| Error::Internal(format!("chain error, broken block for hash {}. {}", h, e)),
)
}

Expand Down Expand Up @@ -249,7 +252,6 @@ impl BlockHandler {
"chain error, broken compact block for hash {}, {}",
h, e
))
.into()
})
}

Expand All @@ -262,7 +264,7 @@ impl BlockHandler {
return Err(Error::NotFound(format!(
"Header for height {}, {}",
height, e
)))?
)))
}
}
}
Expand All @@ -286,7 +288,7 @@ impl BlockHandler {
return Err(Error::NotFound(format!(
"Header for height {}, {}",
height, e
)))?
)))
}
}
}
Expand All @@ -304,14 +306,14 @@ impl BlockHandler {
return Err(Error::NotFound(format!(
"Header for output {:?}, {}",
oid, e
)))?
)))
}
}
}
return Err(Error::Argument(format!(
Err(Error::Argument(format!(
"not a valid hash {:?}, height {:?} or output commit {:?}",
hash, height, commit
)))?;
)))
}
}

Expand All @@ -323,7 +325,7 @@ fn check_block_param(input: &str) -> Result<(), Error> {
return Err(Error::Argument(format!(
"Not a valid hash or height value {}",
input
)))?;
)));
}
Ok(())
}
Expand All @@ -335,7 +337,7 @@ impl Handler for BlockHandler {
Err(e) => {
return response(
StatusCode::BAD_REQUEST,
format!("failed to parse request: {}", e),
format!("failed to parse input: {}", e),
);
}
Ok(h) => h,
Expand Down
23 changes: 10 additions & 13 deletions api/src/handlers/chain_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Grin Developers
// Copyright 2021 The Grin Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,7 @@ impl ChainHandler {
pub fn get_tip(&self) -> Result<Tip, Error> {
let head = w(&self.chain)?
.head()
.map_err(|e| Error::Internal(format!("can't get tip: {}", e)))?;
.map_err(|e| Error::Internal(format!("can't get head: {}", e)))?;
Ok(Tip::from_tip(head))
}
}
Expand All @@ -53,9 +53,12 @@ pub struct ChainValidationHandler {

impl ChainValidationHandler {
pub fn validate_chain(&self, fast_validation: bool) -> Result<(), Error> {
w(&self.chain)?
.validate(fast_validation)
.map_err(|e| Error::Internal(format!("chain fast validation error. {}", e)))
w(&self.chain)?.validate(fast_validation).map_err(|e| {
Error::Internal(format!(
"chain fast validation ({}) error: {}",
fast_validation, e
))
})
}
}

Expand Down Expand Up @@ -434,8 +437,7 @@ impl KernelHandler {
"invalid excess {}, get length {}, expected 33",
excess_s,
excess_v.len()
))
.into());
)));
}
let excess = Commitment::from_vec(excess_v);

Expand Down Expand Up @@ -520,12 +522,7 @@ impl KernelHandler {
height,
mmr_index,
});
kernel.ok_or_else(|| {
Error::from(Error::NotFound(format!(
"kernel value for excess {}",
excess_s
)))
})
kernel.ok_or_else(|| Error::NotFound(format!("kernel value for excess {}", excess_s)))
}
}

Expand Down
Loading

0 comments on commit 89a28e0

Please sign in to comment.