referrerpolicy=no-referrer-when-downgrade

sc_service/chain_ops/
export_blocks.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use 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
31/// Performs the blocks export.
32pub 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	// Exporting blocks is implemented as a future, because we want the operation to be
54	// interruptible.
55	//
56	// Every time we write a block to the output, the `Future` re-schedules itself and returns
57	// `Poll::Pending`.
58	// This makes it possible either to interleave other operations in-between the block exports,
59	// or to stop the operation completely.
60	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		// Re-schedule the task in order to continue the operation.
102		cx.waker().wake_by_ref();
103		Poll::Pending
104	});
105
106	Box::pin(export)
107}