Skip to content

Commit 73fd384

Browse files
committed
blocks
1 parent 116bbd4 commit 73fd384

File tree

3 files changed

+68
-40
lines changed

3 files changed

+68
-40
lines changed

src/args.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,33 @@ use crate::blocking;
1010
/// mempool-space --arg
1111
/// mempool-space_ARG_STRING
1212
/// mempool-space --arg_string
13-
pub fn generic_sys_call(option: &str, sub_string: &str) {
13+
pub fn generic_sys_call(option: &str, sub_string: &str) -> String {
1414
use std::process::Command;
1515

16-
if sub_string == "v1" {
17-
print!("TODO: support --version v1 api versioning.")
18-
} else if sub_string == "v2" {
19-
print!("TODO: support --version v2 api versioning.")
16+
//if sub_string == "v1" {
17+
// print!("TODO: support --version v1 api versioning.");
18+
//} else if sub_string == "v2" {
19+
// print!("TODO: support --version v2 api versioning.");
20+
//} else {
21+
let output = if cfg!(target_os = "windows") {
22+
Command::new(format!("mempool-space_{}", option))
23+
.args(["/C", sub_string])
24+
.output()
25+
.expect("failed to execute process")
2026
} else {
21-
let output = if cfg!(target_os = "windows") {
22-
Command::new(format!("mempool-space_{}", option))
23-
.args(["/C", sub_string])
24-
.output()
25-
.expect("failed to execute process")
26-
} else {
27-
Command::new(format!("mempool-space_{}", option))
28-
.arg(sub_string)
29-
//.arg("echo hello")
30-
.output()
31-
.expect("failed to execute process")
32-
};
33-
34-
let result = String::from_utf8(output.stdout)
35-
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
36-
.unwrap();
37-
print!("{}", result);
38-
}
27+
Command::new(format!("mempool-space_{}", option))
28+
.arg(sub_string)
29+
//.arg("echo hello")
30+
.output()
31+
.expect("failed to execute process")
32+
};
33+
34+
let result = String::from_utf8(output.stdout)
35+
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
36+
.unwrap();
37+
print!("{}", result);
38+
result
39+
//}
3940
}
4041
/// GET /api/v1/historical-price?currency=CURRENCY&timestamp=TIMESTAMP
4142
/// <https://mempool.space/docs/api/rest#get-historical-price>
@@ -66,6 +67,17 @@ pub fn block_txs(block_hash: &str, start_index: &str) {
6667
let _res = blocking(&format!("block/{}/txs/{}", block_hash, &"0"));
6768
}
6869
}
70+
/// GET /api/v1/blocks[/:startHeight]
71+
/// <https://mempool.space/docs/api/rest#get-blocks>
72+
pub fn blocks(start_height: &str) {
73+
//TODO blocks_tip_height
74+
let start_height_int = start_height.parse::<i32>().unwrap_or(0);
75+
if start_height_int >= 0 {
76+
let _res = blocking(&format!("v1/blocks/{}", start_height));
77+
} else {
78+
let _res = blocking(&format!("v1/blocks"));
79+
}
80+
}
6981

7082
/// <https://mempool.space/docs/api/rest>
7183
/// - [API/REST](https://mempool.space/docs/api/rest)
@@ -159,8 +171,9 @@ pub struct Args {
159171
pub block_start_index: Option<String>,
160172

161173
/// - V1 BLOCKS <BLOCK_HEIGHT>
162-
/// `https://mempool.space/api/v1/blocks/<BLOCK_HEIGHT>`
174+
/// `https://mempool.space/api/v1/blocks/<BLOCKS_START_HEIGHT>`
163175
pub blocks: Option<String>,
176+
164177
/// - V1 BLOCKS_BULK <BLOCK_HEIGHT_START> <BLOCK_HEIGHT_STOP>
165178
/// `https://mempool.space/api/v1/blocks-bulk/<BLOCK_HEIGHT_START>/<BLOCK_HEIGHT_STOP>`
166179
pub blocks_bulk: Option<String>,
@@ -254,7 +267,7 @@ impl Args {
254267
opts.optopt("", "block_txs", "block txs api call", "BLOCK_TXS");
255268
opts.optopt("", "block_start_index", "block txs api call", "BLOCK_START_INDEX");
256269

257-
opts.optopt("", "blocks", "block txids api call", "BLOCKS");
270+
opts.optopt("", "blocks", "block txids api call", "BLOCKS_START_HEIGHT");
258271
opts.optopt("", "blocks_bulk", "block txids api call", "BLOCKS_BULK");
259272

260273
//OPTOPT
@@ -390,6 +403,11 @@ impl Args {
390403
block_txs(&arg_block_txs.unwrap(), &arg_block_start_index.unwrap());
391404
std::process::exit(0);
392405
}
406+
if matches.opt_present("blocks") {
407+
let arg_blocks = matches.opt_str("blocks"); //expect a integer as string
408+
blocks(&arg_blocks.unwrap());
409+
std::process::exit(0);
410+
}
393411

394412
if matches.opt_present("h")
395413
|| (matches.free.is_empty()
@@ -492,6 +510,7 @@ impl Args {
492510
// V1 BLOCKS
493511
// https://mempool.space/api/v1/blocks/<BLOCK_HEIGHT>"
494512
blocks: matches.opt_str("blocks"),
513+
495514
// V1 BLOCKS_BULK
496515
// https://mempool.space/api/v1/blocks-bulk/<BLOCK_HEIGHT_START>/<BLOCK_HEIGHT_STOP>"
497516
blocks_bulk: matches.opt_str("blocks_bulk"),

src/bin/mempool-space_blocks.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
use std::io::Read;
2-
use std::time::{Instant, SystemTime};
3-
41
use mempool_space::blocking;
5-
use reqwest::Url;
6-
7-
// use ureq::get;
8-
9-
const URL: &str = "https://mempool.space/api/blocks";
10-
// const URL: &str = "https://mempool.space/api/blocks/tip";
11-
// const URL: &str = "https://mempool.space/api/blocks/tip/height";
2+
use std::env;
123

134
fn main() {
14-
let n = 1;
155
{
16-
let start = Instant::now();
17-
let blocks = String::from("blocks");
18-
let res = blocking(&blocks);
19-
println!("blocking {:?} {:?} bytes", start.elapsed(), res);
6+
let args: Vec<String> = env::args().collect();
7+
let mut block = &String::from("");
8+
if args.len() > 1 {
9+
block = &args[1];
10+
} else {
11+
// silence is golden
12+
std::process::exit(0);
13+
}
14+
let _res = blocking(&format!("v1/blocks/{}", &block));
2015
}
2116
}

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,20 @@ mod tests {
421421
);
422422
}
423423
#[test]
424+
fn test_blocks() {
425+
// GET /api/v1/blocks[/:startHeight]
426+
let binding = format!("v1/blocks/730000").clone();
427+
let get_block_txid: &str = blocking(&binding).expect("returns current txid from block index");
428+
let get_block_txid = generic_sys_call("blocks", "730000");
429+
let blocks_tip_height = generic_sys_call("blocks_tip_height", "extraneous_arg");
430+
use crate::args::blocks;
431+
blocks(&"");
432+
blocks(&"0");
433+
blocks(&"730000");
434+
blocks(&blocks_tip_height);
435+
wait("1");
436+
}
437+
#[test]
424438
fn test_blockheight() {
425439
let blockheight = blockheight::blockheight();
426440
assert_ne!(0 as f64, blockheight.unwrap());

0 commit comments

Comments
 (0)