mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 21:57:55 +00:00
ea1c8bd2db
* Receive and import target block body * Request target block * minor: wording * Check for block body in the test * Import target block justifications * Fix: do not fail block validation if no justifications received * Fix: import target blocks without justifications Co-authored-by: arkpar <arkady.paronyan@gmail.com>
98 lines
3.3 KiB
Rust
98 lines
3.3 KiB
Rust
// Copyright 2022 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 <http://www.gnu.org/licenses/>.
|
|
|
|
use codec::{Decode, Encode};
|
|
pub use sp_finality_grandpa::{AuthorityList, SetId};
|
|
use sp_runtime::traits::{Block as BlockT, NumberFor};
|
|
use std::fmt;
|
|
|
|
/// Scale-encoded warp sync proof response.
|
|
pub struct EncodedProof(pub Vec<u8>);
|
|
|
|
/// Warp sync request
|
|
#[derive(Encode, Decode, Debug)]
|
|
pub struct WarpProofRequest<B: BlockT> {
|
|
/// Start collecting proofs from this block.
|
|
pub begin: B::Hash,
|
|
}
|
|
|
|
/// Proof verification result.
|
|
pub enum VerificationResult<Block: BlockT> {
|
|
/// 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 retrieveing and verifying warp sync proofs.
|
|
pub trait WarpSyncProvider<Block: BlockT>: 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<EncodedProof, Box<dyn std::error::Error + Send + Sync>>;
|
|
/// Verify warp proof against current set of authorities.
|
|
fn verify(
|
|
&self,
|
|
proof: &EncodedProof,
|
|
set_id: SetId,
|
|
authorities: AuthorityList,
|
|
) -> Result<VerificationResult<Block>, Box<dyn std::error::Error + Send + Sync>>;
|
|
/// 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<Block: BlockT> {
|
|
/// Waiting for peers to connect.
|
|
AwaitingPeers,
|
|
/// Downloading and verifying grandpa warp proofs.
|
|
DownloadingWarpProofs,
|
|
/// Downloading target block.
|
|
DownloadingTargetBlock,
|
|
/// Downloading state data.
|
|
DownloadingState,
|
|
/// Importing state.
|
|
ImportingState,
|
|
/// Downloading block history.
|
|
DownloadingBlocks(NumberFor<Block>),
|
|
}
|
|
|
|
impl<Block: BlockT> fmt::Display for WarpSyncPhase<Block> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
match self {
|
|
Self::AwaitingPeers => write!(f, "Waiting for peers"),
|
|
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<Block: BlockT> {
|
|
/// Estimated download percentage.
|
|
pub phase: WarpSyncPhase<Block>,
|
|
/// Total bytes downloaded so far.
|
|
pub total_bytes: u64,
|
|
}
|