adding simple search functionality and value checking

master
lalanza808 5 years ago
parent aaf6325c23
commit 58c93f8cba

@ -20,14 +20,16 @@ impl Default for RPCPayload {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct RPCParams { pub struct RPCParams {
pub hash: Option<String>, pub hash: Option<String>,
pub txs_hashes: Option<Vec<String>> pub txs_hashes: Option<Vec<String>>,
pub height: Option<String>
} }
impl Default for RPCParams { impl Default for RPCParams {
fn default() -> RPCParams { fn default() -> RPCParams {
RPCParams { RPCParams {
hash: None, hash: None,
txs_hashes: None txs_hashes: None,
height: None
} }
} }
} }
@ -105,6 +107,7 @@ pub struct BlockHeader {
pub height: u32, pub height: u32,
pub major_version: u8, pub major_version: u8,
pub minor_version: u8, pub minor_version: u8,
pub miner_tx_hash: Option<String>,
pub nonce: u32, pub nonce: u32,
pub num_txes: u32, pub num_txes: u32,
pub orphan_status: bool, pub orphan_status: bool,
@ -126,3 +129,19 @@ pub struct GetTransactionsTxs {
pub in_pool: bool, pub in_pool: bool,
pub output_indices: Vec<u32>, pub output_indices: Vec<u32>,
} }
#[derive(Serialize, Deserialize)]
pub struct GetBlock {
pub id: String,
pub jsonrpc: String,
pub result: GetBlockResult
}
#[derive(Serialize, Deserialize)]
pub struct GetBlockResult {
pub blob: String,
pub block_header: BlockHeader,
pub credits: u8,
pub miner_tx_hash: String,
pub tx_hashes: Option<Vec<String>>,
}

@ -4,15 +4,17 @@
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
extern crate reqwest; extern crate reqwest;
mod data_types; mod data_types;
use rocket::http::RawStr;
use rocket::response::Redirect;
use rocket_contrib::json::{Json, JsonValue}; use rocket_contrib::json::{Json, JsonValue};
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use rocket_contrib::serve::StaticFiles; use rocket_contrib::serve::StaticFiles;
use reqwest::blocking::RequestBuilder; use reqwest::blocking::{RequestBuilder, Client};
use std::env; use std::env;
use data_types::*; use data_types::*;
fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder { fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
let http_client = reqwest::blocking::Client::new(); let http_client = Client::new();
let url = format!( let url = format!(
"{}/json_rpc", "{}/json_rpc",
env::var("DAEMON_URI").unwrap() env::var("DAEMON_URI").unwrap()
@ -26,7 +28,7 @@ fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
} }
fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder { fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
let http_client = reqwest::blocking::Client::new(); let http_client = Client::new();
let url = format!( let url = format!(
"{}/{}", "{}/{}",
env::var("DAEMON_URI").unwrap(), env::var("DAEMON_URI").unwrap(),
@ -35,9 +37,7 @@ fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
http_client.post(&url).json(&params) http_client.post(&url).json(&params)
} }
// /block #[get("/block/hash/<block_hash>")]
#[get("/hash/<block_hash>")]
fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> { fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
let params = RPCParams { let params = RPCParams {
hash: Some(block_hash), hash: Some(block_hash),
@ -48,7 +48,19 @@ fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
Json(res.result.block_header) Json(res.result.block_header)
} }
#[get("/tx/<tx_hash>")] #[get("/block/height/<block_height>")]
fn get_block_by_height(block_height: String) -> String {
let params = RPCParams {
height: Some(block_height),
..Default::default()
};
let res: GetBlock = issue_rpc(&"get_block", Some(params))
.send().unwrap().json().unwrap();
// Json(res.result.block_header)
serde_json::to_string(&res).unwrap()
}
#[get("/transaction/<tx_hash>")]
fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions> { fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions> {
let params: JsonValue = json!({"txs_hashes": [&tx_hash]}); let params: JsonValue = json!({"txs_hashes": [&tx_hash]});
let res: GetTransactions = issue_raw_rpc(&"get_transactions", params) let res: GetTransactions = issue_raw_rpc(&"get_transactions", params)
@ -56,20 +68,70 @@ fn get_block_header_by_transaction_hash(tx_hash: String) -> Json<GetTransactions
Json(res) Json(res)
} }
// / #[get("/search?<value>")]
fn search(value: &RawStr) -> Redirect {
let sl: usize = value.len();
let first_byte = value.get(0..1).unwrap();
println!("Search value: {}", value);
println!("First byte: {}", first_byte);
#[get("/info")] if sl < 10 {
fn get_daemon_info() -> Json<GetInfoResult> { match value.parse::<u32>() {
let res: GetInfo = issue_rpc(&"get_info", None) Ok(v) => {
.send().unwrap().json().unwrap(); println!("Found: {}", v);
Json(res.result) // "this looks like a block height"
} return Redirect::found(uri!(get_block_by_height: value.as_str()));
},
Err(e) => {
println!("Error: {}", e);
// "this is an invalid search query"
return Redirect::found(uri!(index));
}
}
} else if sl < 95 {
// "this looks like a tx or block hash"
return Redirect::found(uri!(index));
} else if sl == 95 {
match first_byte {
"9" => {
println!("This looks like a testnet address");
return Redirect::found(uri!(index));
},
"A" => {
println!("This looks like a testnet subaddress");
return Redirect::found(uri!(index));
},
"5" => {
println!("This looks like a stagenet address");
return Redirect::found(uri!(index));
},
"7" => {
println!("This looks like a stagenet subaddress");
return Redirect::found(uri!(index));
},
"4" => {
println!("This looks like a mainnet address");
return Redirect::found(uri!(index));
},
"8" => {
println!("This looks like a mainnet subaddress");
return Redirect::found(uri!(index));
},
_ => {
println!("Not sure what this is");
return Redirect::found(uri!(index));
}
}
} else if sl == 105 {
// "this looks like an integrated address"
return Redirect::found(uri!(index));
} else {
// "no idea what this is"
return Redirect::found(uri!(index));
};
#[post("/", format = "application/x-www-form-urlencoded")] // println!("No if stmt matched");
fn something() -> Template { // return Redirect::found(uri!(index));
let res: GetInfo = issue_rpc(&"get_info", None)
.send().unwrap().json().unwrap();
Template::render("search", &res.result)
} }
#[get("/")] #[get("/")]
@ -93,12 +155,11 @@ fn main() {
Ok(_) => { Ok(_) => {
rocket::ignite() rocket::ignite()
.mount("/", routes![ .mount("/", routes![
index, something, index,
get_daemon_info search,
]) get_block_by_height,
.mount("/block", routes![
get_block_header_by_block_hash, get_block_header_by_block_hash,
get_block_header_by_transaction_hash get_block_header_by_transaction_hash,
]) ])
.mount("/static", StaticFiles::from("./static")) .mount("/static", StaticFiles::from("./static"))
.register(catchers![not_found]) .register(catchers![not_found])

@ -12,8 +12,8 @@
</div> </div>
<div class="search"> <div class="search">
<h2>Search</h2> <h2>Search</h2>
<form action="/" method="post"> <form action="/search" method="get">
<input type="text" name="search_value" placeholder="Enter a transaction hash, block hash, or block height."> <input type="text" name="value" placeholder="Enter a transaction hash, block hash, or block height.">
<input type="submit" value="Search"> <input type="submit" value="Search">
</form> </form>
</div> </div>
@ -32,7 +32,8 @@
<div class=""> <div class="">
<h2>Previous Block</h2> <h2>Previous Block</h2>
</div> </div>
<!--
<h1>get_info</h1>
<p>alt_blocks_count: {{ alt_blocks_count }}</p> <p>alt_blocks_count: {{ alt_blocks_count }}</p>
<p>block_size_limit: {{ block_size_limit }}</p> <p>block_size_limit: {{ block_size_limit }}</p>
<p>block_size_median: {{ block_size_median }}</p> <p>block_size_median: {{ block_size_median }}</p>
@ -71,7 +72,7 @@
<p>was_bootstrap_ever_used: {{ was_bootstrap_ever_used }}</p> <p>was_bootstrap_ever_used: {{ was_bootstrap_ever_used }}</p>
<p>white_peerlist_size: {{ white_peerlist_size }}</p> <p>white_peerlist_size: {{ white_peerlist_size }}</p>
<p>wide_cumulative_difficulty: {{ wide_cumulative_difficulty }}</p> <p>wide_cumulative_difficulty: {{ wide_cumulative_difficulty }}</p>
<p>wide_difficulty: {{ wide_difficulty }}</p> --> <p>wide_difficulty: {{ wide_difficulty }}</p>
</body> </body>
</html> </html>

Loading…
Cancel
Save