Skip to content

Commit 8254849

Browse files
committed
src:args:lib.rs:--block_raw --blocks_timestamp
1 parent fe19794 commit 8254849

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

src/args.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl Args {
220220
opts.optopt("", "block_header", "block-header api call", "BLOCK_HEADER");
221221
opts.optopt("", "block_height", "block-height api call", "BLOCK_HEIGHT");
222222
opts.optopt("", "blocks_timestamp", "blocks-timestamp api call", "BLOCKS_TIMESTAMP");
223+
opts.optopt("", "block_raw", "block-raw api call", "BLOCK_RAW");
223224

224225
//OPTOPT
225226
opts.optopt("c", "config", "sets the configuration file", "CONFIG");
@@ -319,6 +320,11 @@ impl Args {
319320
generic_sys_call("blocks_timestamp", &blocks_timestamp.unwrap());
320321
std::process::exit(0);
321322
}
323+
if matches.opt_present("block_raw") {
324+
let block_raw = matches.opt_str("block_raw");
325+
generic_sys_call("block_raw", &block_raw.unwrap());
326+
std::process::exit(0);
327+
}
322328

323329
if matches.opt_present("h")
324330
|| (matches.free.is_empty()

src/bin/mempool-space_block_raw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use std::env;
44
fn main() {
55
{
66
let args: Vec<String> = env::args().collect();
7-
let mut address = &String::from("");
7+
let mut block_hash = &String::from("");
88
if args.len() > 1 {
9-
address = &args[1];
9+
block_hash = &args[1];
1010
} else {
1111
// silence is golden
1212
std::process::exit(0);
1313
}
14-
let _res = blocking(&format!("/address/{}", &address));
14+
let _res = blocking(&format!("/block/{}/raw", &block_hash));
1515
}
1616
}

src/lib.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,31 @@ const URL: &str = "https://mempool.space/api";
1919

2020
/// `pub fn blocking(api: &String) -> Result<&str>`
2121
pub fn blocking(api: &String) -> Result<&str> {
22+
//print!("api={}", api);
2223
let call = format!("{}/{}", URL, api);
2324
let mut body = ureq::get(&call)
2425
.call()
2526
.expect("calls to blocking(api: &String) needs to include /v1/<api_endpoint> in some cases.")
2627
.into_reader();
2728
let mut buf = Vec::new();
2829
body.read_to_end(&mut buf).unwrap();
29-
let text = match std::str::from_utf8(&buf) {
30-
Ok(s) => s,
31-
Err(_) => panic!("Invalid ASCII data"),
32-
};
33-
print!("{}", text);
30+
if !api.ends_with("raw") {
31+
//print!("!api.ends_with raw");
32+
let text = match std::str::from_utf8(&buf) {
33+
Ok(s) => s,
34+
Err(_) => panic!("Invalid ASCII data"),
35+
};
36+
print!("{}", text);
37+
} else {
38+
if api.ends_with("raw") {
39+
//print!("api.ends_with raw");
40+
print!("{:?}", &buf);
41+
}
42+
if api.ends_with("something_else") {
43+
//print!("api.ends_with something_else");
44+
print!("{:?}", &buf);
45+
}
46+
}
3447
Ok(api)
3548
}
3649

@@ -302,22 +315,26 @@ mod tests {
302315
// GET /api/block-height:height
303316
let binding = format!("block-height/615615").clone();
304317
let block_height: &str = blocking(&binding).expect("an existing block hash is needed");
305-
let block_height= generic_sys_call(
306-
"block_height",
307-
"615615",
308-
);
318+
let block_height = generic_sys_call("block_height", "615615");
309319
wait("1");
310320
}
311-
fn test_timestamp() {
321+
fn test_blocks_timestamp() {
312322
// GET /api/v1/mining/blocks/timestamp/:timestamp
313323
let binding = format!("v1/mining/blocks/timestamp/1672531200").clone();
314324
let timestamp: &str = blocking(&binding).expect("an existing block hash is needed");
315-
let timestamp= generic_sys_call(
316-
"timestamp",
317-
"1672531200",
318-
);
325+
let timestamp = generic_sys_call("blocks_timestamp", "1672531200");
319326
wait("1");
320327
}
328+
fn test_block_raw() {
329+
// GET /api/block/:hash/raw
330+
let binding = format!("block/0000000000000000000065bda8f8a88f2e1e00d9a6887a43d640e52a4c7660f2").clone();
331+
let block_raw: &str = blocking(&binding).expect("an existing block hash is needed");
332+
let block_raw = generic_sys_call(
333+
"block_raw",
334+
"0000000000000000000065bda8f8a88f2e1e00d9a6887a43d640e52a4c7660f2",
335+
);
336+
wait("100");
337+
}
321338

322339
#[test]
323340
fn test_blockheight() {

0 commit comments

Comments
 (0)