sc_service/chain_ops/
export_blocks.rs1use crate::error::Error;
20use codec::Encode;
21use futures::{future, prelude::*};
22use log::info;
23use sp_runtime::{
24 generic::BlockId,
25 traits::{Block as BlockT, NumberFor, One, SaturatedConversion, Zero},
26};
27
28use sc_client_api::{BlockBackend, HeaderBackend, UsageProvider};
29use std::{io::Write, pin::Pin, sync::Arc, task::Poll};
30
31pub fn export_blocks<B, C>(
33 client: Arc<C>,
34 mut output: impl Write + 'static,
35 from: NumberFor<B>,
36 to: Option<NumberFor<B>>,
37 binary: bool,
38) -> Pin<Box<dyn Future<Output = Result<(), Error>>>>
39where
40 C: HeaderBackend<B> + BlockBackend<B> + UsageProvider<B> + 'static,
41 B: BlockT,
42{
43 let mut block = from;
44
45 let last = match to {
46 Some(v) if v.is_zero() => One::one(),
47 Some(v) => v,
48 None => client.usage_info().chain.best_number,
49 };
50
51 let mut wrote_header = false;
52
53 let export = future::poll_fn(move |cx| {
61 let client = &client;
62
63 if last < block {
64 return Poll::Ready(Err("Invalid block range specified".into()))
65 }
66
67 if !wrote_header {
68 info!("Exporting blocks from #{} to #{}", block, last);
69 if binary {
70 let last_: u64 = last.saturated_into::<u64>();
71 let block_: u64 = block.saturated_into::<u64>();
72 let len: u64 = last_ - block_ + 1;
73 output.write_all(&len.encode())?;
74 }
75 wrote_header = true;
76 }
77
78 match client
79 .block_hash_from_id(&BlockId::number(block))?
80 .map(|hash| client.block(hash))
81 .transpose()?
82 .flatten()
83 {
84 Some(block) =>
85 if binary {
86 output.write_all(&block.encode())?;
87 } else {
88 serde_json::to_writer(&mut output, &block)
89 .map_err(|e| format!("Error writing JSON: {}", e))?;
90 },
91 None => return Poll::Ready(Ok(())),
92 }
93 if (block % 10000u32.into()).is_zero() {
94 info!("#{}", block);
95 }
96 if block == last {
97 return Poll::Ready(Ok(()))
98 }
99 block += One::one();
100
101 cx.waker().wake_by_ref();
103 Poll::Pending
104 });
105
106 Box::pin(export)
107}