// Copyright Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Substrate is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . use codec::{Decode, Encode}; use futures::channel::oneshot; pub use sp_consensus_grandpa::{AuthorityList, SetId}; use sp_runtime::traits::{Block as BlockT, NumberFor}; use std::{fmt, sync::Arc}; /// Scale-encoded warp sync proof response. pub struct EncodedProof(pub Vec); /// Warp sync request #[derive(Encode, Decode, Debug)] pub struct WarpProofRequest { /// Start collecting proofs from this block. pub begin: B::Hash, } /// The different types of warp syncing. pub enum WarpSyncParams { /// Standard warp sync for the chain. WithProvider(Arc>), /// Skip downloading proofs and wait for a header of the state that should be downloaded. /// /// It is expected that the header provider ensures that the header is trusted. WaitForTarget(oneshot::Receiver<::Header>), } /// Proof verification result. pub enum VerificationResult { /// Proof is valid, but the target was not reached. Partial(SetId, AuthorityList, Block::Hash), /// Target finality is proved. Complete(SetId, AuthorityList, Block::Header), } /// Warp sync backend. Handles retrieving and verifying warp sync proofs. pub trait WarpSyncProvider: Send + Sync { /// Generate proof starting at given block hash. The proof is accumulated until maximum proof /// size is reached. fn generate( &self, start: Block::Hash, ) -> Result>; /// Verify warp proof against current set of authorities. fn verify( &self, proof: &EncodedProof, set_id: SetId, authorities: AuthorityList, ) -> Result, Box>; /// Get current list of authorities. This is supposed to be genesis authorities when starting /// sync. fn current_authorities(&self) -> AuthorityList; } /// Reported warp sync phase. #[derive(Clone, Eq, PartialEq, Debug)] pub enum WarpSyncPhase { /// Waiting for peers to connect. AwaitingPeers { required_peers: usize }, /// Waiting for target block to be received. AwaitingTargetBlock, /// Downloading and verifying grandpa warp proofs. DownloadingWarpProofs, /// Downloading target block. DownloadingTargetBlock, /// Downloading state data. DownloadingState, /// Importing state. ImportingState, /// Downloading block history. DownloadingBlocks(NumberFor), } impl fmt::Display for WarpSyncPhase { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::AwaitingPeers { required_peers } => write!(f, "Waiting for {required_peers} peers to be connected"), Self::AwaitingTargetBlock => write!(f, "Waiting for target block to be received"), Self::DownloadingWarpProofs => write!(f, "Downloading finality proofs"), Self::DownloadingTargetBlock => write!(f, "Downloading target block"), Self::DownloadingState => write!(f, "Downloading state"), Self::ImportingState => write!(f, "Importing state"), Self::DownloadingBlocks(n) => write!(f, "Downloading block history (#{})", n), } } } /// Reported warp sync progress. #[derive(Clone, Eq, PartialEq, Debug)] pub struct WarpSyncProgress { /// Estimated download percentage. pub phase: WarpSyncPhase, /// Total bytes downloaded so far. pub total_bytes: u64, }