Skip to content

Commit ffc5819

Browse files
committed
fix: burn-block-height inside at-block context
1 parent 29db45f commit ffc5819

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

clarity/src/vm/database/clarity_db.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,35 +1075,41 @@ impl<'a> ClarityDatabase<'a> {
10751075
/// block height (i.e. that returned by `get_index_block_header_hash` for
10761076
/// `get_current_block_height`).
10771077
pub fn get_current_burnchain_block_height(&mut self) -> Result<u32> {
1078-
let cur_stacks_height = self.store.get_current_block_height();
1078+
let epoch = self.get_clarity_epoch_version()?;
1079+
match epoch {
1080+
// Special case to preserve possibly incorrect behavior (inside at-block) in Epoch 3.0
1081+
StacksEpochId::Epoch30 => {
1082+
self.burn_state_db
1083+
.get_tip_burn_block_height()
1084+
.ok_or_else(|| {
1085+
InterpreterError::Expect("Failed to get burnchain tip height.".into())
1086+
.into()
1087+
})
1088+
}
1089+
_ => {
1090+
let cur_stacks_height = self.store.get_current_block_height();
1091+
1092+
// Before epoch 3.0, we can only access the burn block associated with the last block
1093+
let last_mined_bhh = if epoch.clarity_uses_tip_burn_block() {
1094+
// In epoch 3+, we can access the current burnchain block
1095+
self.get_index_block_header_hash(cur_stacks_height)
1096+
.or_else(|_| self.get_index_block_header_hash(cur_stacks_height - 1))?
1097+
} else if cur_stacks_height == 0 {
1098+
return Ok(self.burn_state_db.get_burn_start_height());
1099+
} else {
1100+
// Safety note: normal subtraction is safe here, because we've already checked
1101+
// that cur_stacks_height > 0.
1102+
self.get_index_block_header_hash(cur_stacks_height - 1)?
1103+
};
10791104

1080-
// Before epoch 3.0, we can only access the burn block associated with the last block
1081-
if !self
1082-
.get_clarity_epoch_version()?
1083-
.clarity_uses_tip_burn_block()
1084-
{
1085-
if cur_stacks_height == 0 {
1086-
return Ok(self.burn_state_db.get_burn_start_height());
1087-
};
1088-
// Safety note: normal subtraction is safe here, because we've already checked
1089-
// that cur_stacks_height > 0.
1090-
let last_mined_bhh = self.get_index_block_header_hash(cur_stacks_height - 1)?;
1091-
1092-
self.get_burnchain_block_height(&last_mined_bhh)
1093-
.ok_or_else(|| {
1094-
InterpreterError::Expect(format!(
1095-
"Block header hash '{}' must return for provided stacks block height {}",
1096-
&last_mined_bhh, cur_stacks_height
1097-
))
1098-
.into()
1099-
})
1100-
} else {
1101-
// In epoch 3+, we can access the current burnchain block
1102-
self.burn_state_db
1103-
.get_tip_burn_block_height()
1104-
.ok_or_else(|| {
1105-
InterpreterError::Expect("Failed to get burnchain tip height.".into()).into()
1106-
})
1105+
self.get_burnchain_block_height(&last_mined_bhh)
1106+
.ok_or_else(|| {
1107+
InterpreterError::Expect(format!(
1108+
"Block header hash '{last_mined_bhh}' must return for provided stacks block height {cur_stacks_height}"
1109+
))
1110+
.into()
1111+
})
1112+
}
11071113
}
11081114
}
11091115

clarity/src/vm/functions/database.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,8 @@ pub fn special_get_burn_block_info(
915915
};
916916

917917
// Note: We assume that we will not have a height bigger than u32::MAX.
918-
let height_value = match u32::try_from(height_value) {
919-
Ok(result) => result,
920-
_ => return Ok(Value::none()),
918+
let Ok(height_value) = u32::try_from(height_value) else {
919+
return Ok(Value::none());
921920
};
922921

923922
match block_info_prop {

0 commit comments

Comments
 (0)