Skip to content

Commit 01370d7

Browse files
authored
Foreign API: Bulk block request (#3751)
* remove non-mainnet check for PIBD requests * add first version of get blocks function * .\api\src\foreign.rs * add a max value to get_blocks, documentation * typo * updates based on feedback
1 parent 0649ba9 commit 01370d7

File tree

5 files changed

+370
-3
lines changed

5 files changed

+370
-3
lines changed

api/src/foreign.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ use crate::handlers::pool_api::PoolHandler;
2323
use crate::handlers::transactions_api::TxHashSetHandler;
2424
use crate::handlers::version_api::VersionHandler;
2525
use crate::pool::{self, BlockChain, PoolAdapter, PoolEntry};
26-
use crate::rest::*;
2726
use crate::types::{
2827
BlockHeaderPrintable, BlockPrintable, LocatedTxKernel, OutputListing, OutputPrintable, Tip,
2928
Version,
3029
};
3130
use crate::util::RwLock;
31+
use crate::{rest::*, BlockListing};
3232
use std::sync::Weak;
3333

3434
/// Main interface into all node API functions.
@@ -139,6 +139,36 @@ where
139139
block_handler.get_block(&hash, include_proof, include_merkle_proof)
140140
}
141141

142+
/// Returns a [`BlockListing`](types/struct.BlockListing.html) of available blocks
143+
/// between `min_height` and `max_height`
144+
/// The method will query the database for blocks starting at the block height `min_height`
145+
/// and continue until `max_height`, skipping any blocks that aren't available.
146+
///
147+
/// # Arguments
148+
/// * `start_height` - starting height to lookup.
149+
/// * `end_height` - ending height to to lookup.
150+
/// * 'max` - The max number of blocks to return.
151+
/// Note this is overriden with BLOCK_TRANSFER_LIMIT if BLOCK_TRANSFER_LIMIT is exceeded
152+
///
153+
/// # Returns
154+
/// * Result Containing:
155+
/// * A [`BlockListing`](types/struct.BlockListing.html)
156+
/// * or [`Error`](struct.Error.html) if an error is encountered.
157+
///
158+
159+
pub fn get_blocks(
160+
&self,
161+
start_height: u64,
162+
end_height: u64,
163+
max: u64,
164+
include_proof: Option<bool>,
165+
) -> Result<BlockListing, Error> {
166+
let block_handler = BlockHandler {
167+
chain: self.chain.clone(),
168+
};
169+
block_handler.get_blocks(start_height, end_height, max, include_proof)
170+
}
171+
142172
/// Returns the node version and block header version (used by grin-wallet).
143173
///
144174
/// # Returns

api/src/foreign_rpc.rs

Lines changed: 258 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::pool::PoolEntry;
2121
use crate::pool::{BlockChain, PoolAdapter};
2222
use crate::rest::Error;
2323
use crate::types::{
24-
BlockHeaderPrintable, BlockPrintable, LocatedTxKernel, OutputListing, OutputPrintable, Tip,
25-
Version,
24+
BlockHeaderPrintable, BlockListing, BlockPrintable, LocatedTxKernel, OutputListing,
25+
OutputPrintable, Tip, Version,
2626
};
2727
use crate::util;
2828

@@ -246,6 +246,251 @@ pub trait ForeignRpc: Sync + Send {
246246
commit: Option<String>,
247247
) -> Result<BlockPrintable, Error>;
248248

249+
/**
250+
Networked version of [Foreign::get_blocks](struct.Foreign.html#method.get_blocks).
251+
252+
# Json rpc example
253+
254+
```
255+
# grin_api::doctest_helper_json_rpc_foreign_assert_response!(
256+
# r#"
257+
{
258+
"jsonrpc": "2.0",
259+
"method": "get_blocks",
260+
"params": [2299309, 2300309, 2, false],
261+
"id": 1
262+
}
263+
# "#
264+
# ,
265+
# r#"
266+
{
267+
"id": 1,
268+
"jsonrpc": "2.0",
269+
"result": {
270+
"Ok": {
271+
"blocks": [
272+
{
273+
"header": {
274+
"cuckoo_solution": [
275+
20354215,
276+
100524565,
277+
169529296,
278+
259818619,
279+
261952555,
280+
265003136,
281+
290685286,
282+
307792709,
283+
329993483,
284+
331550733,
285+
478902211,
286+
707186317,
287+
717277083,
288+
742312701,
289+
763869950,
290+
785680094,
291+
791217416,
292+
1156641404,
293+
1244452354,
294+
1277970471,
295+
1405106926,
296+
1663783361,
297+
1701259732,
298+
1795507572,
299+
1845900835,
300+
2060172013,
301+
2067055232,
302+
2169213199,
303+
2191128830,
304+
2253855427,
305+
2626425322,
306+
2678973678,
307+
2815586448,
308+
2921010487,
309+
3042894274,
310+
3103031603,
311+
3492595971,
312+
3603041347,
313+
3853538391,
314+
3974438280,
315+
4199558832,
316+
4262968379
317+
],
318+
"edge_bits": 32,
319+
"hash": "0004331bb122685f12644e40b163e4557951b2b835ad2493502750ea787af7cc",
320+
"height": 2299309,
321+
"kernel_mmr_size": 8568165,
322+
"kernel_root": "6b4adb9ee193ad043910b5a8c1bac0864ab99f57845101a3b422031bcf5c2ce1",
323+
"nonce": 4185528505858938389,
324+
"output_mmr_size": 13524183,
325+
"output_root": "b642891741b56adaf7762813490d161377d0fbf7b47170d235beef33c25a4d77",
326+
"prev_root": "a0ba3206b6a8089ef05690d40767c41cc0514eaa5031ebce1960a7cc2edcc211",
327+
"previous": "000207548609a9007eacd7dfcdc8006252d6b1ad70864ea8ddebe4ca9e82bd74",
328+
"range_proof_root": "d8cefda00f325fd9a1223454f23276b73d8a1d7c72ec74cdfb9bdf5c77a04dee",
329+
"secondary_scaling": 0,
330+
"timestamp": "2023-06-05T20:18:45+00:00",
331+
"total_difficulty": 2072532663425232,
332+
"total_kernel_offset": "b0a0c21326532b4a91c18d2355aedca4d8ed68b77db9882feb85da8120b4f533",
333+
"version": 5
334+
},
335+
"inputs": [
336+
"092b140b1500812ac58ef68c17a2bbf2ec3531bcf0ce4dc32bbf8a29351d1784d7",
337+
"083b72230921abeacd637dae8505233ab035c20dff1bfdab5ff5bb41b2f5238458"
338+
],
339+
"kernels": [
340+
{
341+
"excess": "08ab720dc374f099e6726e2dceada508a0331bb1f13b8a4e56afde83ff42f7a351",
342+
"excess_sig": "6858120e9758d7587e27fd5dc9c26117a2ce0d5a7d871ce805e03eb494bfa1f86a27991865b3ab709064c43692433fd58f008c3bba2c88ad5f95a0c8ff3cf11f",
343+
"features": "Plain",
344+
"fee": 23500000,
345+
"fee_shift": 0,
346+
"lock_height": 0
347+
},
348+
{
349+
"excess": "08d0a44b22952b03b29e3d88391102c281dcab4763def22cab65ed45e35b9078e8",
350+
"excess_sig": "32f91d5671e334a87843a8b02c550c9e0fbdfe507ee62417cc123b5078d7884701a42e257357a1bed9dc4a8e07540b1629e9fa95a05c44adb5cb001c8fb777ee",
351+
"features": "Coinbase",
352+
"fee": 0,
353+
"fee_shift": 0,
354+
"lock_height": 0
355+
}
356+
],
357+
"outputs": [
358+
{
359+
"block_height": 2299309,
360+
"commit": "0857c94df51dd226fa0c5920aae6d73d069603f973b2e06551698c6d39fdc2c192",
361+
"merkle_proof": null,
362+
"mmr_index": 13524176,
363+
"output_type": "Coinbase",
364+
"proof": null,
365+
"proof_hash": "0937291a8a3c81cea4421fa0d0b291aacb5d46065cfd93747a15f58d99d781b6",
366+
"spent": false
367+
},
368+
{
369+
"block_height": 2299309,
370+
"commit": "08d4681b904695edee6e183cd40564ea0f5589b35d4d386da2eb980a6a92b1b307",
371+
"merkle_proof": null,
372+
"mmr_index": 0,
373+
"output_type": "Transaction",
374+
"proof": null,
375+
"proof_hash": "41694ab6dcd9b1664ca28e79c3302144b99a4c1cb45d13c8728604c1d26e37bf",
376+
"spent": true
377+
},
378+
{
379+
"block_height": 2299309,
380+
"commit": "08255a260a65fc87cfd924780d896eaadb42468b0fe3ba6adeace378793b5d8172",
381+
"merkle_proof": null,
382+
"mmr_index": 13524182,
383+
"output_type": "Transaction",
384+
"proof": null,
385+
"proof_hash": "58c77a5716ec4806dbddac64a83d6e4351b6eeffca391be1b11ec74aac0514dc",
386+
"spent": false
387+
}
388+
]
389+
},
390+
{
391+
"header": {
392+
"cuckoo_solution": [
393+
898450,
394+
353949138,
395+
440882514,
396+
500154010,
397+
555236503,
398+
615120852,
399+
740100750,
400+
754668484,
401+
1056458121,
402+
1071299788,
403+
1130460099,
404+
1414281857,
405+
1444894533,
406+
1481124421,
407+
1551877341,
408+
1666859923,
409+
1682642953,
410+
1837365586,
411+
1845508478,
412+
1872787697,
413+
2040619654,
414+
2078971700,
415+
2104947318,
416+
2206501084,
417+
2233951742,
418+
2360961460,
419+
2378988856,
420+
2402500295,
421+
2438384422,
422+
2532261092,
423+
2879360933,
424+
3011869457,
425+
3023365279,
426+
3412207020,
427+
3509607650,
428+
3793770861,
429+
3850043972,
430+
3873426868,
431+
3965579806,
432+
4007877324,
433+
4090157476,
434+
4141650723
435+
],
436+
"edge_bits": 32,
437+
"hash": "00006871e1fb8e7dddcc46343d7fbba14d08946c67b4568f3c2e98ec8c554ae9",
438+
"height": 2299310,
439+
"kernel_mmr_size": 8568166,
440+
"kernel_root": "87184dc2f9efa6467ce797191c5d3ef086403d0103ba0b5adc6a71ed203a053c",
441+
"nonce": 13726392224838330049,
442+
"output_mmr_size": 13524184,
443+
"output_root": "9570fbccef29609c5d3c68b07771bf4e7e80d0b139d9bd0215d1e9d1aaaed813",
444+
"prev_root": "df1c67366b9cdd8deea570534a00a320748899e146288be067c0f402038e6aa0",
445+
"previous": "0004331bb122685f12644e40b163e4557951b2b835ad2493502750ea787af7cc",
446+
"range_proof_root": "987d7aff01e201269d4c6b00e885b9ed9c10f47205edd7727e3490aab953ca80",
447+
"secondary_scaling": 0,
448+
"timestamp": "2023-06-05T20:19:27+00:00",
449+
"total_difficulty": 2072532872584027,
450+
"total_kernel_offset": "b0a0c21326532b4a91c18d2355aedca4d8ed68b77db9882feb85da8120b4f533",
451+
"version": 5
452+
},
453+
"inputs": [],
454+
"kernels": [
455+
{
456+
"excess": "08224a7946a75071b127af45496ddd3fc438db325cc35c3e4b0fdf23ed27703dd8",
457+
"excess_sig": "d8c81bd8130c30016e38655a32b4c7a1f8fffda34a736dd8cdbcad05d28d09e3708d1f01e21276747eb03f28b9f5a834cb0ef8532330183df2b10d47ae7e68c6",
458+
"features": "Coinbase",
459+
"fee": 0,
460+
"fee_shift": 0,
461+
"lock_height": 0
462+
}
463+
],
464+
"outputs": [
465+
{
466+
"block_height": 2299310,
467+
"commit": "09997d3c1eff72b7efa7bfb52032d713f5907755838c01a6e178a87a0ac170a279",
468+
"merkle_proof": null,
469+
"mmr_index": 13524184,
470+
"output_type": "Coinbase",
471+
"proof": null,
472+
"proof_hash": "6c2c10af5c4b6d2bcf71084c2bd9685ae91427f03a8b78736ab27d6c5bc7e4db",
473+
"spent": false
474+
}
475+
]
476+
}
477+
],
478+
"last_retrieved_height": 2299310
479+
}
480+
}
481+
}
482+
# "#
483+
# );
484+
```
485+
*/
486+
fn get_blocks(
487+
&self,
488+
start_height: u64,
489+
end_height: u64,
490+
max: u64,
491+
include_proof: Option<bool>,
492+
) -> Result<BlockListing, Error>;
493+
249494
/**
250495
Networked version of [Foreign::get_version](struct.Foreign.html#method.get_version).
251496
@@ -760,6 +1005,7 @@ where
7601005
}
7611006
Foreign::get_header(self, height, parsed_hash, commit)
7621007
}
1008+
7631009
fn get_block(
7641010
&self,
7651011
height: Option<u64>,
@@ -775,6 +1021,16 @@ where
7751021
Foreign::get_block(self, height, parsed_hash, commit)
7761022
}
7771023

1024+
fn get_blocks(
1025+
&self,
1026+
start_height: u64,
1027+
end_height: u64,
1028+
max: u64,
1029+
include_proof: Option<bool>,
1030+
) -> Result<BlockListing, Error> {
1031+
Foreign::get_blocks(self, start_height, end_height, max, include_proof)
1032+
}
1033+
7781034
fn get_version(&self) -> Result<Version, Error> {
7791035
Foreign::get_version(self)
7801036
}

0 commit comments

Comments
 (0)