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)]
pub struct RPCParams {
pub hash: Option<String>,
pub txs_hashes: Option<Vec<String>>
pub txs_hashes: Option<Vec<String>>,
pub height: Option<String>
}
impl Default for RPCParams {
fn default() -> RPCParams {
RPCParams {
hash: None,
txs_hashes: None
txs_hashes: None,
height: None
}
}
}
@ -105,6 +107,7 @@ pub struct BlockHeader {
pub height: u32,
pub major_version: u8,
pub minor_version: u8,
pub miner_tx_hash: Option<String>,
pub nonce: u32,
pub num_txes: u32,
pub orphan_status: bool,
@ -126,3 +129,19 @@ pub struct GetTransactionsTxs {
pub in_pool: bool,
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;
extern crate reqwest;
mod data_types;
use rocket::http::RawStr;
use rocket::response::Redirect;
use rocket_contrib::json::{Json, JsonValue};
use rocket_contrib::templates::Template;
use rocket_contrib::serve::StaticFiles;
use reqwest::blocking::RequestBuilder;
use reqwest::blocking::{RequestBuilder, Client};
use std::env;
use data_types::*;
fn issue_rpc(method: &str, params: Option<RPCParams>) -> RequestBuilder {
let http_client = reqwest::blocking::Client::new();
let http_client = Client::new();
let url = format!(
"{}/json_rpc",
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 {
let http_client = reqwest::blocking::Client::new();
let http_client = Client::new();
let url = format!(
"{}/{}",
env::var("DAEMON_URI").unwrap(),
@ -35,9 +37,7 @@ fn issue_raw_rpc(method: &str, params: JsonValue) -> RequestBuilder {
http_client.post(&url).json(&params)
}
// /block
#[get("/hash/<block_hash>")]
#[get("/block/hash/<block_hash>")]
fn get_block_header_by_block_hash(block_hash: String) -> Json<BlockHeader> {
let params = RPCParams {
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)
}
#[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> {
let params: JsonValue = json!({"txs_hashes": [&tx_hash]});
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)
}
// /
#[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")]
fn get_daemon_info() -> Json<GetInfoResult> {
let res: GetInfo = issue_rpc(&"get_info", None)
.send().unwrap().json().unwrap();
Json(res.result)
if sl < 10 {
match value.parse::<u32>() {
Ok(v) => {
println!("Found: {}", v);
// "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")]
fn something() -> Template {
let res: GetInfo = issue_rpc(&"get_info", None)
.send().unwrap().json().unwrap();
Template::render("search", &res.result)
// println!("No if stmt matched");
// return Redirect::found(uri!(index));
}
#[get("/")]
@ -93,12 +155,11 @@ fn main() {
Ok(_) => {
rocket::ignite()
.mount("/", routes![
index, something,
get_daemon_info
])
.mount("/block", routes![
index,
search,
get_block_by_height,
get_block_header_by_block_hash,
get_block_header_by_transaction_hash
get_block_header_by_transaction_hash,
])
.mount("/static", StaticFiles::from("./static"))
.register(catchers![not_found])

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

Loading…
Cancel
Save