use codec::{Decode, Encode};
pub use sp_consensus_grandpa::{AuthorityList, SetId};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::fmt;
pub struct EncodedProof(pub Vec<u8>);
#[derive(Encode, Decode, Debug)]
pub struct WarpProofRequest<B: BlockT> {
pub begin: B::Hash,
}
pub enum VerificationResult<Block: BlockT> {
Partial(SetId, AuthorityList, Block::Hash),
Complete(SetId, AuthorityList, Block::Header),
}
pub trait WarpSyncProvider<Block: BlockT>: Send + Sync {
fn generate(
&self,
start: Block::Hash,
) -> Result<EncodedProof, Box<dyn std::error::Error + Send + Sync>>;
fn verify(
&self,
proof: &EncodedProof,
set_id: SetId,
authorities: AuthorityList,
) -> Result<VerificationResult<Block>, Box<dyn std::error::Error + Send + Sync>>;
fn current_authorities(&self) -> AuthorityList;
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum WarpSyncPhase<Block: BlockT> {
AwaitingPeers { required_peers: usize },
AwaitingTargetBlock,
DownloadingWarpProofs,
DownloadingTargetBlock,
DownloadingState,
ImportingState,
DownloadingBlocks(NumberFor<Block>),
}
impl<Block: BlockT> fmt::Display for WarpSyncPhase<Block> {
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),
}
}
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct WarpSyncProgress<Block: BlockT> {
pub phase: WarpSyncPhase<Block>,
pub total_bytes: u64,
}