Skip to content

Commit 9b15fdc

Browse files
author
Mikhail Zabaluev
committed
Pull from block 0 if no checkpoints intersect
In the client task, fall back to block 0 as the starting point for pulling headers or blocks from, if no checkpoints sent by the requesting node are found to be ancestors of the end block and present in the local storage.
1 parent a534fe5 commit 9b15fdc

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

jormungandr/src/client.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn handle_input(
8383
TaskAction::GetHeaders(handle.async_reply(get_headers(task_data.storage.clone(), ids)))
8484
}
8585
ClientMsg::GetHeadersRange(checkpoints, to, handle) => TaskAction::GetHeadersRange(
86-
handle_get_headers_range(task_data.storage.clone(), checkpoints, to, handle),
86+
handle_get_headers_range(task_data, checkpoints, to, handle),
8787
),
8888
ClientMsg::GetBlocks(ids, handle) => {
8989
TaskAction::GetBlocks(handle.async_reply(get_blocks(task_data.storage.clone(), ids)))
@@ -92,12 +92,7 @@ pub fn handle_input(
9292
handle_get_blocks_range(&task_data.storage, from, to, handle),
9393
),
9494
ClientMsg::PullBlocksToTip(from, handle) => {
95-
TaskAction::PullBlocksToTip(handle_pull_blocks_to_tip(
96-
task_data.storage.clone(),
97-
task_data.blockchain_tip.clone(),
98-
from,
99-
handle,
100-
))
95+
TaskAction::PullBlocksToTip(handle_pull_blocks_to_tip(task_data, from, handle))
10196
}
10297
}
10398
}
@@ -109,15 +104,18 @@ fn get_block_tip(blockchain_tip: &Tip) -> impl Future<Item = Header, Error = Err
109104
}
110105

111106
fn handle_get_headers_range(
112-
storage: Storage,
107+
task_data: &TaskData,
113108
checkpoints: Vec<HeaderHash>,
114109
to: HeaderHash,
115110
handle: ReplyStreamHandle<Header>,
116111
) -> impl Future<Item = (), Error = ()> {
112+
let storage = task_data.storage.clone();
113+
let block0_hash = task_data.block0_hash;
117114
storage
118115
.find_closest_ancestor(checkpoints, to)
119116
.then(move |res| match res {
120-
Ok(Some(from)) => {
117+
Ok(maybe_ancestor) => {
118+
let from = maybe_ancestor.unwrap_or(block0_hash);
121119
let fut = storage
122120
.send_from_to(
123121
from,
@@ -127,10 +125,6 @@ fn handle_get_headers_range(
127125
.map_err(|_: ReplySendError| ());
128126
Either::A(fut)
129127
}
130-
Ok(None) => Either::B(handle.async_error(Error::not_found(
131-
"none of the checkpoints found in the local storage \
132-
are ancestors of the requested end block",
133-
))),
134128
Err(e) => Either::B(handle.async_error(e.into())),
135129
})
136130
}
@@ -178,27 +172,27 @@ fn get_headers(
178172
}
179173

180174
fn handle_pull_blocks_to_tip(
181-
storage: Storage,
182-
blockchain_tip: Tip,
175+
task_data: &TaskData,
183176
checkpoints: Vec<HeaderHash>,
184177
handle: ReplyStreamHandle<Block>,
185178
) -> impl Future<Item = (), Error = ()> {
186-
blockchain_tip
179+
let storage = task_data.storage.clone();
180+
let block0_hash = task_data.block0_hash;
181+
task_data
182+
.blockchain_tip
187183
.get_ref()
188184
.and_then(move |tip| {
189185
let tip_hash = tip.hash();
190186
storage
191187
.find_closest_ancestor(checkpoints, tip_hash)
192-
.map(move |maybe_ancestor| (storage, maybe_ancestor, tip_hash))
188+
.map(move |maybe_ancestor| {
189+
(storage, maybe_ancestor.unwrap_or(block0_hash), tip_hash)
190+
})
193191
})
194192
.then(move |res| match res {
195-
Ok((storage, Some(from), to)) => {
193+
Ok((storage, from, to)) => {
196194
Either::A(storage.send_from_to(from, to, handle).map_err(|_| ()))
197195
}
198-
Ok((_, None, _)) => Either::B(handle.async_error(Error::not_found(
199-
"none of the checkpoints found in the local storage \
200-
are ancestors of the current tip",
201-
))),
202196
Err(e) => Either::B(handle.async_error(e.into())),
203197
})
204198
}

0 commit comments

Comments
 (0)