Skip to content

Commit b90f4cc

Browse files
committed
fix(esplora): revert back to using a for loop in chain_update
..to preserve the original control flow. In case of error we want to return the last local hash that is tried for equality with the remote hash. To do so we store a copy of the current local hash on each iteration.
1 parent 7d70b6a commit b90f4cc

2 files changed

Lines changed: 36 additions & 28 deletions

File tree

crates/esplora/src/async_ext.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,31 +224,35 @@ async fn chain_update<S: Sleeper>(
224224
local_tip: &CheckPoint,
225225
anchors: &BTreeSet<(ConfirmationBlockTime, Txid)>,
226226
) -> Result<CheckPoint, Error> {
227-
let mut cp = local_tip.clone();
227+
let mut point_of_agreement = None;
228+
let mut local_cp_hash = local_tip.hash();
228229
let mut conflicts = vec![];
229230

230-
let mut tip = loop {
231-
let remote_hash = match fetch_block(client, latest_blocks, cp.height()).await? {
231+
for local_cp in local_tip.iter() {
232+
let remote_hash = match fetch_block(client, latest_blocks, local_cp.height()).await? {
232233
Some(hash) => hash,
233234
None => continue,
234235
};
235-
if remote_hash == cp.hash() {
236-
break cp;
236+
if remote_hash == local_cp.hash() {
237+
point_of_agreement = Some(local_cp);
238+
break;
237239
}
238-
// it is not strictly necessary to include all the conflicted heights (we do need the
240+
local_cp_hash = local_cp.hash();
241+
// It is not strictly necessary to include all the conflicted heights (we do need the
239242
// first one) but it seems prudent to make sure the updated chain's heights are a
240243
// superset of the existing chain after update.
241244
conflicts.push(BlockId {
242-
height: cp.height(),
245+
height: local_cp.height(),
243246
hash: remote_hash,
244247
});
245-
cp = match cp.prev() {
246-
Some(prev) => prev,
247-
None => {
248-
return Err(Box::new(esplora_client::Error::HeaderHashNotFound(
249-
cp.hash(),
250-
)));
251-
}
248+
}
249+
250+
let mut tip = match point_of_agreement {
251+
Some(tip) => tip,
252+
None => {
253+
return Err(Box::new(esplora_client::Error::HeaderHashNotFound(
254+
local_cp_hash,
255+
)));
252256
}
253257
};
254258

crates/esplora/src/blocking_ext.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,35 @@ fn chain_update(
209209
local_tip: &CheckPoint,
210210
anchors: &BTreeSet<(ConfirmationBlockTime, Txid)>,
211211
) -> Result<CheckPoint, Error> {
212-
let mut cp = local_tip.clone();
212+
let mut point_of_agreement = None;
213+
let mut local_cp_hash = local_tip.hash();
213214
let mut conflicts = vec![];
214215

215-
let mut tip = loop {
216-
let remote_hash = match fetch_block(client, latest_blocks, cp.height())? {
216+
for local_cp in local_tip.iter() {
217+
let remote_hash = match fetch_block(client, latest_blocks, local_cp.height())? {
217218
Some(hash) => hash,
218219
None => continue,
219220
};
220-
if remote_hash == cp.hash() {
221-
break cp;
221+
if remote_hash == local_cp.hash() {
222+
point_of_agreement = Some(local_cp);
223+
break;
222224
}
223-
// it is not strictly necessary to include all the conflicted heights (we do need the
225+
local_cp_hash = local_cp.hash();
226+
// It is not strictly necessary to include all the conflicted heights (we do need the
224227
// first one) but it seems prudent to make sure the updated chain's heights are a
225228
// superset of the existing chain after update.
226229
conflicts.push(BlockId {
227-
height: cp.height(),
230+
height: local_cp.height(),
228231
hash: remote_hash,
229232
});
230-
cp = match cp.prev() {
231-
Some(prev) => prev,
232-
None => {
233-
return Err(Box::new(esplora_client::Error::HeaderHashNotFound(
234-
cp.hash(),
235-
)));
236-
}
233+
}
234+
235+
let mut tip = match point_of_agreement {
236+
Some(tip) => tip,
237+
None => {
238+
return Err(Box::new(esplora_client::Error::HeaderHashNotFound(
239+
local_cp_hash,
240+
)));
237241
}
238242
};
239243

0 commit comments

Comments
 (0)