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
104
105
106
107
108
109
110
111
use codec::{Decode, Encode};
use futures::channel::oneshot;
pub use sp_finality_grandpa::{AuthorityList, SetId};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::{fmt, sync::Arc};
pub struct EncodedProof(pub Vec<u8>);
#[derive(Encode, Decode, Debug)]
pub struct WarpProofRequest<B: BlockT> {
pub begin: B::Hash,
}
pub enum WarpSyncParams<Block: BlockT> {
WithProvider(Arc<dyn WarpSyncProvider<Block>>),
WaitForTarget(oneshot::Receiver<<Block as BlockT>::Header>),
}
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,
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 => write!(f, "Waiting for peers"),
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,
}