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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::block::Block;
use anyhow::ensure;
use aptos_crypto::hash::HashValue;
use aptos_types::validator_verifier::ValidatorVerifier;
use serde::{Deserialize, Serialize};
use short_hex_str::AsShortHexStr;
use std::fmt;
pub const MAX_BLOCKS_PER_REQUEST: u64 = 10;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct BlockRetrievalRequest {
block_id: HashValue,
num_blocks: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
target_block_id: Option<HashValue>,
}
impl BlockRetrievalRequest {
pub fn new(block_id: HashValue, num_blocks: u64) -> Self {
Self {
block_id,
num_blocks,
target_block_id: None,
}
}
pub fn new_with_target_block_id(
block_id: HashValue,
num_blocks: u64,
target_block_id: HashValue,
) -> Self {
Self {
block_id,
num_blocks,
target_block_id: Some(target_block_id),
}
}
pub fn block_id(&self) -> HashValue {
self.block_id
}
pub fn num_blocks(&self) -> u64 {
self.num_blocks
}
pub fn target_block_id(&self) -> Option<HashValue> {
self.target_block_id
}
pub fn match_target_id(&self, hash_value: HashValue) -> bool {
self.target_block_id.map_or(false, |id| id == hash_value)
}
}
impl fmt::Display for BlockRetrievalRequest {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[BlockRetrievalRequest starting from id {} with {} blocks]",
self.block_id, self.num_blocks
)
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum BlockRetrievalStatus {
Succeeded,
IdNotFound,
NotEnoughBlocks,
SucceededWithTarget,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct BlockRetrievalResponse {
status: BlockRetrievalStatus,
blocks: Vec<Block>,
}
impl BlockRetrievalResponse {
pub fn new(status: BlockRetrievalStatus, blocks: Vec<Block>) -> Self {
Self { status, blocks }
}
pub fn status(&self) -> BlockRetrievalStatus {
self.status.clone()
}
pub fn blocks(&self) -> &Vec<Block> {
&self.blocks
}
pub fn verify(
&self,
retrieval_request: BlockRetrievalRequest,
sig_verifier: &ValidatorVerifier,
) -> anyhow::Result<()> {
ensure!(
self.status != BlockRetrievalStatus::Succeeded
|| self.blocks.len() as u64 == retrieval_request.num_blocks(),
"not enough blocks returned, expect {}, get {}",
retrieval_request.num_blocks(),
self.blocks.len(),
);
ensure!(
self.status != BlockRetrievalStatus::SucceededWithTarget
|| (!self.blocks.is_empty()
&& retrieval_request.match_target_id(self.blocks.last().unwrap().id())),
"target not found in blocks returned, expect {:?}",
retrieval_request.target_block_id(),
);
self.blocks
.iter()
.try_fold(retrieval_request.block_id(), |expected_id, block| {
block.validate_signature(sig_verifier)?;
block.verify_well_formed()?;
ensure!(
block.id() == expected_id,
"blocks doesn't form a chain: expect {}, get {}",
expected_id,
block.id()
);
Ok(block.parent_id())
})
.map(|_| ())
}
}
impl fmt::Display for BlockRetrievalResponse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.status() {
BlockRetrievalStatus::Succeeded | BlockRetrievalStatus::SucceededWithTarget => {
write!(
f,
"[BlockRetrievalResponse: status: {:?}, num_blocks: {}, block_ids: ",
self.status(),
self.blocks().len(),
)?;
f.debug_list()
.entries(self.blocks.iter().map(|b| b.id().short_str()))
.finish()?;
write!(f, "]")
}
_ => write!(f, "[BlockRetrievalResponse: status: {:?}]", self.status()),
}
}
}