Skip to content

Commit 06b6d7e

Browse files
evanlinjinLagginTimes
authored andcommitted
fix(chain): Fix merge_chains logic
* Base tip must always have data. * Update should be able to introduce data to a placeholder checkpoint in the original chain. * Remove unnecessary logic.
1 parent b73dcd6 commit 06b6d7e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

crates/chain/src/local_chain.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ where
2626
let mut base: Option<CheckPoint<D>> = None;
2727

2828
for cp in init_cp.iter() {
29-
if cp.height() >= start_height {
30-
if let Some(data) = cp.data() {
29+
// Base tip should always have data.
30+
if let Some(data) = cp.data() {
31+
if cp.height() >= start_height {
3132
extension.insert(cp.height(), data);
33+
} else {
34+
base = Some(cp);
35+
break;
3236
}
33-
} else {
34-
base = Some(cp);
35-
break;
3637
}
3738
}
3839

@@ -47,19 +48,13 @@ where
4748
};
4849
}
4950

50-
let mut new_tip = match base {
51+
let new_tip = match base {
5152
Some(base) => base
5253
.extend(extension)
5354
.expect("extension is strictly greater than base"),
5455
None => LocalChain::from_blocks(extension)?.tip(),
5556
};
5657

57-
if new_tip.data_ref().is_none() {
58-
new_tip = new_tip
59-
.find_data(new_tip.height())
60-
.expect("genesis checkpoint should have data");
61-
}
62-
6358
init_cp = new_tip;
6459
}
6560

@@ -703,6 +698,11 @@ where
703698
return Ok((new_tip, changeset));
704699
}
705700
}
701+
// Even if the hashes are the same, the update may contain data which the
702+
// original does not have.
703+
if let (None, Some(u_data)) = (o.data_ref(), u.data()) {
704+
changeset.blocks.insert(u.height(), Some(u_data));
705+
}
706706
} else {
707707
// We have an invalidation height so we set the height to the updated hash and
708708
// also purge all the original chain block hashes above this block.

0 commit comments

Comments
 (0)