Skip to content

Commit 79b2f6a

Browse files
committed
blocks_bulk
1 parent 9056dbb commit 79b2f6a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/args.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,30 @@ pub fn block_txs(block_hash: &str, start_index: &str) {
7171
/// <https://mempool.space/docs/api/rest#get-blocks>
7272
pub fn blocks(start_height: &str) {
7373
//TODO blocks_tip_height
74+
let blocks_tip_height = generic_sys_call("blocks_tip_height", &"extraneous_arg");
75+
let blocks_tip_height_int = blocks_tip_height.parse::<i32>().unwrap_or(0);
7476
let start_height_int = start_height.parse::<i32>().unwrap_or(0);
75-
if start_height_int >= 0 {
77+
if start_height_int >= 0 && start_height_int <= blocks_tip_height_int {
7678
let _res = blocking(&format!("v1/blocks/{}", start_height));
7779
} else {
7880
let _res = blocking(&format!("v1/blocks"));
7981
}
8082
}
83+
/// GET /api/v1/blocks-bulk/:minHeight[/:maxHeight]
84+
/// <https://mempool.space/docs/api/rest#get-blocks-bulk>
85+
pub fn blocks_bulk(min_height: &str, max_height: &str) {
86+
//TODO blocks_tip_height
87+
let min_height_int = min_height.parse::<i32>().unwrap_or(0);
88+
let max_height_int = max_height.parse::<i32>().unwrap_or(0);
89+
if min_height_int >= 0 && max_height_int >= 0 && min_height_int <= max_height_int {
90+
let _res = blocking(&format!("v1/blocks-bulk/{}/{}", min_height, max_height));
91+
} else if min_height_int >= 0 && max_height_int >= 0 && min_height_int >= max_height_int {
92+
let _res = blocking(&format!("v1/blocks-bulk/{}/{}", max_height, min_height));
93+
} else {
94+
let blocks_tip_height = generic_sys_call("blocks_tip_height", &"extraneous_arg");
95+
let _res = blocking(&format!("v1/blocks-bulk/{}/{}", min_height, blocks_tip_height));
96+
}
97+
}
8198

8299
/// <https://mempool.space/docs/api/rest>
83100
/// - [API/REST](https://mempool.space/docs/api/rest)

src/bin/mempool-space_blocks_bulk.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use mempool_space::blocking;
2+
use std::env;
3+
4+
fn main() {
5+
{
6+
let args: Vec<String> = env::args().collect();
7+
if args.len() == 2 {
8+
let min_height = &args[1];
9+
let _res = blocking(&format!("v1/blocks-bulk/{}", &min_height));
10+
} else if args.len() == 3 {
11+
let min_height = &args[1];
12+
let max_height = &args[2];
13+
let _res = blocking(&format!("v1/blocks-bulk/{}/{}", &min_height, &max_height));
14+
} else {
15+
// silence is golden
16+
std::process::exit(0);
17+
}
18+
}
19+
}

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,22 @@ mod tests {
435435
wait("1");
436436
}
437437
#[test]
438+
//#[should_panic(expected = "This API is disabled. Set config.MEMPOOL.MAX_BLOCKS_BULK_QUERY to a positive number to enable it.
439+
#[should_panic]
440+
fn test_blocks_bulk() {
441+
// GET /api/v1/blocks-bulk/:minHeight[/:maxHeight]
442+
let binding = format!("v1/blocks-bulk/730000/840000").clone();
443+
let get_block_txid: &str = blocking(&binding).expect("returns current txid from block index");
444+
445+
let get_block_txid = generic_sys_call("blocks_bulk", "730000/840000");
446+
let blocks_tip_height = generic_sys_call("blocks_tip_height", "extraneous_arg");
447+
use crate::args::blocks_bulk;
448+
blocks_bulk(&"0", &"0");
449+
blocks_bulk(&"0", &"1");
450+
blocks_bulk(&"730000", &"840000");
451+
wait("1");
452+
}
453+
#[test]
438454
fn test_blockheight() {
439455
let blockheight = blockheight::blockheight();
440456
assert_ne!(0 as f64, blockheight.unwrap());

0 commit comments

Comments
 (0)