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