Skip to content

fix: burn-block-height inside of at-block #5524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions clarity/src/vm/database/clarity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,35 +1075,41 @@ impl<'a> ClarityDatabase<'a> {
/// block height (i.e. that returned by `get_index_block_header_hash` for
/// `get_current_block_height`).
pub fn get_current_burnchain_block_height(&mut self) -> Result<u32> {
let cur_stacks_height = self.store.get_current_block_height();
let epoch = self.get_clarity_epoch_version()?;
match epoch {
// Special case to preserve possibly incorrect behavior (inside at-block) in Epoch 3.0
StacksEpochId::Epoch30 => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
StacksEpochId::Epoch30 => {
StacksEpochId::Epoch30 | StacksEpochId::Epoch31 => {

(Seems obvious, but let's not forget about it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why we avoid using the _ case in stacks-core... we can't forget about it. May want to practice the same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when I created this PR I was hoping to get it into Epoch 3.1, but that opportunity has passed

Copy link
Collaborator

@hugocaillard hugocaillard Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why we avoid using the _ case in stacks-core

Oh right, there should be a clippy rule about that but I can't find one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's allowed by default and can't be set in clippy.toml

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. There were some arguments made about not applying that rule any way, but in most cases, we should manually follow that rule.

self.burn_state_db
.get_tip_burn_block_height()
.ok_or_else(|| {
InterpreterError::Expect("Failed to get burnchain tip height.".into())
.into()
})
}
_ => {
let cur_stacks_height = self.store.get_current_block_height();

// Before epoch 3.0, we can only access the burn block associated with the last block
let last_mined_bhh = if epoch.clarity_uses_tip_burn_block() {
// In epoch 3+, we can access the current burnchain block
self.get_index_block_header_hash(cur_stacks_height)
.or_else(|_| self.get_index_block_header_hash(cur_stacks_height - 1))?
} else if cur_stacks_height == 0 {
return Ok(self.burn_state_db.get_burn_start_height());
} else {
// Safety note: normal subtraction is safe here, because we've already checked
// that cur_stacks_height > 0.
self.get_index_block_header_hash(cur_stacks_height - 1)?
};

// Before epoch 3.0, we can only access the burn block associated with the last block
if !self
.get_clarity_epoch_version()?
.clarity_uses_tip_burn_block()
{
if cur_stacks_height == 0 {
return Ok(self.burn_state_db.get_burn_start_height());
};
// Safety note: normal subtraction is safe here, because we've already checked
// that cur_stacks_height > 0.
let last_mined_bhh = self.get_index_block_header_hash(cur_stacks_height - 1)?;

self.get_burnchain_block_height(&last_mined_bhh)
.ok_or_else(|| {
InterpreterError::Expect(format!(
"Block header hash '{}' must return for provided stacks block height {}",
&last_mined_bhh, cur_stacks_height
))
.into()
})
} else {
// In epoch 3+, we can access the current burnchain block
self.burn_state_db
.get_tip_burn_block_height()
.ok_or_else(|| {
InterpreterError::Expect("Failed to get burnchain tip height.".into()).into()
})
self.get_burnchain_block_height(&last_mined_bhh)
.ok_or_else(|| {
InterpreterError::Expect(format!(
"Block header hash '{last_mined_bhh}' must return for provided stacks block height {cur_stacks_height}"
))
.into()
})
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions clarity/src/vm/functions/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,8 @@ pub fn special_get_burn_block_info(
};

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

match block_info_prop {
Expand Down